ioctl.c 138 KB

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