ioctl.c 108 KB

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