ioctl.c 108 KB

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