ioctl.c 108 KB

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