ioctl.c 79 KB

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