ioctl.c 108 KB

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