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