ioctl.c 71 KB

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