ioctl.c 97 KB

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