ioctl.c 138 KB

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