ioctl.c 97 KB

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