ioctl.c 138 KB

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