ioctl.c 137 KB

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