ioctl.c 130 KB

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