ioctl.c 118 KB

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