ioctl.c 82 KB

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