ioctl.c 138 KB

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