ioctl.c 80 KB

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