ioctl.c 129 KB

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