ioctl.c 74 KB

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