ioctl.c 108 KB

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