ioctl.c 130 KB

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