ioctl.c 129 KB

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