ioctl.c 129 KB

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