ioctl.c 118 KB

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