ioctl.c 114 KB

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