ioctl.c 120 KB

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