ioctl.c 136 KB

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