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