ioctl.c 89 KB

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