ioctl.c 79 KB

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