ioctl.c 137 KB

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