ioctl.c 131 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320
  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 <linux/fileattr.h>
  29. #include <linux/fsverity.h>
  30. #include <linux/sched/xacct.h>
  31. #include <linux/io_uring/cmd.h>
  32. #include "ctree.h"
  33. #include "disk-io.h"
  34. #include "export.h"
  35. #include "transaction.h"
  36. #include "btrfs_inode.h"
  37. #include "volumes.h"
  38. #include "locking.h"
  39. #include "backref.h"
  40. #include "send.h"
  41. #include "dev-replace.h"
  42. #include "props.h"
  43. #include "sysfs.h"
  44. #include "qgroup.h"
  45. #include "tree-log.h"
  46. #include "compression.h"
  47. #include "space-info.h"
  48. #include "block-group.h"
  49. #include "fs.h"
  50. #include "accessors.h"
  51. #include "extent-tree.h"
  52. #include "root-tree.h"
  53. #include "defrag.h"
  54. #include "dir-item.h"
  55. #include "uuid-tree.h"
  56. #include "ioctl.h"
  57. #include "file.h"
  58. #include "scrub.h"
  59. #include "super.h"
  60. #ifdef CONFIG_64BIT
  61. /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
  62. * structures are incorrect, as the timespec structure from userspace
  63. * is 4 bytes too small. We define these alternatives here to teach
  64. * the kernel about the 32-bit struct packing.
  65. */
  66. struct btrfs_ioctl_timespec_32 {
  67. __u64 sec;
  68. __u32 nsec;
  69. } __attribute__ ((__packed__));
  70. struct btrfs_ioctl_received_subvol_args_32 {
  71. char uuid[BTRFS_UUID_SIZE]; /* in */
  72. __u64 stransid; /* in */
  73. __u64 rtransid; /* out */
  74. struct btrfs_ioctl_timespec_32 stime; /* in */
  75. struct btrfs_ioctl_timespec_32 rtime; /* out */
  76. __u64 flags; /* in */
  77. __u64 reserved[16]; /* in */
  78. } __attribute__ ((__packed__));
  79. #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
  80. struct btrfs_ioctl_received_subvol_args_32)
  81. #endif
  82. #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
  83. struct btrfs_ioctl_send_args_32 {
  84. __s64 send_fd; /* in */
  85. __u64 clone_sources_count; /* in */
  86. compat_uptr_t clone_sources; /* in */
  87. __u64 parent_root; /* in */
  88. __u64 flags; /* in */
  89. __u32 version; /* in */
  90. __u8 reserved[28]; /* in */
  91. } __attribute__ ((__packed__));
  92. #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
  93. struct btrfs_ioctl_send_args_32)
  94. struct btrfs_ioctl_encoded_io_args_32 {
  95. compat_uptr_t iov;
  96. compat_ulong_t iovcnt;
  97. __s64 offset;
  98. __u64 flags;
  99. __u64 len;
  100. __u64 unencoded_len;
  101. __u64 unencoded_offset;
  102. __u32 compression;
  103. __u32 encryption;
  104. __u8 reserved[64];
  105. };
  106. #define BTRFS_IOC_ENCODED_READ_32 _IOR(BTRFS_IOCTL_MAGIC, 64, \
  107. struct btrfs_ioctl_encoded_io_args_32)
  108. #define BTRFS_IOC_ENCODED_WRITE_32 _IOW(BTRFS_IOCTL_MAGIC, 64, \
  109. struct btrfs_ioctl_encoded_io_args_32)
  110. #endif
  111. /* Mask out flags that are inappropriate for the given type of inode. */
  112. static unsigned int btrfs_mask_fsflags_for_type(const struct inode *inode,
  113. unsigned int flags)
  114. {
  115. if (S_ISDIR(inode->i_mode))
  116. return flags;
  117. else if (S_ISREG(inode->i_mode))
  118. return flags & ~FS_DIRSYNC_FL;
  119. else
  120. return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
  121. }
  122. /*
  123. * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
  124. * ioctl.
  125. */
  126. static unsigned int btrfs_inode_flags_to_fsflags(const struct btrfs_inode *inode)
  127. {
  128. unsigned int iflags = 0;
  129. u32 flags = inode->flags;
  130. u32 ro_flags = inode->ro_flags;
  131. if (flags & BTRFS_INODE_SYNC)
  132. iflags |= FS_SYNC_FL;
  133. if (flags & BTRFS_INODE_IMMUTABLE)
  134. iflags |= FS_IMMUTABLE_FL;
  135. if (flags & BTRFS_INODE_APPEND)
  136. iflags |= FS_APPEND_FL;
  137. if (flags & BTRFS_INODE_NODUMP)
  138. iflags |= FS_NODUMP_FL;
  139. if (flags & BTRFS_INODE_NOATIME)
  140. iflags |= FS_NOATIME_FL;
  141. if (flags & BTRFS_INODE_DIRSYNC)
  142. iflags |= FS_DIRSYNC_FL;
  143. if (flags & BTRFS_INODE_NODATACOW)
  144. iflags |= FS_NOCOW_FL;
  145. if (ro_flags & BTRFS_INODE_RO_VERITY)
  146. iflags |= FS_VERITY_FL;
  147. if (flags & BTRFS_INODE_NOCOMPRESS)
  148. iflags |= FS_NOCOMP_FL;
  149. else if (flags & BTRFS_INODE_COMPRESS)
  150. iflags |= FS_COMPR_FL;
  151. return iflags;
  152. }
  153. /*
  154. * Update inode->i_flags based on the btrfs internal flags.
  155. */
  156. void btrfs_sync_inode_flags_to_i_flags(struct btrfs_inode *inode)
  157. {
  158. unsigned int new_fl = 0;
  159. if (inode->flags & BTRFS_INODE_SYNC)
  160. new_fl |= S_SYNC;
  161. if (inode->flags & BTRFS_INODE_IMMUTABLE)
  162. new_fl |= S_IMMUTABLE;
  163. if (inode->flags & BTRFS_INODE_APPEND)
  164. new_fl |= S_APPEND;
  165. if (inode->flags & BTRFS_INODE_NOATIME)
  166. new_fl |= S_NOATIME;
  167. if (inode->flags & BTRFS_INODE_DIRSYNC)
  168. new_fl |= S_DIRSYNC;
  169. if (inode->ro_flags & BTRFS_INODE_RO_VERITY)
  170. new_fl |= S_VERITY;
  171. set_mask_bits(&inode->vfs_inode.i_flags,
  172. S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC |
  173. S_VERITY, new_fl);
  174. }
  175. /*
  176. * Check if @flags are a supported and valid set of FS_*_FL flags and that
  177. * the old and new flags are not conflicting
  178. */
  179. static int check_fsflags(unsigned int old_flags, unsigned int flags)
  180. {
  181. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  182. FS_NOATIME_FL | FS_NODUMP_FL | \
  183. FS_SYNC_FL | FS_DIRSYNC_FL | \
  184. FS_NOCOMP_FL | FS_COMPR_FL |
  185. FS_NOCOW_FL))
  186. return -EOPNOTSUPP;
  187. /* COMPR and NOCOMP on new/old are valid */
  188. if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
  189. return -EINVAL;
  190. if ((flags & FS_COMPR_FL) && (flags & FS_NOCOW_FL))
  191. return -EINVAL;
  192. /* NOCOW and compression options are mutually exclusive */
  193. if ((old_flags & FS_NOCOW_FL) && (flags & (FS_COMPR_FL | FS_NOCOMP_FL)))
  194. return -EINVAL;
  195. if ((flags & FS_NOCOW_FL) && (old_flags & (FS_COMPR_FL | FS_NOCOMP_FL)))
  196. return -EINVAL;
  197. return 0;
  198. }
  199. static int check_fsflags_compatible(const struct btrfs_fs_info *fs_info,
  200. unsigned int flags)
  201. {
  202. if (btrfs_is_zoned(fs_info) && (flags & FS_NOCOW_FL))
  203. return -EPERM;
  204. return 0;
  205. }
  206. int btrfs_check_ioctl_vol_args_path(const struct btrfs_ioctl_vol_args *vol_args)
  207. {
  208. if (memchr(vol_args->name, 0, sizeof(vol_args->name)) == NULL)
  209. return -ENAMETOOLONG;
  210. return 0;
  211. }
  212. static int btrfs_check_ioctl_vol_args2_subvol_name(const struct btrfs_ioctl_vol_args_v2 *vol_args2)
  213. {
  214. if (memchr(vol_args2->name, 0, sizeof(vol_args2->name)) == NULL)
  215. return -ENAMETOOLONG;
  216. return 0;
  217. }
  218. /*
  219. * Set flags/xflags from the internal inode flags. The remaining items of
  220. * fsxattr are zeroed.
  221. */
  222. int btrfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
  223. {
  224. const struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
  225. fileattr_fill_flags(fa, btrfs_inode_flags_to_fsflags(inode));
  226. return 0;
  227. }
  228. int btrfs_fileattr_set(struct mnt_idmap *idmap,
  229. struct dentry *dentry, struct file_kattr *fa)
  230. {
  231. struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
  232. struct btrfs_root *root = inode->root;
  233. struct btrfs_fs_info *fs_info = root->fs_info;
  234. struct btrfs_trans_handle *trans;
  235. unsigned int fsflags, old_fsflags;
  236. int ret;
  237. const char *comp = NULL;
  238. u32 inode_flags;
  239. if (btrfs_root_readonly(root))
  240. return -EROFS;
  241. if (fileattr_has_fsx(fa))
  242. return -EOPNOTSUPP;
  243. fsflags = btrfs_mask_fsflags_for_type(&inode->vfs_inode, fa->flags);
  244. old_fsflags = btrfs_inode_flags_to_fsflags(inode);
  245. ret = check_fsflags(old_fsflags, fsflags);
  246. if (ret)
  247. return ret;
  248. ret = check_fsflags_compatible(fs_info, fsflags);
  249. if (ret)
  250. return ret;
  251. inode_flags = inode->flags;
  252. if (fsflags & FS_SYNC_FL)
  253. inode_flags |= BTRFS_INODE_SYNC;
  254. else
  255. inode_flags &= ~BTRFS_INODE_SYNC;
  256. if (fsflags & FS_IMMUTABLE_FL)
  257. inode_flags |= BTRFS_INODE_IMMUTABLE;
  258. else
  259. inode_flags &= ~BTRFS_INODE_IMMUTABLE;
  260. if (fsflags & FS_APPEND_FL)
  261. inode_flags |= BTRFS_INODE_APPEND;
  262. else
  263. inode_flags &= ~BTRFS_INODE_APPEND;
  264. if (fsflags & FS_NODUMP_FL)
  265. inode_flags |= BTRFS_INODE_NODUMP;
  266. else
  267. inode_flags &= ~BTRFS_INODE_NODUMP;
  268. if (fsflags & FS_NOATIME_FL)
  269. inode_flags |= BTRFS_INODE_NOATIME;
  270. else
  271. inode_flags &= ~BTRFS_INODE_NOATIME;
  272. /* If coming from FS_IOC_FSSETXATTR then skip unconverted flags */
  273. if (!fa->flags_valid) {
  274. /* 1 item for the inode */
  275. trans = btrfs_start_transaction(root, 1);
  276. if (IS_ERR(trans))
  277. return PTR_ERR(trans);
  278. goto update_flags;
  279. }
  280. if (fsflags & FS_DIRSYNC_FL)
  281. inode_flags |= BTRFS_INODE_DIRSYNC;
  282. else
  283. inode_flags &= ~BTRFS_INODE_DIRSYNC;
  284. if (fsflags & FS_NOCOW_FL) {
  285. if (S_ISREG(inode->vfs_inode.i_mode)) {
  286. /*
  287. * It's safe to turn csums off here, no extents exist.
  288. * Otherwise we want the flag to reflect the real COW
  289. * status of the file and will not set it.
  290. */
  291. if (inode->vfs_inode.i_size == 0)
  292. inode_flags |= BTRFS_INODE_NODATACOW |
  293. BTRFS_INODE_NODATASUM;
  294. } else {
  295. inode_flags |= BTRFS_INODE_NODATACOW;
  296. }
  297. } else {
  298. /*
  299. * Revert back under same assumptions as above
  300. */
  301. if (S_ISREG(inode->vfs_inode.i_mode)) {
  302. if (inode->vfs_inode.i_size == 0)
  303. inode_flags &= ~(BTRFS_INODE_NODATACOW |
  304. BTRFS_INODE_NODATASUM);
  305. } else {
  306. inode_flags &= ~BTRFS_INODE_NODATACOW;
  307. }
  308. }
  309. /*
  310. * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
  311. * flag may be changed automatically if compression code won't make
  312. * things smaller.
  313. */
  314. if (fsflags & FS_NOCOMP_FL) {
  315. inode_flags &= ~BTRFS_INODE_COMPRESS;
  316. inode_flags |= BTRFS_INODE_NOCOMPRESS;
  317. } else if (fsflags & FS_COMPR_FL) {
  318. if (IS_SWAPFILE(&inode->vfs_inode))
  319. return -ETXTBSY;
  320. inode_flags |= BTRFS_INODE_COMPRESS;
  321. inode_flags &= ~BTRFS_INODE_NOCOMPRESS;
  322. comp = btrfs_compress_type2str(fs_info->compress_type);
  323. if (!comp || comp[0] == 0)
  324. comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
  325. } else {
  326. inode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
  327. }
  328. /*
  329. * 1 for inode item
  330. * 2 for properties
  331. */
  332. trans = btrfs_start_transaction(root, 3);
  333. if (IS_ERR(trans))
  334. return PTR_ERR(trans);
  335. if (comp) {
  336. ret = btrfs_set_prop(trans, inode, "btrfs.compression",
  337. comp, strlen(comp), 0);
  338. if (unlikely(ret)) {
  339. btrfs_abort_transaction(trans, ret);
  340. goto out_end_trans;
  341. }
  342. } else {
  343. ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL, 0, 0);
  344. if (unlikely(ret && ret != -ENODATA)) {
  345. btrfs_abort_transaction(trans, ret);
  346. goto out_end_trans;
  347. }
  348. }
  349. update_flags:
  350. inode->flags = inode_flags;
  351. btrfs_update_inode_mapping_flags(inode);
  352. btrfs_sync_inode_flags_to_i_flags(inode);
  353. inode_inc_iversion(&inode->vfs_inode);
  354. inode_set_ctime_current(&inode->vfs_inode);
  355. ret = btrfs_update_inode(trans, inode);
  356. out_end_trans:
  357. btrfs_end_transaction(trans);
  358. return ret;
  359. }
  360. static int btrfs_ioctl_getversion(const struct inode *inode, int __user *arg)
  361. {
  362. return put_user(inode->i_generation, arg);
  363. }
  364. static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info,
  365. void __user *arg)
  366. {
  367. struct btrfs_device *device;
  368. struct fstrim_range range;
  369. u64 minlen = ULLONG_MAX;
  370. u64 num_devices = 0;
  371. int ret;
  372. if (!capable(CAP_SYS_ADMIN))
  373. return -EPERM;
  374. /*
  375. * btrfs_trim_block_group() depends on space cache, which is not
  376. * available in zoned filesystem. So, disallow fitrim on a zoned
  377. * filesystem for now.
  378. */
  379. if (btrfs_is_zoned(fs_info))
  380. return -EOPNOTSUPP;
  381. /*
  382. * If the fs is mounted with nologreplay, which requires it to be
  383. * mounted in RO mode as well, we can not allow discard on free space
  384. * inside block groups, because log trees refer to extents that are not
  385. * pinned in a block group's free space cache (pinning the extents is
  386. * precisely the first phase of replaying a log tree).
  387. */
  388. if (btrfs_test_opt(fs_info, NOLOGREPLAY))
  389. return -EROFS;
  390. rcu_read_lock();
  391. list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
  392. dev_list) {
  393. if (!device->bdev || !bdev_max_discard_sectors(device->bdev))
  394. continue;
  395. num_devices++;
  396. minlen = min_t(u64, bdev_discard_granularity(device->bdev),
  397. minlen);
  398. }
  399. rcu_read_unlock();
  400. if (!num_devices)
  401. return -EOPNOTSUPP;
  402. if (copy_from_user(&range, arg, sizeof(range)))
  403. return -EFAULT;
  404. /*
  405. * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
  406. * block group is in the logical address space, which can be any
  407. * sectorsize aligned bytenr in the range [0, U64_MAX].
  408. */
  409. if (range.len < fs_info->sectorsize)
  410. return -EINVAL;
  411. range.minlen = max(range.minlen, minlen);
  412. ret = btrfs_trim_fs(fs_info, &range);
  413. if (copy_to_user(arg, &range, sizeof(range)))
  414. return -EFAULT;
  415. return ret;
  416. }
  417. /*
  418. * Calculate the number of transaction items to reserve for creating a subvolume
  419. * or snapshot, not including the inode, directory entries, or parent directory.
  420. */
  421. static unsigned int create_subvol_num_items(const struct btrfs_qgroup_inherit *inherit)
  422. {
  423. /*
  424. * 1 to add root block
  425. * 1 to add root item
  426. * 1 to add root ref
  427. * 1 to add root backref
  428. * 1 to add UUID item
  429. * 1 to add qgroup info
  430. * 1 to add qgroup limit
  431. *
  432. * Ideally the last two would only be accounted if qgroups are enabled,
  433. * but that can change between now and the time we would insert them.
  434. */
  435. unsigned int num_items = 7;
  436. if (inherit) {
  437. /* 2 to add qgroup relations for each inherited qgroup */
  438. num_items += 2 * inherit->num_qgroups;
  439. }
  440. return num_items;
  441. }
  442. static noinline int create_subvol(struct mnt_idmap *idmap,
  443. struct inode *dir, struct dentry *dentry,
  444. struct btrfs_qgroup_inherit *inherit)
  445. {
  446. struct btrfs_fs_info *fs_info = inode_to_fs_info(dir);
  447. struct btrfs_trans_handle *trans;
  448. struct btrfs_key key;
  449. struct btrfs_root_item AUTO_KFREE(root_item);
  450. struct btrfs_inode_item *inode_item;
  451. struct extent_buffer *leaf;
  452. struct btrfs_root *root = BTRFS_I(dir)->root;
  453. struct btrfs_root *new_root;
  454. struct btrfs_block_rsv block_rsv;
  455. struct timespec64 cur_time = current_time(dir);
  456. struct btrfs_new_inode_args new_inode_args = {
  457. .dir = dir,
  458. .dentry = dentry,
  459. .subvol = true,
  460. };
  461. unsigned int trans_num_items;
  462. int ret;
  463. dev_t anon_dev;
  464. u64 objectid;
  465. u64 qgroup_reserved = 0;
  466. root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
  467. if (!root_item)
  468. return -ENOMEM;
  469. ret = btrfs_get_free_objectid(fs_info->tree_root, &objectid);
  470. if (ret)
  471. return ret;
  472. /*
  473. * Don't create subvolume whose level is not zero. Or qgroup will be
  474. * screwed up since it assumes subvolume qgroup's level to be 0.
  475. */
  476. if (btrfs_qgroup_level(objectid))
  477. return -ENOSPC;
  478. ret = get_anon_bdev(&anon_dev);
  479. if (ret < 0)
  480. return ret;
  481. new_inode_args.inode = btrfs_new_subvol_inode(idmap, dir);
  482. if (!new_inode_args.inode) {
  483. ret = -ENOMEM;
  484. goto out_anon_dev;
  485. }
  486. ret = btrfs_new_inode_prepare(&new_inode_args, &trans_num_items);
  487. if (ret)
  488. goto out_inode;
  489. trans_num_items += create_subvol_num_items(inherit);
  490. btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
  491. ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
  492. trans_num_items, false);
  493. if (ret)
  494. goto out_new_inode_args;
  495. qgroup_reserved = block_rsv.qgroup_rsv_reserved;
  496. trans = btrfs_start_transaction(root, 0);
  497. if (IS_ERR(trans)) {
  498. ret = PTR_ERR(trans);
  499. goto out_release_rsv;
  500. }
  501. btrfs_qgroup_convert_reserved_meta(root, qgroup_reserved);
  502. qgroup_reserved = 0;
  503. trans->block_rsv = &block_rsv;
  504. trans->bytes_reserved = block_rsv.size;
  505. ret = btrfs_qgroup_inherit(trans, 0, objectid, btrfs_root_id(root), inherit);
  506. if (ret)
  507. goto out;
  508. leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
  509. 0, BTRFS_NESTING_NORMAL);
  510. if (IS_ERR(leaf)) {
  511. ret = PTR_ERR(leaf);
  512. goto out;
  513. }
  514. btrfs_mark_buffer_dirty(trans, leaf);
  515. inode_item = &root_item->inode;
  516. btrfs_set_stack_inode_generation(inode_item, 1);
  517. btrfs_set_stack_inode_size(inode_item, 3);
  518. btrfs_set_stack_inode_nlink(inode_item, 1);
  519. btrfs_set_stack_inode_nbytes(inode_item,
  520. fs_info->nodesize);
  521. btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
  522. btrfs_set_root_flags(root_item, 0);
  523. btrfs_set_root_limit(root_item, 0);
  524. btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
  525. btrfs_set_root_bytenr(root_item, leaf->start);
  526. btrfs_set_root_generation(root_item, trans->transid);
  527. btrfs_set_root_level(root_item, 0);
  528. btrfs_set_root_refs(root_item, 1);
  529. btrfs_set_root_used(root_item, leaf->len);
  530. btrfs_set_root_last_snapshot(root_item, 0);
  531. btrfs_set_root_generation_v2(root_item,
  532. btrfs_root_generation(root_item));
  533. generate_random_guid(root_item->uuid);
  534. btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
  535. btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
  536. root_item->ctime = root_item->otime;
  537. btrfs_set_root_ctransid(root_item, trans->transid);
  538. btrfs_set_root_otransid(root_item, trans->transid);
  539. btrfs_tree_unlock(leaf);
  540. btrfs_set_root_dirid(root_item, BTRFS_FIRST_FREE_OBJECTID);
  541. key.objectid = objectid;
  542. key.type = BTRFS_ROOT_ITEM_KEY;
  543. key.offset = 0;
  544. ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
  545. root_item);
  546. if (ret) {
  547. int ret2;
  548. /*
  549. * Since we don't abort the transaction in this case, free the
  550. * tree block so that we don't leak space and leave the
  551. * filesystem in an inconsistent state (an extent item in the
  552. * extent tree with a backreference for a root that does not
  553. * exists).
  554. */
  555. btrfs_tree_lock(leaf);
  556. btrfs_clear_buffer_dirty(trans, leaf);
  557. btrfs_tree_unlock(leaf);
  558. ret2 = btrfs_free_tree_block(trans, objectid, leaf, 0, 1);
  559. if (unlikely(ret2 < 0))
  560. btrfs_abort_transaction(trans, ret2);
  561. free_extent_buffer(leaf);
  562. goto out;
  563. }
  564. free_extent_buffer(leaf);
  565. leaf = NULL;
  566. new_root = btrfs_get_new_fs_root(fs_info, objectid, &anon_dev);
  567. if (IS_ERR(new_root)) {
  568. ret = PTR_ERR(new_root);
  569. btrfs_abort_transaction(trans, ret);
  570. goto out;
  571. }
  572. /* anon_dev is owned by new_root now. */
  573. anon_dev = 0;
  574. BTRFS_I(new_inode_args.inode)->root = new_root;
  575. /* ... and new_root is owned by new_inode_args.inode now. */
  576. ret = btrfs_record_root_in_trans(trans, new_root);
  577. if (unlikely(ret)) {
  578. btrfs_abort_transaction(trans, ret);
  579. goto out;
  580. }
  581. ret = btrfs_uuid_tree_add(trans, root_item->uuid,
  582. BTRFS_UUID_KEY_SUBVOL, objectid);
  583. if (unlikely(ret)) {
  584. btrfs_abort_transaction(trans, ret);
  585. goto out;
  586. }
  587. btrfs_record_new_subvolume(trans, BTRFS_I(dir));
  588. ret = btrfs_create_new_inode(trans, &new_inode_args);
  589. if (unlikely(ret)) {
  590. btrfs_abort_transaction(trans, ret);
  591. goto out;
  592. }
  593. /*
  594. * Subvolumes have orphans cleaned on first dentry lookup. A new
  595. * subvolume cannot have any orphans, so we should set the bit before we
  596. * add the subvolume dentry to the dentry cache, so that it is in the
  597. * same state as a subvolume after first lookup.
  598. */
  599. set_bit(BTRFS_ROOT_ORPHAN_CLEANUP, &new_root->state);
  600. d_instantiate_new(dentry, new_inode_args.inode);
  601. new_inode_args.inode = NULL;
  602. out:
  603. trans->block_rsv = NULL;
  604. trans->bytes_reserved = 0;
  605. btrfs_end_transaction(trans);
  606. out_release_rsv:
  607. btrfs_block_rsv_release(fs_info, &block_rsv, (u64)-1, NULL);
  608. if (qgroup_reserved)
  609. btrfs_qgroup_free_meta_prealloc(root, qgroup_reserved);
  610. out_new_inode_args:
  611. btrfs_new_inode_args_destroy(&new_inode_args);
  612. out_inode:
  613. iput(new_inode_args.inode);
  614. out_anon_dev:
  615. if (anon_dev)
  616. free_anon_bdev(anon_dev);
  617. return ret;
  618. }
  619. static int create_snapshot(struct btrfs_root *root, struct inode *dir,
  620. struct dentry *dentry, bool readonly,
  621. struct btrfs_qgroup_inherit *inherit)
  622. {
  623. struct btrfs_fs_info *fs_info = inode_to_fs_info(dir);
  624. struct inode *inode;
  625. struct btrfs_pending_snapshot *pending_snapshot;
  626. unsigned int trans_num_items;
  627. struct btrfs_trans_handle *trans;
  628. struct btrfs_block_rsv *block_rsv;
  629. u64 qgroup_reserved = 0;
  630. int ret;
  631. /* We do not support snapshotting right now. */
  632. if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
  633. btrfs_warn(fs_info,
  634. "extent tree v2 doesn't support snapshotting yet");
  635. return -EOPNOTSUPP;
  636. }
  637. if (btrfs_root_refs(&root->root_item) == 0)
  638. return -ENOENT;
  639. if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
  640. return -EINVAL;
  641. if (atomic_read(&root->nr_swapfiles)) {
  642. btrfs_warn(fs_info,
  643. "cannot snapshot subvolume with active swapfile");
  644. return -ETXTBSY;
  645. }
  646. pending_snapshot = kzalloc_obj(*pending_snapshot);
  647. if (!pending_snapshot)
  648. return -ENOMEM;
  649. ret = get_anon_bdev(&pending_snapshot->anon_dev);
  650. if (ret < 0)
  651. goto free_pending;
  652. pending_snapshot->root_item = kzalloc_obj(struct btrfs_root_item);
  653. pending_snapshot->path = btrfs_alloc_path();
  654. if (!pending_snapshot->root_item || !pending_snapshot->path) {
  655. ret = -ENOMEM;
  656. goto free_pending;
  657. }
  658. block_rsv = &pending_snapshot->block_rsv;
  659. btrfs_init_block_rsv(block_rsv, BTRFS_BLOCK_RSV_TEMP);
  660. /*
  661. * 1 to add dir item
  662. * 1 to add dir index
  663. * 1 to update parent inode item
  664. */
  665. trans_num_items = create_subvol_num_items(inherit) + 3;
  666. ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root, block_rsv,
  667. trans_num_items, false);
  668. if (ret)
  669. goto free_pending;
  670. qgroup_reserved = block_rsv->qgroup_rsv_reserved;
  671. pending_snapshot->dentry = dentry;
  672. pending_snapshot->root = root;
  673. pending_snapshot->readonly = readonly;
  674. pending_snapshot->dir = BTRFS_I(dir);
  675. pending_snapshot->inherit = inherit;
  676. trans = btrfs_start_transaction(root, 0);
  677. if (IS_ERR(trans)) {
  678. ret = PTR_ERR(trans);
  679. goto fail;
  680. }
  681. ret = btrfs_record_root_in_trans(trans, BTRFS_I(dir)->root);
  682. if (ret) {
  683. btrfs_end_transaction(trans);
  684. goto fail;
  685. }
  686. btrfs_qgroup_convert_reserved_meta(root, qgroup_reserved);
  687. qgroup_reserved = 0;
  688. trans->pending_snapshot = pending_snapshot;
  689. ret = btrfs_commit_transaction(trans);
  690. if (ret)
  691. goto fail;
  692. ret = pending_snapshot->error;
  693. if (ret)
  694. goto fail;
  695. ret = btrfs_orphan_cleanup(pending_snapshot->snap);
  696. if (ret)
  697. goto fail;
  698. inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
  699. if (IS_ERR(inode)) {
  700. ret = PTR_ERR(inode);
  701. goto fail;
  702. }
  703. d_instantiate(dentry, inode);
  704. ret = 0;
  705. pending_snapshot->anon_dev = 0;
  706. fail:
  707. /* Prevent double freeing of anon_dev */
  708. if (ret && pending_snapshot->snap)
  709. pending_snapshot->snap->anon_dev = 0;
  710. btrfs_put_root(pending_snapshot->snap);
  711. btrfs_block_rsv_release(fs_info, block_rsv, (u64)-1, NULL);
  712. if (qgroup_reserved)
  713. btrfs_qgroup_free_meta_prealloc(root, qgroup_reserved);
  714. free_pending:
  715. if (pending_snapshot->anon_dev)
  716. free_anon_bdev(pending_snapshot->anon_dev);
  717. kfree(pending_snapshot->root_item);
  718. btrfs_free_path(pending_snapshot->path);
  719. kfree(pending_snapshot);
  720. return ret;
  721. }
  722. /*
  723. * Create a new subvolume below @parent. This is largely modeled after
  724. * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
  725. * inside this filesystem so it's quite a bit simpler.
  726. */
  727. static noinline int btrfs_mksubvol(struct dentry *parent,
  728. struct mnt_idmap *idmap,
  729. struct qstr *qname, struct btrfs_root *snap_src,
  730. bool readonly,
  731. struct btrfs_qgroup_inherit *inherit)
  732. {
  733. struct inode *dir = d_inode(parent);
  734. struct btrfs_fs_info *fs_info = inode_to_fs_info(dir);
  735. struct dentry *dentry;
  736. struct fscrypt_str name_str = FSTR_INIT((char *)qname->name, qname->len);
  737. int ret;
  738. dentry = start_creating_killable(idmap, parent, qname);
  739. if (IS_ERR(dentry))
  740. return PTR_ERR(dentry);
  741. ret = may_create_dentry(idmap, dir, dentry);
  742. if (ret)
  743. goto out_dput;
  744. /*
  745. * even if this name doesn't exist, we may get hash collisions.
  746. * check for them now when we can safely fail
  747. */
  748. ret = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, dir->i_ino, &name_str);
  749. if (ret)
  750. goto out_dput;
  751. down_read(&fs_info->subvol_sem);
  752. if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
  753. goto out_up_read;
  754. if (snap_src)
  755. ret = create_snapshot(snap_src, dir, dentry, readonly, inherit);
  756. else
  757. ret = create_subvol(idmap, dir, dentry, inherit);
  758. if (!ret)
  759. fsnotify_mkdir(dir, dentry);
  760. out_up_read:
  761. up_read(&fs_info->subvol_sem);
  762. out_dput:
  763. end_creating(dentry);
  764. return ret;
  765. }
  766. static noinline int btrfs_mksnapshot(struct dentry *parent,
  767. struct mnt_idmap *idmap,
  768. struct qstr *qname,
  769. struct btrfs_root *root,
  770. bool readonly,
  771. struct btrfs_qgroup_inherit *inherit)
  772. {
  773. int ret;
  774. /*
  775. * Force new buffered writes to reserve space even when NOCOW is
  776. * possible. This is to avoid later writeback (running delalloc) to
  777. * fallback to COW mode and unexpectedly fail with ENOSPC.
  778. */
  779. btrfs_drew_read_lock(&root->snapshot_lock);
  780. ret = btrfs_start_delalloc_snapshot(root, false);
  781. if (ret)
  782. goto out;
  783. /*
  784. * All previous writes have started writeback in NOCOW mode, so now
  785. * we force future writes to fallback to COW mode during snapshot
  786. * creation.
  787. */
  788. atomic_inc(&root->snapshot_force_cow);
  789. btrfs_wait_ordered_extents(root, U64_MAX, NULL);
  790. ret = btrfs_mksubvol(parent, idmap, qname, root, readonly, inherit);
  791. atomic_dec(&root->snapshot_force_cow);
  792. out:
  793. btrfs_drew_read_unlock(&root->snapshot_lock);
  794. return ret;
  795. }
  796. /*
  797. * Try to start exclusive operation @type or cancel it if it's running.
  798. *
  799. * Return:
  800. * 0 - normal mode, newly claimed op started
  801. * >0 - normal mode, something else is running,
  802. * return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS to user space
  803. * ECANCELED - cancel mode, successful cancel
  804. * ENOTCONN - cancel mode, operation not running anymore
  805. */
  806. static int exclop_start_or_cancel_reloc(struct btrfs_fs_info *fs_info,
  807. enum btrfs_exclusive_operation type, bool cancel)
  808. {
  809. if (!cancel) {
  810. /* Start normal op */
  811. if (!btrfs_exclop_start(fs_info, type))
  812. return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  813. /* Exclusive operation is now claimed */
  814. return 0;
  815. }
  816. /* Cancel running op */
  817. if (btrfs_exclop_start_try_lock(fs_info, type)) {
  818. /*
  819. * This blocks any exclop finish from setting it to NONE, so we
  820. * request cancellation. Either it runs and we will wait for it,
  821. * or it has finished and no waiting will happen.
  822. */
  823. atomic_inc(&fs_info->reloc_cancel_req);
  824. btrfs_exclop_start_unlock(fs_info);
  825. if (test_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags))
  826. wait_on_bit(&fs_info->flags, BTRFS_FS_RELOC_RUNNING,
  827. TASK_INTERRUPTIBLE);
  828. return -ECANCELED;
  829. }
  830. /* Something else is running or none */
  831. return -ENOTCONN;
  832. }
  833. static noinline int btrfs_ioctl_resize(struct file *file,
  834. void __user *arg)
  835. {
  836. BTRFS_DEV_LOOKUP_ARGS(args);
  837. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  838. struct btrfs_fs_info *fs_info = root->fs_info;
  839. u64 new_size;
  840. u64 old_size;
  841. u64 devid = 1;
  842. struct btrfs_ioctl_vol_args *vol_args;
  843. struct btrfs_device *device = NULL;
  844. char *sizestr;
  845. char *devstr = NULL;
  846. int ret = 0;
  847. int mod = 0;
  848. bool cancel;
  849. if (!capable(CAP_SYS_ADMIN))
  850. return -EPERM;
  851. ret = mnt_want_write_file(file);
  852. if (ret)
  853. return ret;
  854. /*
  855. * Read the arguments before checking exclusivity to be able to
  856. * distinguish regular resize and cancel
  857. */
  858. vol_args = memdup_user(arg, sizeof(*vol_args));
  859. if (IS_ERR(vol_args)) {
  860. ret = PTR_ERR(vol_args);
  861. goto out_drop;
  862. }
  863. ret = btrfs_check_ioctl_vol_args_path(vol_args);
  864. if (ret < 0)
  865. goto out_free;
  866. sizestr = vol_args->name;
  867. cancel = (strcmp("cancel", sizestr) == 0);
  868. ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_RESIZE, cancel);
  869. if (ret)
  870. goto out_free;
  871. /* Exclusive operation is now claimed */
  872. devstr = strchr(sizestr, ':');
  873. if (devstr) {
  874. sizestr = devstr + 1;
  875. *devstr = '\0';
  876. devstr = vol_args->name;
  877. ret = kstrtoull(devstr, 10, &devid);
  878. if (ret)
  879. goto out_finish;
  880. if (!devid) {
  881. ret = -EINVAL;
  882. goto out_finish;
  883. }
  884. btrfs_info(fs_info, "resizing devid %llu", devid);
  885. }
  886. args.devid = devid;
  887. device = btrfs_find_device(fs_info->fs_devices, &args);
  888. if (!device) {
  889. btrfs_info(fs_info, "resizer unable to find device %llu",
  890. devid);
  891. ret = -ENODEV;
  892. goto out_finish;
  893. }
  894. if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
  895. btrfs_info(fs_info,
  896. "resizer unable to apply on readonly device %llu",
  897. devid);
  898. ret = -EPERM;
  899. goto out_finish;
  900. }
  901. if (!strcmp(sizestr, "max"))
  902. new_size = bdev_nr_bytes(device->bdev);
  903. else {
  904. char *retptr;
  905. if (sizestr[0] == '-') {
  906. mod = -1;
  907. sizestr++;
  908. } else if (sizestr[0] == '+') {
  909. mod = 1;
  910. sizestr++;
  911. }
  912. new_size = memparse(sizestr, &retptr);
  913. if (*retptr != '\0' || new_size == 0) {
  914. ret = -EINVAL;
  915. goto out_finish;
  916. }
  917. }
  918. if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
  919. ret = -EPERM;
  920. goto out_finish;
  921. }
  922. old_size = btrfs_device_get_total_bytes(device);
  923. if (mod < 0) {
  924. if (new_size > old_size) {
  925. ret = -EINVAL;
  926. goto out_finish;
  927. }
  928. new_size = old_size - new_size;
  929. } else if (mod > 0) {
  930. if (new_size > ULLONG_MAX - old_size) {
  931. ret = -ERANGE;
  932. goto out_finish;
  933. }
  934. new_size = old_size + new_size;
  935. }
  936. if (new_size < SZ_256M) {
  937. ret = -EINVAL;
  938. goto out_finish;
  939. }
  940. if (new_size > bdev_nr_bytes(device->bdev)) {
  941. ret = -EFBIG;
  942. goto out_finish;
  943. }
  944. new_size = round_down(new_size, fs_info->sectorsize);
  945. if (new_size > old_size) {
  946. struct btrfs_trans_handle *trans;
  947. trans = btrfs_start_transaction(root, 0);
  948. if (IS_ERR(trans)) {
  949. ret = PTR_ERR(trans);
  950. goto out_finish;
  951. }
  952. ret = btrfs_grow_device(trans, device, new_size);
  953. btrfs_commit_transaction(trans);
  954. } else if (new_size < old_size) {
  955. ret = btrfs_shrink_device(device, new_size);
  956. } /* equal, nothing need to do */
  957. if (ret == 0 && new_size != old_size)
  958. btrfs_info(fs_info,
  959. "resize device %s (devid %llu) from %llu to %llu",
  960. btrfs_dev_name(device), device->devid,
  961. old_size, new_size);
  962. out_finish:
  963. btrfs_exclop_finish(fs_info);
  964. out_free:
  965. kfree(vol_args);
  966. out_drop:
  967. mnt_drop_write_file(file);
  968. return ret;
  969. }
  970. static noinline int __btrfs_ioctl_snap_create(struct file *file,
  971. struct mnt_idmap *idmap,
  972. const char *name, unsigned long fd, bool subvol,
  973. bool readonly,
  974. struct btrfs_qgroup_inherit *inherit)
  975. {
  976. int ret;
  977. struct qstr qname = QSTR_INIT(name, strlen(name));
  978. if (!S_ISDIR(file_inode(file)->i_mode))
  979. return -ENOTDIR;
  980. ret = mnt_want_write_file(file);
  981. if (ret)
  982. return ret;
  983. if (strchr(name, '/')) {
  984. ret = -EINVAL;
  985. goto out_drop_write;
  986. }
  987. if (qname.name[0] == '.' &&
  988. (qname.len == 1 || (qname.name[1] == '.' && qname.len == 2))) {
  989. ret = -EEXIST;
  990. goto out_drop_write;
  991. }
  992. if (subvol) {
  993. ret = btrfs_mksubvol(file_dentry(file), idmap, &qname, NULL,
  994. readonly, inherit);
  995. } else {
  996. CLASS(fd, src)(fd);
  997. struct inode *src_inode;
  998. if (fd_empty(src)) {
  999. ret = -EINVAL;
  1000. goto out_drop_write;
  1001. }
  1002. src_inode = file_inode(fd_file(src));
  1003. if (src_inode->i_sb != file_inode(file)->i_sb) {
  1004. btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
  1005. "Snapshot src from another FS");
  1006. ret = -EXDEV;
  1007. } else if (!inode_owner_or_capable(idmap, src_inode)) {
  1008. /*
  1009. * Subvolume creation is not restricted, but snapshots
  1010. * are limited to own subvolumes only
  1011. */
  1012. ret = -EPERM;
  1013. } else if (btrfs_ino(BTRFS_I(src_inode)) != BTRFS_FIRST_FREE_OBJECTID) {
  1014. /*
  1015. * Snapshots must be made with the src_inode referring
  1016. * to the subvolume inode, otherwise the permission
  1017. * checking above is useless because we may have
  1018. * permission on a lower directory but not the subvol
  1019. * itself.
  1020. */
  1021. ret = -EINVAL;
  1022. } else {
  1023. ret = btrfs_mksnapshot(file_dentry(file), idmap, &qname,
  1024. BTRFS_I(src_inode)->root,
  1025. readonly, inherit);
  1026. }
  1027. }
  1028. out_drop_write:
  1029. mnt_drop_write_file(file);
  1030. return ret;
  1031. }
  1032. static noinline int btrfs_ioctl_snap_create(struct file *file,
  1033. void __user *arg, bool subvol)
  1034. {
  1035. struct btrfs_ioctl_vol_args *vol_args;
  1036. int ret;
  1037. if (!S_ISDIR(file_inode(file)->i_mode))
  1038. return -ENOTDIR;
  1039. vol_args = memdup_user(arg, sizeof(*vol_args));
  1040. if (IS_ERR(vol_args))
  1041. return PTR_ERR(vol_args);
  1042. ret = btrfs_check_ioctl_vol_args_path(vol_args);
  1043. if (ret < 0)
  1044. goto out;
  1045. ret = __btrfs_ioctl_snap_create(file, file_mnt_idmap(file),
  1046. vol_args->name, vol_args->fd, subvol,
  1047. false, NULL);
  1048. out:
  1049. kfree(vol_args);
  1050. return ret;
  1051. }
  1052. static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
  1053. void __user *arg, bool subvol)
  1054. {
  1055. struct btrfs_ioctl_vol_args_v2 *vol_args;
  1056. int ret;
  1057. bool readonly = false;
  1058. struct btrfs_qgroup_inherit *inherit = NULL;
  1059. if (!S_ISDIR(file_inode(file)->i_mode))
  1060. return -ENOTDIR;
  1061. vol_args = memdup_user(arg, sizeof(*vol_args));
  1062. if (IS_ERR(vol_args))
  1063. return PTR_ERR(vol_args);
  1064. ret = btrfs_check_ioctl_vol_args2_subvol_name(vol_args);
  1065. if (ret < 0)
  1066. goto free_args;
  1067. if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK) {
  1068. ret = -EOPNOTSUPP;
  1069. goto free_args;
  1070. }
  1071. if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
  1072. readonly = true;
  1073. if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
  1074. struct btrfs_fs_info *fs_info = inode_to_fs_info(file_inode(file));
  1075. if (vol_args->size < sizeof(*inherit) ||
  1076. vol_args->size > PAGE_SIZE) {
  1077. ret = -EINVAL;
  1078. goto free_args;
  1079. }
  1080. inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
  1081. if (IS_ERR(inherit)) {
  1082. ret = PTR_ERR(inherit);
  1083. goto free_args;
  1084. }
  1085. ret = btrfs_qgroup_check_inherit(fs_info, inherit, vol_args->size);
  1086. if (ret < 0)
  1087. goto free_inherit;
  1088. }
  1089. ret = __btrfs_ioctl_snap_create(file, file_mnt_idmap(file),
  1090. vol_args->name, vol_args->fd, subvol,
  1091. readonly, inherit);
  1092. if (ret)
  1093. goto free_inherit;
  1094. free_inherit:
  1095. kfree(inherit);
  1096. free_args:
  1097. kfree(vol_args);
  1098. return ret;
  1099. }
  1100. static noinline int btrfs_ioctl_subvol_getflags(struct btrfs_inode *inode,
  1101. void __user *arg)
  1102. {
  1103. struct btrfs_root *root = inode->root;
  1104. struct btrfs_fs_info *fs_info = root->fs_info;
  1105. int ret = 0;
  1106. u64 flags = 0;
  1107. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
  1108. return -EINVAL;
  1109. down_read(&fs_info->subvol_sem);
  1110. if (btrfs_root_readonly(root))
  1111. flags |= BTRFS_SUBVOL_RDONLY;
  1112. up_read(&fs_info->subvol_sem);
  1113. if (copy_to_user(arg, &flags, sizeof(flags)))
  1114. ret = -EFAULT;
  1115. return ret;
  1116. }
  1117. static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
  1118. void __user *arg)
  1119. {
  1120. struct inode *inode = file_inode(file);
  1121. struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
  1122. struct btrfs_root *root = BTRFS_I(inode)->root;
  1123. struct btrfs_trans_handle *trans;
  1124. u64 root_flags;
  1125. u64 flags;
  1126. int ret;
  1127. if (!inode_owner_or_capable(file_mnt_idmap(file), inode))
  1128. return -EPERM;
  1129. ret = mnt_want_write_file(file);
  1130. if (ret)
  1131. return ret;
  1132. if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
  1133. ret = -EINVAL;
  1134. goto out_drop_write;
  1135. }
  1136. if (copy_from_user(&flags, arg, sizeof(flags))) {
  1137. ret = -EFAULT;
  1138. goto out_drop_write;
  1139. }
  1140. if (flags & ~BTRFS_SUBVOL_RDONLY) {
  1141. ret = -EOPNOTSUPP;
  1142. goto out_drop_write;
  1143. }
  1144. down_write(&fs_info->subvol_sem);
  1145. /* nothing to do */
  1146. if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
  1147. goto out_drop_sem;
  1148. root_flags = btrfs_root_flags(&root->root_item);
  1149. if (flags & BTRFS_SUBVOL_RDONLY) {
  1150. btrfs_set_root_flags(&root->root_item,
  1151. root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
  1152. } else {
  1153. /*
  1154. * Block RO -> RW transition if this subvolume is involved in
  1155. * send
  1156. */
  1157. spin_lock(&root->root_item_lock);
  1158. if (root->send_in_progress == 0) {
  1159. btrfs_set_root_flags(&root->root_item,
  1160. root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
  1161. spin_unlock(&root->root_item_lock);
  1162. } else {
  1163. spin_unlock(&root->root_item_lock);
  1164. btrfs_warn(fs_info,
  1165. "Attempt to set subvolume %llu read-write during send",
  1166. btrfs_root_id(root));
  1167. ret = -EPERM;
  1168. goto out_drop_sem;
  1169. }
  1170. }
  1171. trans = btrfs_start_transaction(root, 1);
  1172. if (IS_ERR(trans)) {
  1173. ret = PTR_ERR(trans);
  1174. goto out_reset;
  1175. }
  1176. ret = btrfs_update_root(trans, fs_info->tree_root,
  1177. &root->root_key, &root->root_item);
  1178. if (ret < 0) {
  1179. btrfs_end_transaction(trans);
  1180. goto out_reset;
  1181. }
  1182. ret = btrfs_commit_transaction(trans);
  1183. out_reset:
  1184. if (ret)
  1185. btrfs_set_root_flags(&root->root_item, root_flags);
  1186. out_drop_sem:
  1187. up_write(&fs_info->subvol_sem);
  1188. out_drop_write:
  1189. mnt_drop_write_file(file);
  1190. return ret;
  1191. }
  1192. static noinline bool key_in_sk(const struct btrfs_key *key,
  1193. const struct btrfs_ioctl_search_key *sk)
  1194. {
  1195. struct btrfs_key test;
  1196. int ret;
  1197. test.objectid = sk->min_objectid;
  1198. test.type = sk->min_type;
  1199. test.offset = sk->min_offset;
  1200. ret = btrfs_comp_cpu_keys(key, &test);
  1201. if (ret < 0)
  1202. return false;
  1203. test.objectid = sk->max_objectid;
  1204. test.type = sk->max_type;
  1205. test.offset = sk->max_offset;
  1206. ret = btrfs_comp_cpu_keys(key, &test);
  1207. if (ret > 0)
  1208. return false;
  1209. return true;
  1210. }
  1211. static noinline int copy_to_sk(struct btrfs_path *path,
  1212. struct btrfs_key *key,
  1213. const struct btrfs_ioctl_search_key *sk,
  1214. u64 *buf_size,
  1215. char __user *ubuf,
  1216. unsigned long *sk_offset,
  1217. int *num_found)
  1218. {
  1219. u64 found_transid;
  1220. struct extent_buffer *leaf;
  1221. struct btrfs_ioctl_search_header sh;
  1222. struct btrfs_key test;
  1223. unsigned long item_off;
  1224. unsigned long item_len;
  1225. int nritems;
  1226. int i;
  1227. int slot;
  1228. int ret = 0;
  1229. leaf = path->nodes[0];
  1230. slot = path->slots[0];
  1231. nritems = btrfs_header_nritems(leaf);
  1232. if (btrfs_header_generation(leaf) > sk->max_transid) {
  1233. i = nritems;
  1234. goto advance_key;
  1235. }
  1236. found_transid = btrfs_header_generation(leaf);
  1237. for (i = slot; i < nritems; i++) {
  1238. item_off = btrfs_item_ptr_offset(leaf, i);
  1239. item_len = btrfs_item_size(leaf, i);
  1240. btrfs_item_key_to_cpu(leaf, key, i);
  1241. if (!key_in_sk(key, sk))
  1242. continue;
  1243. if (sizeof(sh) + item_len > *buf_size) {
  1244. if (*num_found)
  1245. return 1;
  1246. /*
  1247. * return one empty item back for v1, which does not
  1248. * handle -EOVERFLOW
  1249. */
  1250. *buf_size = sizeof(sh) + item_len;
  1251. item_len = 0;
  1252. ret = -EOVERFLOW;
  1253. }
  1254. if (sizeof(sh) + item_len + *sk_offset > *buf_size)
  1255. return 1;
  1256. sh.objectid = key->objectid;
  1257. sh.type = key->type;
  1258. sh.offset = key->offset;
  1259. sh.len = item_len;
  1260. sh.transid = found_transid;
  1261. /*
  1262. * Copy search result header. If we fault then loop again so we
  1263. * can fault in the pages and -EFAULT there if there's a
  1264. * problem. Otherwise we'll fault and then copy the buffer in
  1265. * properly this next time through
  1266. */
  1267. if (copy_to_user_nofault(ubuf + *sk_offset, &sh, sizeof(sh)))
  1268. return 0;
  1269. *sk_offset += sizeof(sh);
  1270. if (item_len) {
  1271. char __user *up = ubuf + *sk_offset;
  1272. /*
  1273. * Copy the item, same behavior as above, but reset the
  1274. * * sk_offset so we copy the full thing again.
  1275. */
  1276. if (read_extent_buffer_to_user_nofault(leaf, up,
  1277. item_off, item_len)) {
  1278. *sk_offset -= sizeof(sh);
  1279. return 0;
  1280. }
  1281. *sk_offset += item_len;
  1282. }
  1283. (*num_found)++;
  1284. /* -EOVERFLOW from above. */
  1285. if (ret)
  1286. return ret;
  1287. if (*num_found >= sk->nr_items)
  1288. return 1;
  1289. }
  1290. advance_key:
  1291. ret = 0;
  1292. test.objectid = sk->max_objectid;
  1293. test.type = sk->max_type;
  1294. test.offset = sk->max_offset;
  1295. if (btrfs_comp_cpu_keys(key, &test) >= 0)
  1296. ret = 1;
  1297. else if (key->offset < (u64)-1)
  1298. key->offset++;
  1299. else if (key->type < (u8)-1) {
  1300. key->offset = 0;
  1301. key->type++;
  1302. } else if (key->objectid < (u64)-1) {
  1303. key->offset = 0;
  1304. key->type = 0;
  1305. key->objectid++;
  1306. } else
  1307. ret = 1;
  1308. /*
  1309. * 0: all items from this leaf copied, continue with next
  1310. * 1: * more items can be copied, but unused buffer is too small
  1311. * * all items were found
  1312. * Either way, it will stops the loop which iterates to the next
  1313. * leaf
  1314. * -EOVERFLOW: item was to large for buffer
  1315. * -EFAULT: could not copy extent buffer back to userspace
  1316. */
  1317. return ret;
  1318. }
  1319. static noinline int search_ioctl(struct btrfs_root *root,
  1320. struct btrfs_ioctl_search_key *sk,
  1321. u64 *buf_size,
  1322. char __user *ubuf)
  1323. {
  1324. struct btrfs_fs_info *info = root->fs_info;
  1325. struct btrfs_key key;
  1326. BTRFS_PATH_AUTO_FREE(path);
  1327. int ret;
  1328. int num_found = 0;
  1329. unsigned long sk_offset = 0;
  1330. if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
  1331. *buf_size = sizeof(struct btrfs_ioctl_search_header);
  1332. return -EOVERFLOW;
  1333. }
  1334. path = btrfs_alloc_path();
  1335. if (!path)
  1336. return -ENOMEM;
  1337. if (sk->tree_id == 0) {
  1338. /* Search the root that we got passed. */
  1339. root = btrfs_grab_root(root);
  1340. } else {
  1341. /* Look up the root from the arguments. */
  1342. root = btrfs_get_fs_root(info, sk->tree_id, true);
  1343. if (IS_ERR(root))
  1344. return PTR_ERR(root);
  1345. }
  1346. key.objectid = sk->min_objectid;
  1347. key.type = sk->min_type;
  1348. key.offset = sk->min_offset;
  1349. while (1) {
  1350. /*
  1351. * Ensure that the whole user buffer is faulted in at sub-page
  1352. * granularity, otherwise the loop may live-lock.
  1353. */
  1354. if (fault_in_subpage_writeable(ubuf + sk_offset, *buf_size - sk_offset)) {
  1355. ret = -EFAULT;
  1356. break;
  1357. }
  1358. ret = btrfs_search_forward(root, &key, path, sk->min_transid);
  1359. if (ret)
  1360. break;
  1361. ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
  1362. &sk_offset, &num_found);
  1363. btrfs_release_path(path);
  1364. if (ret)
  1365. break;
  1366. }
  1367. /* Normalize return values from btrfs_search_forward() and copy_to_sk(). */
  1368. if (ret > 0)
  1369. ret = 0;
  1370. sk->nr_items = num_found;
  1371. btrfs_put_root(root);
  1372. return ret;
  1373. }
  1374. static noinline int btrfs_ioctl_tree_search(struct btrfs_root *root,
  1375. void __user *argp)
  1376. {
  1377. struct btrfs_ioctl_search_args __user *uargs = argp;
  1378. struct btrfs_ioctl_search_key sk;
  1379. int ret;
  1380. u64 buf_size;
  1381. if (!capable(CAP_SYS_ADMIN))
  1382. return -EPERM;
  1383. if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
  1384. return -EFAULT;
  1385. buf_size = sizeof(uargs->buf);
  1386. ret = search_ioctl(root, &sk, &buf_size, uargs->buf);
  1387. /*
  1388. * In the origin implementation an overflow is handled by returning a
  1389. * search header with a len of zero, so reset ret.
  1390. */
  1391. if (ret == -EOVERFLOW)
  1392. ret = 0;
  1393. if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
  1394. ret = -EFAULT;
  1395. return ret;
  1396. }
  1397. static noinline int btrfs_ioctl_tree_search_v2(struct btrfs_root *root,
  1398. void __user *argp)
  1399. {
  1400. struct btrfs_ioctl_search_args_v2 __user *uarg = argp;
  1401. struct btrfs_ioctl_search_args_v2 args;
  1402. int ret;
  1403. u64 buf_size;
  1404. const u64 buf_limit = SZ_16M;
  1405. if (!capable(CAP_SYS_ADMIN))
  1406. return -EPERM;
  1407. /* copy search header and buffer size */
  1408. if (copy_from_user(&args, uarg, sizeof(args)))
  1409. return -EFAULT;
  1410. buf_size = args.buf_size;
  1411. /* limit result size to 16MB */
  1412. if (buf_size > buf_limit)
  1413. buf_size = buf_limit;
  1414. ret = search_ioctl(root, &args.key, &buf_size,
  1415. (char __user *)(&uarg->buf[0]));
  1416. if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
  1417. ret = -EFAULT;
  1418. else if (ret == -EOVERFLOW &&
  1419. copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
  1420. ret = -EFAULT;
  1421. return ret;
  1422. }
  1423. /*
  1424. * Search INODE_REFs to identify path name of 'dirid' directory
  1425. * in a 'tree_id' tree. and sets path name to 'name'.
  1426. */
  1427. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  1428. u64 tree_id, u64 dirid, char *name)
  1429. {
  1430. struct btrfs_root *root;
  1431. struct btrfs_key key;
  1432. char *ptr;
  1433. int ret = -1;
  1434. int slot;
  1435. int len;
  1436. int total_len = 0;
  1437. struct btrfs_inode_ref *iref;
  1438. struct extent_buffer *l;
  1439. BTRFS_PATH_AUTO_FREE(path);
  1440. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  1441. name[0]='\0';
  1442. return 0;
  1443. }
  1444. path = btrfs_alloc_path();
  1445. if (!path)
  1446. return -ENOMEM;
  1447. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
  1448. root = btrfs_get_fs_root(info, tree_id, true);
  1449. if (IS_ERR(root)) {
  1450. ret = PTR_ERR(root);
  1451. root = NULL;
  1452. goto out;
  1453. }
  1454. key.objectid = dirid;
  1455. key.type = BTRFS_INODE_REF_KEY;
  1456. key.offset = (u64)-1;
  1457. while (1) {
  1458. ret = btrfs_search_backwards(root, &key, path);
  1459. if (ret < 0)
  1460. goto out;
  1461. else if (ret > 0) {
  1462. ret = -ENOENT;
  1463. goto out;
  1464. }
  1465. l = path->nodes[0];
  1466. slot = path->slots[0];
  1467. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  1468. len = btrfs_inode_ref_name_len(l, iref);
  1469. ptr -= len + 1;
  1470. total_len += len + 1;
  1471. if (ptr < name) {
  1472. ret = -ENAMETOOLONG;
  1473. goto out;
  1474. }
  1475. *(ptr + len) = '/';
  1476. read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
  1477. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  1478. break;
  1479. btrfs_release_path(path);
  1480. key.objectid = key.offset;
  1481. key.offset = (u64)-1;
  1482. dirid = key.objectid;
  1483. }
  1484. memmove(name, ptr, total_len);
  1485. name[total_len] = '\0';
  1486. ret = 0;
  1487. out:
  1488. btrfs_put_root(root);
  1489. return ret;
  1490. }
  1491. static int btrfs_search_path_in_tree_user(struct mnt_idmap *idmap,
  1492. struct inode *inode,
  1493. struct btrfs_ioctl_ino_lookup_user_args *args)
  1494. {
  1495. struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
  1496. u64 upper_limit = btrfs_ino(BTRFS_I(inode));
  1497. u64 treeid = btrfs_root_id(BTRFS_I(inode)->root);
  1498. u64 dirid = args->dirid;
  1499. unsigned long item_off;
  1500. unsigned long item_len;
  1501. struct btrfs_inode_ref *iref;
  1502. struct btrfs_root_ref *rref;
  1503. struct btrfs_root *root = NULL;
  1504. BTRFS_PATH_AUTO_FREE(path);
  1505. struct btrfs_key key;
  1506. struct extent_buffer *leaf;
  1507. char *ptr;
  1508. int slot;
  1509. int len;
  1510. int total_len = 0;
  1511. int ret;
  1512. path = btrfs_alloc_path();
  1513. if (!path)
  1514. return -ENOMEM;
  1515. /*
  1516. * If the bottom subvolume does not exist directly under upper_limit,
  1517. * construct the path in from the bottom up.
  1518. */
  1519. if (dirid != upper_limit) {
  1520. ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
  1521. root = btrfs_get_fs_root(fs_info, treeid, true);
  1522. if (IS_ERR(root))
  1523. return PTR_ERR(root);
  1524. key.objectid = dirid;
  1525. key.type = BTRFS_INODE_REF_KEY;
  1526. key.offset = (u64)-1;
  1527. while (1) {
  1528. struct btrfs_inode *temp_inode;
  1529. ret = btrfs_search_backwards(root, &key, path);
  1530. if (ret < 0)
  1531. goto out_put;
  1532. else if (ret > 0) {
  1533. ret = -ENOENT;
  1534. goto out_put;
  1535. }
  1536. leaf = path->nodes[0];
  1537. slot = path->slots[0];
  1538. iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
  1539. len = btrfs_inode_ref_name_len(leaf, iref);
  1540. ptr -= len + 1;
  1541. total_len += len + 1;
  1542. if (ptr < args->path) {
  1543. ret = -ENAMETOOLONG;
  1544. goto out_put;
  1545. }
  1546. *(ptr + len) = '/';
  1547. read_extent_buffer(leaf, ptr,
  1548. (unsigned long)(iref + 1), len);
  1549. /*
  1550. * We don't need the path anymore, so release it and
  1551. * avoid deadlocks and lockdep warnings in case
  1552. * btrfs_iget() needs to lookup the inode from its root
  1553. * btree and lock the same leaf.
  1554. */
  1555. btrfs_release_path(path);
  1556. temp_inode = btrfs_iget(key.offset, root);
  1557. if (IS_ERR(temp_inode)) {
  1558. ret = PTR_ERR(temp_inode);
  1559. goto out_put;
  1560. }
  1561. /* Check the read+exec permission of this directory. */
  1562. ret = inode_permission(idmap, &temp_inode->vfs_inode,
  1563. MAY_READ | MAY_EXEC);
  1564. iput(&temp_inode->vfs_inode);
  1565. if (ret)
  1566. goto out_put;
  1567. if (key.offset == upper_limit)
  1568. break;
  1569. if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
  1570. ret = -EACCES;
  1571. goto out_put;
  1572. }
  1573. key.objectid = key.offset;
  1574. key.offset = (u64)-1;
  1575. dirid = key.objectid;
  1576. }
  1577. memmove(args->path, ptr, total_len);
  1578. args->path[total_len] = '\0';
  1579. btrfs_put_root(root);
  1580. root = NULL;
  1581. btrfs_release_path(path);
  1582. }
  1583. /* Get the bottom subvolume's name from ROOT_REF */
  1584. key.objectid = treeid;
  1585. key.type = BTRFS_ROOT_REF_KEY;
  1586. key.offset = args->treeid;
  1587. ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
  1588. if (ret < 0)
  1589. return ret;
  1590. else if (ret > 0)
  1591. return -ENOENT;
  1592. leaf = path->nodes[0];
  1593. slot = path->slots[0];
  1594. btrfs_item_key_to_cpu(leaf, &key, slot);
  1595. item_off = btrfs_item_ptr_offset(leaf, slot);
  1596. item_len = btrfs_item_size(leaf, slot);
  1597. /* Check if dirid in ROOT_REF corresponds to passed dirid */
  1598. rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
  1599. if (args->dirid != btrfs_root_ref_dirid(leaf, rref))
  1600. return -EINVAL;
  1601. /* Copy subvolume's name */
  1602. item_off += sizeof(struct btrfs_root_ref);
  1603. item_len -= sizeof(struct btrfs_root_ref);
  1604. read_extent_buffer(leaf, args->name, item_off, item_len);
  1605. args->name[item_len] = 0;
  1606. out_put:
  1607. btrfs_put_root(root);
  1608. return ret;
  1609. }
  1610. static noinline int btrfs_ioctl_ino_lookup(struct btrfs_root *root,
  1611. void __user *argp)
  1612. {
  1613. struct btrfs_ioctl_ino_lookup_args *args;
  1614. int ret = 0;
  1615. args = memdup_user(argp, sizeof(*args));
  1616. if (IS_ERR(args))
  1617. return PTR_ERR(args);
  1618. /*
  1619. * Unprivileged query to obtain the containing subvolume root id. The
  1620. * path is reset so it's consistent with btrfs_search_path_in_tree.
  1621. */
  1622. if (args->treeid == 0)
  1623. args->treeid = btrfs_root_id(root);
  1624. if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
  1625. args->name[0] = 0;
  1626. goto out;
  1627. }
  1628. if (!capable(CAP_SYS_ADMIN)) {
  1629. ret = -EPERM;
  1630. goto out;
  1631. }
  1632. ret = btrfs_search_path_in_tree(root->fs_info,
  1633. args->treeid, args->objectid,
  1634. args->name);
  1635. out:
  1636. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1637. ret = -EFAULT;
  1638. kfree(args);
  1639. return ret;
  1640. }
  1641. /*
  1642. * Version of ino_lookup ioctl (unprivileged)
  1643. *
  1644. * The main differences from ino_lookup ioctl are:
  1645. *
  1646. * 1. Read + Exec permission will be checked using inode_permission() during
  1647. * path construction. -EACCES will be returned in case of failure.
  1648. * 2. Path construction will be stopped at the inode number which corresponds
  1649. * to the fd with which this ioctl is called. If constructed path does not
  1650. * exist under fd's inode, -EACCES will be returned.
  1651. * 3. The name of bottom subvolume is also searched and filled.
  1652. */
  1653. static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
  1654. {
  1655. struct btrfs_ioctl_ino_lookup_user_args *args;
  1656. struct inode *inode;
  1657. int ret;
  1658. args = memdup_user(argp, sizeof(*args));
  1659. if (IS_ERR(args))
  1660. return PTR_ERR(args);
  1661. inode = file_inode(file);
  1662. if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
  1663. btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
  1664. /*
  1665. * The subvolume does not exist under fd with which this is
  1666. * called
  1667. */
  1668. kfree(args);
  1669. return -EACCES;
  1670. }
  1671. ret = btrfs_search_path_in_tree_user(file_mnt_idmap(file), inode, args);
  1672. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1673. ret = -EFAULT;
  1674. kfree(args);
  1675. return ret;
  1676. }
  1677. /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
  1678. static int btrfs_ioctl_get_subvol_info(struct inode *inode, void __user *argp)
  1679. {
  1680. struct btrfs_ioctl_get_subvol_info_args *subvol_info;
  1681. struct btrfs_fs_info *fs_info;
  1682. struct btrfs_root *root;
  1683. struct btrfs_path *path;
  1684. struct btrfs_key key;
  1685. struct btrfs_root_item *root_item;
  1686. struct btrfs_root_ref *rref;
  1687. struct extent_buffer *leaf;
  1688. unsigned long item_off;
  1689. unsigned long item_len;
  1690. int slot;
  1691. int ret = 0;
  1692. path = btrfs_alloc_path();
  1693. if (!path)
  1694. return -ENOMEM;
  1695. subvol_info = kzalloc_obj(*subvol_info);
  1696. if (!subvol_info) {
  1697. btrfs_free_path(path);
  1698. return -ENOMEM;
  1699. }
  1700. fs_info = BTRFS_I(inode)->root->fs_info;
  1701. /* Get root_item of inode's subvolume */
  1702. key.objectid = btrfs_root_id(BTRFS_I(inode)->root);
  1703. root = btrfs_get_fs_root(fs_info, key.objectid, true);
  1704. if (IS_ERR(root)) {
  1705. ret = PTR_ERR(root);
  1706. goto out_free;
  1707. }
  1708. root_item = &root->root_item;
  1709. subvol_info->treeid = key.objectid;
  1710. subvol_info->generation = btrfs_root_generation(root_item);
  1711. subvol_info->flags = btrfs_root_flags(root_item);
  1712. memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
  1713. memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
  1714. BTRFS_UUID_SIZE);
  1715. memcpy(subvol_info->received_uuid, root_item->received_uuid,
  1716. BTRFS_UUID_SIZE);
  1717. subvol_info->ctransid = btrfs_root_ctransid(root_item);
  1718. subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
  1719. subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
  1720. subvol_info->otransid = btrfs_root_otransid(root_item);
  1721. subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
  1722. subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
  1723. subvol_info->stransid = btrfs_root_stransid(root_item);
  1724. subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
  1725. subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
  1726. subvol_info->rtransid = btrfs_root_rtransid(root_item);
  1727. subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
  1728. subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
  1729. if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
  1730. /* Search root tree for ROOT_BACKREF of this subvolume */
  1731. key.type = BTRFS_ROOT_BACKREF_KEY;
  1732. key.offset = 0;
  1733. ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
  1734. if (ret < 0) {
  1735. goto out;
  1736. } else if (path->slots[0] >=
  1737. btrfs_header_nritems(path->nodes[0])) {
  1738. ret = btrfs_next_leaf(fs_info->tree_root, path);
  1739. if (ret < 0) {
  1740. goto out;
  1741. } else if (unlikely(ret > 0)) {
  1742. ret = -EUCLEAN;
  1743. goto out;
  1744. }
  1745. }
  1746. leaf = path->nodes[0];
  1747. slot = path->slots[0];
  1748. btrfs_item_key_to_cpu(leaf, &key, slot);
  1749. if (key.objectid == subvol_info->treeid &&
  1750. key.type == BTRFS_ROOT_BACKREF_KEY) {
  1751. subvol_info->parent_id = key.offset;
  1752. rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
  1753. subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
  1754. item_off = btrfs_item_ptr_offset(leaf, slot)
  1755. + sizeof(struct btrfs_root_ref);
  1756. item_len = btrfs_item_size(leaf, slot)
  1757. - sizeof(struct btrfs_root_ref);
  1758. read_extent_buffer(leaf, subvol_info->name,
  1759. item_off, item_len);
  1760. } else {
  1761. ret = -ENOENT;
  1762. goto out;
  1763. }
  1764. }
  1765. btrfs_free_path(path);
  1766. path = NULL;
  1767. if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
  1768. ret = -EFAULT;
  1769. out:
  1770. btrfs_put_root(root);
  1771. out_free:
  1772. btrfs_free_path(path);
  1773. kfree(subvol_info);
  1774. return ret;
  1775. }
  1776. /*
  1777. * Return ROOT_REF information of the subvolume containing this inode
  1778. * except the subvolume name.
  1779. */
  1780. static int btrfs_ioctl_get_subvol_rootref(struct btrfs_root *root,
  1781. void __user *argp)
  1782. {
  1783. struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
  1784. struct btrfs_root_ref *rref;
  1785. struct btrfs_path *path;
  1786. struct btrfs_key key;
  1787. struct extent_buffer *leaf;
  1788. u64 objectid;
  1789. int slot;
  1790. int ret;
  1791. u8 found;
  1792. path = btrfs_alloc_path();
  1793. if (!path)
  1794. return -ENOMEM;
  1795. rootrefs = memdup_user(argp, sizeof(*rootrefs));
  1796. if (IS_ERR(rootrefs)) {
  1797. btrfs_free_path(path);
  1798. return PTR_ERR(rootrefs);
  1799. }
  1800. objectid = btrfs_root_id(root);
  1801. key.objectid = objectid;
  1802. key.type = BTRFS_ROOT_REF_KEY;
  1803. key.offset = rootrefs->min_treeid;
  1804. found = 0;
  1805. root = root->fs_info->tree_root;
  1806. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1807. if (ret < 0) {
  1808. goto out;
  1809. } else if (path->slots[0] >=
  1810. btrfs_header_nritems(path->nodes[0])) {
  1811. ret = btrfs_next_leaf(root, path);
  1812. if (ret < 0) {
  1813. goto out;
  1814. } else if (unlikely(ret > 0)) {
  1815. ret = -EUCLEAN;
  1816. goto out;
  1817. }
  1818. }
  1819. while (1) {
  1820. leaf = path->nodes[0];
  1821. slot = path->slots[0];
  1822. btrfs_item_key_to_cpu(leaf, &key, slot);
  1823. if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
  1824. ret = 0;
  1825. goto out;
  1826. }
  1827. if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
  1828. ret = -EOVERFLOW;
  1829. goto out;
  1830. }
  1831. rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
  1832. rootrefs->rootref[found].treeid = key.offset;
  1833. rootrefs->rootref[found].dirid =
  1834. btrfs_root_ref_dirid(leaf, rref);
  1835. found++;
  1836. ret = btrfs_next_item(root, path);
  1837. if (ret < 0) {
  1838. goto out;
  1839. } else if (unlikely(ret > 0)) {
  1840. ret = -EUCLEAN;
  1841. goto out;
  1842. }
  1843. }
  1844. out:
  1845. btrfs_free_path(path);
  1846. if (!ret || ret == -EOVERFLOW) {
  1847. rootrefs->num_items = found;
  1848. /* update min_treeid for next search */
  1849. if (found)
  1850. rootrefs->min_treeid =
  1851. rootrefs->rootref[found - 1].treeid + 1;
  1852. if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
  1853. ret = -EFAULT;
  1854. }
  1855. kfree(rootrefs);
  1856. return ret;
  1857. }
  1858. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  1859. void __user *arg,
  1860. bool destroy_v2)
  1861. {
  1862. struct dentry *parent = file->f_path.dentry;
  1863. struct dentry *dentry;
  1864. struct inode *dir = d_inode(parent);
  1865. struct btrfs_fs_info *fs_info = inode_to_fs_info(dir);
  1866. struct inode *inode;
  1867. struct btrfs_root *root = BTRFS_I(dir)->root;
  1868. struct btrfs_root *dest = NULL;
  1869. struct btrfs_ioctl_vol_args *vol_args = NULL;
  1870. struct btrfs_ioctl_vol_args_v2 *vol_args2 = NULL;
  1871. struct mnt_idmap *idmap = file_mnt_idmap(file);
  1872. char *subvol_name, *subvol_name_ptr = NULL;
  1873. int ret = 0;
  1874. bool destroy_parent = false;
  1875. /* We don't support snapshots with extent tree v2 yet. */
  1876. if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
  1877. btrfs_err(fs_info,
  1878. "extent tree v2 doesn't support snapshot deletion yet");
  1879. return -EOPNOTSUPP;
  1880. }
  1881. if (destroy_v2) {
  1882. vol_args2 = memdup_user(arg, sizeof(*vol_args2));
  1883. if (IS_ERR(vol_args2))
  1884. return PTR_ERR(vol_args2);
  1885. if (vol_args2->flags & ~BTRFS_SUBVOL_DELETE_ARGS_MASK) {
  1886. ret = -EOPNOTSUPP;
  1887. goto out;
  1888. }
  1889. /*
  1890. * If SPEC_BY_ID is not set, we are looking for the subvolume by
  1891. * name, same as v1 currently does.
  1892. */
  1893. if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) {
  1894. ret = btrfs_check_ioctl_vol_args2_subvol_name(vol_args2);
  1895. if (ret < 0)
  1896. goto out;
  1897. subvol_name = vol_args2->name;
  1898. ret = mnt_want_write_file(file);
  1899. if (ret)
  1900. goto out;
  1901. } else {
  1902. struct inode *old_dir;
  1903. if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID) {
  1904. ret = -EINVAL;
  1905. goto out;
  1906. }
  1907. ret = mnt_want_write_file(file);
  1908. if (ret)
  1909. goto out;
  1910. dentry = btrfs_get_dentry(fs_info->sb,
  1911. BTRFS_FIRST_FREE_OBJECTID,
  1912. vol_args2->subvolid, 0);
  1913. if (IS_ERR(dentry)) {
  1914. ret = PTR_ERR(dentry);
  1915. goto out_drop_write;
  1916. }
  1917. /*
  1918. * Change the default parent since the subvolume being
  1919. * deleted can be outside of the current mount point.
  1920. */
  1921. parent = btrfs_get_parent(dentry);
  1922. /*
  1923. * At this point dentry->d_name can point to '/' if the
  1924. * subvolume we want to destroy is outsite of the
  1925. * current mount point, so we need to release the
  1926. * current dentry and execute the lookup to return a new
  1927. * one with ->d_name pointing to the
  1928. * <mount point>/subvol_name.
  1929. */
  1930. dput(dentry);
  1931. if (IS_ERR(parent)) {
  1932. ret = PTR_ERR(parent);
  1933. goto out_drop_write;
  1934. }
  1935. old_dir = dir;
  1936. dir = d_inode(parent);
  1937. /*
  1938. * If v2 was used with SPEC_BY_ID, a new parent was
  1939. * allocated since the subvolume can be outside of the
  1940. * current mount point. Later on we need to release this
  1941. * new parent dentry.
  1942. */
  1943. destroy_parent = true;
  1944. /*
  1945. * On idmapped mounts, deletion via subvolid is
  1946. * restricted to subvolumes that are immediate
  1947. * ancestors of the inode referenced by the file
  1948. * descriptor in the ioctl. Otherwise the idmapping
  1949. * could potentially be abused to delete subvolumes
  1950. * anywhere in the filesystem the user wouldn't be able
  1951. * to delete without an idmapped mount.
  1952. */
  1953. if (old_dir != dir && idmap != &nop_mnt_idmap) {
  1954. ret = -EOPNOTSUPP;
  1955. goto free_parent;
  1956. }
  1957. subvol_name_ptr = btrfs_get_subvol_name_from_objectid(
  1958. fs_info, vol_args2->subvolid);
  1959. if (IS_ERR(subvol_name_ptr)) {
  1960. ret = PTR_ERR(subvol_name_ptr);
  1961. goto free_parent;
  1962. }
  1963. /* subvol_name_ptr is already nul terminated */
  1964. subvol_name = (char *)kbasename(subvol_name_ptr);
  1965. }
  1966. } else {
  1967. vol_args = memdup_user(arg, sizeof(*vol_args));
  1968. if (IS_ERR(vol_args))
  1969. return PTR_ERR(vol_args);
  1970. ret = btrfs_check_ioctl_vol_args_path(vol_args);
  1971. if (ret < 0)
  1972. goto out;
  1973. subvol_name = vol_args->name;
  1974. ret = mnt_want_write_file(file);
  1975. if (ret)
  1976. goto out;
  1977. }
  1978. if (strchr(subvol_name, '/') ||
  1979. strcmp(subvol_name, "..") == 0) {
  1980. ret = -EINVAL;
  1981. goto free_subvol_name;
  1982. }
  1983. if (!S_ISDIR(dir->i_mode)) {
  1984. ret = -ENOTDIR;
  1985. goto free_subvol_name;
  1986. }
  1987. dentry = start_removing_killable(idmap, parent, &QSTR(subvol_name));
  1988. if (IS_ERR(dentry)) {
  1989. ret = PTR_ERR(dentry);
  1990. goto out_end_removing;
  1991. }
  1992. inode = d_inode(dentry);
  1993. dest = BTRFS_I(inode)->root;
  1994. if (!capable(CAP_SYS_ADMIN)) {
  1995. /*
  1996. * Regular user. Only allow this with a special mount
  1997. * option, when the user has write+exec access to the
  1998. * subvol root, and when rmdir(2) would have been
  1999. * allowed.
  2000. *
  2001. * Note that this is _not_ check that the subvol is
  2002. * empty or doesn't contain data that we wouldn't
  2003. * otherwise be able to delete.
  2004. *
  2005. * Users who want to delete empty subvols should try
  2006. * rmdir(2).
  2007. */
  2008. ret = -EPERM;
  2009. if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
  2010. goto out_end_removing;
  2011. /*
  2012. * Do not allow deletion if the parent dir is the same
  2013. * as the dir to be deleted. That means the ioctl
  2014. * must be called on the dentry referencing the root
  2015. * of the subvol, not a random directory contained
  2016. * within it.
  2017. */
  2018. ret = -EINVAL;
  2019. if (root == dest)
  2020. goto out_end_removing;
  2021. ret = inode_permission(idmap, inode, MAY_WRITE | MAY_EXEC);
  2022. if (ret)
  2023. goto out_end_removing;
  2024. }
  2025. /* check if subvolume may be deleted by a user */
  2026. ret = may_delete_dentry(idmap, dir, dentry, true);
  2027. if (ret)
  2028. goto out_end_removing;
  2029. if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
  2030. ret = -EINVAL;
  2031. goto out_end_removing;
  2032. }
  2033. btrfs_inode_lock(BTRFS_I(inode), 0);
  2034. ret = btrfs_delete_subvolume(BTRFS_I(dir), dentry);
  2035. btrfs_inode_unlock(BTRFS_I(inode), 0);
  2036. if (!ret)
  2037. d_delete_notify(dir, dentry);
  2038. out_end_removing:
  2039. end_removing(dentry);
  2040. free_subvol_name:
  2041. kfree(subvol_name_ptr);
  2042. free_parent:
  2043. if (destroy_parent)
  2044. dput(parent);
  2045. out_drop_write:
  2046. mnt_drop_write_file(file);
  2047. out:
  2048. kfree(vol_args2);
  2049. kfree(vol_args);
  2050. return ret;
  2051. }
  2052. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  2053. {
  2054. struct inode *inode = file_inode(file);
  2055. struct btrfs_root *root = BTRFS_I(inode)->root;
  2056. struct btrfs_ioctl_defrag_range_args range = {0};
  2057. int ret;
  2058. ret = mnt_want_write_file(file);
  2059. if (ret)
  2060. return ret;
  2061. if (btrfs_root_readonly(root)) {
  2062. ret = -EROFS;
  2063. goto out;
  2064. }
  2065. switch (inode->i_mode & S_IFMT) {
  2066. case S_IFDIR:
  2067. if (!capable(CAP_SYS_ADMIN)) {
  2068. ret = -EPERM;
  2069. goto out;
  2070. }
  2071. ret = btrfs_defrag_root(root);
  2072. break;
  2073. case S_IFREG:
  2074. /*
  2075. * Note that this does not check the file descriptor for write
  2076. * access. This prevents defragmenting executables that are
  2077. * running and allows defrag on files open in read-only mode.
  2078. */
  2079. if (!capable(CAP_SYS_ADMIN) &&
  2080. inode_permission(&nop_mnt_idmap, inode, MAY_WRITE)) {
  2081. ret = -EPERM;
  2082. goto out;
  2083. }
  2084. /*
  2085. * Don't allow defrag on pre-content watched files, as it could
  2086. * populate the page cache with 0's via readahead.
  2087. */
  2088. if (unlikely(FMODE_FSNOTIFY_HSM(file->f_mode))) {
  2089. ret = -EINVAL;
  2090. goto out;
  2091. }
  2092. if (argp) {
  2093. if (copy_from_user(&range, argp, sizeof(range))) {
  2094. ret = -EFAULT;
  2095. goto out;
  2096. }
  2097. if (range.flags & ~BTRFS_DEFRAG_RANGE_FLAGS_SUPP) {
  2098. ret = -EOPNOTSUPP;
  2099. goto out;
  2100. }
  2101. if ((range.flags & BTRFS_DEFRAG_RANGE_COMPRESS) &&
  2102. (range.flags & BTRFS_DEFRAG_RANGE_NOCOMPRESS)) {
  2103. ret = -EINVAL;
  2104. goto out;
  2105. }
  2106. /* Compression or no-compression require to start the IO. */
  2107. if ((range.flags & BTRFS_DEFRAG_RANGE_COMPRESS) ||
  2108. (range.flags & BTRFS_DEFRAG_RANGE_NOCOMPRESS)) {
  2109. range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
  2110. range.extent_thresh = (u32)-1;
  2111. }
  2112. } else {
  2113. /* the rest are all set to zero by kzalloc */
  2114. range.len = (u64)-1;
  2115. }
  2116. ret = btrfs_defrag_file(BTRFS_I(file_inode(file)), &file->f_ra,
  2117. &range, BTRFS_OLDEST_GENERATION, 0);
  2118. if (ret > 0)
  2119. ret = 0;
  2120. break;
  2121. default:
  2122. ret = -EINVAL;
  2123. }
  2124. out:
  2125. mnt_drop_write_file(file);
  2126. return ret;
  2127. }
  2128. static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
  2129. {
  2130. struct btrfs_ioctl_vol_args *vol_args;
  2131. bool restore_op = false;
  2132. int ret;
  2133. if (!capable(CAP_SYS_ADMIN))
  2134. return -EPERM;
  2135. if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
  2136. btrfs_err(fs_info, "device add not supported on extent tree v2 yet");
  2137. return -EINVAL;
  2138. }
  2139. if (fs_info->fs_devices->temp_fsid) {
  2140. btrfs_err(fs_info,
  2141. "device add not supported on cloned temp-fsid mount");
  2142. return -EINVAL;
  2143. }
  2144. if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_ADD)) {
  2145. if (!btrfs_exclop_start_try_lock(fs_info, BTRFS_EXCLOP_DEV_ADD))
  2146. return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  2147. /*
  2148. * We can do the device add because we have a paused balanced,
  2149. * change the exclusive op type and remember we should bring
  2150. * back the paused balance
  2151. */
  2152. fs_info->exclusive_operation = BTRFS_EXCLOP_DEV_ADD;
  2153. btrfs_exclop_start_unlock(fs_info);
  2154. restore_op = true;
  2155. }
  2156. vol_args = memdup_user(arg, sizeof(*vol_args));
  2157. if (IS_ERR(vol_args)) {
  2158. ret = PTR_ERR(vol_args);
  2159. goto out;
  2160. }
  2161. ret = btrfs_check_ioctl_vol_args_path(vol_args);
  2162. if (ret < 0)
  2163. goto out_free;
  2164. ret = btrfs_init_new_device(fs_info, vol_args->name);
  2165. if (!ret)
  2166. btrfs_info(fs_info, "disk added %s", vol_args->name);
  2167. out_free:
  2168. kfree(vol_args);
  2169. out:
  2170. if (restore_op)
  2171. btrfs_exclop_balance(fs_info, BTRFS_EXCLOP_BALANCE_PAUSED);
  2172. else
  2173. btrfs_exclop_finish(fs_info);
  2174. return ret;
  2175. }
  2176. static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
  2177. {
  2178. BTRFS_DEV_LOOKUP_ARGS(args);
  2179. struct inode *inode = file_inode(file);
  2180. struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
  2181. struct btrfs_ioctl_vol_args_v2 *vol_args;
  2182. struct file *bdev_file = NULL;
  2183. int ret;
  2184. bool cancel = false;
  2185. if (!capable(CAP_SYS_ADMIN))
  2186. return -EPERM;
  2187. vol_args = memdup_user(arg, sizeof(*vol_args));
  2188. if (IS_ERR(vol_args))
  2189. return PTR_ERR(vol_args);
  2190. if (vol_args->flags & ~BTRFS_DEVICE_REMOVE_ARGS_MASK) {
  2191. ret = -EOPNOTSUPP;
  2192. goto out;
  2193. }
  2194. ret = btrfs_check_ioctl_vol_args2_subvol_name(vol_args);
  2195. if (ret < 0)
  2196. goto out;
  2197. if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
  2198. args.devid = vol_args->devid;
  2199. } else if (!strcmp("cancel", vol_args->name)) {
  2200. cancel = true;
  2201. } else {
  2202. ret = btrfs_get_dev_args_from_path(fs_info, &args, vol_args->name);
  2203. if (ret)
  2204. goto out;
  2205. }
  2206. ret = mnt_want_write_file(file);
  2207. if (ret)
  2208. goto out;
  2209. ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_DEV_REMOVE,
  2210. cancel);
  2211. if (ret)
  2212. goto err_drop;
  2213. /* Exclusive operation is now claimed */
  2214. ret = btrfs_rm_device(fs_info, &args, &bdev_file);
  2215. btrfs_exclop_finish(fs_info);
  2216. if (!ret) {
  2217. if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
  2218. btrfs_info(fs_info, "device deleted: id %llu",
  2219. vol_args->devid);
  2220. else
  2221. btrfs_info(fs_info, "device deleted: %s",
  2222. vol_args->name);
  2223. }
  2224. err_drop:
  2225. mnt_drop_write_file(file);
  2226. if (bdev_file)
  2227. bdev_fput(bdev_file);
  2228. out:
  2229. btrfs_put_dev_args_from_path(&args);
  2230. kfree(vol_args);
  2231. return ret;
  2232. }
  2233. static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
  2234. {
  2235. BTRFS_DEV_LOOKUP_ARGS(args);
  2236. struct inode *inode = file_inode(file);
  2237. struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
  2238. struct btrfs_ioctl_vol_args *vol_args;
  2239. struct file *bdev_file = NULL;
  2240. int ret;
  2241. bool cancel = false;
  2242. if (!capable(CAP_SYS_ADMIN))
  2243. return -EPERM;
  2244. vol_args = memdup_user(arg, sizeof(*vol_args));
  2245. if (IS_ERR(vol_args))
  2246. return PTR_ERR(vol_args);
  2247. ret = btrfs_check_ioctl_vol_args_path(vol_args);
  2248. if (ret < 0)
  2249. goto out_free;
  2250. if (!strcmp("cancel", vol_args->name)) {
  2251. cancel = true;
  2252. } else {
  2253. ret = btrfs_get_dev_args_from_path(fs_info, &args, vol_args->name);
  2254. if (ret)
  2255. goto out;
  2256. }
  2257. ret = mnt_want_write_file(file);
  2258. if (ret)
  2259. goto out;
  2260. ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_DEV_REMOVE,
  2261. cancel);
  2262. if (ret == 0) {
  2263. ret = btrfs_rm_device(fs_info, &args, &bdev_file);
  2264. if (!ret)
  2265. btrfs_info(fs_info, "disk deleted %s", vol_args->name);
  2266. btrfs_exclop_finish(fs_info);
  2267. }
  2268. mnt_drop_write_file(file);
  2269. if (bdev_file)
  2270. bdev_fput(bdev_file);
  2271. out:
  2272. btrfs_put_dev_args_from_path(&args);
  2273. out_free:
  2274. kfree(vol_args);
  2275. return ret;
  2276. }
  2277. static long btrfs_ioctl_fs_info(const struct btrfs_fs_info *fs_info,
  2278. void __user *arg)
  2279. {
  2280. struct btrfs_ioctl_fs_info_args *fi_args;
  2281. struct btrfs_device *device;
  2282. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  2283. u64 flags_in;
  2284. int ret = 0;
  2285. fi_args = memdup_user(arg, sizeof(*fi_args));
  2286. if (IS_ERR(fi_args))
  2287. return PTR_ERR(fi_args);
  2288. flags_in = fi_args->flags;
  2289. memset(fi_args, 0, sizeof(*fi_args));
  2290. rcu_read_lock();
  2291. fi_args->num_devices = fs_devices->num_devices;
  2292. list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
  2293. if (device->devid > fi_args->max_id)
  2294. fi_args->max_id = device->devid;
  2295. }
  2296. rcu_read_unlock();
  2297. memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
  2298. fi_args->nodesize = fs_info->nodesize;
  2299. fi_args->sectorsize = fs_info->sectorsize;
  2300. fi_args->clone_alignment = fs_info->sectorsize;
  2301. if (flags_in & BTRFS_FS_INFO_FLAG_CSUM_INFO) {
  2302. fi_args->csum_type = btrfs_super_csum_type(fs_info->super_copy);
  2303. fi_args->csum_size = btrfs_super_csum_size(fs_info->super_copy);
  2304. fi_args->flags |= BTRFS_FS_INFO_FLAG_CSUM_INFO;
  2305. }
  2306. if (flags_in & BTRFS_FS_INFO_FLAG_GENERATION) {
  2307. fi_args->generation = btrfs_get_fs_generation(fs_info);
  2308. fi_args->flags |= BTRFS_FS_INFO_FLAG_GENERATION;
  2309. }
  2310. if (flags_in & BTRFS_FS_INFO_FLAG_METADATA_UUID) {
  2311. memcpy(&fi_args->metadata_uuid, fs_devices->metadata_uuid,
  2312. sizeof(fi_args->metadata_uuid));
  2313. fi_args->flags |= BTRFS_FS_INFO_FLAG_METADATA_UUID;
  2314. }
  2315. if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
  2316. ret = -EFAULT;
  2317. kfree(fi_args);
  2318. return ret;
  2319. }
  2320. static long btrfs_ioctl_dev_info(const struct btrfs_fs_info *fs_info,
  2321. void __user *arg)
  2322. {
  2323. BTRFS_DEV_LOOKUP_ARGS(args);
  2324. struct btrfs_ioctl_dev_info_args *di_args;
  2325. struct btrfs_device *dev;
  2326. int ret = 0;
  2327. di_args = memdup_user(arg, sizeof(*di_args));
  2328. if (IS_ERR(di_args))
  2329. return PTR_ERR(di_args);
  2330. args.devid = di_args->devid;
  2331. if (!btrfs_is_empty_uuid(di_args->uuid))
  2332. args.uuid = di_args->uuid;
  2333. rcu_read_lock();
  2334. dev = btrfs_find_device(fs_info->fs_devices, &args);
  2335. if (!dev) {
  2336. ret = -ENODEV;
  2337. goto out;
  2338. }
  2339. di_args->devid = dev->devid;
  2340. di_args->bytes_used = btrfs_device_get_bytes_used(dev);
  2341. di_args->total_bytes = btrfs_device_get_total_bytes(dev);
  2342. memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
  2343. memcpy(di_args->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
  2344. if (dev->name)
  2345. strscpy(di_args->path, btrfs_dev_name(dev), sizeof(di_args->path));
  2346. else
  2347. di_args->path[0] = '\0';
  2348. out:
  2349. rcu_read_unlock();
  2350. if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
  2351. ret = -EFAULT;
  2352. kfree(di_args);
  2353. return ret;
  2354. }
  2355. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  2356. {
  2357. struct inode *inode = file_inode(file);
  2358. struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
  2359. struct btrfs_root *root = BTRFS_I(inode)->root;
  2360. struct btrfs_root *new_root;
  2361. struct btrfs_dir_item *di;
  2362. struct btrfs_trans_handle *trans;
  2363. struct btrfs_path *path = NULL;
  2364. struct btrfs_disk_key disk_key;
  2365. struct fscrypt_str name = FSTR_INIT("default", 7);
  2366. u64 objectid = 0;
  2367. u64 dir_id;
  2368. int ret;
  2369. if (!capable(CAP_SYS_ADMIN))
  2370. return -EPERM;
  2371. ret = mnt_want_write_file(file);
  2372. if (ret)
  2373. return ret;
  2374. if (copy_from_user(&objectid, argp, sizeof(objectid))) {
  2375. ret = -EFAULT;
  2376. goto out;
  2377. }
  2378. if (!objectid)
  2379. objectid = BTRFS_FS_TREE_OBJECTID;
  2380. new_root = btrfs_get_fs_root(fs_info, objectid, true);
  2381. if (IS_ERR(new_root)) {
  2382. ret = PTR_ERR(new_root);
  2383. goto out;
  2384. }
  2385. if (!btrfs_is_fstree(btrfs_root_id(new_root))) {
  2386. ret = -ENOENT;
  2387. goto out_free;
  2388. }
  2389. path = btrfs_alloc_path();
  2390. if (!path) {
  2391. ret = -ENOMEM;
  2392. goto out_free;
  2393. }
  2394. trans = btrfs_start_transaction(root, 1);
  2395. if (IS_ERR(trans)) {
  2396. ret = PTR_ERR(trans);
  2397. goto out_free;
  2398. }
  2399. dir_id = btrfs_super_root_dir(fs_info->super_copy);
  2400. di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
  2401. dir_id, &name, 1);
  2402. if (IS_ERR_OR_NULL(di)) {
  2403. btrfs_release_path(path);
  2404. btrfs_end_transaction(trans);
  2405. btrfs_err(fs_info,
  2406. "Umm, you don't have the default diritem, this isn't going to work");
  2407. ret = -ENOENT;
  2408. goto out_free;
  2409. }
  2410. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  2411. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  2412. btrfs_release_path(path);
  2413. btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
  2414. btrfs_end_transaction(trans);
  2415. out_free:
  2416. btrfs_put_root(new_root);
  2417. btrfs_free_path(path);
  2418. out:
  2419. mnt_drop_write_file(file);
  2420. return ret;
  2421. }
  2422. static void get_block_group_info(struct list_head *groups_list,
  2423. struct btrfs_ioctl_space_info *space)
  2424. {
  2425. struct btrfs_block_group *block_group;
  2426. space->total_bytes = 0;
  2427. space->used_bytes = 0;
  2428. space->flags = 0;
  2429. list_for_each_entry(block_group, groups_list, list) {
  2430. space->flags = block_group->flags;
  2431. space->total_bytes += block_group->length;
  2432. space->used_bytes += block_group->used;
  2433. }
  2434. }
  2435. static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
  2436. void __user *arg)
  2437. {
  2438. struct btrfs_ioctl_space_args space_args = { 0 };
  2439. struct btrfs_ioctl_space_info space;
  2440. struct btrfs_ioctl_space_info *dest;
  2441. struct btrfs_ioctl_space_info AUTO_KFREE(dest_orig);
  2442. struct btrfs_ioctl_space_info __user *user_dest;
  2443. struct btrfs_space_info *info;
  2444. static const u64 types[] = {
  2445. BTRFS_BLOCK_GROUP_DATA,
  2446. BTRFS_BLOCK_GROUP_SYSTEM,
  2447. BTRFS_BLOCK_GROUP_METADATA,
  2448. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
  2449. };
  2450. int num_types = 4;
  2451. int alloc_size;
  2452. int ret = 0;
  2453. u64 slot_count = 0;
  2454. int i, c;
  2455. if (copy_from_user(&space_args,
  2456. (struct btrfs_ioctl_space_args __user *)arg,
  2457. sizeof(space_args)))
  2458. return -EFAULT;
  2459. for (i = 0; i < num_types; i++) {
  2460. struct btrfs_space_info *tmp;
  2461. info = NULL;
  2462. list_for_each_entry(tmp, &fs_info->space_info, list) {
  2463. if (tmp->flags == types[i]) {
  2464. info = tmp;
  2465. break;
  2466. }
  2467. }
  2468. if (!info)
  2469. continue;
  2470. down_read(&info->groups_sem);
  2471. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2472. if (!list_empty(&info->block_groups[c]))
  2473. slot_count++;
  2474. }
  2475. up_read(&info->groups_sem);
  2476. }
  2477. /*
  2478. * Global block reserve, exported as a space_info
  2479. */
  2480. slot_count++;
  2481. /* space_slots == 0 means they are asking for a count */
  2482. if (space_args.space_slots == 0) {
  2483. space_args.total_spaces = slot_count;
  2484. goto out;
  2485. }
  2486. slot_count = min_t(u64, space_args.space_slots, slot_count);
  2487. alloc_size = sizeof(*dest) * slot_count;
  2488. /* we generally have at most 6 or so space infos, one for each raid
  2489. * level. So, a whole page should be more than enough for everyone
  2490. */
  2491. if (alloc_size > PAGE_SIZE)
  2492. return -ENOMEM;
  2493. space_args.total_spaces = 0;
  2494. dest = kmalloc(alloc_size, GFP_KERNEL);
  2495. if (!dest)
  2496. return -ENOMEM;
  2497. dest_orig = dest;
  2498. /* now we have a buffer to copy into */
  2499. for (i = 0; i < num_types; i++) {
  2500. struct btrfs_space_info *tmp;
  2501. if (!slot_count)
  2502. break;
  2503. info = NULL;
  2504. list_for_each_entry(tmp, &fs_info->space_info, list) {
  2505. if (tmp->flags == types[i]) {
  2506. info = tmp;
  2507. break;
  2508. }
  2509. }
  2510. if (!info)
  2511. continue;
  2512. down_read(&info->groups_sem);
  2513. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  2514. if (!list_empty(&info->block_groups[c])) {
  2515. get_block_group_info(&info->block_groups[c],
  2516. &space);
  2517. memcpy(dest, &space, sizeof(space));
  2518. dest++;
  2519. space_args.total_spaces++;
  2520. slot_count--;
  2521. }
  2522. if (!slot_count)
  2523. break;
  2524. }
  2525. up_read(&info->groups_sem);
  2526. }
  2527. /*
  2528. * Add global block reserve
  2529. */
  2530. if (slot_count) {
  2531. struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
  2532. spin_lock(&block_rsv->lock);
  2533. space.total_bytes = block_rsv->size;
  2534. space.used_bytes = block_rsv->size - block_rsv->reserved;
  2535. spin_unlock(&block_rsv->lock);
  2536. space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
  2537. memcpy(dest, &space, sizeof(space));
  2538. space_args.total_spaces++;
  2539. }
  2540. user_dest = (struct btrfs_ioctl_space_info __user *)
  2541. (arg + sizeof(struct btrfs_ioctl_space_args));
  2542. if (copy_to_user(user_dest, dest_orig, alloc_size))
  2543. return -EFAULT;
  2544. out:
  2545. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  2546. ret = -EFAULT;
  2547. return ret;
  2548. }
  2549. static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
  2550. void __user *argp)
  2551. {
  2552. struct btrfs_trans_handle *trans;
  2553. u64 transid;
  2554. /*
  2555. * Start orphan cleanup here for the given root in case it hasn't been
  2556. * started already by other means. Errors are handled in the other
  2557. * functions during transaction commit.
  2558. */
  2559. btrfs_orphan_cleanup(root);
  2560. trans = btrfs_attach_transaction_barrier(root);
  2561. if (IS_ERR(trans)) {
  2562. if (PTR_ERR(trans) != -ENOENT)
  2563. return PTR_ERR(trans);
  2564. /* No running transaction, don't bother */
  2565. transid = btrfs_get_last_trans_committed(root->fs_info);
  2566. goto out;
  2567. }
  2568. transid = trans->transid;
  2569. btrfs_commit_transaction_async(trans);
  2570. out:
  2571. if (argp)
  2572. if (copy_to_user(argp, &transid, sizeof(transid)))
  2573. return -EFAULT;
  2574. return 0;
  2575. }
  2576. static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
  2577. void __user *argp)
  2578. {
  2579. /* By default wait for the current transaction. */
  2580. u64 transid = 0;
  2581. if (argp)
  2582. if (copy_from_user(&transid, argp, sizeof(transid)))
  2583. return -EFAULT;
  2584. return btrfs_wait_for_commit(fs_info, transid);
  2585. }
  2586. static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
  2587. {
  2588. struct btrfs_fs_info *fs_info = inode_to_fs_info(file_inode(file));
  2589. struct btrfs_ioctl_scrub_args *sa;
  2590. int ret;
  2591. if (!capable(CAP_SYS_ADMIN))
  2592. return -EPERM;
  2593. if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
  2594. btrfs_err(fs_info, "scrub: extent tree v2 not yet supported");
  2595. return -EINVAL;
  2596. }
  2597. sa = memdup_user(arg, sizeof(*sa));
  2598. if (IS_ERR(sa))
  2599. return PTR_ERR(sa);
  2600. if (sa->flags & ~BTRFS_SCRUB_SUPPORTED_FLAGS) {
  2601. ret = -EOPNOTSUPP;
  2602. goto out;
  2603. }
  2604. if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
  2605. ret = mnt_want_write_file(file);
  2606. if (ret)
  2607. goto out;
  2608. }
  2609. ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
  2610. &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
  2611. 0);
  2612. /*
  2613. * Copy scrub args to user space even if btrfs_scrub_dev() returned an
  2614. * error. This is important as it allows user space to know how much
  2615. * progress scrub has done. For example, if scrub is canceled we get
  2616. * -ECANCELED from btrfs_scrub_dev() and return that error back to user
  2617. * space. Later user space can inspect the progress from the structure
  2618. * btrfs_ioctl_scrub_args and resume scrub from where it left off
  2619. * previously (btrfs-progs does this).
  2620. * If we fail to copy the btrfs_ioctl_scrub_args structure to user space
  2621. * then return -EFAULT to signal the structure was not copied or it may
  2622. * be corrupt and unreliable due to a partial copy.
  2623. */
  2624. if (copy_to_user(arg, sa, sizeof(*sa)))
  2625. ret = -EFAULT;
  2626. if (!(sa->flags & BTRFS_SCRUB_READONLY))
  2627. mnt_drop_write_file(file);
  2628. out:
  2629. kfree(sa);
  2630. return ret;
  2631. }
  2632. static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
  2633. {
  2634. if (!capable(CAP_SYS_ADMIN))
  2635. return -EPERM;
  2636. return btrfs_scrub_cancel(fs_info);
  2637. }
  2638. static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
  2639. void __user *arg)
  2640. {
  2641. struct btrfs_ioctl_scrub_args *sa;
  2642. int ret;
  2643. if (!capable(CAP_SYS_ADMIN))
  2644. return -EPERM;
  2645. sa = memdup_user(arg, sizeof(*sa));
  2646. if (IS_ERR(sa))
  2647. return PTR_ERR(sa);
  2648. ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
  2649. if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
  2650. ret = -EFAULT;
  2651. kfree(sa);
  2652. return ret;
  2653. }
  2654. static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
  2655. void __user *arg)
  2656. {
  2657. struct btrfs_ioctl_get_dev_stats *sa;
  2658. int ret;
  2659. sa = memdup_user(arg, sizeof(*sa));
  2660. if (IS_ERR(sa))
  2661. return PTR_ERR(sa);
  2662. if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
  2663. kfree(sa);
  2664. return -EPERM;
  2665. }
  2666. ret = btrfs_get_dev_stats(fs_info, sa);
  2667. if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
  2668. ret = -EFAULT;
  2669. kfree(sa);
  2670. return ret;
  2671. }
  2672. static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
  2673. void __user *arg)
  2674. {
  2675. struct btrfs_ioctl_dev_replace_args *p;
  2676. int ret;
  2677. if (!capable(CAP_SYS_ADMIN))
  2678. return -EPERM;
  2679. if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
  2680. btrfs_err(fs_info, "device replace not supported on extent tree v2 yet");
  2681. return -EINVAL;
  2682. }
  2683. p = memdup_user(arg, sizeof(*p));
  2684. if (IS_ERR(p))
  2685. return PTR_ERR(p);
  2686. switch (p->cmd) {
  2687. case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
  2688. if (sb_rdonly(fs_info->sb)) {
  2689. ret = -EROFS;
  2690. goto out;
  2691. }
  2692. if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) {
  2693. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  2694. } else {
  2695. ret = btrfs_dev_replace_by_ioctl(fs_info, p);
  2696. btrfs_exclop_finish(fs_info);
  2697. }
  2698. break;
  2699. case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
  2700. btrfs_dev_replace_status(fs_info, p);
  2701. ret = 0;
  2702. break;
  2703. case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
  2704. p->result = btrfs_dev_replace_cancel(fs_info);
  2705. ret = 0;
  2706. break;
  2707. default:
  2708. ret = -EINVAL;
  2709. break;
  2710. }
  2711. if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
  2712. ret = -EFAULT;
  2713. out:
  2714. kfree(p);
  2715. return ret;
  2716. }
  2717. static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
  2718. {
  2719. int ret = 0;
  2720. int i;
  2721. u64 rel_ptr;
  2722. int size;
  2723. struct btrfs_ioctl_ino_path_args *ipa = NULL;
  2724. struct inode_fs_paths *ipath __free(inode_fs_paths) = NULL;
  2725. struct btrfs_path *path;
  2726. if (!capable(CAP_DAC_READ_SEARCH))
  2727. return -EPERM;
  2728. path = btrfs_alloc_path();
  2729. if (!path) {
  2730. ret = -ENOMEM;
  2731. goto out;
  2732. }
  2733. ipa = memdup_user(arg, sizeof(*ipa));
  2734. if (IS_ERR(ipa)) {
  2735. ret = PTR_ERR(ipa);
  2736. ipa = NULL;
  2737. goto out;
  2738. }
  2739. size = min_t(u32, ipa->size, 4096);
  2740. ipath = init_ipath(size, root, path);
  2741. if (IS_ERR(ipath)) {
  2742. ret = PTR_ERR(ipath);
  2743. ipath = NULL;
  2744. goto out;
  2745. }
  2746. ret = paths_from_inode(ipa->inum, ipath);
  2747. if (ret < 0)
  2748. goto out;
  2749. for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
  2750. rel_ptr = ipath->fspath->val[i] -
  2751. (u64)(unsigned long)ipath->fspath->val;
  2752. ipath->fspath->val[i] = rel_ptr;
  2753. }
  2754. btrfs_free_path(path);
  2755. path = NULL;
  2756. ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
  2757. ipath->fspath, size);
  2758. if (ret) {
  2759. ret = -EFAULT;
  2760. goto out;
  2761. }
  2762. out:
  2763. btrfs_free_path(path);
  2764. kfree(ipa);
  2765. return ret;
  2766. }
  2767. static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
  2768. void __user *arg, int version)
  2769. {
  2770. int ret = 0;
  2771. int size;
  2772. struct btrfs_ioctl_logical_ino_args *loi;
  2773. struct btrfs_data_container *inodes = NULL;
  2774. bool ignore_offset;
  2775. if (!capable(CAP_SYS_ADMIN))
  2776. return -EPERM;
  2777. loi = memdup_user(arg, sizeof(*loi));
  2778. if (IS_ERR(loi))
  2779. return PTR_ERR(loi);
  2780. if (version == 1) {
  2781. ignore_offset = false;
  2782. size = min_t(u32, loi->size, SZ_64K);
  2783. } else {
  2784. /* All reserved bits must be 0 for now */
  2785. if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
  2786. ret = -EINVAL;
  2787. goto out_loi;
  2788. }
  2789. /* Only accept flags we have defined so far */
  2790. if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
  2791. ret = -EINVAL;
  2792. goto out_loi;
  2793. }
  2794. ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
  2795. size = min_t(u32, loi->size, SZ_16M);
  2796. }
  2797. inodes = init_data_container(size);
  2798. if (IS_ERR(inodes)) {
  2799. ret = PTR_ERR(inodes);
  2800. goto out_loi;
  2801. }
  2802. ret = iterate_inodes_from_logical(loi->logical, fs_info, inodes, ignore_offset);
  2803. if (ret == -EINVAL)
  2804. ret = -ENOENT;
  2805. if (ret < 0)
  2806. goto out;
  2807. ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
  2808. size);
  2809. if (ret)
  2810. ret = -EFAULT;
  2811. out:
  2812. kvfree(inodes);
  2813. out_loi:
  2814. kfree(loi);
  2815. return ret;
  2816. }
  2817. void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
  2818. struct btrfs_ioctl_balance_args *bargs)
  2819. {
  2820. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  2821. bargs->flags = bctl->flags;
  2822. if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
  2823. bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
  2824. if (atomic_read(&fs_info->balance_pause_req))
  2825. bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
  2826. if (atomic_read(&fs_info->balance_cancel_req))
  2827. bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
  2828. memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
  2829. memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
  2830. memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
  2831. spin_lock(&fs_info->balance_lock);
  2832. memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
  2833. spin_unlock(&fs_info->balance_lock);
  2834. }
  2835. /*
  2836. * Try to acquire fs_info::balance_mutex as well as set BTRFS_EXLCOP_BALANCE as
  2837. * required.
  2838. *
  2839. * @fs_info: the filesystem
  2840. * @excl_acquired: ptr to boolean value which is set to false in case balance
  2841. * is being resumed
  2842. *
  2843. * Return 0 on success in which case both fs_info::balance is acquired as well
  2844. * as exclusive ops are blocked. In case of failure return an error code.
  2845. */
  2846. static int btrfs_try_lock_balance(struct btrfs_fs_info *fs_info, bool *excl_acquired)
  2847. {
  2848. int ret;
  2849. /*
  2850. * Exclusive operation is locked. Three possibilities:
  2851. * (1) some other op is running
  2852. * (2) balance is running
  2853. * (3) balance is paused -- special case (think resume)
  2854. */
  2855. while (1) {
  2856. if (btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
  2857. *excl_acquired = true;
  2858. mutex_lock(&fs_info->balance_mutex);
  2859. return 0;
  2860. }
  2861. mutex_lock(&fs_info->balance_mutex);
  2862. if (fs_info->balance_ctl) {
  2863. /* This is either (2) or (3) */
  2864. if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
  2865. /* This is (2) */
  2866. ret = -EINPROGRESS;
  2867. goto out_failure;
  2868. } else {
  2869. mutex_unlock(&fs_info->balance_mutex);
  2870. /*
  2871. * Lock released to allow other waiters to
  2872. * continue, we'll reexamine the status again.
  2873. */
  2874. mutex_lock(&fs_info->balance_mutex);
  2875. if (fs_info->balance_ctl &&
  2876. !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
  2877. /* This is (3) */
  2878. *excl_acquired = false;
  2879. return 0;
  2880. }
  2881. }
  2882. } else {
  2883. /* This is (1) */
  2884. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  2885. goto out_failure;
  2886. }
  2887. mutex_unlock(&fs_info->balance_mutex);
  2888. }
  2889. out_failure:
  2890. mutex_unlock(&fs_info->balance_mutex);
  2891. *excl_acquired = false;
  2892. return ret;
  2893. }
  2894. static long btrfs_ioctl_balance(struct file *file, void __user *arg)
  2895. {
  2896. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  2897. struct btrfs_fs_info *fs_info = root->fs_info;
  2898. struct btrfs_ioctl_balance_args *bargs;
  2899. struct btrfs_balance_control *bctl;
  2900. bool need_unlock = true;
  2901. int ret;
  2902. if (!capable(CAP_SYS_ADMIN))
  2903. return -EPERM;
  2904. ret = mnt_want_write_file(file);
  2905. if (ret)
  2906. return ret;
  2907. bargs = memdup_user(arg, sizeof(*bargs));
  2908. if (IS_ERR(bargs)) {
  2909. ret = PTR_ERR(bargs);
  2910. bargs = NULL;
  2911. goto out;
  2912. }
  2913. ret = btrfs_try_lock_balance(fs_info, &need_unlock);
  2914. if (ret)
  2915. goto out;
  2916. lockdep_assert_held(&fs_info->balance_mutex);
  2917. if (bargs->flags & BTRFS_BALANCE_RESUME) {
  2918. if (!fs_info->balance_ctl) {
  2919. ret = -ENOTCONN;
  2920. goto out_unlock;
  2921. }
  2922. bctl = fs_info->balance_ctl;
  2923. spin_lock(&fs_info->balance_lock);
  2924. bctl->flags |= BTRFS_BALANCE_RESUME;
  2925. spin_unlock(&fs_info->balance_lock);
  2926. btrfs_exclop_balance(fs_info, BTRFS_EXCLOP_BALANCE);
  2927. goto do_balance;
  2928. }
  2929. if (bargs->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
  2930. ret = -EINVAL;
  2931. goto out_unlock;
  2932. }
  2933. if (fs_info->balance_ctl) {
  2934. ret = -EINPROGRESS;
  2935. goto out_unlock;
  2936. }
  2937. bctl = kzalloc_obj(*bctl);
  2938. if (!bctl) {
  2939. ret = -ENOMEM;
  2940. goto out_unlock;
  2941. }
  2942. memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
  2943. memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
  2944. memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
  2945. bctl->flags = bargs->flags;
  2946. do_balance:
  2947. /*
  2948. * Ownership of bctl and exclusive operation goes to btrfs_balance.
  2949. * bctl is freed in reset_balance_state, or, if restriper was paused
  2950. * all the way until unmount, in free_fs_info. The flag should be
  2951. * cleared after reset_balance_state.
  2952. */
  2953. need_unlock = false;
  2954. ret = btrfs_balance(fs_info, bctl, bargs);
  2955. bctl = NULL;
  2956. if (ret == 0 || ret == -ECANCELED) {
  2957. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  2958. ret = -EFAULT;
  2959. }
  2960. kfree(bctl);
  2961. out_unlock:
  2962. mutex_unlock(&fs_info->balance_mutex);
  2963. if (need_unlock)
  2964. btrfs_exclop_finish(fs_info);
  2965. out:
  2966. mnt_drop_write_file(file);
  2967. kfree(bargs);
  2968. return ret;
  2969. }
  2970. static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
  2971. {
  2972. if (!capable(CAP_SYS_ADMIN))
  2973. return -EPERM;
  2974. switch (cmd) {
  2975. case BTRFS_BALANCE_CTL_PAUSE:
  2976. return btrfs_pause_balance(fs_info);
  2977. case BTRFS_BALANCE_CTL_CANCEL:
  2978. return btrfs_cancel_balance(fs_info);
  2979. }
  2980. return -EINVAL;
  2981. }
  2982. static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
  2983. void __user *arg)
  2984. {
  2985. struct btrfs_ioctl_balance_args AUTO_KFREE(bargs);
  2986. int ret = 0;
  2987. if (!capable(CAP_SYS_ADMIN))
  2988. return -EPERM;
  2989. mutex_lock(&fs_info->balance_mutex);
  2990. if (!fs_info->balance_ctl) {
  2991. ret = -ENOTCONN;
  2992. goto out;
  2993. }
  2994. bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
  2995. if (!bargs) {
  2996. ret = -ENOMEM;
  2997. goto out;
  2998. }
  2999. btrfs_update_ioctl_balance_args(fs_info, bargs);
  3000. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  3001. ret = -EFAULT;
  3002. out:
  3003. mutex_unlock(&fs_info->balance_mutex);
  3004. return ret;
  3005. }
  3006. static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
  3007. {
  3008. struct inode *inode = file_inode(file);
  3009. struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
  3010. struct btrfs_ioctl_quota_ctl_args *sa;
  3011. int ret;
  3012. if (!capable(CAP_SYS_ADMIN))
  3013. return -EPERM;
  3014. ret = mnt_want_write_file(file);
  3015. if (ret)
  3016. return ret;
  3017. sa = memdup_user(arg, sizeof(*sa));
  3018. if (IS_ERR(sa)) {
  3019. ret = PTR_ERR(sa);
  3020. goto drop_write;
  3021. }
  3022. switch (sa->cmd) {
  3023. case BTRFS_QUOTA_CTL_ENABLE:
  3024. case BTRFS_QUOTA_CTL_ENABLE_SIMPLE_QUOTA:
  3025. down_write(&fs_info->subvol_sem);
  3026. ret = btrfs_quota_enable(fs_info, sa);
  3027. up_write(&fs_info->subvol_sem);
  3028. break;
  3029. case BTRFS_QUOTA_CTL_DISABLE:
  3030. /*
  3031. * Lock the cleaner mutex to prevent races with concurrent
  3032. * relocation, because relocation may be building backrefs for
  3033. * blocks of the quota root while we are deleting the root. This
  3034. * is like dropping fs roots of deleted snapshots/subvolumes, we
  3035. * need the same protection.
  3036. *
  3037. * This also prevents races between concurrent tasks trying to
  3038. * disable quotas, because we will unlock and relock
  3039. * qgroup_ioctl_lock across BTRFS_FS_QUOTA_ENABLED changes.
  3040. *
  3041. * We take this here because we have the dependency of
  3042. *
  3043. * inode_lock -> subvol_sem
  3044. *
  3045. * because of rename. With relocation we can prealloc extents,
  3046. * so that makes the dependency chain
  3047. *
  3048. * cleaner_mutex -> inode_lock -> subvol_sem
  3049. *
  3050. * so we must take the cleaner_mutex here before we take the
  3051. * subvol_sem. The deadlock can't actually happen, but this
  3052. * quiets lockdep.
  3053. */
  3054. mutex_lock(&fs_info->cleaner_mutex);
  3055. down_write(&fs_info->subvol_sem);
  3056. ret = btrfs_quota_disable(fs_info);
  3057. up_write(&fs_info->subvol_sem);
  3058. mutex_unlock(&fs_info->cleaner_mutex);
  3059. break;
  3060. default:
  3061. ret = -EINVAL;
  3062. break;
  3063. }
  3064. kfree(sa);
  3065. drop_write:
  3066. mnt_drop_write_file(file);
  3067. return ret;
  3068. }
  3069. static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
  3070. {
  3071. struct inode *inode = file_inode(file);
  3072. struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
  3073. struct btrfs_root *root = BTRFS_I(inode)->root;
  3074. struct btrfs_ioctl_qgroup_assign_args *sa;
  3075. struct btrfs_qgroup_list *prealloc = NULL;
  3076. struct btrfs_trans_handle *trans;
  3077. int ret;
  3078. int err;
  3079. if (!capable(CAP_SYS_ADMIN))
  3080. return -EPERM;
  3081. if (!btrfs_qgroup_enabled(fs_info))
  3082. return -ENOTCONN;
  3083. ret = mnt_want_write_file(file);
  3084. if (ret)
  3085. return ret;
  3086. sa = memdup_user(arg, sizeof(*sa));
  3087. if (IS_ERR(sa)) {
  3088. ret = PTR_ERR(sa);
  3089. goto drop_write;
  3090. }
  3091. if (sa->assign) {
  3092. prealloc = kzalloc_obj(*prealloc);
  3093. if (!prealloc) {
  3094. ret = -ENOMEM;
  3095. goto out;
  3096. }
  3097. }
  3098. /* 2 BTRFS_QGROUP_RELATION_KEY items. */
  3099. trans = btrfs_start_transaction(root, 2);
  3100. if (IS_ERR(trans)) {
  3101. ret = PTR_ERR(trans);
  3102. goto out;
  3103. }
  3104. /*
  3105. * Prealloc ownership is moved to the relation handler, there it's used
  3106. * or freed on error.
  3107. */
  3108. if (sa->assign) {
  3109. ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst, prealloc);
  3110. prealloc = NULL;
  3111. } else {
  3112. ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
  3113. }
  3114. /* update qgroup status and info */
  3115. mutex_lock(&fs_info->qgroup_ioctl_lock);
  3116. err = btrfs_run_qgroups(trans);
  3117. mutex_unlock(&fs_info->qgroup_ioctl_lock);
  3118. if (err < 0)
  3119. btrfs_warn(fs_info,
  3120. "qgroup status update failed after %s relation, marked as inconsistent",
  3121. sa->assign ? "adding" : "deleting");
  3122. err = btrfs_end_transaction(trans);
  3123. if (err && !ret)
  3124. ret = err;
  3125. out:
  3126. kfree(prealloc);
  3127. kfree(sa);
  3128. drop_write:
  3129. mnt_drop_write_file(file);
  3130. return ret;
  3131. }
  3132. static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
  3133. {
  3134. struct inode *inode = file_inode(file);
  3135. struct btrfs_root *root = BTRFS_I(inode)->root;
  3136. struct btrfs_ioctl_qgroup_create_args *sa;
  3137. struct btrfs_trans_handle *trans;
  3138. int ret;
  3139. int err;
  3140. if (!capable(CAP_SYS_ADMIN))
  3141. return -EPERM;
  3142. if (!btrfs_qgroup_enabled(root->fs_info))
  3143. return -ENOTCONN;
  3144. ret = mnt_want_write_file(file);
  3145. if (ret)
  3146. return ret;
  3147. sa = memdup_user(arg, sizeof(*sa));
  3148. if (IS_ERR(sa)) {
  3149. ret = PTR_ERR(sa);
  3150. goto drop_write;
  3151. }
  3152. if (!sa->qgroupid) {
  3153. ret = -EINVAL;
  3154. goto out;
  3155. }
  3156. if (sa->create && btrfs_is_fstree(sa->qgroupid)) {
  3157. ret = -EINVAL;
  3158. goto out;
  3159. }
  3160. /*
  3161. * 1 BTRFS_QGROUP_INFO_KEY item.
  3162. * 1 BTRFS_QGROUP_LIMIT_KEY item.
  3163. */
  3164. trans = btrfs_start_transaction(root, 2);
  3165. if (IS_ERR(trans)) {
  3166. ret = PTR_ERR(trans);
  3167. goto out;
  3168. }
  3169. if (sa->create) {
  3170. ret = btrfs_create_qgroup(trans, sa->qgroupid);
  3171. } else {
  3172. ret = btrfs_remove_qgroup(trans, sa->qgroupid);
  3173. }
  3174. err = btrfs_end_transaction(trans);
  3175. if (err && !ret)
  3176. ret = err;
  3177. out:
  3178. kfree(sa);
  3179. drop_write:
  3180. mnt_drop_write_file(file);
  3181. return ret;
  3182. }
  3183. static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
  3184. {
  3185. struct inode *inode = file_inode(file);
  3186. struct btrfs_root *root = BTRFS_I(inode)->root;
  3187. struct btrfs_ioctl_qgroup_limit_args *sa;
  3188. struct btrfs_trans_handle *trans;
  3189. int ret;
  3190. int err;
  3191. u64 qgroupid;
  3192. if (!capable(CAP_SYS_ADMIN))
  3193. return -EPERM;
  3194. if (!btrfs_qgroup_enabled(root->fs_info))
  3195. return -ENOTCONN;
  3196. ret = mnt_want_write_file(file);
  3197. if (ret)
  3198. return ret;
  3199. sa = memdup_user(arg, sizeof(*sa));
  3200. if (IS_ERR(sa)) {
  3201. ret = PTR_ERR(sa);
  3202. goto drop_write;
  3203. }
  3204. /* 1 BTRFS_QGROUP_LIMIT_KEY item. */
  3205. trans = btrfs_start_transaction(root, 1);
  3206. if (IS_ERR(trans)) {
  3207. ret = PTR_ERR(trans);
  3208. goto out;
  3209. }
  3210. qgroupid = sa->qgroupid;
  3211. if (!qgroupid) {
  3212. /* take the current subvol as qgroup */
  3213. qgroupid = btrfs_root_id(root);
  3214. }
  3215. ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
  3216. err = btrfs_end_transaction(trans);
  3217. if (err && !ret)
  3218. ret = err;
  3219. out:
  3220. kfree(sa);
  3221. drop_write:
  3222. mnt_drop_write_file(file);
  3223. return ret;
  3224. }
  3225. static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
  3226. {
  3227. struct inode *inode = file_inode(file);
  3228. struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
  3229. struct btrfs_ioctl_quota_rescan_args *qsa;
  3230. int ret;
  3231. if (!capable(CAP_SYS_ADMIN))
  3232. return -EPERM;
  3233. if (!btrfs_qgroup_enabled(fs_info))
  3234. return -ENOTCONN;
  3235. ret = mnt_want_write_file(file);
  3236. if (ret)
  3237. return ret;
  3238. qsa = memdup_user(arg, sizeof(*qsa));
  3239. if (IS_ERR(qsa)) {
  3240. ret = PTR_ERR(qsa);
  3241. goto drop_write;
  3242. }
  3243. if (qsa->flags) {
  3244. ret = -EINVAL;
  3245. goto out;
  3246. }
  3247. ret = btrfs_qgroup_rescan(fs_info);
  3248. out:
  3249. kfree(qsa);
  3250. drop_write:
  3251. mnt_drop_write_file(file);
  3252. return ret;
  3253. }
  3254. static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info,
  3255. void __user *arg)
  3256. {
  3257. struct btrfs_ioctl_quota_rescan_args qsa = {0};
  3258. if (!capable(CAP_SYS_ADMIN))
  3259. return -EPERM;
  3260. if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  3261. qsa.flags = 1;
  3262. qsa.progress = fs_info->qgroup_rescan_progress.objectid;
  3263. }
  3264. if (copy_to_user(arg, &qsa, sizeof(qsa)))
  3265. return -EFAULT;
  3266. return 0;
  3267. }
  3268. static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info)
  3269. {
  3270. if (!capable(CAP_SYS_ADMIN))
  3271. return -EPERM;
  3272. return btrfs_qgroup_wait_for_completion(fs_info, true);
  3273. }
  3274. static long _btrfs_ioctl_set_received_subvol(struct file *file,
  3275. struct mnt_idmap *idmap,
  3276. struct btrfs_ioctl_received_subvol_args *sa)
  3277. {
  3278. struct inode *inode = file_inode(file);
  3279. struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
  3280. struct btrfs_root *root = BTRFS_I(inode)->root;
  3281. struct btrfs_root_item *root_item = &root->root_item;
  3282. struct btrfs_trans_handle *trans;
  3283. struct timespec64 ct = current_time(inode);
  3284. int ret = 0;
  3285. int received_uuid_changed;
  3286. if (!inode_owner_or_capable(idmap, inode))
  3287. return -EPERM;
  3288. ret = mnt_want_write_file(file);
  3289. if (ret < 0)
  3290. return ret;
  3291. down_write(&fs_info->subvol_sem);
  3292. if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
  3293. ret = -EINVAL;
  3294. goto out;
  3295. }
  3296. if (btrfs_root_readonly(root)) {
  3297. ret = -EROFS;
  3298. goto out;
  3299. }
  3300. received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
  3301. BTRFS_UUID_SIZE);
  3302. /*
  3303. * Before we attempt to add the new received uuid, check if we have room
  3304. * for it in case there's already an item. If the size of the existing
  3305. * item plus this root's ID (u64) exceeds the maximum item size, we can
  3306. * return here without the need to abort a transaction. If we don't do
  3307. * this check, the btrfs_uuid_tree_add() call below would fail with
  3308. * -EOVERFLOW and result in a transaction abort. Malicious users could
  3309. * exploit this to turn the fs into RO mode.
  3310. */
  3311. if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
  3312. ret = btrfs_uuid_tree_check_overflow(fs_info, sa->uuid,
  3313. BTRFS_UUID_KEY_RECEIVED_SUBVOL);
  3314. if (ret < 0)
  3315. goto out;
  3316. }
  3317. /*
  3318. * 1 - root item
  3319. * 2 - uuid items (received uuid + subvol uuid)
  3320. */
  3321. trans = btrfs_start_transaction(root, 3);
  3322. if (IS_ERR(trans)) {
  3323. ret = PTR_ERR(trans);
  3324. trans = NULL;
  3325. goto out;
  3326. }
  3327. sa->rtransid = trans->transid;
  3328. sa->rtime.sec = ct.tv_sec;
  3329. sa->rtime.nsec = ct.tv_nsec;
  3330. if (received_uuid_changed &&
  3331. !btrfs_is_empty_uuid(root_item->received_uuid)) {
  3332. ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
  3333. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  3334. btrfs_root_id(root));
  3335. if (unlikely(ret && ret != -ENOENT)) {
  3336. btrfs_end_transaction(trans);
  3337. goto out;
  3338. }
  3339. }
  3340. memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
  3341. btrfs_set_root_stransid(root_item, sa->stransid);
  3342. btrfs_set_root_rtransid(root_item, sa->rtransid);
  3343. btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
  3344. btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
  3345. btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
  3346. btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
  3347. ret = btrfs_update_root(trans, fs_info->tree_root,
  3348. &root->root_key, &root->root_item);
  3349. if (unlikely(ret < 0)) {
  3350. btrfs_abort_transaction(trans, ret);
  3351. btrfs_end_transaction(trans);
  3352. goto out;
  3353. }
  3354. if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
  3355. ret = btrfs_uuid_tree_add(trans, sa->uuid,
  3356. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  3357. btrfs_root_id(root));
  3358. if (unlikely(ret < 0 && ret != -EEXIST)) {
  3359. btrfs_abort_transaction(trans, ret);
  3360. btrfs_end_transaction(trans);
  3361. goto out;
  3362. }
  3363. }
  3364. ret = btrfs_commit_transaction(trans);
  3365. out:
  3366. up_write(&fs_info->subvol_sem);
  3367. mnt_drop_write_file(file);
  3368. return ret;
  3369. }
  3370. #ifdef CONFIG_64BIT
  3371. static long btrfs_ioctl_set_received_subvol_32(struct file *file,
  3372. void __user *arg)
  3373. {
  3374. struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
  3375. struct btrfs_ioctl_received_subvol_args *args64 = NULL;
  3376. int ret = 0;
  3377. args32 = memdup_user(arg, sizeof(*args32));
  3378. if (IS_ERR(args32))
  3379. return PTR_ERR(args32);
  3380. args64 = kmalloc_obj(*args64);
  3381. if (!args64) {
  3382. ret = -ENOMEM;
  3383. goto out;
  3384. }
  3385. memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
  3386. args64->stransid = args32->stransid;
  3387. args64->rtransid = args32->rtransid;
  3388. args64->stime.sec = args32->stime.sec;
  3389. args64->stime.nsec = args32->stime.nsec;
  3390. args64->rtime.sec = args32->rtime.sec;
  3391. args64->rtime.nsec = args32->rtime.nsec;
  3392. args64->flags = args32->flags;
  3393. ret = _btrfs_ioctl_set_received_subvol(file, file_mnt_idmap(file), args64);
  3394. if (ret)
  3395. goto out;
  3396. memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
  3397. args32->stransid = args64->stransid;
  3398. args32->rtransid = args64->rtransid;
  3399. args32->stime.sec = args64->stime.sec;
  3400. args32->stime.nsec = args64->stime.nsec;
  3401. args32->rtime.sec = args64->rtime.sec;
  3402. args32->rtime.nsec = args64->rtime.nsec;
  3403. args32->flags = args64->flags;
  3404. ret = copy_to_user(arg, args32, sizeof(*args32));
  3405. if (ret)
  3406. ret = -EFAULT;
  3407. out:
  3408. kfree(args32);
  3409. kfree(args64);
  3410. return ret;
  3411. }
  3412. #endif
  3413. static long btrfs_ioctl_set_received_subvol(struct file *file,
  3414. void __user *arg)
  3415. {
  3416. struct btrfs_ioctl_received_subvol_args *sa = NULL;
  3417. int ret = 0;
  3418. sa = memdup_user(arg, sizeof(*sa));
  3419. if (IS_ERR(sa))
  3420. return PTR_ERR(sa);
  3421. ret = _btrfs_ioctl_set_received_subvol(file, file_mnt_idmap(file), sa);
  3422. if (ret)
  3423. goto out;
  3424. ret = copy_to_user(arg, sa, sizeof(*sa));
  3425. if (ret)
  3426. ret = -EFAULT;
  3427. out:
  3428. kfree(sa);
  3429. return ret;
  3430. }
  3431. static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info,
  3432. void __user *arg)
  3433. {
  3434. size_t len;
  3435. int ret;
  3436. char label[BTRFS_LABEL_SIZE];
  3437. spin_lock(&fs_info->super_lock);
  3438. memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
  3439. spin_unlock(&fs_info->super_lock);
  3440. len = strnlen(label, BTRFS_LABEL_SIZE);
  3441. if (len == BTRFS_LABEL_SIZE) {
  3442. btrfs_warn(fs_info,
  3443. "label is too long, return the first %zu bytes",
  3444. --len);
  3445. }
  3446. ret = copy_to_user(arg, label, len);
  3447. return ret ? -EFAULT : 0;
  3448. }
  3449. static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
  3450. {
  3451. struct inode *inode = file_inode(file);
  3452. struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
  3453. struct btrfs_root *root = BTRFS_I(inode)->root;
  3454. struct btrfs_super_block *super_block = fs_info->super_copy;
  3455. struct btrfs_trans_handle *trans;
  3456. char label[BTRFS_LABEL_SIZE];
  3457. int ret;
  3458. if (!capable(CAP_SYS_ADMIN))
  3459. return -EPERM;
  3460. if (copy_from_user(label, arg, sizeof(label)))
  3461. return -EFAULT;
  3462. if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
  3463. btrfs_err(fs_info,
  3464. "unable to set label with more than %d bytes",
  3465. BTRFS_LABEL_SIZE - 1);
  3466. return -EINVAL;
  3467. }
  3468. ret = mnt_want_write_file(file);
  3469. if (ret)
  3470. return ret;
  3471. trans = btrfs_start_transaction(root, 0);
  3472. if (IS_ERR(trans)) {
  3473. ret = PTR_ERR(trans);
  3474. goto out_unlock;
  3475. }
  3476. spin_lock(&fs_info->super_lock);
  3477. strscpy(super_block->label, label);
  3478. spin_unlock(&fs_info->super_lock);
  3479. ret = btrfs_commit_transaction(trans);
  3480. out_unlock:
  3481. mnt_drop_write_file(file);
  3482. return ret;
  3483. }
  3484. #define INIT_FEATURE_FLAGS(suffix) \
  3485. { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
  3486. .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
  3487. .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
  3488. int btrfs_ioctl_get_supported_features(void __user *arg)
  3489. {
  3490. static const struct btrfs_ioctl_feature_flags features[3] = {
  3491. INIT_FEATURE_FLAGS(SUPP),
  3492. INIT_FEATURE_FLAGS(SAFE_SET),
  3493. INIT_FEATURE_FLAGS(SAFE_CLEAR)
  3494. };
  3495. if (copy_to_user(arg, &features, sizeof(features)))
  3496. return -EFAULT;
  3497. return 0;
  3498. }
  3499. static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info,
  3500. void __user *arg)
  3501. {
  3502. struct btrfs_super_block *super_block = fs_info->super_copy;
  3503. struct btrfs_ioctl_feature_flags features;
  3504. features.compat_flags = btrfs_super_compat_flags(super_block);
  3505. features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
  3506. features.incompat_flags = btrfs_super_incompat_flags(super_block);
  3507. if (copy_to_user(arg, &features, sizeof(features)))
  3508. return -EFAULT;
  3509. return 0;
  3510. }
  3511. static int check_feature_bits(const struct btrfs_fs_info *fs_info,
  3512. enum btrfs_feature_set set,
  3513. u64 change_mask, u64 flags, u64 supported_flags,
  3514. u64 safe_set, u64 safe_clear)
  3515. {
  3516. const char *type = btrfs_feature_set_name(set);
  3517. const char AUTO_KFREE(names);
  3518. u64 disallowed, unsupported;
  3519. u64 set_mask = flags & change_mask;
  3520. u64 clear_mask = ~flags & change_mask;
  3521. unsupported = set_mask & ~supported_flags;
  3522. if (unsupported) {
  3523. names = btrfs_printable_features(set, unsupported);
  3524. if (names)
  3525. btrfs_warn(fs_info,
  3526. "this kernel does not support the %s feature bit%s",
  3527. names, strchr(names, ',') ? "s" : "");
  3528. else
  3529. btrfs_warn(fs_info,
  3530. "this kernel does not support %s bits 0x%llx",
  3531. type, unsupported);
  3532. return -EOPNOTSUPP;
  3533. }
  3534. disallowed = set_mask & ~safe_set;
  3535. if (disallowed) {
  3536. names = btrfs_printable_features(set, disallowed);
  3537. if (names)
  3538. btrfs_warn(fs_info,
  3539. "can't set the %s feature bit%s while mounted",
  3540. names, strchr(names, ',') ? "s" : "");
  3541. else
  3542. btrfs_warn(fs_info,
  3543. "can't set %s bits 0x%llx while mounted",
  3544. type, disallowed);
  3545. return -EPERM;
  3546. }
  3547. disallowed = clear_mask & ~safe_clear;
  3548. if (disallowed) {
  3549. names = btrfs_printable_features(set, disallowed);
  3550. if (names)
  3551. btrfs_warn(fs_info,
  3552. "can't clear the %s feature bit%s while mounted",
  3553. names, strchr(names, ',') ? "s" : "");
  3554. else
  3555. btrfs_warn(fs_info,
  3556. "can't clear %s bits 0x%llx while mounted",
  3557. type, disallowed);
  3558. return -EPERM;
  3559. }
  3560. return 0;
  3561. }
  3562. #define check_feature(fs_info, change_mask, flags, mask_base) \
  3563. check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
  3564. BTRFS_FEATURE_ ## mask_base ## _SUPP, \
  3565. BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
  3566. BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
  3567. static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
  3568. {
  3569. struct inode *inode = file_inode(file);
  3570. struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
  3571. struct btrfs_root *root = BTRFS_I(inode)->root;
  3572. struct btrfs_super_block *super_block = fs_info->super_copy;
  3573. struct btrfs_ioctl_feature_flags flags[2];
  3574. struct btrfs_trans_handle *trans;
  3575. u64 newflags;
  3576. int ret;
  3577. if (!capable(CAP_SYS_ADMIN))
  3578. return -EPERM;
  3579. if (copy_from_user(flags, arg, sizeof(flags)))
  3580. return -EFAULT;
  3581. /* Nothing to do */
  3582. if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
  3583. !flags[0].incompat_flags)
  3584. return 0;
  3585. ret = check_feature(fs_info, flags[0].compat_flags,
  3586. flags[1].compat_flags, COMPAT);
  3587. if (ret)
  3588. return ret;
  3589. ret = check_feature(fs_info, flags[0].compat_ro_flags,
  3590. flags[1].compat_ro_flags, COMPAT_RO);
  3591. if (ret)
  3592. return ret;
  3593. ret = check_feature(fs_info, flags[0].incompat_flags,
  3594. flags[1].incompat_flags, INCOMPAT);
  3595. if (ret)
  3596. return ret;
  3597. ret = mnt_want_write_file(file);
  3598. if (ret)
  3599. return ret;
  3600. trans = btrfs_start_transaction(root, 0);
  3601. if (IS_ERR(trans)) {
  3602. ret = PTR_ERR(trans);
  3603. goto out_drop_write;
  3604. }
  3605. spin_lock(&fs_info->super_lock);
  3606. newflags = btrfs_super_compat_flags(super_block);
  3607. newflags |= flags[0].compat_flags & flags[1].compat_flags;
  3608. newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
  3609. btrfs_set_super_compat_flags(super_block, newflags);
  3610. newflags = btrfs_super_compat_ro_flags(super_block);
  3611. newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
  3612. newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
  3613. btrfs_set_super_compat_ro_flags(super_block, newflags);
  3614. newflags = btrfs_super_incompat_flags(super_block);
  3615. newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
  3616. newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
  3617. btrfs_set_super_incompat_flags(super_block, newflags);
  3618. spin_unlock(&fs_info->super_lock);
  3619. ret = btrfs_commit_transaction(trans);
  3620. out_drop_write:
  3621. mnt_drop_write_file(file);
  3622. return ret;
  3623. }
  3624. static int _btrfs_ioctl_send(struct btrfs_root *root, void __user *argp, bool compat)
  3625. {
  3626. struct btrfs_ioctl_send_args *arg;
  3627. int ret;
  3628. if (compat) {
  3629. #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
  3630. struct btrfs_ioctl_send_args_32 args32 = { 0 };
  3631. ret = copy_from_user(&args32, argp, sizeof(args32));
  3632. if (ret)
  3633. return -EFAULT;
  3634. arg = kzalloc_obj(*arg);
  3635. if (!arg)
  3636. return -ENOMEM;
  3637. arg->send_fd = args32.send_fd;
  3638. arg->clone_sources_count = args32.clone_sources_count;
  3639. arg->clone_sources = compat_ptr(args32.clone_sources);
  3640. arg->parent_root = args32.parent_root;
  3641. arg->flags = args32.flags;
  3642. arg->version = args32.version;
  3643. memcpy(arg->reserved, args32.reserved,
  3644. sizeof(args32.reserved));
  3645. #else
  3646. return -ENOTTY;
  3647. #endif
  3648. } else {
  3649. arg = memdup_user(argp, sizeof(*arg));
  3650. if (IS_ERR(arg))
  3651. return PTR_ERR(arg);
  3652. }
  3653. ret = btrfs_ioctl_send(root, arg);
  3654. kfree(arg);
  3655. return ret;
  3656. }
  3657. static int btrfs_ioctl_encoded_read(struct file *file, void __user *argp,
  3658. bool compat)
  3659. {
  3660. struct btrfs_ioctl_encoded_io_args args = { 0 };
  3661. size_t copy_end_kernel = offsetofend(struct btrfs_ioctl_encoded_io_args,
  3662. flags);
  3663. size_t copy_end;
  3664. struct btrfs_inode *inode = BTRFS_I(file_inode(file));
  3665. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  3666. struct extent_io_tree *io_tree = &inode->io_tree;
  3667. struct iovec iovstack[UIO_FASTIOV];
  3668. struct iovec *iov = iovstack;
  3669. struct iov_iter iter;
  3670. loff_t pos;
  3671. struct kiocb kiocb;
  3672. ssize_t ret;
  3673. u64 disk_bytenr, disk_io_size;
  3674. struct extent_state *cached_state = NULL;
  3675. if (!capable(CAP_SYS_ADMIN)) {
  3676. ret = -EPERM;
  3677. goto out_acct;
  3678. }
  3679. if (compat) {
  3680. #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
  3681. struct btrfs_ioctl_encoded_io_args_32 args32;
  3682. copy_end = offsetofend(struct btrfs_ioctl_encoded_io_args_32,
  3683. flags);
  3684. if (copy_from_user(&args32, argp, copy_end)) {
  3685. ret = -EFAULT;
  3686. goto out_acct;
  3687. }
  3688. args.iov = compat_ptr(args32.iov);
  3689. args.iovcnt = args32.iovcnt;
  3690. args.offset = args32.offset;
  3691. args.flags = args32.flags;
  3692. #else
  3693. return -ENOTTY;
  3694. #endif
  3695. } else {
  3696. copy_end = copy_end_kernel;
  3697. if (copy_from_user(&args, argp, copy_end)) {
  3698. ret = -EFAULT;
  3699. goto out_acct;
  3700. }
  3701. }
  3702. if (args.flags != 0) {
  3703. ret = -EINVAL;
  3704. goto out_acct;
  3705. }
  3706. ret = import_iovec(ITER_DEST, args.iov, args.iovcnt, ARRAY_SIZE(iovstack),
  3707. &iov, &iter);
  3708. if (ret < 0)
  3709. goto out_acct;
  3710. if (iov_iter_count(&iter) == 0) {
  3711. ret = 0;
  3712. goto out_iov;
  3713. }
  3714. pos = args.offset;
  3715. ret = rw_verify_area(READ, file, &pos, args.len);
  3716. if (ret < 0)
  3717. goto out_iov;
  3718. init_sync_kiocb(&kiocb, file);
  3719. kiocb.ki_pos = pos;
  3720. ret = btrfs_encoded_read(&kiocb, &iter, &args, &cached_state,
  3721. &disk_bytenr, &disk_io_size);
  3722. if (ret == -EIOCBQUEUED) {
  3723. bool unlocked = false;
  3724. u64 start, lockend, count;
  3725. start = ALIGN_DOWN(kiocb.ki_pos, fs_info->sectorsize);
  3726. lockend = start + BTRFS_MAX_UNCOMPRESSED - 1;
  3727. if (args.compression)
  3728. count = disk_io_size;
  3729. else
  3730. count = args.len;
  3731. ret = btrfs_encoded_read_regular(&kiocb, &iter, start, lockend,
  3732. &cached_state, disk_bytenr,
  3733. disk_io_size, count,
  3734. args.compression, &unlocked);
  3735. if (!unlocked) {
  3736. btrfs_unlock_extent(io_tree, start, lockend, &cached_state);
  3737. btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
  3738. }
  3739. }
  3740. if (ret >= 0) {
  3741. fsnotify_access(file);
  3742. if (copy_to_user(argp + copy_end,
  3743. (char *)&args + copy_end_kernel,
  3744. sizeof(args) - copy_end_kernel))
  3745. ret = -EFAULT;
  3746. }
  3747. out_iov:
  3748. kfree(iov);
  3749. out_acct:
  3750. if (ret > 0)
  3751. add_rchar(current, ret);
  3752. inc_syscr(current);
  3753. return ret;
  3754. }
  3755. static int btrfs_ioctl_encoded_write(struct file *file, void __user *argp, bool compat)
  3756. {
  3757. struct btrfs_ioctl_encoded_io_args args;
  3758. struct iovec iovstack[UIO_FASTIOV];
  3759. struct iovec *iov = iovstack;
  3760. struct iov_iter iter;
  3761. loff_t pos;
  3762. struct kiocb kiocb;
  3763. ssize_t ret;
  3764. if (!capable(CAP_SYS_ADMIN)) {
  3765. ret = -EPERM;
  3766. goto out_acct;
  3767. }
  3768. if (!(file->f_mode & FMODE_WRITE)) {
  3769. ret = -EBADF;
  3770. goto out_acct;
  3771. }
  3772. if (compat) {
  3773. #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
  3774. struct btrfs_ioctl_encoded_io_args_32 args32;
  3775. if (copy_from_user(&args32, argp, sizeof(args32))) {
  3776. ret = -EFAULT;
  3777. goto out_acct;
  3778. }
  3779. args.iov = compat_ptr(args32.iov);
  3780. args.iovcnt = args32.iovcnt;
  3781. args.offset = args32.offset;
  3782. args.flags = args32.flags;
  3783. args.len = args32.len;
  3784. args.unencoded_len = args32.unencoded_len;
  3785. args.unencoded_offset = args32.unencoded_offset;
  3786. args.compression = args32.compression;
  3787. args.encryption = args32.encryption;
  3788. memcpy(args.reserved, args32.reserved, sizeof(args.reserved));
  3789. #else
  3790. return -ENOTTY;
  3791. #endif
  3792. } else {
  3793. if (copy_from_user(&args, argp, sizeof(args))) {
  3794. ret = -EFAULT;
  3795. goto out_acct;
  3796. }
  3797. }
  3798. ret = -EINVAL;
  3799. if (args.flags != 0)
  3800. goto out_acct;
  3801. if (memchr_inv(args.reserved, 0, sizeof(args.reserved)))
  3802. goto out_acct;
  3803. if (args.compression == BTRFS_ENCODED_IO_COMPRESSION_NONE &&
  3804. args.encryption == BTRFS_ENCODED_IO_ENCRYPTION_NONE)
  3805. goto out_acct;
  3806. if (args.compression >= BTRFS_ENCODED_IO_COMPRESSION_TYPES ||
  3807. args.encryption >= BTRFS_ENCODED_IO_ENCRYPTION_TYPES)
  3808. goto out_acct;
  3809. if (args.unencoded_offset > args.unencoded_len)
  3810. goto out_acct;
  3811. if (args.len > args.unencoded_len - args.unencoded_offset)
  3812. goto out_acct;
  3813. ret = import_iovec(ITER_SOURCE, args.iov, args.iovcnt, ARRAY_SIZE(iovstack),
  3814. &iov, &iter);
  3815. if (ret < 0)
  3816. goto out_acct;
  3817. if (iov_iter_count(&iter) == 0) {
  3818. ret = 0;
  3819. goto out_iov;
  3820. }
  3821. pos = args.offset;
  3822. ret = rw_verify_area(WRITE, file, &pos, args.len);
  3823. if (ret < 0)
  3824. goto out_iov;
  3825. init_sync_kiocb(&kiocb, file);
  3826. ret = kiocb_set_rw_flags(&kiocb, 0, WRITE);
  3827. if (ret)
  3828. goto out_iov;
  3829. kiocb.ki_pos = pos;
  3830. file_start_write(file);
  3831. ret = btrfs_do_write_iter(&kiocb, &iter, &args);
  3832. if (ret > 0)
  3833. fsnotify_modify(file);
  3834. file_end_write(file);
  3835. out_iov:
  3836. kfree(iov);
  3837. out_acct:
  3838. if (ret > 0)
  3839. add_wchar(current, ret);
  3840. inc_syscw(current);
  3841. return ret;
  3842. }
  3843. struct btrfs_uring_encoded_data {
  3844. struct btrfs_ioctl_encoded_io_args args;
  3845. struct iovec iovstack[UIO_FASTIOV];
  3846. struct iovec *iov;
  3847. struct iov_iter iter;
  3848. };
  3849. /*
  3850. * Context that's attached to an encoded read io_uring command, in cmd->pdu. It
  3851. * contains the fields in btrfs_uring_read_extent that are necessary to finish
  3852. * off and cleanup the I/O in btrfs_uring_read_finished.
  3853. */
  3854. struct btrfs_uring_priv {
  3855. struct io_uring_cmd *cmd;
  3856. struct page **pages;
  3857. unsigned long nr_pages;
  3858. struct kiocb iocb;
  3859. struct iovec *iov;
  3860. struct iov_iter iter;
  3861. struct extent_state *cached_state;
  3862. u64 count;
  3863. u64 start;
  3864. u64 lockend;
  3865. int err;
  3866. bool compressed;
  3867. };
  3868. struct io_btrfs_cmd {
  3869. struct btrfs_uring_encoded_data *data;
  3870. struct btrfs_uring_priv *priv;
  3871. };
  3872. static void btrfs_uring_read_finished(struct io_tw_req tw_req, io_tw_token_t tw)
  3873. {
  3874. struct io_uring_cmd *cmd = io_uring_cmd_from_tw(tw_req);
  3875. struct io_btrfs_cmd *bc = io_uring_cmd_to_pdu(cmd, struct io_btrfs_cmd);
  3876. struct btrfs_uring_priv *priv = bc->priv;
  3877. struct btrfs_inode *inode = BTRFS_I(file_inode(priv->iocb.ki_filp));
  3878. struct extent_io_tree *io_tree = &inode->io_tree;
  3879. pgoff_t index;
  3880. u64 cur;
  3881. size_t page_offset;
  3882. ssize_t ret;
  3883. /* The inode lock has already been acquired in btrfs_uring_read_extent. */
  3884. btrfs_lockdep_inode_acquire(inode, i_rwsem);
  3885. if (priv->err) {
  3886. ret = priv->err;
  3887. goto out;
  3888. }
  3889. if (priv->compressed) {
  3890. index = 0;
  3891. page_offset = 0;
  3892. } else {
  3893. index = (priv->iocb.ki_pos - priv->start) >> PAGE_SHIFT;
  3894. page_offset = offset_in_page(priv->iocb.ki_pos - priv->start);
  3895. }
  3896. cur = 0;
  3897. while (cur < priv->count) {
  3898. size_t bytes = min_t(size_t, priv->count - cur, PAGE_SIZE - page_offset);
  3899. if (copy_page_to_iter(priv->pages[index], page_offset, bytes,
  3900. &priv->iter) != bytes) {
  3901. ret = -EFAULT;
  3902. goto out;
  3903. }
  3904. index++;
  3905. cur += bytes;
  3906. page_offset = 0;
  3907. }
  3908. ret = priv->count;
  3909. out:
  3910. btrfs_unlock_extent(io_tree, priv->start, priv->lockend, &priv->cached_state);
  3911. btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
  3912. io_uring_cmd_done(cmd, ret, IO_URING_CMD_TASK_WORK_ISSUE_FLAGS);
  3913. add_rchar(current, ret);
  3914. for (index = 0; index < priv->nr_pages; index++)
  3915. __free_page(priv->pages[index]);
  3916. kfree(priv->pages);
  3917. kfree(priv->iov);
  3918. kfree(priv);
  3919. kfree(bc->data);
  3920. }
  3921. void btrfs_uring_read_extent_endio(void *ctx, int err)
  3922. {
  3923. struct btrfs_uring_priv *priv = ctx;
  3924. struct io_btrfs_cmd *bc = io_uring_cmd_to_pdu(priv->cmd, struct io_btrfs_cmd);
  3925. priv->err = err;
  3926. bc->priv = priv;
  3927. io_uring_cmd_complete_in_task(priv->cmd, btrfs_uring_read_finished);
  3928. }
  3929. static int btrfs_uring_read_extent(struct kiocb *iocb, struct iov_iter *iter,
  3930. u64 start, u64 lockend,
  3931. struct extent_state *cached_state,
  3932. u64 disk_bytenr, u64 disk_io_size,
  3933. size_t count, bool compressed,
  3934. struct iovec *iov, struct io_uring_cmd *cmd)
  3935. {
  3936. struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp));
  3937. struct extent_io_tree *io_tree = &inode->io_tree;
  3938. struct page **pages = NULL;
  3939. struct btrfs_uring_priv *priv = NULL;
  3940. unsigned long nr_pages;
  3941. int ret;
  3942. nr_pages = DIV_ROUND_UP(disk_io_size, PAGE_SIZE);
  3943. pages = kzalloc_objs(struct page *, nr_pages, GFP_NOFS);
  3944. if (!pages)
  3945. return -ENOMEM;
  3946. ret = btrfs_alloc_page_array(nr_pages, pages, 0);
  3947. if (ret) {
  3948. ret = -ENOMEM;
  3949. goto out_fail;
  3950. }
  3951. priv = kmalloc_obj(*priv, GFP_NOFS);
  3952. if (!priv) {
  3953. ret = -ENOMEM;
  3954. goto out_fail;
  3955. }
  3956. priv->iocb = *iocb;
  3957. priv->iov = iov;
  3958. priv->iter = *iter;
  3959. priv->count = count;
  3960. priv->cmd = cmd;
  3961. priv->cached_state = cached_state;
  3962. priv->compressed = compressed;
  3963. priv->nr_pages = nr_pages;
  3964. priv->pages = pages;
  3965. priv->start = start;
  3966. priv->lockend = lockend;
  3967. priv->err = 0;
  3968. ret = btrfs_encoded_read_regular_fill_pages(inode, disk_bytenr,
  3969. disk_io_size, pages, priv);
  3970. if (ret && ret != -EIOCBQUEUED)
  3971. goto out_fail;
  3972. /*
  3973. * If we return -EIOCBQUEUED, we're deferring the cleanup to
  3974. * btrfs_uring_read_finished(), which will handle unlocking the extent
  3975. * and inode and freeing the allocations.
  3976. */
  3977. /*
  3978. * We're returning to userspace with the inode lock held, and that's
  3979. * okay - it'll get unlocked in a worker thread. Call
  3980. * btrfs_lockdep_inode_release() to avoid confusing lockdep.
  3981. */
  3982. btrfs_lockdep_inode_release(inode, i_rwsem);
  3983. return -EIOCBQUEUED;
  3984. out_fail:
  3985. btrfs_unlock_extent(io_tree, start, lockend, &cached_state);
  3986. btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
  3987. kfree(priv);
  3988. for (int i = 0; i < nr_pages; i++) {
  3989. if (pages[i])
  3990. __free_page(pages[i]);
  3991. }
  3992. kfree(pages);
  3993. return ret;
  3994. }
  3995. static int btrfs_uring_encoded_read(struct io_uring_cmd *cmd, unsigned int issue_flags)
  3996. {
  3997. struct file *file = cmd->file;
  3998. struct btrfs_inode *inode = BTRFS_I(file->f_inode);
  3999. struct extent_io_tree *io_tree = &inode->io_tree;
  4000. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  4001. size_t copy_end_kernel = offsetofend(struct btrfs_ioctl_encoded_io_args, flags);
  4002. size_t copy_end;
  4003. int ret;
  4004. u64 disk_bytenr, disk_io_size;
  4005. loff_t pos;
  4006. struct kiocb kiocb;
  4007. struct extent_state *cached_state = NULL;
  4008. u64 start, lockend;
  4009. void __user *sqe_addr;
  4010. struct io_btrfs_cmd *bc = io_uring_cmd_to_pdu(cmd, struct io_btrfs_cmd);
  4011. struct btrfs_uring_encoded_data *data = NULL;
  4012. if (cmd->flags & IORING_URING_CMD_REISSUE)
  4013. data = bc->data;
  4014. if (!capable(CAP_SYS_ADMIN)) {
  4015. ret = -EPERM;
  4016. goto out_acct;
  4017. }
  4018. sqe_addr = u64_to_user_ptr(READ_ONCE(cmd->sqe->addr));
  4019. if (issue_flags & IO_URING_F_COMPAT) {
  4020. #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
  4021. copy_end = offsetofend(struct btrfs_ioctl_encoded_io_args_32, flags);
  4022. #else
  4023. ret = -ENOTTY;
  4024. goto out_acct;
  4025. #endif
  4026. } else {
  4027. copy_end = copy_end_kernel;
  4028. }
  4029. if (!data) {
  4030. data = kzalloc_obj(*data, GFP_NOFS);
  4031. if (!data) {
  4032. ret = -ENOMEM;
  4033. goto out_acct;
  4034. }
  4035. bc->data = data;
  4036. if (issue_flags & IO_URING_F_COMPAT) {
  4037. #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
  4038. struct btrfs_ioctl_encoded_io_args_32 args32;
  4039. if (copy_from_user(&args32, sqe_addr, copy_end)) {
  4040. ret = -EFAULT;
  4041. goto out_acct;
  4042. }
  4043. data->args.iov = compat_ptr(args32.iov);
  4044. data->args.iovcnt = args32.iovcnt;
  4045. data->args.offset = args32.offset;
  4046. data->args.flags = args32.flags;
  4047. #endif
  4048. } else {
  4049. if (copy_from_user(&data->args, sqe_addr, copy_end)) {
  4050. ret = -EFAULT;
  4051. goto out_acct;
  4052. }
  4053. }
  4054. if (data->args.flags != 0) {
  4055. ret = -EINVAL;
  4056. goto out_acct;
  4057. }
  4058. data->iov = data->iovstack;
  4059. ret = import_iovec(ITER_DEST, data->args.iov, data->args.iovcnt,
  4060. ARRAY_SIZE(data->iovstack), &data->iov,
  4061. &data->iter);
  4062. if (ret < 0)
  4063. goto out_acct;
  4064. if (iov_iter_count(&data->iter) == 0) {
  4065. ret = 0;
  4066. goto out_free;
  4067. }
  4068. }
  4069. pos = data->args.offset;
  4070. ret = rw_verify_area(READ, file, &pos, data->args.len);
  4071. if (ret < 0)
  4072. goto out_free;
  4073. init_sync_kiocb(&kiocb, file);
  4074. kiocb.ki_pos = pos;
  4075. if (issue_flags & IO_URING_F_NONBLOCK)
  4076. kiocb.ki_flags |= IOCB_NOWAIT;
  4077. start = ALIGN_DOWN(pos, fs_info->sectorsize);
  4078. lockend = start + BTRFS_MAX_UNCOMPRESSED - 1;
  4079. ret = btrfs_encoded_read(&kiocb, &data->iter, &data->args, &cached_state,
  4080. &disk_bytenr, &disk_io_size);
  4081. if (ret == -EAGAIN)
  4082. goto out_acct;
  4083. if (ret < 0 && ret != -EIOCBQUEUED)
  4084. goto out_free;
  4085. file_accessed(file);
  4086. if (copy_to_user(sqe_addr + copy_end,
  4087. (const char *)&data->args + copy_end_kernel,
  4088. sizeof(data->args) - copy_end_kernel)) {
  4089. if (ret == -EIOCBQUEUED) {
  4090. btrfs_unlock_extent(io_tree, start, lockend, &cached_state);
  4091. btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
  4092. }
  4093. ret = -EFAULT;
  4094. goto out_free;
  4095. }
  4096. if (ret == -EIOCBQUEUED) {
  4097. u64 count = min_t(u64, iov_iter_count(&data->iter), disk_io_size);
  4098. /* Match ioctl by not returning past EOF if uncompressed. */
  4099. if (!data->args.compression)
  4100. count = min_t(u64, count, data->args.len);
  4101. ret = btrfs_uring_read_extent(&kiocb, &data->iter, start, lockend,
  4102. cached_state, disk_bytenr, disk_io_size,
  4103. count, data->args.compression,
  4104. data->iov, cmd);
  4105. goto out_acct;
  4106. }
  4107. out_free:
  4108. kfree(data->iov);
  4109. out_acct:
  4110. if (ret > 0)
  4111. add_rchar(current, ret);
  4112. inc_syscr(current);
  4113. if (ret != -EIOCBQUEUED && ret != -EAGAIN)
  4114. kfree(data);
  4115. return ret;
  4116. }
  4117. static int btrfs_uring_encoded_write(struct io_uring_cmd *cmd, unsigned int issue_flags)
  4118. {
  4119. struct file *file = cmd->file;
  4120. loff_t pos;
  4121. struct kiocb kiocb;
  4122. ssize_t ret;
  4123. void __user *sqe_addr;
  4124. struct io_btrfs_cmd *bc = io_uring_cmd_to_pdu(cmd, struct io_btrfs_cmd);
  4125. struct btrfs_uring_encoded_data *data = NULL;
  4126. if (cmd->flags & IORING_URING_CMD_REISSUE)
  4127. data = bc->data;
  4128. if (!capable(CAP_SYS_ADMIN)) {
  4129. ret = -EPERM;
  4130. goto out_acct;
  4131. }
  4132. sqe_addr = u64_to_user_ptr(READ_ONCE(cmd->sqe->addr));
  4133. if (!(file->f_mode & FMODE_WRITE)) {
  4134. ret = -EBADF;
  4135. goto out_acct;
  4136. }
  4137. if (!data) {
  4138. data = kzalloc_obj(*data, GFP_NOFS);
  4139. if (!data) {
  4140. ret = -ENOMEM;
  4141. goto out_acct;
  4142. }
  4143. bc->data = data;
  4144. if (issue_flags & IO_URING_F_COMPAT) {
  4145. #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
  4146. struct btrfs_ioctl_encoded_io_args_32 args32;
  4147. if (copy_from_user(&args32, sqe_addr, sizeof(args32))) {
  4148. ret = -EFAULT;
  4149. goto out_acct;
  4150. }
  4151. data->args.iov = compat_ptr(args32.iov);
  4152. data->args.iovcnt = args32.iovcnt;
  4153. data->args.offset = args32.offset;
  4154. data->args.flags = args32.flags;
  4155. data->args.len = args32.len;
  4156. data->args.unencoded_len = args32.unencoded_len;
  4157. data->args.unencoded_offset = args32.unencoded_offset;
  4158. data->args.compression = args32.compression;
  4159. data->args.encryption = args32.encryption;
  4160. memcpy(data->args.reserved, args32.reserved,
  4161. sizeof(data->args.reserved));
  4162. #else
  4163. ret = -ENOTTY;
  4164. goto out_acct;
  4165. #endif
  4166. } else {
  4167. if (copy_from_user(&data->args, sqe_addr, sizeof(data->args))) {
  4168. ret = -EFAULT;
  4169. goto out_acct;
  4170. }
  4171. }
  4172. ret = -EINVAL;
  4173. if (data->args.flags != 0)
  4174. goto out_acct;
  4175. if (memchr_inv(data->args.reserved, 0, sizeof(data->args.reserved)))
  4176. goto out_acct;
  4177. if (data->args.compression == BTRFS_ENCODED_IO_COMPRESSION_NONE &&
  4178. data->args.encryption == BTRFS_ENCODED_IO_ENCRYPTION_NONE)
  4179. goto out_acct;
  4180. if (data->args.compression >= BTRFS_ENCODED_IO_COMPRESSION_TYPES ||
  4181. data->args.encryption >= BTRFS_ENCODED_IO_ENCRYPTION_TYPES)
  4182. goto out_acct;
  4183. if (data->args.unencoded_offset > data->args.unencoded_len)
  4184. goto out_acct;
  4185. if (data->args.len > data->args.unencoded_len - data->args.unencoded_offset)
  4186. goto out_acct;
  4187. data->iov = data->iovstack;
  4188. ret = import_iovec(ITER_SOURCE, data->args.iov, data->args.iovcnt,
  4189. ARRAY_SIZE(data->iovstack), &data->iov,
  4190. &data->iter);
  4191. if (ret < 0)
  4192. goto out_acct;
  4193. if (iov_iter_count(&data->iter) == 0) {
  4194. ret = 0;
  4195. goto out_iov;
  4196. }
  4197. }
  4198. if (issue_flags & IO_URING_F_NONBLOCK) {
  4199. ret = -EAGAIN;
  4200. goto out_acct;
  4201. }
  4202. pos = data->args.offset;
  4203. ret = rw_verify_area(WRITE, file, &pos, data->args.len);
  4204. if (ret < 0)
  4205. goto out_iov;
  4206. init_sync_kiocb(&kiocb, file);
  4207. ret = kiocb_set_rw_flags(&kiocb, 0, WRITE);
  4208. if (ret)
  4209. goto out_iov;
  4210. kiocb.ki_pos = pos;
  4211. file_start_write(file);
  4212. ret = btrfs_do_write_iter(&kiocb, &data->iter, &data->args);
  4213. if (ret > 0)
  4214. fsnotify_modify(file);
  4215. file_end_write(file);
  4216. out_iov:
  4217. kfree(data->iov);
  4218. out_acct:
  4219. if (ret > 0)
  4220. add_wchar(current, ret);
  4221. inc_syscw(current);
  4222. if (ret != -EAGAIN)
  4223. kfree(data);
  4224. return ret;
  4225. }
  4226. int btrfs_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
  4227. {
  4228. if (btrfs_is_shutdown(inode_to_fs_info(file_inode(cmd->file))))
  4229. return -EIO;
  4230. switch (cmd->cmd_op) {
  4231. case BTRFS_IOC_ENCODED_READ:
  4232. #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
  4233. case BTRFS_IOC_ENCODED_READ_32:
  4234. #endif
  4235. return btrfs_uring_encoded_read(cmd, issue_flags);
  4236. case BTRFS_IOC_ENCODED_WRITE:
  4237. #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
  4238. case BTRFS_IOC_ENCODED_WRITE_32:
  4239. #endif
  4240. return btrfs_uring_encoded_write(cmd, issue_flags);
  4241. }
  4242. return -EINVAL;
  4243. }
  4244. static int btrfs_ioctl_subvol_sync(struct btrfs_fs_info *fs_info, void __user *argp)
  4245. {
  4246. struct btrfs_root *root;
  4247. struct btrfs_ioctl_subvol_wait args = { 0 };
  4248. signed long sched_ret;
  4249. int refs;
  4250. u64 root_flags;
  4251. bool wait_for_deletion = false;
  4252. bool found = false;
  4253. if (copy_from_user(&args, argp, sizeof(args)))
  4254. return -EFAULT;
  4255. switch (args.mode) {
  4256. case BTRFS_SUBVOL_SYNC_WAIT_FOR_QUEUED:
  4257. /*
  4258. * Wait for the first one deleted that waits until all previous
  4259. * are cleaned.
  4260. */
  4261. spin_lock(&fs_info->trans_lock);
  4262. if (!list_empty(&fs_info->dead_roots)) {
  4263. root = list_last_entry(&fs_info->dead_roots,
  4264. struct btrfs_root, root_list);
  4265. args.subvolid = btrfs_root_id(root);
  4266. found = true;
  4267. }
  4268. spin_unlock(&fs_info->trans_lock);
  4269. if (!found)
  4270. return -ENOENT;
  4271. fallthrough;
  4272. case BTRFS_SUBVOL_SYNC_WAIT_FOR_ONE:
  4273. if ((0 < args.subvolid && args.subvolid < BTRFS_FIRST_FREE_OBJECTID) ||
  4274. BTRFS_LAST_FREE_OBJECTID < args.subvolid)
  4275. return -EINVAL;
  4276. break;
  4277. case BTRFS_SUBVOL_SYNC_COUNT:
  4278. spin_lock(&fs_info->trans_lock);
  4279. args.count = list_count_nodes(&fs_info->dead_roots);
  4280. spin_unlock(&fs_info->trans_lock);
  4281. if (copy_to_user(argp, &args, sizeof(args)))
  4282. return -EFAULT;
  4283. return 0;
  4284. case BTRFS_SUBVOL_SYNC_PEEK_FIRST:
  4285. spin_lock(&fs_info->trans_lock);
  4286. /* Last in the list was deleted first. */
  4287. if (!list_empty(&fs_info->dead_roots)) {
  4288. root = list_last_entry(&fs_info->dead_roots,
  4289. struct btrfs_root, root_list);
  4290. args.subvolid = btrfs_root_id(root);
  4291. } else {
  4292. args.subvolid = 0;
  4293. }
  4294. spin_unlock(&fs_info->trans_lock);
  4295. if (copy_to_user(argp, &args, sizeof(args)))
  4296. return -EFAULT;
  4297. return 0;
  4298. case BTRFS_SUBVOL_SYNC_PEEK_LAST:
  4299. spin_lock(&fs_info->trans_lock);
  4300. /* First in the list was deleted last. */
  4301. if (!list_empty(&fs_info->dead_roots)) {
  4302. root = list_first_entry(&fs_info->dead_roots,
  4303. struct btrfs_root, root_list);
  4304. args.subvolid = btrfs_root_id(root);
  4305. } else {
  4306. args.subvolid = 0;
  4307. }
  4308. spin_unlock(&fs_info->trans_lock);
  4309. if (copy_to_user(argp, &args, sizeof(args)))
  4310. return -EFAULT;
  4311. return 0;
  4312. default:
  4313. return -EINVAL;
  4314. }
  4315. /* 32bit limitation: fs_roots_radix key is not wide enough. */
  4316. if (sizeof(unsigned long) != sizeof(u64) && args.subvolid > U32_MAX)
  4317. return -EOVERFLOW;
  4318. while (1) {
  4319. /* Wait for the specific one. */
  4320. if (down_read_interruptible(&fs_info->subvol_sem) == -EINTR)
  4321. return -EINTR;
  4322. refs = -1;
  4323. spin_lock(&fs_info->fs_roots_radix_lock);
  4324. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  4325. (unsigned long)args.subvolid);
  4326. if (root) {
  4327. spin_lock(&root->root_item_lock);
  4328. refs = btrfs_root_refs(&root->root_item);
  4329. root_flags = btrfs_root_flags(&root->root_item);
  4330. spin_unlock(&root->root_item_lock);
  4331. }
  4332. spin_unlock(&fs_info->fs_roots_radix_lock);
  4333. up_read(&fs_info->subvol_sem);
  4334. /* Subvolume does not exist. */
  4335. if (!root)
  4336. return -ENOENT;
  4337. /* Subvolume not deleted at all. */
  4338. if (refs > 0)
  4339. return -EEXIST;
  4340. /* We've waited and now the subvolume is gone. */
  4341. if (wait_for_deletion && refs == -1) {
  4342. /* Return the one we waited for as the last one. */
  4343. if (copy_to_user(argp, &args, sizeof(args)))
  4344. return -EFAULT;
  4345. return 0;
  4346. }
  4347. /* Subvolume not found on the first try (deleted or never existed). */
  4348. if (refs == -1)
  4349. return -ENOENT;
  4350. wait_for_deletion = true;
  4351. ASSERT(root_flags & BTRFS_ROOT_SUBVOL_DEAD);
  4352. sched_ret = schedule_timeout_interruptible(HZ);
  4353. /* Early wake up or error. */
  4354. if (sched_ret != 0)
  4355. return -EINTR;
  4356. }
  4357. return 0;
  4358. }
  4359. #ifdef CONFIG_BTRFS_EXPERIMENTAL
  4360. static int btrfs_ioctl_shutdown(struct btrfs_fs_info *fs_info, unsigned long arg)
  4361. {
  4362. int ret = 0;
  4363. u32 flags;
  4364. if (!capable(CAP_SYS_ADMIN))
  4365. return -EPERM;
  4366. if (get_user(flags, (u32 __user *)arg))
  4367. return -EFAULT;
  4368. if (flags >= BTRFS_SHUTDOWN_FLAGS_LAST)
  4369. return -EINVAL;
  4370. if (btrfs_is_shutdown(fs_info))
  4371. return 0;
  4372. switch (flags) {
  4373. case BTRFS_SHUTDOWN_FLAGS_LOGFLUSH:
  4374. case BTRFS_SHUTDOWN_FLAGS_DEFAULT:
  4375. ret = freeze_super(fs_info->sb, FREEZE_HOLDER_KERNEL, NULL);
  4376. if (ret)
  4377. return ret;
  4378. btrfs_force_shutdown(fs_info);
  4379. ret = thaw_super(fs_info->sb, FREEZE_HOLDER_KERNEL, NULL);
  4380. if (ret)
  4381. return ret;
  4382. break;
  4383. case BTRFS_SHUTDOWN_FLAGS_NOLOGFLUSH:
  4384. btrfs_force_shutdown(fs_info);
  4385. break;
  4386. }
  4387. return ret;
  4388. }
  4389. #endif
  4390. long btrfs_ioctl(struct file *file, unsigned int
  4391. cmd, unsigned long arg)
  4392. {
  4393. struct inode *inode = file_inode(file);
  4394. struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
  4395. struct btrfs_root *root = BTRFS_I(inode)->root;
  4396. void __user *argp = (void __user *)arg;
  4397. switch (cmd) {
  4398. case FS_IOC_GETVERSION:
  4399. return btrfs_ioctl_getversion(inode, argp);
  4400. case FS_IOC_GETFSLABEL:
  4401. return btrfs_ioctl_get_fslabel(fs_info, argp);
  4402. case FS_IOC_SETFSLABEL:
  4403. return btrfs_ioctl_set_fslabel(file, argp);
  4404. case FITRIM:
  4405. return btrfs_ioctl_fitrim(fs_info, argp);
  4406. case BTRFS_IOC_SNAP_CREATE:
  4407. return btrfs_ioctl_snap_create(file, argp, false);
  4408. case BTRFS_IOC_SNAP_CREATE_V2:
  4409. return btrfs_ioctl_snap_create_v2(file, argp, false);
  4410. case BTRFS_IOC_SUBVOL_CREATE:
  4411. return btrfs_ioctl_snap_create(file, argp, true);
  4412. case BTRFS_IOC_SUBVOL_CREATE_V2:
  4413. return btrfs_ioctl_snap_create_v2(file, argp, true);
  4414. case BTRFS_IOC_SNAP_DESTROY:
  4415. return btrfs_ioctl_snap_destroy(file, argp, false);
  4416. case BTRFS_IOC_SNAP_DESTROY_V2:
  4417. return btrfs_ioctl_snap_destroy(file, argp, true);
  4418. case BTRFS_IOC_SUBVOL_GETFLAGS:
  4419. return btrfs_ioctl_subvol_getflags(BTRFS_I(inode), argp);
  4420. case BTRFS_IOC_SUBVOL_SETFLAGS:
  4421. return btrfs_ioctl_subvol_setflags(file, argp);
  4422. case BTRFS_IOC_DEFAULT_SUBVOL:
  4423. return btrfs_ioctl_default_subvol(file, argp);
  4424. case BTRFS_IOC_DEFRAG:
  4425. return btrfs_ioctl_defrag(file, NULL);
  4426. case BTRFS_IOC_DEFRAG_RANGE:
  4427. return btrfs_ioctl_defrag(file, argp);
  4428. case BTRFS_IOC_RESIZE:
  4429. return btrfs_ioctl_resize(file, argp);
  4430. case BTRFS_IOC_ADD_DEV:
  4431. return btrfs_ioctl_add_dev(fs_info, argp);
  4432. case BTRFS_IOC_RM_DEV:
  4433. return btrfs_ioctl_rm_dev(file, argp);
  4434. case BTRFS_IOC_RM_DEV_V2:
  4435. return btrfs_ioctl_rm_dev_v2(file, argp);
  4436. case BTRFS_IOC_FS_INFO:
  4437. return btrfs_ioctl_fs_info(fs_info, argp);
  4438. case BTRFS_IOC_DEV_INFO:
  4439. return btrfs_ioctl_dev_info(fs_info, argp);
  4440. case BTRFS_IOC_TREE_SEARCH:
  4441. return btrfs_ioctl_tree_search(root, argp);
  4442. case BTRFS_IOC_TREE_SEARCH_V2:
  4443. return btrfs_ioctl_tree_search_v2(root, argp);
  4444. case BTRFS_IOC_INO_LOOKUP:
  4445. return btrfs_ioctl_ino_lookup(root, argp);
  4446. case BTRFS_IOC_INO_PATHS:
  4447. return btrfs_ioctl_ino_to_path(root, argp);
  4448. case BTRFS_IOC_LOGICAL_INO:
  4449. return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
  4450. case BTRFS_IOC_LOGICAL_INO_V2:
  4451. return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
  4452. case BTRFS_IOC_SPACE_INFO:
  4453. return btrfs_ioctl_space_info(fs_info, argp);
  4454. case BTRFS_IOC_SYNC: {
  4455. int ret;
  4456. ret = btrfs_start_delalloc_roots(fs_info, LONG_MAX, false);
  4457. if (ret)
  4458. return ret;
  4459. ret = btrfs_sync_fs(inode->i_sb, 1);
  4460. /*
  4461. * There may be work for the cleaner kthread to do (subvolume
  4462. * deletion, delayed iputs, defrag inodes, etc), so wake it up.
  4463. */
  4464. wake_up_process(fs_info->cleaner_kthread);
  4465. return ret;
  4466. }
  4467. case BTRFS_IOC_START_SYNC:
  4468. return btrfs_ioctl_start_sync(root, argp);
  4469. case BTRFS_IOC_WAIT_SYNC:
  4470. return btrfs_ioctl_wait_sync(fs_info, argp);
  4471. case BTRFS_IOC_SCRUB:
  4472. return btrfs_ioctl_scrub(file, argp);
  4473. case BTRFS_IOC_SCRUB_CANCEL:
  4474. return btrfs_ioctl_scrub_cancel(fs_info);
  4475. case BTRFS_IOC_SCRUB_PROGRESS:
  4476. return btrfs_ioctl_scrub_progress(fs_info, argp);
  4477. case BTRFS_IOC_BALANCE_V2:
  4478. return btrfs_ioctl_balance(file, argp);
  4479. case BTRFS_IOC_BALANCE_CTL:
  4480. return btrfs_ioctl_balance_ctl(fs_info, arg);
  4481. case BTRFS_IOC_BALANCE_PROGRESS:
  4482. return btrfs_ioctl_balance_progress(fs_info, argp);
  4483. case BTRFS_IOC_SET_RECEIVED_SUBVOL:
  4484. return btrfs_ioctl_set_received_subvol(file, argp);
  4485. #ifdef CONFIG_64BIT
  4486. case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
  4487. return btrfs_ioctl_set_received_subvol_32(file, argp);
  4488. #endif
  4489. case BTRFS_IOC_SEND:
  4490. return _btrfs_ioctl_send(root, argp, false);
  4491. #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
  4492. case BTRFS_IOC_SEND_32:
  4493. return _btrfs_ioctl_send(root, argp, true);
  4494. #endif
  4495. case BTRFS_IOC_GET_DEV_STATS:
  4496. return btrfs_ioctl_get_dev_stats(fs_info, argp);
  4497. case BTRFS_IOC_QUOTA_CTL:
  4498. return btrfs_ioctl_quota_ctl(file, argp);
  4499. case BTRFS_IOC_QGROUP_ASSIGN:
  4500. return btrfs_ioctl_qgroup_assign(file, argp);
  4501. case BTRFS_IOC_QGROUP_CREATE:
  4502. return btrfs_ioctl_qgroup_create(file, argp);
  4503. case BTRFS_IOC_QGROUP_LIMIT:
  4504. return btrfs_ioctl_qgroup_limit(file, argp);
  4505. case BTRFS_IOC_QUOTA_RESCAN:
  4506. return btrfs_ioctl_quota_rescan(file, argp);
  4507. case BTRFS_IOC_QUOTA_RESCAN_STATUS:
  4508. return btrfs_ioctl_quota_rescan_status(fs_info, argp);
  4509. case BTRFS_IOC_QUOTA_RESCAN_WAIT:
  4510. return btrfs_ioctl_quota_rescan_wait(fs_info);
  4511. case BTRFS_IOC_DEV_REPLACE:
  4512. return btrfs_ioctl_dev_replace(fs_info, argp);
  4513. case BTRFS_IOC_GET_SUPPORTED_FEATURES:
  4514. return btrfs_ioctl_get_supported_features(argp);
  4515. case BTRFS_IOC_GET_FEATURES:
  4516. return btrfs_ioctl_get_features(fs_info, argp);
  4517. case BTRFS_IOC_SET_FEATURES:
  4518. return btrfs_ioctl_set_features(file, argp);
  4519. case BTRFS_IOC_GET_SUBVOL_INFO:
  4520. return btrfs_ioctl_get_subvol_info(inode, argp);
  4521. case BTRFS_IOC_GET_SUBVOL_ROOTREF:
  4522. return btrfs_ioctl_get_subvol_rootref(root, argp);
  4523. case BTRFS_IOC_INO_LOOKUP_USER:
  4524. return btrfs_ioctl_ino_lookup_user(file, argp);
  4525. case FS_IOC_ENABLE_VERITY:
  4526. return fsverity_ioctl_enable(file, (const void __user *)argp);
  4527. case FS_IOC_MEASURE_VERITY:
  4528. return fsverity_ioctl_measure(file, argp);
  4529. case FS_IOC_READ_VERITY_METADATA:
  4530. return fsverity_ioctl_read_metadata(file, argp);
  4531. case BTRFS_IOC_ENCODED_READ:
  4532. return btrfs_ioctl_encoded_read(file, argp, false);
  4533. case BTRFS_IOC_ENCODED_WRITE:
  4534. return btrfs_ioctl_encoded_write(file, argp, false);
  4535. #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
  4536. case BTRFS_IOC_ENCODED_READ_32:
  4537. return btrfs_ioctl_encoded_read(file, argp, true);
  4538. case BTRFS_IOC_ENCODED_WRITE_32:
  4539. return btrfs_ioctl_encoded_write(file, argp, true);
  4540. #endif
  4541. case BTRFS_IOC_SUBVOL_SYNC_WAIT:
  4542. return btrfs_ioctl_subvol_sync(fs_info, argp);
  4543. #ifdef CONFIG_BTRFS_EXPERIMENTAL
  4544. case BTRFS_IOC_SHUTDOWN:
  4545. return btrfs_ioctl_shutdown(fs_info, arg);
  4546. #endif
  4547. }
  4548. return -ENOTTY;
  4549. }
  4550. #ifdef CONFIG_COMPAT
  4551. long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  4552. {
  4553. /*
  4554. * These all access 32-bit values anyway so no further
  4555. * handling is necessary.
  4556. */
  4557. switch (cmd) {
  4558. case FS_IOC32_GETVERSION:
  4559. cmd = FS_IOC_GETVERSION;
  4560. break;
  4561. }
  4562. return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  4563. }
  4564. #endif