ioctl.c 108 KB

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