ioctl.c 133 KB

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