ioctl.c 129 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372
  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 <linux/uaccess.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. #include "props.h"
  59. #include "sysfs.h"
  60. #include "qgroup.h"
  61. #ifdef CONFIG_64BIT
  62. /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
  63. * structures are incorrect, as the timespec structure from userspace
  64. * is 4 bytes too small. We define these alternatives here to teach
  65. * the kernel about the 32-bit struct packing.
  66. */
  67. struct btrfs_ioctl_timespec_32 {
  68. __u64 sec;
  69. __u32 nsec;
  70. } __attribute__ ((__packed__));
  71. struct btrfs_ioctl_received_subvol_args_32 {
  72. char uuid[BTRFS_UUID_SIZE]; /* in */
  73. __u64 stransid; /* in */
  74. __u64 rtransid; /* out */
  75. struct btrfs_ioctl_timespec_32 stime; /* in */
  76. struct btrfs_ioctl_timespec_32 rtime; /* out */
  77. __u64 flags; /* in */
  78. __u64 reserved[16]; /* in */
  79. } __attribute__ ((__packed__));
  80. #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
  81. struct btrfs_ioctl_received_subvol_args_32)
  82. #endif
  83. static int btrfs_clone(struct inode *src, struct inode *inode,
  84. u64 off, u64 olen, u64 olen_aligned, u64 destoff);
  85. /* Mask out flags that are inappropriate for the given type of inode. */
  86. static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
  87. {
  88. if (S_ISDIR(mode))
  89. return flags;
  90. else if (S_ISREG(mode))
  91. return flags & ~FS_DIRSYNC_FL;
  92. else
  93. return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
  94. }
  95. /*
  96. * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
  97. */
  98. static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
  99. {
  100. unsigned int iflags = 0;
  101. if (flags & BTRFS_INODE_SYNC)
  102. iflags |= FS_SYNC_FL;
  103. if (flags & BTRFS_INODE_IMMUTABLE)
  104. iflags |= FS_IMMUTABLE_FL;
  105. if (flags & BTRFS_INODE_APPEND)
  106. iflags |= FS_APPEND_FL;
  107. if (flags & BTRFS_INODE_NODUMP)
  108. iflags |= FS_NODUMP_FL;
  109. if (flags & BTRFS_INODE_NOATIME)
  110. iflags |= FS_NOATIME_FL;
  111. if (flags & BTRFS_INODE_DIRSYNC)
  112. iflags |= FS_DIRSYNC_FL;
  113. if (flags & BTRFS_INODE_NODATACOW)
  114. iflags |= FS_NOCOW_FL;
  115. if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
  116. iflags |= FS_COMPR_FL;
  117. else if (flags & BTRFS_INODE_NOCOMPRESS)
  118. iflags |= FS_NOCOMP_FL;
  119. return iflags;
  120. }
  121. /*
  122. * Update inode->i_flags based on the btrfs internal flags.
  123. */
  124. void btrfs_update_iflags(struct inode *inode)
  125. {
  126. struct btrfs_inode *ip = BTRFS_I(inode);
  127. unsigned int new_fl = 0;
  128. if (ip->flags & BTRFS_INODE_SYNC)
  129. new_fl |= S_SYNC;
  130. if (ip->flags & BTRFS_INODE_IMMUTABLE)
  131. new_fl |= S_IMMUTABLE;
  132. if (ip->flags & BTRFS_INODE_APPEND)
  133. new_fl |= S_APPEND;
  134. if (ip->flags & BTRFS_INODE_NOATIME)
  135. new_fl |= S_NOATIME;
  136. if (ip->flags & BTRFS_INODE_DIRSYNC)
  137. new_fl |= S_DIRSYNC;
  138. set_mask_bits(&inode->i_flags,
  139. S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
  140. new_fl);
  141. }
  142. /*
  143. * Inherit flags from the parent inode.
  144. *
  145. * Currently only the compression flags and the cow flags are inherited.
  146. */
  147. void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
  148. {
  149. unsigned int flags;
  150. if (!dir)
  151. return;
  152. flags = BTRFS_I(dir)->flags;
  153. if (flags & BTRFS_INODE_NOCOMPRESS) {
  154. BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
  155. BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
  156. } else if (flags & BTRFS_INODE_COMPRESS) {
  157. BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
  158. BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
  159. }
  160. if (flags & BTRFS_INODE_NODATACOW) {
  161. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
  162. if (S_ISREG(inode->i_mode))
  163. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  164. }
  165. btrfs_update_iflags(inode);
  166. }
  167. static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
  168. {
  169. struct btrfs_inode *ip = BTRFS_I(file_inode(file));
  170. unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
  171. if (copy_to_user(arg, &flags, sizeof(flags)))
  172. return -EFAULT;
  173. return 0;
  174. }
  175. static int check_flags(unsigned int flags)
  176. {
  177. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  178. FS_NOATIME_FL | FS_NODUMP_FL | \
  179. FS_SYNC_FL | FS_DIRSYNC_FL | \
  180. FS_NOCOMP_FL | FS_COMPR_FL |
  181. FS_NOCOW_FL))
  182. return -EOPNOTSUPP;
  183. if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
  184. return -EINVAL;
  185. return 0;
  186. }
  187. static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
  188. {
  189. struct inode *inode = file_inode(file);
  190. struct btrfs_inode *ip = BTRFS_I(inode);
  191. struct btrfs_root *root = ip->root;
  192. struct btrfs_trans_handle *trans;
  193. unsigned int flags, oldflags;
  194. int ret;
  195. u64 ip_oldflags;
  196. unsigned int i_oldflags;
  197. umode_t mode;
  198. if (!inode_owner_or_capable(inode))
  199. return -EPERM;
  200. if (btrfs_root_readonly(root))
  201. return -EROFS;
  202. if (copy_from_user(&flags, arg, sizeof(flags)))
  203. return -EFAULT;
  204. ret = check_flags(flags);
  205. if (ret)
  206. return ret;
  207. ret = mnt_want_write_file(file);
  208. if (ret)
  209. return ret;
  210. mutex_lock(&inode->i_mutex);
  211. ip_oldflags = ip->flags;
  212. i_oldflags = inode->i_flags;
  213. mode = inode->i_mode;
  214. flags = btrfs_mask_flags(inode->i_mode, flags);
  215. oldflags = btrfs_flags_to_ioctl(ip->flags);
  216. if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
  217. if (!capable(CAP_LINUX_IMMUTABLE)) {
  218. ret = -EPERM;
  219. goto out_unlock;
  220. }
  221. }
  222. if (flags & FS_SYNC_FL)
  223. ip->flags |= BTRFS_INODE_SYNC;
  224. else
  225. ip->flags &= ~BTRFS_INODE_SYNC;
  226. if (flags & FS_IMMUTABLE_FL)
  227. ip->flags |= BTRFS_INODE_IMMUTABLE;
  228. else
  229. ip->flags &= ~BTRFS_INODE_IMMUTABLE;
  230. if (flags & FS_APPEND_FL)
  231. ip->flags |= BTRFS_INODE_APPEND;
  232. else
  233. ip->flags &= ~BTRFS_INODE_APPEND;
  234. if (flags & FS_NODUMP_FL)
  235. ip->flags |= BTRFS_INODE_NODUMP;
  236. else
  237. ip->flags &= ~BTRFS_INODE_NODUMP;
  238. if (flags & FS_NOATIME_FL)
  239. ip->flags |= BTRFS_INODE_NOATIME;
  240. else
  241. ip->flags &= ~BTRFS_INODE_NOATIME;
  242. if (flags & FS_DIRSYNC_FL)
  243. ip->flags |= BTRFS_INODE_DIRSYNC;
  244. else
  245. ip->flags &= ~BTRFS_INODE_DIRSYNC;
  246. if (flags & FS_NOCOW_FL) {
  247. if (S_ISREG(mode)) {
  248. /*
  249. * It's safe to turn csums off here, no extents exist.
  250. * Otherwise we want the flag to reflect the real COW
  251. * status of the file and will not set it.
  252. */
  253. if (inode->i_size == 0)
  254. ip->flags |= BTRFS_INODE_NODATACOW
  255. | BTRFS_INODE_NODATASUM;
  256. } else {
  257. ip->flags |= BTRFS_INODE_NODATACOW;
  258. }
  259. } else {
  260. /*
  261. * Revert back under same assuptions as above
  262. */
  263. if (S_ISREG(mode)) {
  264. if (inode->i_size == 0)
  265. ip->flags &= ~(BTRFS_INODE_NODATACOW
  266. | BTRFS_INODE_NODATASUM);
  267. } else {
  268. ip->flags &= ~BTRFS_INODE_NODATACOW;
  269. }
  270. }
  271. /*
  272. * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
  273. * flag may be changed automatically if compression code won't make
  274. * things smaller.
  275. */
  276. if (flags & FS_NOCOMP_FL) {
  277. ip->flags &= ~BTRFS_INODE_COMPRESS;
  278. ip->flags |= BTRFS_INODE_NOCOMPRESS;
  279. ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
  280. if (ret && ret != -ENODATA)
  281. goto out_drop;
  282. } else if (flags & FS_COMPR_FL) {
  283. const char *comp;
  284. ip->flags |= BTRFS_INODE_COMPRESS;
  285. ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
  286. if (root->fs_info->compress_type == BTRFS_COMPRESS_LZO)
  287. comp = "lzo";
  288. else
  289. comp = "zlib";
  290. ret = btrfs_set_prop(inode, "btrfs.compression",
  291. comp, strlen(comp), 0);
  292. if (ret)
  293. goto out_drop;
  294. } else {
  295. ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
  296. if (ret && ret != -ENODATA)
  297. goto out_drop;
  298. ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
  299. }
  300. trans = btrfs_start_transaction(root, 1);
  301. if (IS_ERR(trans)) {
  302. ret = PTR_ERR(trans);
  303. goto out_drop;
  304. }
  305. btrfs_update_iflags(inode);
  306. inode_inc_iversion(inode);
  307. inode->i_ctime = CURRENT_TIME;
  308. ret = btrfs_update_inode(trans, root, inode);
  309. btrfs_end_transaction(trans, root);
  310. out_drop:
  311. if (ret) {
  312. ip->flags = ip_oldflags;
  313. inode->i_flags = i_oldflags;
  314. }
  315. out_unlock:
  316. mutex_unlock(&inode->i_mutex);
  317. mnt_drop_write_file(file);
  318. return ret;
  319. }
  320. static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
  321. {
  322. struct inode *inode = file_inode(file);
  323. return put_user(inode->i_generation, arg);
  324. }
  325. static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
  326. {
  327. struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
  328. struct btrfs_device *device;
  329. struct request_queue *q;
  330. struct fstrim_range range;
  331. u64 minlen = ULLONG_MAX;
  332. u64 num_devices = 0;
  333. u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
  334. int ret;
  335. if (!capable(CAP_SYS_ADMIN))
  336. return -EPERM;
  337. rcu_read_lock();
  338. list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
  339. dev_list) {
  340. if (!device->bdev)
  341. continue;
  342. q = bdev_get_queue(device->bdev);
  343. if (blk_queue_discard(q)) {
  344. num_devices++;
  345. minlen = min((u64)q->limits.discard_granularity,
  346. minlen);
  347. }
  348. }
  349. rcu_read_unlock();
  350. if (!num_devices)
  351. return -EOPNOTSUPP;
  352. if (copy_from_user(&range, arg, sizeof(range)))
  353. return -EFAULT;
  354. if (range.start > total_bytes ||
  355. range.len < fs_info->sb->s_blocksize)
  356. return -EINVAL;
  357. range.len = min(range.len, total_bytes - range.start);
  358. range.minlen = max(range.minlen, minlen);
  359. ret = btrfs_trim_fs(fs_info->tree_root, &range);
  360. if (ret < 0)
  361. return ret;
  362. if (copy_to_user(arg, &range, sizeof(range)))
  363. return -EFAULT;
  364. return 0;
  365. }
  366. int btrfs_is_empty_uuid(u8 *uuid)
  367. {
  368. int i;
  369. for (i = 0; i < BTRFS_UUID_SIZE; i++) {
  370. if (uuid[i])
  371. return 0;
  372. }
  373. return 1;
  374. }
  375. static noinline int create_subvol(struct inode *dir,
  376. struct dentry *dentry,
  377. char *name, int namelen,
  378. u64 *async_transid,
  379. struct btrfs_qgroup_inherit *inherit)
  380. {
  381. struct btrfs_trans_handle *trans;
  382. struct btrfs_key key;
  383. struct btrfs_root_item root_item;
  384. struct btrfs_inode_item *inode_item;
  385. struct extent_buffer *leaf;
  386. struct btrfs_root *root = BTRFS_I(dir)->root;
  387. struct btrfs_root *new_root;
  388. struct btrfs_block_rsv block_rsv;
  389. struct timespec cur_time = CURRENT_TIME;
  390. struct inode *inode;
  391. int ret;
  392. int err;
  393. u64 objectid;
  394. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  395. u64 index = 0;
  396. u64 qgroup_reserved;
  397. uuid_le new_uuid;
  398. ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
  399. if (ret)
  400. return ret;
  401. btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
  402. /*
  403. * The same as the snapshot creation, please see the comment
  404. * of create_snapshot().
  405. */
  406. ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
  407. 8, &qgroup_reserved, false);
  408. if (ret)
  409. return ret;
  410. trans = btrfs_start_transaction(root, 0);
  411. if (IS_ERR(trans)) {
  412. ret = PTR_ERR(trans);
  413. btrfs_subvolume_release_metadata(root, &block_rsv,
  414. qgroup_reserved);
  415. return ret;
  416. }
  417. trans->block_rsv = &block_rsv;
  418. trans->bytes_reserved = block_rsv.size;
  419. ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
  420. if (ret)
  421. goto fail;
  422. leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
  423. if (IS_ERR(leaf)) {
  424. ret = PTR_ERR(leaf);
  425. goto fail;
  426. }
  427. memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
  428. btrfs_set_header_bytenr(leaf, leaf->start);
  429. btrfs_set_header_generation(leaf, trans->transid);
  430. btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
  431. btrfs_set_header_owner(leaf, objectid);
  432. write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
  433. BTRFS_FSID_SIZE);
  434. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  435. btrfs_header_chunk_tree_uuid(leaf),
  436. BTRFS_UUID_SIZE);
  437. btrfs_mark_buffer_dirty(leaf);
  438. memset(&root_item, 0, sizeof(root_item));
  439. inode_item = &root_item.inode;
  440. btrfs_set_stack_inode_generation(inode_item, 1);
  441. btrfs_set_stack_inode_size(inode_item, 3);
  442. btrfs_set_stack_inode_nlink(inode_item, 1);
  443. btrfs_set_stack_inode_nbytes(inode_item, root->nodesize);
  444. btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
  445. btrfs_set_root_flags(&root_item, 0);
  446. btrfs_set_root_limit(&root_item, 0);
  447. btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
  448. btrfs_set_root_bytenr(&root_item, leaf->start);
  449. btrfs_set_root_generation(&root_item, trans->transid);
  450. btrfs_set_root_level(&root_item, 0);
  451. btrfs_set_root_refs(&root_item, 1);
  452. btrfs_set_root_used(&root_item, leaf->len);
  453. btrfs_set_root_last_snapshot(&root_item, 0);
  454. btrfs_set_root_generation_v2(&root_item,
  455. btrfs_root_generation(&root_item));
  456. uuid_le_gen(&new_uuid);
  457. memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
  458. btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
  459. btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
  460. root_item.ctime = root_item.otime;
  461. btrfs_set_root_ctransid(&root_item, trans->transid);
  462. btrfs_set_root_otransid(&root_item, trans->transid);
  463. btrfs_tree_unlock(leaf);
  464. free_extent_buffer(leaf);
  465. leaf = NULL;
  466. btrfs_set_root_dirid(&root_item, new_dirid);
  467. key.objectid = objectid;
  468. key.offset = 0;
  469. key.type = BTRFS_ROOT_ITEM_KEY;
  470. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  471. &root_item);
  472. if (ret)
  473. goto fail;
  474. key.offset = (u64)-1;
  475. new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
  476. if (IS_ERR(new_root)) {
  477. btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
  478. ret = PTR_ERR(new_root);
  479. goto fail;
  480. }
  481. btrfs_record_root_in_trans(trans, new_root);
  482. ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
  483. if (ret) {
  484. /* We potentially lose an unused inode item here */
  485. btrfs_abort_transaction(trans, root, ret);
  486. goto fail;
  487. }
  488. /*
  489. * insert the directory item
  490. */
  491. ret = btrfs_set_inode_index(dir, &index);
  492. if (ret) {
  493. btrfs_abort_transaction(trans, root, ret);
  494. goto fail;
  495. }
  496. ret = btrfs_insert_dir_item(trans, root,
  497. name, namelen, dir, &key,
  498. BTRFS_FT_DIR, index);
  499. if (ret) {
  500. btrfs_abort_transaction(trans, root, ret);
  501. goto fail;
  502. }
  503. btrfs_i_size_write(dir, dir->i_size + namelen * 2);
  504. ret = btrfs_update_inode(trans, root, dir);
  505. BUG_ON(ret);
  506. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  507. objectid, root->root_key.objectid,
  508. btrfs_ino(dir), index, name, namelen);
  509. BUG_ON(ret);
  510. ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
  511. root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
  512. objectid);
  513. if (ret)
  514. btrfs_abort_transaction(trans, root, ret);
  515. fail:
  516. trans->block_rsv = NULL;
  517. trans->bytes_reserved = 0;
  518. btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
  519. if (async_transid) {
  520. *async_transid = trans->transid;
  521. err = btrfs_commit_transaction_async(trans, root, 1);
  522. if (err)
  523. err = btrfs_commit_transaction(trans, root);
  524. } else {
  525. err = btrfs_commit_transaction(trans, root);
  526. }
  527. if (err && !ret)
  528. ret = err;
  529. if (!ret) {
  530. inode = btrfs_lookup_dentry(dir, dentry);
  531. if (IS_ERR(inode))
  532. return PTR_ERR(inode);
  533. d_instantiate(dentry, inode);
  534. }
  535. return ret;
  536. }
  537. static void btrfs_wait_nocow_write(struct btrfs_root *root)
  538. {
  539. s64 writers;
  540. DEFINE_WAIT(wait);
  541. do {
  542. prepare_to_wait(&root->subv_writers->wait, &wait,
  543. TASK_UNINTERRUPTIBLE);
  544. writers = percpu_counter_sum(&root->subv_writers->counter);
  545. if (writers)
  546. schedule();
  547. finish_wait(&root->subv_writers->wait, &wait);
  548. } while (writers);
  549. }
  550. static int create_snapshot(struct btrfs_root *root, struct inode *dir,
  551. struct dentry *dentry, char *name, int namelen,
  552. u64 *async_transid, bool readonly,
  553. struct btrfs_qgroup_inherit *inherit)
  554. {
  555. struct inode *inode;
  556. struct btrfs_pending_snapshot *pending_snapshot;
  557. struct btrfs_trans_handle *trans;
  558. int ret;
  559. if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
  560. return -EINVAL;
  561. atomic_inc(&root->will_be_snapshoted);
  562. smp_mb__after_atomic();
  563. btrfs_wait_nocow_write(root);
  564. ret = btrfs_start_delalloc_inodes(root, 0);
  565. if (ret)
  566. goto out;
  567. btrfs_wait_ordered_extents(root, -1);
  568. pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
  569. if (!pending_snapshot) {
  570. ret = -ENOMEM;
  571. goto out;
  572. }
  573. btrfs_init_block_rsv(&pending_snapshot->block_rsv,
  574. BTRFS_BLOCK_RSV_TEMP);
  575. /*
  576. * 1 - parent dir inode
  577. * 2 - dir entries
  578. * 1 - root item
  579. * 2 - root ref/backref
  580. * 1 - root of snapshot
  581. * 1 - UUID item
  582. */
  583. ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
  584. &pending_snapshot->block_rsv, 8,
  585. &pending_snapshot->qgroup_reserved,
  586. false);
  587. if (ret)
  588. goto free;
  589. pending_snapshot->dentry = dentry;
  590. pending_snapshot->root = root;
  591. pending_snapshot->readonly = readonly;
  592. pending_snapshot->dir = dir;
  593. pending_snapshot->inherit = inherit;
  594. trans = btrfs_start_transaction(root, 0);
  595. if (IS_ERR(trans)) {
  596. ret = PTR_ERR(trans);
  597. goto fail;
  598. }
  599. spin_lock(&root->fs_info->trans_lock);
  600. list_add(&pending_snapshot->list,
  601. &trans->transaction->pending_snapshots);
  602. spin_unlock(&root->fs_info->trans_lock);
  603. if (async_transid) {
  604. *async_transid = trans->transid;
  605. ret = btrfs_commit_transaction_async(trans,
  606. root->fs_info->extent_root, 1);
  607. if (ret)
  608. ret = btrfs_commit_transaction(trans, root);
  609. } else {
  610. ret = btrfs_commit_transaction(trans,
  611. root->fs_info->extent_root);
  612. }
  613. if (ret)
  614. goto fail;
  615. ret = pending_snapshot->error;
  616. if (ret)
  617. goto fail;
  618. ret = btrfs_orphan_cleanup(pending_snapshot->snap);
  619. if (ret)
  620. goto fail;
  621. /*
  622. * If orphan cleanup did remove any orphans, it means the tree was
  623. * modified and therefore the commit root is not the same as the
  624. * current root anymore. This is a problem, because send uses the
  625. * commit root and therefore can see inode items that don't exist
  626. * in the current root anymore, and for example make calls to
  627. * btrfs_iget, which will do tree lookups based on the current root
  628. * and not on the commit root. Those lookups will fail, returning a
  629. * -ESTALE error, and making send fail with that error. So make sure
  630. * a send does not see any orphans we have just removed, and that it
  631. * will see the same inodes regardless of whether a transaction
  632. * commit happened before it started (meaning that the commit root
  633. * will be the same as the current root) or not.
  634. */
  635. if (readonly && pending_snapshot->snap->node !=
  636. pending_snapshot->snap->commit_root) {
  637. trans = btrfs_join_transaction(pending_snapshot->snap);
  638. if (IS_ERR(trans) && PTR_ERR(trans) != -ENOENT) {
  639. ret = PTR_ERR(trans);
  640. goto fail;
  641. }
  642. if (!IS_ERR(trans)) {
  643. ret = btrfs_commit_transaction(trans,
  644. pending_snapshot->snap);
  645. if (ret)
  646. goto fail;
  647. }
  648. }
  649. inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
  650. if (IS_ERR(inode)) {
  651. ret = PTR_ERR(inode);
  652. goto fail;
  653. }
  654. d_instantiate(dentry, inode);
  655. ret = 0;
  656. fail:
  657. btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
  658. &pending_snapshot->block_rsv,
  659. pending_snapshot->qgroup_reserved);
  660. free:
  661. kfree(pending_snapshot);
  662. out:
  663. atomic_dec(&root->will_be_snapshoted);
  664. return ret;
  665. }
  666. /* copy of may_delete in fs/namei.c()
  667. * Check whether we can remove a link victim from directory dir, check
  668. * whether the type of victim is right.
  669. * 1. We can't do it if dir is read-only (done in permission())
  670. * 2. We should have write and exec permissions on dir
  671. * 3. We can't remove anything from append-only dir
  672. * 4. We can't do anything with immutable dir (done in permission())
  673. * 5. If the sticky bit on dir is set we should either
  674. * a. be owner of dir, or
  675. * b. be owner of victim, or
  676. * c. have CAP_FOWNER capability
  677. * 6. If the victim is append-only or immutable we can't do antyhing with
  678. * links pointing to it.
  679. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  680. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  681. * 9. We can't remove a root or mountpoint.
  682. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  683. * nfs_async_unlink().
  684. */
  685. static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
  686. {
  687. int error;
  688. if (!victim->d_inode)
  689. return -ENOENT;
  690. BUG_ON(victim->d_parent->d_inode != dir);
  691. audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
  692. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  693. if (error)
  694. return error;
  695. if (IS_APPEND(dir))
  696. return -EPERM;
  697. if (check_sticky(dir, victim->d_inode) || IS_APPEND(victim->d_inode) ||
  698. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  699. return -EPERM;
  700. if (isdir) {
  701. if (!S_ISDIR(victim->d_inode->i_mode))
  702. return -ENOTDIR;
  703. if (IS_ROOT(victim))
  704. return -EBUSY;
  705. } else if (S_ISDIR(victim->d_inode->i_mode))
  706. return -EISDIR;
  707. if (IS_DEADDIR(dir))
  708. return -ENOENT;
  709. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  710. return -EBUSY;
  711. return 0;
  712. }
  713. /* copy of may_create in fs/namei.c() */
  714. static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
  715. {
  716. if (child->d_inode)
  717. return -EEXIST;
  718. if (IS_DEADDIR(dir))
  719. return -ENOENT;
  720. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  721. }
  722. /*
  723. * Create a new subvolume below @parent. This is largely modeled after
  724. * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
  725. * inside this filesystem so it's quite a bit simpler.
  726. */
  727. static noinline int btrfs_mksubvol(struct path *parent,
  728. char *name, int namelen,
  729. struct btrfs_root *snap_src,
  730. u64 *async_transid, bool readonly,
  731. struct btrfs_qgroup_inherit *inherit)
  732. {
  733. struct inode *dir = parent->dentry->d_inode;
  734. struct dentry *dentry;
  735. int error;
  736. error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
  737. if (error == -EINTR)
  738. return error;
  739. dentry = lookup_one_len(name, parent->dentry, namelen);
  740. error = PTR_ERR(dentry);
  741. if (IS_ERR(dentry))
  742. goto out_unlock;
  743. error = -EEXIST;
  744. if (dentry->d_inode)
  745. goto out_dput;
  746. error = btrfs_may_create(dir, dentry);
  747. if (error)
  748. goto out_dput;
  749. /*
  750. * even if this name doesn't exist, we may get hash collisions.
  751. * check for them now when we can safely fail
  752. */
  753. error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
  754. dir->i_ino, name,
  755. namelen);
  756. if (error)
  757. goto out_dput;
  758. down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  759. if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
  760. goto out_up_read;
  761. if (snap_src) {
  762. error = create_snapshot(snap_src, dir, dentry, name, namelen,
  763. async_transid, readonly, inherit);
  764. } else {
  765. error = create_subvol(dir, dentry, name, namelen,
  766. async_transid, inherit);
  767. }
  768. if (!error)
  769. fsnotify_mkdir(dir, dentry);
  770. out_up_read:
  771. up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  772. out_dput:
  773. dput(dentry);
  774. out_unlock:
  775. mutex_unlock(&dir->i_mutex);
  776. return error;
  777. }
  778. /*
  779. * When we're defragging a range, we don't want to kick it off again
  780. * if it is really just waiting for delalloc to send it down.
  781. * If we find a nice big extent or delalloc range for the bytes in the
  782. * file you want to defrag, we return 0 to let you know to skip this
  783. * part of the file
  784. */
  785. static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
  786. {
  787. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  788. struct extent_map *em = NULL;
  789. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  790. u64 end;
  791. read_lock(&em_tree->lock);
  792. em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
  793. read_unlock(&em_tree->lock);
  794. if (em) {
  795. end = extent_map_end(em);
  796. free_extent_map(em);
  797. if (end - offset > thresh)
  798. return 0;
  799. }
  800. /* if we already have a nice delalloc here, just stop */
  801. thresh /= 2;
  802. end = count_range_bits(io_tree, &offset, offset + thresh,
  803. thresh, EXTENT_DELALLOC, 1);
  804. if (end >= thresh)
  805. return 0;
  806. return 1;
  807. }
  808. /*
  809. * helper function to walk through a file and find extents
  810. * newer than a specific transid, and smaller than thresh.
  811. *
  812. * This is used by the defragging code to find new and small
  813. * extents
  814. */
  815. static int find_new_extents(struct btrfs_root *root,
  816. struct inode *inode, u64 newer_than,
  817. u64 *off, u32 thresh)
  818. {
  819. struct btrfs_path *path;
  820. struct btrfs_key min_key;
  821. struct extent_buffer *leaf;
  822. struct btrfs_file_extent_item *extent;
  823. int type;
  824. int ret;
  825. u64 ino = btrfs_ino(inode);
  826. path = btrfs_alloc_path();
  827. if (!path)
  828. return -ENOMEM;
  829. min_key.objectid = ino;
  830. min_key.type = BTRFS_EXTENT_DATA_KEY;
  831. min_key.offset = *off;
  832. while (1) {
  833. ret = btrfs_search_forward(root, &min_key, path, newer_than);
  834. if (ret != 0)
  835. goto none;
  836. process_slot:
  837. if (min_key.objectid != ino)
  838. goto none;
  839. if (min_key.type != BTRFS_EXTENT_DATA_KEY)
  840. goto none;
  841. leaf = path->nodes[0];
  842. extent = btrfs_item_ptr(leaf, path->slots[0],
  843. struct btrfs_file_extent_item);
  844. type = btrfs_file_extent_type(leaf, extent);
  845. if (type == BTRFS_FILE_EXTENT_REG &&
  846. btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
  847. check_defrag_in_cache(inode, min_key.offset, thresh)) {
  848. *off = min_key.offset;
  849. btrfs_free_path(path);
  850. return 0;
  851. }
  852. path->slots[0]++;
  853. if (path->slots[0] < btrfs_header_nritems(leaf)) {
  854. btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
  855. goto process_slot;
  856. }
  857. if (min_key.offset == (u64)-1)
  858. goto none;
  859. min_key.offset++;
  860. btrfs_release_path(path);
  861. }
  862. none:
  863. btrfs_free_path(path);
  864. return -ENOENT;
  865. }
  866. static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
  867. {
  868. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  869. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  870. struct extent_map *em;
  871. u64 len = PAGE_CACHE_SIZE;
  872. /*
  873. * hopefully we have this extent in the tree already, try without
  874. * the full extent lock
  875. */
  876. read_lock(&em_tree->lock);
  877. em = lookup_extent_mapping(em_tree, start, len);
  878. read_unlock(&em_tree->lock);
  879. if (!em) {
  880. struct extent_state *cached = NULL;
  881. u64 end = start + len - 1;
  882. /* get the big lock and read metadata off disk */
  883. lock_extent_bits(io_tree, start, end, 0, &cached);
  884. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  885. unlock_extent_cached(io_tree, start, end, &cached, GFP_NOFS);
  886. if (IS_ERR(em))
  887. return NULL;
  888. }
  889. return em;
  890. }
  891. static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
  892. {
  893. struct extent_map *next;
  894. bool ret = true;
  895. /* this is the last extent */
  896. if (em->start + em->len >= i_size_read(inode))
  897. return false;
  898. next = defrag_lookup_extent(inode, em->start + em->len);
  899. if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
  900. ret = false;
  901. else if ((em->block_start + em->block_len == next->block_start) &&
  902. (em->block_len > 128 * 1024 && next->block_len > 128 * 1024))
  903. ret = false;
  904. free_extent_map(next);
  905. return ret;
  906. }
  907. static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
  908. u64 *last_len, u64 *skip, u64 *defrag_end,
  909. int compress)
  910. {
  911. struct extent_map *em;
  912. int ret = 1;
  913. bool next_mergeable = true;
  914. /*
  915. * make sure that once we start defragging an extent, we keep on
  916. * defragging it
  917. */
  918. if (start < *defrag_end)
  919. return 1;
  920. *skip = 0;
  921. em = defrag_lookup_extent(inode, start);
  922. if (!em)
  923. return 0;
  924. /* this will cover holes, and inline extents */
  925. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  926. ret = 0;
  927. goto out;
  928. }
  929. next_mergeable = defrag_check_next_extent(inode, em);
  930. /*
  931. * we hit a real extent, if it is big or the next extent is not a
  932. * real extent, don't bother defragging it
  933. */
  934. if (!compress && (*last_len == 0 || *last_len >= thresh) &&
  935. (em->len >= thresh || !next_mergeable))
  936. ret = 0;
  937. out:
  938. /*
  939. * last_len ends up being a counter of how many bytes we've defragged.
  940. * every time we choose not to defrag an extent, we reset *last_len
  941. * so that the next tiny extent will force a defrag.
  942. *
  943. * The end result of this is that tiny extents before a single big
  944. * extent will force at least part of that big extent to be defragged.
  945. */
  946. if (ret) {
  947. *defrag_end = extent_map_end(em);
  948. } else {
  949. *last_len = 0;
  950. *skip = extent_map_end(em);
  951. *defrag_end = 0;
  952. }
  953. free_extent_map(em);
  954. return ret;
  955. }
  956. /*
  957. * it doesn't do much good to defrag one or two pages
  958. * at a time. This pulls in a nice chunk of pages
  959. * to COW and defrag.
  960. *
  961. * It also makes sure the delalloc code has enough
  962. * dirty data to avoid making new small extents as part
  963. * of the defrag
  964. *
  965. * It's a good idea to start RA on this range
  966. * before calling this.
  967. */
  968. static int cluster_pages_for_defrag(struct inode *inode,
  969. struct page **pages,
  970. unsigned long start_index,
  971. unsigned long num_pages)
  972. {
  973. unsigned long file_end;
  974. u64 isize = i_size_read(inode);
  975. u64 page_start;
  976. u64 page_end;
  977. u64 page_cnt;
  978. int ret;
  979. int i;
  980. int i_done;
  981. struct btrfs_ordered_extent *ordered;
  982. struct extent_state *cached_state = NULL;
  983. struct extent_io_tree *tree;
  984. gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
  985. file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
  986. if (!isize || start_index > file_end)
  987. return 0;
  988. page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
  989. ret = btrfs_delalloc_reserve_space(inode,
  990. page_cnt << PAGE_CACHE_SHIFT);
  991. if (ret)
  992. return ret;
  993. i_done = 0;
  994. tree = &BTRFS_I(inode)->io_tree;
  995. /* step one, lock all the pages */
  996. for (i = 0; i < page_cnt; i++) {
  997. struct page *page;
  998. again:
  999. page = find_or_create_page(inode->i_mapping,
  1000. start_index + i, mask);
  1001. if (!page)
  1002. break;
  1003. page_start = page_offset(page);
  1004. page_end = page_start + PAGE_CACHE_SIZE - 1;
  1005. while (1) {
  1006. lock_extent_bits(tree, page_start, page_end,
  1007. 0, &cached_state);
  1008. ordered = btrfs_lookup_ordered_extent(inode,
  1009. page_start);
  1010. unlock_extent_cached(tree, page_start, page_end,
  1011. &cached_state, GFP_NOFS);
  1012. if (!ordered)
  1013. break;
  1014. unlock_page(page);
  1015. btrfs_start_ordered_extent(inode, ordered, 1);
  1016. btrfs_put_ordered_extent(ordered);
  1017. lock_page(page);
  1018. /*
  1019. * we unlocked the page above, so we need check if
  1020. * it was released or not.
  1021. */
  1022. if (page->mapping != inode->i_mapping) {
  1023. unlock_page(page);
  1024. page_cache_release(page);
  1025. goto again;
  1026. }
  1027. }
  1028. if (!PageUptodate(page)) {
  1029. btrfs_readpage(NULL, page);
  1030. lock_page(page);
  1031. if (!PageUptodate(page)) {
  1032. unlock_page(page);
  1033. page_cache_release(page);
  1034. ret = -EIO;
  1035. break;
  1036. }
  1037. }
  1038. if (page->mapping != inode->i_mapping) {
  1039. unlock_page(page);
  1040. page_cache_release(page);
  1041. goto again;
  1042. }
  1043. pages[i] = page;
  1044. i_done++;
  1045. }
  1046. if (!i_done || ret)
  1047. goto out;
  1048. if (!(inode->i_sb->s_flags & MS_ACTIVE))
  1049. goto out;
  1050. /*
  1051. * so now we have a nice long stream of locked
  1052. * and up to date pages, lets wait on them
  1053. */
  1054. for (i = 0; i < i_done; i++)
  1055. wait_on_page_writeback(pages[i]);
  1056. page_start = page_offset(pages[0]);
  1057. page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
  1058. lock_extent_bits(&BTRFS_I(inode)->io_tree,
  1059. page_start, page_end - 1, 0, &cached_state);
  1060. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
  1061. page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
  1062. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
  1063. &cached_state, GFP_NOFS);
  1064. if (i_done != page_cnt) {
  1065. spin_lock(&BTRFS_I(inode)->lock);
  1066. BTRFS_I(inode)->outstanding_extents++;
  1067. spin_unlock(&BTRFS_I(inode)->lock);
  1068. btrfs_delalloc_release_space(inode,
  1069. (page_cnt - i_done) << PAGE_CACHE_SHIFT);
  1070. }
  1071. set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
  1072. &cached_state, GFP_NOFS);
  1073. unlock_extent_cached(&BTRFS_I(inode)->io_tree,
  1074. page_start, page_end - 1, &cached_state,
  1075. GFP_NOFS);
  1076. for (i = 0; i < i_done; i++) {
  1077. clear_page_dirty_for_io(pages[i]);
  1078. ClearPageChecked(pages[i]);
  1079. set_page_extent_mapped(pages[i]);
  1080. set_page_dirty(pages[i]);
  1081. unlock_page(pages[i]);
  1082. page_cache_release(pages[i]);
  1083. }
  1084. return i_done;
  1085. out:
  1086. for (i = 0; i < i_done; i++) {
  1087. unlock_page(pages[i]);
  1088. page_cache_release(pages[i]);
  1089. }
  1090. btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
  1091. return ret;
  1092. }
  1093. int btrfs_defrag_file(struct inode *inode, struct file *file,
  1094. struct btrfs_ioctl_defrag_range_args *range,
  1095. u64 newer_than, unsigned long max_to_defrag)
  1096. {
  1097. struct btrfs_root *root = BTRFS_I(inode)->root;
  1098. struct file_ra_state *ra = NULL;
  1099. unsigned long last_index;
  1100. u64 isize = i_size_read(inode);
  1101. u64 last_len = 0;
  1102. u64 skip = 0;
  1103. u64 defrag_end = 0;
  1104. u64 newer_off = range->start;
  1105. unsigned long i;
  1106. unsigned long ra_index = 0;
  1107. int ret;
  1108. int defrag_count = 0;
  1109. int compress_type = BTRFS_COMPRESS_ZLIB;
  1110. u32 extent_thresh = range->extent_thresh;
  1111. unsigned long max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
  1112. unsigned long cluster = max_cluster;
  1113. u64 new_align = ~((u64)128 * 1024 - 1);
  1114. struct page **pages = NULL;
  1115. if (isize == 0)
  1116. return 0;
  1117. if (range->start >= isize)
  1118. return -EINVAL;
  1119. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
  1120. if (range->compress_type > BTRFS_COMPRESS_TYPES)
  1121. return -EINVAL;
  1122. if (range->compress_type)
  1123. compress_type = range->compress_type;
  1124. }
  1125. if (extent_thresh == 0)
  1126. extent_thresh = 256 * 1024;
  1127. /*
  1128. * if we were not given a file, allocate a readahead
  1129. * context
  1130. */
  1131. if (!file) {
  1132. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  1133. if (!ra)
  1134. return -ENOMEM;
  1135. file_ra_state_init(ra, inode->i_mapping);
  1136. } else {
  1137. ra = &file->f_ra;
  1138. }
  1139. pages = kmalloc_array(max_cluster, sizeof(struct page *),
  1140. GFP_NOFS);
  1141. if (!pages) {
  1142. ret = -ENOMEM;
  1143. goto out_ra;
  1144. }
  1145. /* find the last page to defrag */
  1146. if (range->start + range->len > range->start) {
  1147. last_index = min_t(u64, isize - 1,
  1148. range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
  1149. } else {
  1150. last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  1151. }
  1152. if (newer_than) {
  1153. ret = find_new_extents(root, inode, newer_than,
  1154. &newer_off, 64 * 1024);
  1155. if (!ret) {
  1156. range->start = newer_off;
  1157. /*
  1158. * we always align our defrag to help keep
  1159. * the extents in the file evenly spaced
  1160. */
  1161. i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
  1162. } else
  1163. goto out_ra;
  1164. } else {
  1165. i = range->start >> PAGE_CACHE_SHIFT;
  1166. }
  1167. if (!max_to_defrag)
  1168. max_to_defrag = last_index + 1;
  1169. /*
  1170. * make writeback starts from i, so the defrag range can be
  1171. * written sequentially.
  1172. */
  1173. if (i < inode->i_mapping->writeback_index)
  1174. inode->i_mapping->writeback_index = i;
  1175. while (i <= last_index && defrag_count < max_to_defrag &&
  1176. (i < DIV_ROUND_UP(i_size_read(inode), PAGE_CACHE_SIZE))) {
  1177. /*
  1178. * make sure we stop running if someone unmounts
  1179. * the FS
  1180. */
  1181. if (!(inode->i_sb->s_flags & MS_ACTIVE))
  1182. break;
  1183. if (btrfs_defrag_cancelled(root->fs_info)) {
  1184. printk(KERN_DEBUG "BTRFS: defrag_file cancelled\n");
  1185. ret = -EAGAIN;
  1186. break;
  1187. }
  1188. if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
  1189. extent_thresh, &last_len, &skip,
  1190. &defrag_end, range->flags &
  1191. BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1192. unsigned long next;
  1193. /*
  1194. * the should_defrag function tells us how much to skip
  1195. * bump our counter by the suggested amount
  1196. */
  1197. next = DIV_ROUND_UP(skip, PAGE_CACHE_SIZE);
  1198. i = max(i + 1, next);
  1199. continue;
  1200. }
  1201. if (!newer_than) {
  1202. cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
  1203. PAGE_CACHE_SHIFT) - i;
  1204. cluster = min(cluster, max_cluster);
  1205. } else {
  1206. cluster = max_cluster;
  1207. }
  1208. if (i + cluster > ra_index) {
  1209. ra_index = max(i, ra_index);
  1210. btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
  1211. cluster);
  1212. ra_index += max_cluster;
  1213. }
  1214. mutex_lock(&inode->i_mutex);
  1215. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
  1216. BTRFS_I(inode)->force_compress = compress_type;
  1217. ret = cluster_pages_for_defrag(inode, pages, i, cluster);
  1218. if (ret < 0) {
  1219. mutex_unlock(&inode->i_mutex);
  1220. goto out_ra;
  1221. }
  1222. defrag_count += ret;
  1223. balance_dirty_pages_ratelimited(inode->i_mapping);
  1224. mutex_unlock(&inode->i_mutex);
  1225. if (newer_than) {
  1226. if (newer_off == (u64)-1)
  1227. break;
  1228. if (ret > 0)
  1229. i += ret;
  1230. newer_off = max(newer_off + 1,
  1231. (u64)i << PAGE_CACHE_SHIFT);
  1232. ret = find_new_extents(root, inode,
  1233. newer_than, &newer_off,
  1234. 64 * 1024);
  1235. if (!ret) {
  1236. range->start = newer_off;
  1237. i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
  1238. } else {
  1239. break;
  1240. }
  1241. } else {
  1242. if (ret > 0) {
  1243. i += ret;
  1244. last_len += ret << PAGE_CACHE_SHIFT;
  1245. } else {
  1246. i++;
  1247. last_len = 0;
  1248. }
  1249. }
  1250. }
  1251. if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
  1252. filemap_flush(inode->i_mapping);
  1253. if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
  1254. &BTRFS_I(inode)->runtime_flags))
  1255. filemap_flush(inode->i_mapping);
  1256. }
  1257. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1258. /* the filemap_flush will queue IO into the worker threads, but
  1259. * we have to make sure the IO is actually started and that
  1260. * ordered extents get created before we return
  1261. */
  1262. atomic_inc(&root->fs_info->async_submit_draining);
  1263. while (atomic_read(&root->fs_info->nr_async_submits) ||
  1264. atomic_read(&root->fs_info->async_delalloc_pages)) {
  1265. wait_event(root->fs_info->async_submit_wait,
  1266. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  1267. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  1268. }
  1269. atomic_dec(&root->fs_info->async_submit_draining);
  1270. }
  1271. if (range->compress_type == BTRFS_COMPRESS_LZO) {
  1272. btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
  1273. }
  1274. ret = defrag_count;
  1275. out_ra:
  1276. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
  1277. mutex_lock(&inode->i_mutex);
  1278. BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
  1279. mutex_unlock(&inode->i_mutex);
  1280. }
  1281. if (!file)
  1282. kfree(ra);
  1283. kfree(pages);
  1284. return ret;
  1285. }
  1286. static noinline int btrfs_ioctl_resize(struct file *file,
  1287. void __user *arg)
  1288. {
  1289. u64 new_size;
  1290. u64 old_size;
  1291. u64 devid = 1;
  1292. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  1293. struct btrfs_ioctl_vol_args *vol_args;
  1294. struct btrfs_trans_handle *trans;
  1295. struct btrfs_device *device = NULL;
  1296. char *sizestr;
  1297. char *retptr;
  1298. char *devstr = NULL;
  1299. int ret = 0;
  1300. int mod = 0;
  1301. if (!capable(CAP_SYS_ADMIN))
  1302. return -EPERM;
  1303. ret = mnt_want_write_file(file);
  1304. if (ret)
  1305. return ret;
  1306. if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
  1307. 1)) {
  1308. mnt_drop_write_file(file);
  1309. return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  1310. }
  1311. mutex_lock(&root->fs_info->volume_mutex);
  1312. vol_args = memdup_user(arg, sizeof(*vol_args));
  1313. if (IS_ERR(vol_args)) {
  1314. ret = PTR_ERR(vol_args);
  1315. goto out;
  1316. }
  1317. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1318. sizestr = vol_args->name;
  1319. devstr = strchr(sizestr, ':');
  1320. if (devstr) {
  1321. sizestr = devstr + 1;
  1322. *devstr = '\0';
  1323. devstr = vol_args->name;
  1324. ret = kstrtoull(devstr, 10, &devid);
  1325. if (ret)
  1326. goto out_free;
  1327. if (!devid) {
  1328. ret = -EINVAL;
  1329. goto out_free;
  1330. }
  1331. btrfs_info(root->fs_info, "resizing devid %llu", devid);
  1332. }
  1333. device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
  1334. if (!device) {
  1335. btrfs_info(root->fs_info, "resizer unable to find device %llu",
  1336. devid);
  1337. ret = -ENODEV;
  1338. goto out_free;
  1339. }
  1340. if (!device->writeable) {
  1341. btrfs_info(root->fs_info,
  1342. "resizer unable to apply on readonly device %llu",
  1343. devid);
  1344. ret = -EPERM;
  1345. goto out_free;
  1346. }
  1347. if (!strcmp(sizestr, "max"))
  1348. new_size = device->bdev->bd_inode->i_size;
  1349. else {
  1350. if (sizestr[0] == '-') {
  1351. mod = -1;
  1352. sizestr++;
  1353. } else if (sizestr[0] == '+') {
  1354. mod = 1;
  1355. sizestr++;
  1356. }
  1357. new_size = memparse(sizestr, &retptr);
  1358. if (*retptr != '\0' || new_size == 0) {
  1359. ret = -EINVAL;
  1360. goto out_free;
  1361. }
  1362. }
  1363. if (device->is_tgtdev_for_dev_replace) {
  1364. ret = -EPERM;
  1365. goto out_free;
  1366. }
  1367. old_size = btrfs_device_get_total_bytes(device);
  1368. if (mod < 0) {
  1369. if (new_size > old_size) {
  1370. ret = -EINVAL;
  1371. goto out_free;
  1372. }
  1373. new_size = old_size - new_size;
  1374. } else if (mod > 0) {
  1375. if (new_size > ULLONG_MAX - old_size) {
  1376. ret = -ERANGE;
  1377. goto out_free;
  1378. }
  1379. new_size = old_size + new_size;
  1380. }
  1381. if (new_size < 256 * 1024 * 1024) {
  1382. ret = -EINVAL;
  1383. goto out_free;
  1384. }
  1385. if (new_size > device->bdev->bd_inode->i_size) {
  1386. ret = -EFBIG;
  1387. goto out_free;
  1388. }
  1389. do_div(new_size, root->sectorsize);
  1390. new_size *= root->sectorsize;
  1391. printk_in_rcu(KERN_INFO "BTRFS: new size for %s is %llu\n",
  1392. rcu_str_deref(device->name), new_size);
  1393. if (new_size > old_size) {
  1394. trans = btrfs_start_transaction(root, 0);
  1395. if (IS_ERR(trans)) {
  1396. ret = PTR_ERR(trans);
  1397. goto out_free;
  1398. }
  1399. ret = btrfs_grow_device(trans, device, new_size);
  1400. btrfs_commit_transaction(trans, root);
  1401. } else if (new_size < old_size) {
  1402. ret = btrfs_shrink_device(device, new_size);
  1403. } /* equal, nothing need to do */
  1404. out_free:
  1405. kfree(vol_args);
  1406. out:
  1407. mutex_unlock(&root->fs_info->volume_mutex);
  1408. atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
  1409. mnt_drop_write_file(file);
  1410. return ret;
  1411. }
  1412. static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
  1413. char *name, unsigned long fd, int subvol,
  1414. u64 *transid, bool readonly,
  1415. struct btrfs_qgroup_inherit *inherit)
  1416. {
  1417. int namelen;
  1418. int ret = 0;
  1419. ret = mnt_want_write_file(file);
  1420. if (ret)
  1421. goto out;
  1422. namelen = strlen(name);
  1423. if (strchr(name, '/')) {
  1424. ret = -EINVAL;
  1425. goto out_drop_write;
  1426. }
  1427. if (name[0] == '.' &&
  1428. (namelen == 1 || (name[1] == '.' && namelen == 2))) {
  1429. ret = -EEXIST;
  1430. goto out_drop_write;
  1431. }
  1432. if (subvol) {
  1433. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1434. NULL, transid, readonly, inherit);
  1435. } else {
  1436. struct fd src = fdget(fd);
  1437. struct inode *src_inode;
  1438. if (!src.file) {
  1439. ret = -EINVAL;
  1440. goto out_drop_write;
  1441. }
  1442. src_inode = file_inode(src.file);
  1443. if (src_inode->i_sb != file_inode(file)->i_sb) {
  1444. btrfs_info(BTRFS_I(src_inode)->root->fs_info,
  1445. "Snapshot src from another FS");
  1446. ret = -EXDEV;
  1447. } else if (!inode_owner_or_capable(src_inode)) {
  1448. /*
  1449. * Subvolume creation is not restricted, but snapshots
  1450. * are limited to own subvolumes only
  1451. */
  1452. ret = -EPERM;
  1453. } else {
  1454. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  1455. BTRFS_I(src_inode)->root,
  1456. transid, readonly, inherit);
  1457. }
  1458. fdput(src);
  1459. }
  1460. out_drop_write:
  1461. mnt_drop_write_file(file);
  1462. out:
  1463. return ret;
  1464. }
  1465. static noinline int btrfs_ioctl_snap_create(struct file *file,
  1466. void __user *arg, int subvol)
  1467. {
  1468. struct btrfs_ioctl_vol_args *vol_args;
  1469. int ret;
  1470. vol_args = memdup_user(arg, sizeof(*vol_args));
  1471. if (IS_ERR(vol_args))
  1472. return PTR_ERR(vol_args);
  1473. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1474. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1475. vol_args->fd, subvol,
  1476. NULL, false, NULL);
  1477. kfree(vol_args);
  1478. return ret;
  1479. }
  1480. static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
  1481. void __user *arg, int subvol)
  1482. {
  1483. struct btrfs_ioctl_vol_args_v2 *vol_args;
  1484. int ret;
  1485. u64 transid = 0;
  1486. u64 *ptr = NULL;
  1487. bool readonly = false;
  1488. struct btrfs_qgroup_inherit *inherit = NULL;
  1489. vol_args = memdup_user(arg, sizeof(*vol_args));
  1490. if (IS_ERR(vol_args))
  1491. return PTR_ERR(vol_args);
  1492. vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
  1493. if (vol_args->flags &
  1494. ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
  1495. BTRFS_SUBVOL_QGROUP_INHERIT)) {
  1496. ret = -EOPNOTSUPP;
  1497. goto free_args;
  1498. }
  1499. if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
  1500. ptr = &transid;
  1501. if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
  1502. readonly = true;
  1503. if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
  1504. if (vol_args->size > PAGE_CACHE_SIZE) {
  1505. ret = -EINVAL;
  1506. goto free_args;
  1507. }
  1508. inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
  1509. if (IS_ERR(inherit)) {
  1510. ret = PTR_ERR(inherit);
  1511. goto free_args;
  1512. }
  1513. }
  1514. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  1515. vol_args->fd, subvol, ptr,
  1516. readonly, inherit);
  1517. if (ret)
  1518. goto free_inherit;
  1519. if (ptr && copy_to_user(arg +
  1520. offsetof(struct btrfs_ioctl_vol_args_v2,
  1521. transid),
  1522. ptr, sizeof(*ptr)))
  1523. ret = -EFAULT;
  1524. free_inherit:
  1525. kfree(inherit);
  1526. free_args:
  1527. kfree(vol_args);
  1528. return ret;
  1529. }
  1530. static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
  1531. void __user *arg)
  1532. {
  1533. struct inode *inode = file_inode(file);
  1534. struct btrfs_root *root = BTRFS_I(inode)->root;
  1535. int ret = 0;
  1536. u64 flags = 0;
  1537. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
  1538. return -EINVAL;
  1539. down_read(&root->fs_info->subvol_sem);
  1540. if (btrfs_root_readonly(root))
  1541. flags |= BTRFS_SUBVOL_RDONLY;
  1542. up_read(&root->fs_info->subvol_sem);
  1543. if (copy_to_user(arg, &flags, sizeof(flags)))
  1544. ret = -EFAULT;
  1545. return ret;
  1546. }
  1547. static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
  1548. void __user *arg)
  1549. {
  1550. struct inode *inode = file_inode(file);
  1551. struct btrfs_root *root = BTRFS_I(inode)->root;
  1552. struct btrfs_trans_handle *trans;
  1553. u64 root_flags;
  1554. u64 flags;
  1555. int ret = 0;
  1556. if (!inode_owner_or_capable(inode))
  1557. return -EPERM;
  1558. ret = mnt_want_write_file(file);
  1559. if (ret)
  1560. goto out;
  1561. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  1562. ret = -EINVAL;
  1563. goto out_drop_write;
  1564. }
  1565. if (copy_from_user(&flags, arg, sizeof(flags))) {
  1566. ret = -EFAULT;
  1567. goto out_drop_write;
  1568. }
  1569. if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
  1570. ret = -EINVAL;
  1571. goto out_drop_write;
  1572. }
  1573. if (flags & ~BTRFS_SUBVOL_RDONLY) {
  1574. ret = -EOPNOTSUPP;
  1575. goto out_drop_write;
  1576. }
  1577. down_write(&root->fs_info->subvol_sem);
  1578. /* nothing to do */
  1579. if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
  1580. goto out_drop_sem;
  1581. root_flags = btrfs_root_flags(&root->root_item);
  1582. if (flags & BTRFS_SUBVOL_RDONLY) {
  1583. btrfs_set_root_flags(&root->root_item,
  1584. root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
  1585. } else {
  1586. /*
  1587. * Block RO -> RW transition if this subvolume is involved in
  1588. * send
  1589. */
  1590. spin_lock(&root->root_item_lock);
  1591. if (root->send_in_progress == 0) {
  1592. btrfs_set_root_flags(&root->root_item,
  1593. root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
  1594. spin_unlock(&root->root_item_lock);
  1595. } else {
  1596. spin_unlock(&root->root_item_lock);
  1597. btrfs_warn(root->fs_info,
  1598. "Attempt to set subvolume %llu read-write during send",
  1599. root->root_key.objectid);
  1600. ret = -EPERM;
  1601. goto out_drop_sem;
  1602. }
  1603. }
  1604. trans = btrfs_start_transaction(root, 1);
  1605. if (IS_ERR(trans)) {
  1606. ret = PTR_ERR(trans);
  1607. goto out_reset;
  1608. }
  1609. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  1610. &root->root_key, &root->root_item);
  1611. btrfs_commit_transaction(trans, root);
  1612. out_reset:
  1613. if (ret)
  1614. btrfs_set_root_flags(&root->root_item, root_flags);
  1615. out_drop_sem:
  1616. up_write(&root->fs_info->subvol_sem);
  1617. out_drop_write:
  1618. mnt_drop_write_file(file);
  1619. out:
  1620. return ret;
  1621. }
  1622. /*
  1623. * helper to check if the subvolume references other subvolumes
  1624. */
  1625. static noinline int may_destroy_subvol(struct btrfs_root *root)
  1626. {
  1627. struct btrfs_path *path;
  1628. struct btrfs_dir_item *di;
  1629. struct btrfs_key key;
  1630. u64 dir_id;
  1631. int ret;
  1632. path = btrfs_alloc_path();
  1633. if (!path)
  1634. return -ENOMEM;
  1635. /* Make sure this root isn't set as the default subvol */
  1636. dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
  1637. di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
  1638. dir_id, "default", 7, 0);
  1639. if (di && !IS_ERR(di)) {
  1640. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
  1641. if (key.objectid == root->root_key.objectid) {
  1642. ret = -EPERM;
  1643. btrfs_err(root->fs_info, "deleting default subvolume "
  1644. "%llu is not allowed", key.objectid);
  1645. goto out;
  1646. }
  1647. btrfs_release_path(path);
  1648. }
  1649. key.objectid = root->root_key.objectid;
  1650. key.type = BTRFS_ROOT_REF_KEY;
  1651. key.offset = (u64)-1;
  1652. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  1653. &key, path, 0, 0);
  1654. if (ret < 0)
  1655. goto out;
  1656. BUG_ON(ret == 0);
  1657. ret = 0;
  1658. if (path->slots[0] > 0) {
  1659. path->slots[0]--;
  1660. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1661. if (key.objectid == root->root_key.objectid &&
  1662. key.type == BTRFS_ROOT_REF_KEY)
  1663. ret = -ENOTEMPTY;
  1664. }
  1665. out:
  1666. btrfs_free_path(path);
  1667. return ret;
  1668. }
  1669. static noinline int key_in_sk(struct btrfs_key *key,
  1670. struct btrfs_ioctl_search_key *sk)
  1671. {
  1672. struct btrfs_key test;
  1673. int ret;
  1674. test.objectid = sk->min_objectid;
  1675. test.type = sk->min_type;
  1676. test.offset = sk->min_offset;
  1677. ret = btrfs_comp_cpu_keys(key, &test);
  1678. if (ret < 0)
  1679. return 0;
  1680. test.objectid = sk->max_objectid;
  1681. test.type = sk->max_type;
  1682. test.offset = sk->max_offset;
  1683. ret = btrfs_comp_cpu_keys(key, &test);
  1684. if (ret > 0)
  1685. return 0;
  1686. return 1;
  1687. }
  1688. static noinline int copy_to_sk(struct btrfs_root *root,
  1689. struct btrfs_path *path,
  1690. struct btrfs_key *key,
  1691. struct btrfs_ioctl_search_key *sk,
  1692. size_t *buf_size,
  1693. char __user *ubuf,
  1694. unsigned long *sk_offset,
  1695. int *num_found)
  1696. {
  1697. u64 found_transid;
  1698. struct extent_buffer *leaf;
  1699. struct btrfs_ioctl_search_header sh;
  1700. unsigned long item_off;
  1701. unsigned long item_len;
  1702. int nritems;
  1703. int i;
  1704. int slot;
  1705. int ret = 0;
  1706. leaf = path->nodes[0];
  1707. slot = path->slots[0];
  1708. nritems = btrfs_header_nritems(leaf);
  1709. if (btrfs_header_generation(leaf) > sk->max_transid) {
  1710. i = nritems;
  1711. goto advance_key;
  1712. }
  1713. found_transid = btrfs_header_generation(leaf);
  1714. for (i = slot; i < nritems; i++) {
  1715. item_off = btrfs_item_ptr_offset(leaf, i);
  1716. item_len = btrfs_item_size_nr(leaf, i);
  1717. btrfs_item_key_to_cpu(leaf, key, i);
  1718. if (!key_in_sk(key, sk))
  1719. continue;
  1720. if (sizeof(sh) + item_len > *buf_size) {
  1721. if (*num_found) {
  1722. ret = 1;
  1723. goto out;
  1724. }
  1725. /*
  1726. * return one empty item back for v1, which does not
  1727. * handle -EOVERFLOW
  1728. */
  1729. *buf_size = sizeof(sh) + item_len;
  1730. item_len = 0;
  1731. ret = -EOVERFLOW;
  1732. }
  1733. if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
  1734. ret = 1;
  1735. goto out;
  1736. }
  1737. sh.objectid = key->objectid;
  1738. sh.offset = key->offset;
  1739. sh.type = key->type;
  1740. sh.len = item_len;
  1741. sh.transid = found_transid;
  1742. /* copy search result header */
  1743. if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) {
  1744. ret = -EFAULT;
  1745. goto out;
  1746. }
  1747. *sk_offset += sizeof(sh);
  1748. if (item_len) {
  1749. char __user *up = ubuf + *sk_offset;
  1750. /* copy the item */
  1751. if (read_extent_buffer_to_user(leaf, up,
  1752. item_off, item_len)) {
  1753. ret = -EFAULT;
  1754. goto out;
  1755. }
  1756. *sk_offset += item_len;
  1757. }
  1758. (*num_found)++;
  1759. if (ret) /* -EOVERFLOW from above */
  1760. goto out;
  1761. if (*num_found >= sk->nr_items) {
  1762. ret = 1;
  1763. goto out;
  1764. }
  1765. }
  1766. advance_key:
  1767. ret = 0;
  1768. if (key->offset < (u64)-1 && key->offset < sk->max_offset)
  1769. key->offset++;
  1770. else if (key->type < (u8)-1 && key->type < sk->max_type) {
  1771. key->offset = 0;
  1772. key->type++;
  1773. } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
  1774. key->offset = 0;
  1775. key->type = 0;
  1776. key->objectid++;
  1777. } else
  1778. ret = 1;
  1779. out:
  1780. /*
  1781. * 0: all items from this leaf copied, continue with next
  1782. * 1: * more items can be copied, but unused buffer is too small
  1783. * * all items were found
  1784. * Either way, it will stops the loop which iterates to the next
  1785. * leaf
  1786. * -EOVERFLOW: item was to large for buffer
  1787. * -EFAULT: could not copy extent buffer back to userspace
  1788. */
  1789. return ret;
  1790. }
  1791. static noinline int search_ioctl(struct inode *inode,
  1792. struct btrfs_ioctl_search_key *sk,
  1793. size_t *buf_size,
  1794. char __user *ubuf)
  1795. {
  1796. struct btrfs_root *root;
  1797. struct btrfs_key key;
  1798. struct btrfs_path *path;
  1799. struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
  1800. int ret;
  1801. int num_found = 0;
  1802. unsigned long sk_offset = 0;
  1803. if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
  1804. *buf_size = sizeof(struct btrfs_ioctl_search_header);
  1805. return -EOVERFLOW;
  1806. }
  1807. path = btrfs_alloc_path();
  1808. if (!path)
  1809. return -ENOMEM;
  1810. if (sk->tree_id == 0) {
  1811. /* search the root of the inode that was passed */
  1812. root = BTRFS_I(inode)->root;
  1813. } else {
  1814. key.objectid = sk->tree_id;
  1815. key.type = BTRFS_ROOT_ITEM_KEY;
  1816. key.offset = (u64)-1;
  1817. root = btrfs_read_fs_root_no_name(info, &key);
  1818. if (IS_ERR(root)) {
  1819. printk(KERN_ERR "BTRFS: could not find root %llu\n",
  1820. sk->tree_id);
  1821. btrfs_free_path(path);
  1822. return -ENOENT;
  1823. }
  1824. }
  1825. key.objectid = sk->min_objectid;
  1826. key.type = sk->min_type;
  1827. key.offset = sk->min_offset;
  1828. while (1) {
  1829. ret = btrfs_search_forward(root, &key, path, sk->min_transid);
  1830. if (ret != 0) {
  1831. if (ret > 0)
  1832. ret = 0;
  1833. goto err;
  1834. }
  1835. ret = copy_to_sk(root, path, &key, sk, buf_size, ubuf,
  1836. &sk_offset, &num_found);
  1837. btrfs_release_path(path);
  1838. if (ret)
  1839. break;
  1840. }
  1841. if (ret > 0)
  1842. ret = 0;
  1843. err:
  1844. sk->nr_items = num_found;
  1845. btrfs_free_path(path);
  1846. return ret;
  1847. }
  1848. static noinline int btrfs_ioctl_tree_search(struct file *file,
  1849. void __user *argp)
  1850. {
  1851. struct btrfs_ioctl_search_args __user *uargs;
  1852. struct btrfs_ioctl_search_key sk;
  1853. struct inode *inode;
  1854. int ret;
  1855. size_t buf_size;
  1856. if (!capable(CAP_SYS_ADMIN))
  1857. return -EPERM;
  1858. uargs = (struct btrfs_ioctl_search_args __user *)argp;
  1859. if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
  1860. return -EFAULT;
  1861. buf_size = sizeof(uargs->buf);
  1862. inode = file_inode(file);
  1863. ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
  1864. /*
  1865. * In the origin implementation an overflow is handled by returning a
  1866. * search header with a len of zero, so reset ret.
  1867. */
  1868. if (ret == -EOVERFLOW)
  1869. ret = 0;
  1870. if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
  1871. ret = -EFAULT;
  1872. return ret;
  1873. }
  1874. static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
  1875. void __user *argp)
  1876. {
  1877. struct btrfs_ioctl_search_args_v2 __user *uarg;
  1878. struct btrfs_ioctl_search_args_v2 args;
  1879. struct inode *inode;
  1880. int ret;
  1881. size_t buf_size;
  1882. const size_t buf_limit = 16 * 1024 * 1024;
  1883. if (!capable(CAP_SYS_ADMIN))
  1884. return -EPERM;
  1885. /* copy search header and buffer size */
  1886. uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
  1887. if (copy_from_user(&args, uarg, sizeof(args)))
  1888. return -EFAULT;
  1889. buf_size = args.buf_size;
  1890. if (buf_size < sizeof(struct btrfs_ioctl_search_header))
  1891. return -EOVERFLOW;
  1892. /* limit result size to 16MB */
  1893. if (buf_size > buf_limit)
  1894. buf_size = buf_limit;
  1895. inode = file_inode(file);
  1896. ret = search_ioctl(inode, &args.key, &buf_size,
  1897. (char *)(&uarg->buf[0]));
  1898. if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
  1899. ret = -EFAULT;
  1900. else if (ret == -EOVERFLOW &&
  1901. copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
  1902. ret = -EFAULT;
  1903. return ret;
  1904. }
  1905. /*
  1906. * Search INODE_REFs to identify path name of 'dirid' directory
  1907. * in a 'tree_id' tree. and sets path name to 'name'.
  1908. */
  1909. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  1910. u64 tree_id, u64 dirid, char *name)
  1911. {
  1912. struct btrfs_root *root;
  1913. struct btrfs_key key;
  1914. char *ptr;
  1915. int ret = -1;
  1916. int slot;
  1917. int len;
  1918. int total_len = 0;
  1919. struct btrfs_inode_ref *iref;
  1920. struct extent_buffer *l;
  1921. struct btrfs_path *path;
  1922. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  1923. name[0]='\0';
  1924. return 0;
  1925. }
  1926. path = btrfs_alloc_path();
  1927. if (!path)
  1928. return -ENOMEM;
  1929. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
  1930. key.objectid = tree_id;
  1931. key.type = BTRFS_ROOT_ITEM_KEY;
  1932. key.offset = (u64)-1;
  1933. root = btrfs_read_fs_root_no_name(info, &key);
  1934. if (IS_ERR(root)) {
  1935. printk(KERN_ERR "BTRFS: could not find root %llu\n", tree_id);
  1936. ret = -ENOENT;
  1937. goto out;
  1938. }
  1939. key.objectid = dirid;
  1940. key.type = BTRFS_INODE_REF_KEY;
  1941. key.offset = (u64)-1;
  1942. while (1) {
  1943. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1944. if (ret < 0)
  1945. goto out;
  1946. else if (ret > 0) {
  1947. ret = btrfs_previous_item(root, path, dirid,
  1948. BTRFS_INODE_REF_KEY);
  1949. if (ret < 0)
  1950. goto out;
  1951. else if (ret > 0) {
  1952. ret = -ENOENT;
  1953. goto out;
  1954. }
  1955. }
  1956. l = path->nodes[0];
  1957. slot = path->slots[0];
  1958. btrfs_item_key_to_cpu(l, &key, slot);
  1959. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  1960. len = btrfs_inode_ref_name_len(l, iref);
  1961. ptr -= len + 1;
  1962. total_len += len + 1;
  1963. if (ptr < name) {
  1964. ret = -ENAMETOOLONG;
  1965. goto out;
  1966. }
  1967. *(ptr + len) = '/';
  1968. read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
  1969. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  1970. break;
  1971. btrfs_release_path(path);
  1972. key.objectid = key.offset;
  1973. key.offset = (u64)-1;
  1974. dirid = key.objectid;
  1975. }
  1976. memmove(name, ptr, total_len);
  1977. name[total_len] = '\0';
  1978. ret = 0;
  1979. out:
  1980. btrfs_free_path(path);
  1981. return ret;
  1982. }
  1983. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  1984. void __user *argp)
  1985. {
  1986. struct btrfs_ioctl_ino_lookup_args *args;
  1987. struct inode *inode;
  1988. int ret;
  1989. if (!capable(CAP_SYS_ADMIN))
  1990. return -EPERM;
  1991. args = memdup_user(argp, sizeof(*args));
  1992. if (IS_ERR(args))
  1993. return PTR_ERR(args);
  1994. inode = file_inode(file);
  1995. if (args->treeid == 0)
  1996. args->treeid = BTRFS_I(inode)->root->root_key.objectid;
  1997. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  1998. args->treeid, args->objectid,
  1999. args->name);
  2000. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  2001. ret = -EFAULT;
  2002. kfree(args);
  2003. return ret;
  2004. }
  2005. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  2006. void __user *arg)
  2007. {
  2008. struct dentry *parent = file->f_path.dentry;
  2009. struct dentry *dentry;
  2010. struct inode *dir = parent->d_inode;
  2011. struct inode *inode;
  2012. struct btrfs_root *root = BTRFS_I(dir)->root;
  2013. struct btrfs_root *dest = NULL;
  2014. struct btrfs_ioctl_vol_args *vol_args;
  2015. struct btrfs_trans_handle *trans;
  2016. struct btrfs_block_rsv block_rsv;
  2017. u64 root_flags;
  2018. u64 qgroup_reserved;
  2019. int namelen;
  2020. int ret;
  2021. int err = 0;
  2022. vol_args = memdup_user(arg, sizeof(*vol_args));
  2023. if (IS_ERR(vol_args))
  2024. return PTR_ERR(vol_args);
  2025. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  2026. namelen = strlen(vol_args->name);
  2027. if (strchr(vol_args->name, '/') ||
  2028. strncmp(vol_args->name, "..", namelen) == 0) {
  2029. err = -EINVAL;
  2030. goto out;
  2031. }
  2032. err = mnt_want_write_file(file);
  2033. if (err)
  2034. goto out;
  2035. err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
  2036. if (err == -EINTR)
  2037. goto out_drop_write;
  2038. dentry = lookup_one_len(vol_args->name, parent, namelen);
  2039. if (IS_ERR(dentry)) {
  2040. err = PTR_ERR(dentry);
  2041. goto out_unlock_dir;
  2042. }
  2043. if (!dentry->d_inode) {
  2044. err = -ENOENT;
  2045. goto out_dput;
  2046. }
  2047. inode = dentry->d_inode;
  2048. dest = BTRFS_I(inode)->root;
  2049. if (!capable(CAP_SYS_ADMIN)) {
  2050. /*
  2051. * Regular user. Only allow this with a special mount
  2052. * option, when the user has write+exec access to the
  2053. * subvol root, and when rmdir(2) would have been
  2054. * allowed.
  2055. *
  2056. * Note that this is _not_ check that the subvol is
  2057. * empty or doesn't contain data that we wouldn't
  2058. * otherwise be able to delete.
  2059. *
  2060. * Users who want to delete empty subvols should try
  2061. * rmdir(2).
  2062. */
  2063. err = -EPERM;
  2064. if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  2065. goto out_dput;
  2066. /*
  2067. * Do not allow deletion if the parent dir is the same
  2068. * as the dir to be deleted. That means the ioctl
  2069. * must be called on the dentry referencing the root
  2070. * of the subvol, not a random directory contained
  2071. * within it.
  2072. */
  2073. err = -EINVAL;
  2074. if (root == dest)
  2075. goto out_dput;
  2076. err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
  2077. if (err)
  2078. goto out_dput;
  2079. }
  2080. /* check if subvolume may be deleted by a user */
  2081. err = btrfs_may_delete(dir, dentry, 1);
  2082. if (err)
  2083. goto out_dput;
  2084. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  2085. err = -EINVAL;
  2086. goto out_dput;
  2087. }
  2088. mutex_lock(&inode->i_mutex);
  2089. /*
  2090. * Don't allow to delete a subvolume with send in progress. This is
  2091. * inside the i_mutex so the error handling that has to drop the bit
  2092. * again is not run concurrently.
  2093. */
  2094. spin_lock(&dest->root_item_lock);
  2095. root_flags = btrfs_root_flags(&dest->root_item);
  2096. if (dest->send_in_progress == 0) {
  2097. btrfs_set_root_flags(&dest->root_item,
  2098. root_flags | BTRFS_ROOT_SUBVOL_DEAD);
  2099. spin_unlock(&dest->root_item_lock);
  2100. } else {
  2101. spin_unlock(&dest->root_item_lock);
  2102. btrfs_warn(root->fs_info,
  2103. "Attempt to delete subvolume %llu during send",
  2104. dest->root_key.objectid);
  2105. err = -EPERM;
  2106. goto out_unlock_inode;
  2107. }
  2108. down_write(&root->fs_info->subvol_sem);
  2109. err = may_destroy_subvol(dest);
  2110. if (err)
  2111. goto out_up_write;
  2112. btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
  2113. /*
  2114. * One for dir inode, two for dir entries, two for root
  2115. * ref/backref.
  2116. */
  2117. err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
  2118. 5, &qgroup_reserved, true);
  2119. if (err)
  2120. goto out_up_write;
  2121. trans = btrfs_start_transaction(root, 0);
  2122. if (IS_ERR(trans)) {
  2123. err = PTR_ERR(trans);
  2124. goto out_release;
  2125. }
  2126. trans->block_rsv = &block_rsv;
  2127. trans->bytes_reserved = block_rsv.size;
  2128. ret = btrfs_unlink_subvol(trans, root, dir,
  2129. dest->root_key.objectid,
  2130. dentry->d_name.name,
  2131. dentry->d_name.len);
  2132. if (ret) {
  2133. err = ret;
  2134. btrfs_abort_transaction(trans, root, ret);
  2135. goto out_end_trans;
  2136. }
  2137. btrfs_record_root_in_trans(trans, dest);
  2138. memset(&dest->root_item.drop_progress, 0,
  2139. sizeof(dest->root_item.drop_progress));
  2140. dest->root_item.drop_level = 0;
  2141. btrfs_set_root_refs(&dest->root_item, 0);
  2142. if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
  2143. ret = btrfs_insert_orphan_item(trans,
  2144. root->fs_info->tree_root,
  2145. dest->root_key.objectid);
  2146. if (ret) {
  2147. btrfs_abort_transaction(trans, root, ret);
  2148. err = ret;
  2149. goto out_end_trans;
  2150. }
  2151. }
  2152. ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
  2153. dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
  2154. dest->root_key.objectid);
  2155. if (ret && ret != -ENOENT) {
  2156. btrfs_abort_transaction(trans, root, ret);
  2157. err = ret;
  2158. goto out_end_trans;
  2159. }
  2160. if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
  2161. ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
  2162. dest->root_item.received_uuid,
  2163. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  2164. dest->root_key.objectid);
  2165. if (ret && ret != -ENOENT) {
  2166. btrfs_abort_transaction(trans, root, ret);
  2167. err = ret;
  2168. goto out_end_trans;
  2169. }
  2170. }
  2171. out_end_trans:
  2172. trans->block_rsv = NULL;
  2173. trans->bytes_reserved = 0;
  2174. ret = btrfs_end_transaction(trans, root);
  2175. if (ret && !err)
  2176. err = ret;
  2177. inode->i_flags |= S_DEAD;
  2178. out_release:
  2179. btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
  2180. out_up_write:
  2181. up_write(&root->fs_info->subvol_sem);
  2182. if (err) {
  2183. spin_lock(&dest->root_item_lock);
  2184. root_flags = btrfs_root_flags(&dest->root_item);
  2185. btrfs_set_root_flags(&dest->root_item,
  2186. root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
  2187. spin_unlock(&dest->root_item_lock);
  2188. }
  2189. out_unlock_inode:
  2190. mutex_unlock(&inode->i_mutex);
  2191. if (!err) {
  2192. d_invalidate(dentry);
  2193. btrfs_invalidate_inodes(dest);
  2194. d_delete(dentry);
  2195. ASSERT(dest->send_in_progress == 0);
  2196. /* the last ref */
  2197. if (dest->ino_cache_inode) {
  2198. iput(dest->ino_cache_inode);
  2199. dest->ino_cache_inode = NULL;
  2200. }
  2201. }
  2202. out_dput:
  2203. dput(dentry);
  2204. out_unlock_dir:
  2205. mutex_unlock(&dir->i_mutex);
  2206. out_drop_write:
  2207. mnt_drop_write_file(file);
  2208. out:
  2209. kfree(vol_args);
  2210. return err;
  2211. }
  2212. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  2213. {
  2214. struct inode *inode = file_inode(file);
  2215. struct btrfs_root *root = BTRFS_I(inode)->root;
  2216. struct btrfs_ioctl_defrag_range_args *range;
  2217. int ret;
  2218. ret = mnt_want_write_file(file);
  2219. if (ret)
  2220. return ret;
  2221. if (btrfs_root_readonly(root)) {
  2222. ret = -EROFS;
  2223. goto out;
  2224. }
  2225. switch (inode->i_mode & S_IFMT) {
  2226. case S_IFDIR:
  2227. if (!capable(CAP_SYS_ADMIN)) {
  2228. ret = -EPERM;
  2229. goto out;
  2230. }
  2231. ret = btrfs_defrag_root(root);
  2232. if (ret)
  2233. goto out;
  2234. ret = btrfs_defrag_root(root->fs_info->extent_root);
  2235. break;
  2236. case S_IFREG:
  2237. if (!(file->f_mode & FMODE_WRITE)) {
  2238. ret = -EINVAL;
  2239. goto out;
  2240. }
  2241. range = kzalloc(sizeof(*range), GFP_KERNEL);
  2242. if (!range) {
  2243. ret = -ENOMEM;
  2244. goto out;
  2245. }
  2246. if (argp) {
  2247. if (copy_from_user(range, argp,
  2248. sizeof(*range))) {
  2249. ret = -EFAULT;
  2250. kfree(range);
  2251. goto out;
  2252. }
  2253. /* compression requires us to start the IO */
  2254. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  2255. range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
  2256. range->extent_thresh = (u32)-1;
  2257. }
  2258. } else {
  2259. /* the rest are all set to zero by kzalloc */
  2260. range->len = (u64)-1;
  2261. }
  2262. ret = btrfs_defrag_file(file_inode(file), file,
  2263. range, 0, 0);
  2264. if (ret > 0)
  2265. ret = 0;
  2266. kfree(range);
  2267. break;
  2268. default:
  2269. ret = -EINVAL;
  2270. }
  2271. out:
  2272. mnt_drop_write_file(file);
  2273. return ret;
  2274. }
  2275. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  2276. {
  2277. struct btrfs_ioctl_vol_args *vol_args;
  2278. int ret;
  2279. if (!capable(CAP_SYS_ADMIN))
  2280. return -EPERM;
  2281. if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
  2282. 1)) {
  2283. return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  2284. }
  2285. mutex_lock(&root->fs_info->volume_mutex);
  2286. vol_args = memdup_user(arg, sizeof(*vol_args));
  2287. if (IS_ERR(vol_args)) {
  2288. ret = PTR_ERR(vol_args);
  2289. goto out;
  2290. }
  2291. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  2292. ret = btrfs_init_new_device(root, vol_args->name);
  2293. if (!ret)
  2294. btrfs_info(root->fs_info, "disk added %s",vol_args->name);
  2295. kfree(vol_args);
  2296. out:
  2297. mutex_unlock(&root->fs_info->volume_mutex);
  2298. atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
  2299. return ret;
  2300. }
  2301. static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
  2302. {
  2303. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  2304. struct btrfs_ioctl_vol_args *vol_args;
  2305. int ret;
  2306. if (!capable(CAP_SYS_ADMIN))
  2307. return -EPERM;
  2308. ret = mnt_want_write_file(file);
  2309. if (ret)
  2310. return ret;
  2311. vol_args = memdup_user(arg, sizeof(*vol_args));
  2312. if (IS_ERR(vol_args)) {
  2313. ret = PTR_ERR(vol_args);
  2314. goto err_drop;
  2315. }
  2316. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  2317. if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
  2318. 1)) {
  2319. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  2320. goto out;
  2321. }
  2322. mutex_lock(&root->fs_info->volume_mutex);
  2323. ret = btrfs_rm_device(root, vol_args->name);
  2324. mutex_unlock(&root->fs_info->volume_mutex);
  2325. atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
  2326. if (!ret)
  2327. btrfs_info(root->fs_info, "disk deleted %s",vol_args->name);
  2328. out:
  2329. kfree(vol_args);
  2330. err_drop:
  2331. mnt_drop_write_file(file);
  2332. return ret;
  2333. }
  2334. static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
  2335. {
  2336. struct btrfs_ioctl_fs_info_args *fi_args;
  2337. struct btrfs_device *device;
  2338. struct btrfs_device *next;
  2339. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  2340. int ret = 0;
  2341. fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
  2342. if (!fi_args)
  2343. return -ENOMEM;
  2344. mutex_lock(&fs_devices->device_list_mutex);
  2345. fi_args->num_devices = fs_devices->num_devices;
  2346. memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
  2347. list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
  2348. if (device->devid > fi_args->max_id)
  2349. fi_args->max_id = device->devid;
  2350. }
  2351. mutex_unlock(&fs_devices->device_list_mutex);
  2352. fi_args->nodesize = root->fs_info->super_copy->nodesize;
  2353. fi_args->sectorsize = root->fs_info->super_copy->sectorsize;
  2354. fi_args->clone_alignment = root->fs_info->super_copy->sectorsize;
  2355. if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
  2356. ret = -EFAULT;
  2357. kfree(fi_args);
  2358. return ret;
  2359. }
  2360. static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
  2361. {
  2362. struct btrfs_ioctl_dev_info_args *di_args;
  2363. struct btrfs_device *dev;
  2364. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  2365. int ret = 0;
  2366. char *s_uuid = NULL;
  2367. di_args = memdup_user(arg, sizeof(*di_args));
  2368. if (IS_ERR(di_args))
  2369. return PTR_ERR(di_args);
  2370. if (!btrfs_is_empty_uuid(di_args->uuid))
  2371. s_uuid = di_args->uuid;
  2372. mutex_lock(&fs_devices->device_list_mutex);
  2373. dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
  2374. if (!dev) {
  2375. ret = -ENODEV;
  2376. goto out;
  2377. }
  2378. di_args->devid = dev->devid;
  2379. di_args->bytes_used = btrfs_device_get_bytes_used(dev);
  2380. di_args->total_bytes = btrfs_device_get_total_bytes(dev);
  2381. memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
  2382. if (dev->name) {
  2383. struct rcu_string *name;
  2384. rcu_read_lock();
  2385. name = rcu_dereference(dev->name);
  2386. strncpy(di_args->path, name->str, sizeof(di_args->path));
  2387. rcu_read_unlock();
  2388. di_args->path[sizeof(di_args->path) - 1] = 0;
  2389. } else {
  2390. di_args->path[0] = '\0';
  2391. }
  2392. out:
  2393. mutex_unlock(&fs_devices->device_list_mutex);
  2394. if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
  2395. ret = -EFAULT;
  2396. kfree(di_args);
  2397. return ret;
  2398. }
  2399. static struct page *extent_same_get_page(struct inode *inode, u64 off)
  2400. {
  2401. struct page *page;
  2402. pgoff_t index;
  2403. struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
  2404. index = off >> PAGE_CACHE_SHIFT;
  2405. page = grab_cache_page(inode->i_mapping, index);
  2406. if (!page)
  2407. return NULL;
  2408. if (!PageUptodate(page)) {
  2409. if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
  2410. 0))
  2411. return NULL;
  2412. lock_page(page);
  2413. if (!PageUptodate(page)) {
  2414. unlock_page(page);
  2415. page_cache_release(page);
  2416. return NULL;
  2417. }
  2418. }
  2419. unlock_page(page);
  2420. return page;
  2421. }
  2422. static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
  2423. {
  2424. /* do any pending delalloc/csum calc on src, one way or
  2425. another, and lock file content */
  2426. while (1) {
  2427. struct btrfs_ordered_extent *ordered;
  2428. lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
  2429. ordered = btrfs_lookup_first_ordered_extent(inode,
  2430. off + len - 1);
  2431. if ((!ordered ||
  2432. ordered->file_offset + ordered->len <= off ||
  2433. ordered->file_offset >= off + len) &&
  2434. !test_range_bit(&BTRFS_I(inode)->io_tree, off,
  2435. off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
  2436. if (ordered)
  2437. btrfs_put_ordered_extent(ordered);
  2438. break;
  2439. }
  2440. unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
  2441. if (ordered)
  2442. btrfs_put_ordered_extent(ordered);
  2443. btrfs_wait_ordered_range(inode, off, len);
  2444. }
  2445. }
  2446. static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
  2447. struct inode *inode2, u64 loff2, u64 len)
  2448. {
  2449. unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
  2450. unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
  2451. mutex_unlock(&inode1->i_mutex);
  2452. mutex_unlock(&inode2->i_mutex);
  2453. }
  2454. static void btrfs_double_lock(struct inode *inode1, u64 loff1,
  2455. struct inode *inode2, u64 loff2, u64 len)
  2456. {
  2457. if (inode1 < inode2) {
  2458. swap(inode1, inode2);
  2459. swap(loff1, loff2);
  2460. }
  2461. mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
  2462. lock_extent_range(inode1, loff1, len);
  2463. if (inode1 != inode2) {
  2464. mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
  2465. lock_extent_range(inode2, loff2, len);
  2466. }
  2467. }
  2468. static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
  2469. u64 dst_loff, u64 len)
  2470. {
  2471. int ret = 0;
  2472. struct page *src_page, *dst_page;
  2473. unsigned int cmp_len = PAGE_CACHE_SIZE;
  2474. void *addr, *dst_addr;
  2475. while (len) {
  2476. if (len < PAGE_CACHE_SIZE)
  2477. cmp_len = len;
  2478. src_page = extent_same_get_page(src, loff);
  2479. if (!src_page)
  2480. return -EINVAL;
  2481. dst_page = extent_same_get_page(dst, dst_loff);
  2482. if (!dst_page) {
  2483. page_cache_release(src_page);
  2484. return -EINVAL;
  2485. }
  2486. addr = kmap_atomic(src_page);
  2487. dst_addr = kmap_atomic(dst_page);
  2488. flush_dcache_page(src_page);
  2489. flush_dcache_page(dst_page);
  2490. if (memcmp(addr, dst_addr, cmp_len))
  2491. ret = BTRFS_SAME_DATA_DIFFERS;
  2492. kunmap_atomic(addr);
  2493. kunmap_atomic(dst_addr);
  2494. page_cache_release(src_page);
  2495. page_cache_release(dst_page);
  2496. if (ret)
  2497. break;
  2498. loff += cmp_len;
  2499. dst_loff += cmp_len;
  2500. len -= cmp_len;
  2501. }
  2502. return ret;
  2503. }
  2504. static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
  2505. {
  2506. u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
  2507. if (off + len > inode->i_size || off + len < off)
  2508. return -EINVAL;
  2509. /* Check that we are block aligned - btrfs_clone() requires this */
  2510. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
  2511. return -EINVAL;
  2512. return 0;
  2513. }
  2514. static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
  2515. struct inode *dst, u64 dst_loff)
  2516. {
  2517. int ret;
  2518. /*
  2519. * btrfs_clone() can't handle extents in the same file
  2520. * yet. Once that works, we can drop this check and replace it
  2521. * with a check for the same inode, but overlapping extents.
  2522. */
  2523. if (src == dst)
  2524. return -EINVAL;
  2525. if (len == 0)
  2526. return 0;
  2527. btrfs_double_lock(src, loff, dst, dst_loff, len);
  2528. ret = extent_same_check_offsets(src, loff, len);
  2529. if (ret)
  2530. goto out_unlock;
  2531. ret = extent_same_check_offsets(dst, dst_loff, len);
  2532. if (ret)
  2533. goto out_unlock;
  2534. /* don't make the dst file partly checksummed */
  2535. if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
  2536. (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
  2537. ret = -EINVAL;
  2538. goto out_unlock;
  2539. }
  2540. ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
  2541. if (ret == 0)
  2542. ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
  2543. out_unlock:
  2544. btrfs_double_unlock(src, loff, dst, dst_loff, len);
  2545. return ret;
  2546. }
  2547. #define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
  2548. static long btrfs_ioctl_file_extent_same(struct file *file,
  2549. struct btrfs_ioctl_same_args __user *argp)
  2550. {
  2551. struct btrfs_ioctl_same_args *same;
  2552. struct btrfs_ioctl_same_extent_info *info;
  2553. struct inode *src = file_inode(file);
  2554. u64 off;
  2555. u64 len;
  2556. int i;
  2557. int ret;
  2558. unsigned long size;
  2559. u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
  2560. bool is_admin = capable(CAP_SYS_ADMIN);
  2561. u16 count;
  2562. if (!(file->f_mode & FMODE_READ))
  2563. return -EINVAL;
  2564. ret = mnt_want_write_file(file);
  2565. if (ret)
  2566. return ret;
  2567. if (get_user(count, &argp->dest_count)) {
  2568. ret = -EFAULT;
  2569. goto out;
  2570. }
  2571. size = offsetof(struct btrfs_ioctl_same_args __user, info[count]);
  2572. same = memdup_user(argp, size);
  2573. if (IS_ERR(same)) {
  2574. ret = PTR_ERR(same);
  2575. goto out;
  2576. }
  2577. off = same->logical_offset;
  2578. len = same->length;
  2579. /*
  2580. * Limit the total length we will dedupe for each operation.
  2581. * This is intended to bound the total time spent in this
  2582. * ioctl to something sane.
  2583. */
  2584. if (len > BTRFS_MAX_DEDUPE_LEN)
  2585. len = BTRFS_MAX_DEDUPE_LEN;
  2586. if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
  2587. /*
  2588. * Btrfs does not support blocksize < page_size. As a
  2589. * result, btrfs_cmp_data() won't correctly handle
  2590. * this situation without an update.
  2591. */
  2592. ret = -EINVAL;
  2593. goto out;
  2594. }
  2595. ret = -EISDIR;
  2596. if (S_ISDIR(src->i_mode))
  2597. goto out;
  2598. ret = -EACCES;
  2599. if (!S_ISREG(src->i_mode))
  2600. goto out;
  2601. /* pre-format output fields to sane values */
  2602. for (i = 0; i < count; i++) {
  2603. same->info[i].bytes_deduped = 0ULL;
  2604. same->info[i].status = 0;
  2605. }
  2606. for (i = 0, info = same->info; i < count; i++, info++) {
  2607. struct inode *dst;
  2608. struct fd dst_file = fdget(info->fd);
  2609. if (!dst_file.file) {
  2610. info->status = -EBADF;
  2611. continue;
  2612. }
  2613. dst = file_inode(dst_file.file);
  2614. if (!(is_admin || (dst_file.file->f_mode & FMODE_WRITE))) {
  2615. info->status = -EINVAL;
  2616. } else if (file->f_path.mnt != dst_file.file->f_path.mnt) {
  2617. info->status = -EXDEV;
  2618. } else if (S_ISDIR(dst->i_mode)) {
  2619. info->status = -EISDIR;
  2620. } else if (!S_ISREG(dst->i_mode)) {
  2621. info->status = -EACCES;
  2622. } else {
  2623. info->status = btrfs_extent_same(src, off, len, dst,
  2624. info->logical_offset);
  2625. if (info->status == 0)
  2626. info->bytes_deduped += len;
  2627. }
  2628. fdput(dst_file);
  2629. }
  2630. ret = copy_to_user(argp, same, size);
  2631. if (ret)
  2632. ret = -EFAULT;
  2633. out:
  2634. mnt_drop_write_file(file);
  2635. return ret;
  2636. }
  2637. /* Helper to check and see if this root currently has a ref on the given disk
  2638. * bytenr. If it does then we need to update the quota for this root. This
  2639. * doesn't do anything if quotas aren't enabled.
  2640. */
  2641. static int check_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2642. u64 disko)
  2643. {
  2644. struct seq_list tree_mod_seq_elem = {};
  2645. struct ulist *roots;
  2646. struct ulist_iterator uiter;
  2647. struct ulist_node *root_node = NULL;
  2648. int ret;
  2649. if (!root->fs_info->quota_enabled)
  2650. return 1;
  2651. btrfs_get_tree_mod_seq(root->fs_info, &tree_mod_seq_elem);
  2652. ret = btrfs_find_all_roots(trans, root->fs_info, disko,
  2653. tree_mod_seq_elem.seq, &roots);
  2654. if (ret < 0)
  2655. goto out;
  2656. ret = 0;
  2657. ULIST_ITER_INIT(&uiter);
  2658. while ((root_node = ulist_next(roots, &uiter))) {
  2659. if (root_node->val == root->objectid) {
  2660. ret = 1;
  2661. break;
  2662. }
  2663. }
  2664. ulist_free(roots);
  2665. out:
  2666. btrfs_put_tree_mod_seq(root->fs_info, &tree_mod_seq_elem);
  2667. return ret;
  2668. }
  2669. static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
  2670. struct inode *inode,
  2671. u64 endoff,
  2672. const u64 destoff,
  2673. const u64 olen)
  2674. {
  2675. struct btrfs_root *root = BTRFS_I(inode)->root;
  2676. int ret;
  2677. inode_inc_iversion(inode);
  2678. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  2679. /*
  2680. * We round up to the block size at eof when determining which
  2681. * extents to clone above, but shouldn't round up the file size.
  2682. */
  2683. if (endoff > destoff + olen)
  2684. endoff = destoff + olen;
  2685. if (endoff > inode->i_size)
  2686. btrfs_i_size_write(inode, endoff);
  2687. ret = btrfs_update_inode(trans, root, inode);
  2688. if (ret) {
  2689. btrfs_abort_transaction(trans, root, ret);
  2690. btrfs_end_transaction(trans, root);
  2691. goto out;
  2692. }
  2693. ret = btrfs_end_transaction(trans, root);
  2694. out:
  2695. return ret;
  2696. }
  2697. static void clone_update_extent_map(struct inode *inode,
  2698. const struct btrfs_trans_handle *trans,
  2699. const struct btrfs_path *path,
  2700. const u64 hole_offset,
  2701. const u64 hole_len)
  2702. {
  2703. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  2704. struct extent_map *em;
  2705. int ret;
  2706. em = alloc_extent_map();
  2707. if (!em) {
  2708. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  2709. &BTRFS_I(inode)->runtime_flags);
  2710. return;
  2711. }
  2712. if (path) {
  2713. struct btrfs_file_extent_item *fi;
  2714. fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2715. struct btrfs_file_extent_item);
  2716. btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
  2717. em->generation = -1;
  2718. if (btrfs_file_extent_type(path->nodes[0], fi) ==
  2719. BTRFS_FILE_EXTENT_INLINE)
  2720. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  2721. &BTRFS_I(inode)->runtime_flags);
  2722. } else {
  2723. em->start = hole_offset;
  2724. em->len = hole_len;
  2725. em->ram_bytes = em->len;
  2726. em->orig_start = hole_offset;
  2727. em->block_start = EXTENT_MAP_HOLE;
  2728. em->block_len = 0;
  2729. em->orig_block_len = 0;
  2730. em->compress_type = BTRFS_COMPRESS_NONE;
  2731. em->generation = trans->transid;
  2732. }
  2733. while (1) {
  2734. write_lock(&em_tree->lock);
  2735. ret = add_extent_mapping(em_tree, em, 1);
  2736. write_unlock(&em_tree->lock);
  2737. if (ret != -EEXIST) {
  2738. free_extent_map(em);
  2739. break;
  2740. }
  2741. btrfs_drop_extent_cache(inode, em->start,
  2742. em->start + em->len - 1, 0);
  2743. }
  2744. if (ret)
  2745. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  2746. &BTRFS_I(inode)->runtime_flags);
  2747. }
  2748. /**
  2749. * btrfs_clone() - clone a range from inode file to another
  2750. *
  2751. * @src: Inode to clone from
  2752. * @inode: Inode to clone to
  2753. * @off: Offset within source to start clone from
  2754. * @olen: Original length, passed by user, of range to clone
  2755. * @olen_aligned: Block-aligned value of olen, extent_same uses
  2756. * identical values here
  2757. * @destoff: Offset within @inode to start clone
  2758. */
  2759. static int btrfs_clone(struct inode *src, struct inode *inode,
  2760. const u64 off, const u64 olen, const u64 olen_aligned,
  2761. const u64 destoff)
  2762. {
  2763. struct btrfs_root *root = BTRFS_I(inode)->root;
  2764. struct btrfs_path *path = NULL;
  2765. struct extent_buffer *leaf;
  2766. struct btrfs_trans_handle *trans;
  2767. char *buf = NULL;
  2768. struct btrfs_key key;
  2769. u32 nritems;
  2770. int slot;
  2771. int ret;
  2772. int no_quota;
  2773. const u64 len = olen_aligned;
  2774. u64 last_disko = 0;
  2775. u64 last_dest_end = destoff;
  2776. ret = -ENOMEM;
  2777. buf = vmalloc(root->nodesize);
  2778. if (!buf)
  2779. return ret;
  2780. path = btrfs_alloc_path();
  2781. if (!path) {
  2782. vfree(buf);
  2783. return ret;
  2784. }
  2785. path->reada = 2;
  2786. /* clone data */
  2787. key.objectid = btrfs_ino(src);
  2788. key.type = BTRFS_EXTENT_DATA_KEY;
  2789. key.offset = off;
  2790. while (1) {
  2791. /*
  2792. * note the key will change type as we walk through the
  2793. * tree.
  2794. */
  2795. path->leave_spinning = 1;
  2796. ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
  2797. 0, 0);
  2798. if (ret < 0)
  2799. goto out;
  2800. /*
  2801. * First search, if no extent item that starts at offset off was
  2802. * found but the previous item is an extent item, it's possible
  2803. * it might overlap our target range, therefore process it.
  2804. */
  2805. if (key.offset == off && ret > 0 && path->slots[0] > 0) {
  2806. btrfs_item_key_to_cpu(path->nodes[0], &key,
  2807. path->slots[0] - 1);
  2808. if (key.type == BTRFS_EXTENT_DATA_KEY)
  2809. path->slots[0]--;
  2810. }
  2811. nritems = btrfs_header_nritems(path->nodes[0]);
  2812. process_slot:
  2813. no_quota = 1;
  2814. if (path->slots[0] >= nritems) {
  2815. ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
  2816. if (ret < 0)
  2817. goto out;
  2818. if (ret > 0)
  2819. break;
  2820. nritems = btrfs_header_nritems(path->nodes[0]);
  2821. }
  2822. leaf = path->nodes[0];
  2823. slot = path->slots[0];
  2824. btrfs_item_key_to_cpu(leaf, &key, slot);
  2825. if (key.type > BTRFS_EXTENT_DATA_KEY ||
  2826. key.objectid != btrfs_ino(src))
  2827. break;
  2828. if (key.type == BTRFS_EXTENT_DATA_KEY) {
  2829. struct btrfs_file_extent_item *extent;
  2830. int type;
  2831. u32 size;
  2832. struct btrfs_key new_key;
  2833. u64 disko = 0, diskl = 0;
  2834. u64 datao = 0, datal = 0;
  2835. u8 comp;
  2836. u64 drop_start;
  2837. extent = btrfs_item_ptr(leaf, slot,
  2838. struct btrfs_file_extent_item);
  2839. comp = btrfs_file_extent_compression(leaf, extent);
  2840. type = btrfs_file_extent_type(leaf, extent);
  2841. if (type == BTRFS_FILE_EXTENT_REG ||
  2842. type == BTRFS_FILE_EXTENT_PREALLOC) {
  2843. disko = btrfs_file_extent_disk_bytenr(leaf,
  2844. extent);
  2845. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  2846. extent);
  2847. datao = btrfs_file_extent_offset(leaf, extent);
  2848. datal = btrfs_file_extent_num_bytes(leaf,
  2849. extent);
  2850. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  2851. /* take upper bound, may be compressed */
  2852. datal = btrfs_file_extent_ram_bytes(leaf,
  2853. extent);
  2854. }
  2855. /*
  2856. * The first search might have left us at an extent
  2857. * item that ends before our target range's start, can
  2858. * happen if we have holes and NO_HOLES feature enabled.
  2859. */
  2860. if (key.offset + datal <= off) {
  2861. path->slots[0]++;
  2862. goto process_slot;
  2863. } else if (key.offset >= off + len) {
  2864. break;
  2865. }
  2866. size = btrfs_item_size_nr(leaf, slot);
  2867. read_extent_buffer(leaf, buf,
  2868. btrfs_item_ptr_offset(leaf, slot),
  2869. size);
  2870. btrfs_release_path(path);
  2871. path->leave_spinning = 0;
  2872. memcpy(&new_key, &key, sizeof(new_key));
  2873. new_key.objectid = btrfs_ino(inode);
  2874. if (off <= key.offset)
  2875. new_key.offset = key.offset + destoff - off;
  2876. else
  2877. new_key.offset = destoff;
  2878. /*
  2879. * Deal with a hole that doesn't have an extent item
  2880. * that represents it (NO_HOLES feature enabled).
  2881. * This hole is either in the middle of the cloning
  2882. * range or at the beginning (fully overlaps it or
  2883. * partially overlaps it).
  2884. */
  2885. if (new_key.offset != last_dest_end)
  2886. drop_start = last_dest_end;
  2887. else
  2888. drop_start = new_key.offset;
  2889. /*
  2890. * 1 - adjusting old extent (we may have to split it)
  2891. * 1 - add new extent
  2892. * 1 - inode update
  2893. */
  2894. trans = btrfs_start_transaction(root, 3);
  2895. if (IS_ERR(trans)) {
  2896. ret = PTR_ERR(trans);
  2897. goto out;
  2898. }
  2899. if (type == BTRFS_FILE_EXTENT_REG ||
  2900. type == BTRFS_FILE_EXTENT_PREALLOC) {
  2901. /*
  2902. * a | --- range to clone ---| b
  2903. * | ------------- extent ------------- |
  2904. */
  2905. /* subtract range b */
  2906. if (key.offset + datal > off + len)
  2907. datal = off + len - key.offset;
  2908. /* subtract range a */
  2909. if (off > key.offset) {
  2910. datao += off - key.offset;
  2911. datal -= off - key.offset;
  2912. }
  2913. ret = btrfs_drop_extents(trans, root, inode,
  2914. drop_start,
  2915. new_key.offset + datal,
  2916. 1);
  2917. if (ret) {
  2918. if (ret != -EOPNOTSUPP)
  2919. btrfs_abort_transaction(trans,
  2920. root, ret);
  2921. btrfs_end_transaction(trans, root);
  2922. goto out;
  2923. }
  2924. ret = btrfs_insert_empty_item(trans, root, path,
  2925. &new_key, size);
  2926. if (ret) {
  2927. btrfs_abort_transaction(trans, root,
  2928. ret);
  2929. btrfs_end_transaction(trans, root);
  2930. goto out;
  2931. }
  2932. leaf = path->nodes[0];
  2933. slot = path->slots[0];
  2934. write_extent_buffer(leaf, buf,
  2935. btrfs_item_ptr_offset(leaf, slot),
  2936. size);
  2937. extent = btrfs_item_ptr(leaf, slot,
  2938. struct btrfs_file_extent_item);
  2939. /* disko == 0 means it's a hole */
  2940. if (!disko)
  2941. datao = 0;
  2942. btrfs_set_file_extent_offset(leaf, extent,
  2943. datao);
  2944. btrfs_set_file_extent_num_bytes(leaf, extent,
  2945. datal);
  2946. /*
  2947. * We need to look up the roots that point at
  2948. * this bytenr and see if the new root does. If
  2949. * it does not we need to make sure we update
  2950. * quotas appropriately.
  2951. */
  2952. if (disko && root != BTRFS_I(src)->root &&
  2953. disko != last_disko) {
  2954. no_quota = check_ref(trans, root,
  2955. disko);
  2956. if (no_quota < 0) {
  2957. btrfs_abort_transaction(trans,
  2958. root,
  2959. ret);
  2960. btrfs_end_transaction(trans,
  2961. root);
  2962. ret = no_quota;
  2963. goto out;
  2964. }
  2965. }
  2966. if (disko) {
  2967. inode_add_bytes(inode, datal);
  2968. ret = btrfs_inc_extent_ref(trans, root,
  2969. disko, diskl, 0,
  2970. root->root_key.objectid,
  2971. btrfs_ino(inode),
  2972. new_key.offset - datao,
  2973. no_quota);
  2974. if (ret) {
  2975. btrfs_abort_transaction(trans,
  2976. root,
  2977. ret);
  2978. btrfs_end_transaction(trans,
  2979. root);
  2980. goto out;
  2981. }
  2982. }
  2983. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  2984. u64 skip = 0;
  2985. u64 trim = 0;
  2986. u64 aligned_end = 0;
  2987. if (off > key.offset) {
  2988. skip = off - key.offset;
  2989. new_key.offset += skip;
  2990. }
  2991. if (key.offset + datal > off + len)
  2992. trim = key.offset + datal - (off + len);
  2993. if (comp && (skip || trim)) {
  2994. ret = -EINVAL;
  2995. btrfs_end_transaction(trans, root);
  2996. goto out;
  2997. }
  2998. size -= skip + trim;
  2999. datal -= skip + trim;
  3000. aligned_end = ALIGN(new_key.offset + datal,
  3001. root->sectorsize);
  3002. ret = btrfs_drop_extents(trans, root, inode,
  3003. drop_start,
  3004. aligned_end,
  3005. 1);
  3006. if (ret) {
  3007. if (ret != -EOPNOTSUPP)
  3008. btrfs_abort_transaction(trans,
  3009. root, ret);
  3010. btrfs_end_transaction(trans, root);
  3011. goto out;
  3012. }
  3013. ret = btrfs_insert_empty_item(trans, root, path,
  3014. &new_key, size);
  3015. if (ret) {
  3016. btrfs_abort_transaction(trans, root,
  3017. ret);
  3018. btrfs_end_transaction(trans, root);
  3019. goto out;
  3020. }
  3021. if (skip) {
  3022. u32 start =
  3023. btrfs_file_extent_calc_inline_size(0);
  3024. memmove(buf+start, buf+start+skip,
  3025. datal);
  3026. }
  3027. leaf = path->nodes[0];
  3028. slot = path->slots[0];
  3029. write_extent_buffer(leaf, buf,
  3030. btrfs_item_ptr_offset(leaf, slot),
  3031. size);
  3032. inode_add_bytes(inode, datal);
  3033. }
  3034. /* If we have an implicit hole (NO_HOLES feature). */
  3035. if (drop_start < new_key.offset)
  3036. clone_update_extent_map(inode, trans,
  3037. NULL, drop_start,
  3038. new_key.offset - drop_start);
  3039. clone_update_extent_map(inode, trans, path, 0, 0);
  3040. btrfs_mark_buffer_dirty(leaf);
  3041. btrfs_release_path(path);
  3042. last_dest_end = ALIGN(new_key.offset + datal,
  3043. root->sectorsize);
  3044. ret = clone_finish_inode_update(trans, inode,
  3045. last_dest_end,
  3046. destoff, olen);
  3047. if (ret)
  3048. goto out;
  3049. if (new_key.offset + datal >= destoff + len)
  3050. break;
  3051. }
  3052. btrfs_release_path(path);
  3053. key.offset++;
  3054. }
  3055. ret = 0;
  3056. if (last_dest_end < destoff + len) {
  3057. /*
  3058. * We have an implicit hole (NO_HOLES feature is enabled) that
  3059. * fully or partially overlaps our cloning range at its end.
  3060. */
  3061. btrfs_release_path(path);
  3062. /*
  3063. * 1 - remove extent(s)
  3064. * 1 - inode update
  3065. */
  3066. trans = btrfs_start_transaction(root, 2);
  3067. if (IS_ERR(trans)) {
  3068. ret = PTR_ERR(trans);
  3069. goto out;
  3070. }
  3071. ret = btrfs_drop_extents(trans, root, inode,
  3072. last_dest_end, destoff + len, 1);
  3073. if (ret) {
  3074. if (ret != -EOPNOTSUPP)
  3075. btrfs_abort_transaction(trans, root, ret);
  3076. btrfs_end_transaction(trans, root);
  3077. goto out;
  3078. }
  3079. clone_update_extent_map(inode, trans, NULL, last_dest_end,
  3080. destoff + len - last_dest_end);
  3081. ret = clone_finish_inode_update(trans, inode, destoff + len,
  3082. destoff, olen);
  3083. }
  3084. out:
  3085. btrfs_free_path(path);
  3086. vfree(buf);
  3087. return ret;
  3088. }
  3089. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  3090. u64 off, u64 olen, u64 destoff)
  3091. {
  3092. struct inode *inode = file_inode(file);
  3093. struct btrfs_root *root = BTRFS_I(inode)->root;
  3094. struct fd src_file;
  3095. struct inode *src;
  3096. int ret;
  3097. u64 len = olen;
  3098. u64 bs = root->fs_info->sb->s_blocksize;
  3099. int same_inode = 0;
  3100. /*
  3101. * TODO:
  3102. * - split compressed inline extents. annoying: we need to
  3103. * decompress into destination's address_space (the file offset
  3104. * may change, so source mapping won't do), then recompress (or
  3105. * otherwise reinsert) a subrange.
  3106. *
  3107. * - split destination inode's inline extents. The inline extents can
  3108. * be either compressed or non-compressed.
  3109. */
  3110. /* the destination must be opened for writing */
  3111. if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
  3112. return -EINVAL;
  3113. if (btrfs_root_readonly(root))
  3114. return -EROFS;
  3115. ret = mnt_want_write_file(file);
  3116. if (ret)
  3117. return ret;
  3118. src_file = fdget(srcfd);
  3119. if (!src_file.file) {
  3120. ret = -EBADF;
  3121. goto out_drop_write;
  3122. }
  3123. ret = -EXDEV;
  3124. if (src_file.file->f_path.mnt != file->f_path.mnt)
  3125. goto out_fput;
  3126. src = file_inode(src_file.file);
  3127. ret = -EINVAL;
  3128. if (src == inode)
  3129. same_inode = 1;
  3130. /* the src must be open for reading */
  3131. if (!(src_file.file->f_mode & FMODE_READ))
  3132. goto out_fput;
  3133. /* don't make the dst file partly checksummed */
  3134. if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
  3135. (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
  3136. goto out_fput;
  3137. ret = -EISDIR;
  3138. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  3139. goto out_fput;
  3140. ret = -EXDEV;
  3141. if (src->i_sb != inode->i_sb)
  3142. goto out_fput;
  3143. if (!same_inode) {
  3144. if (inode < src) {
  3145. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  3146. mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
  3147. } else {
  3148. mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
  3149. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  3150. }
  3151. } else {
  3152. mutex_lock(&src->i_mutex);
  3153. }
  3154. /* determine range to clone */
  3155. ret = -EINVAL;
  3156. if (off + len > src->i_size || off + len < off)
  3157. goto out_unlock;
  3158. if (len == 0)
  3159. olen = len = src->i_size - off;
  3160. /* if we extend to eof, continue to block boundary */
  3161. if (off + len == src->i_size)
  3162. len = ALIGN(src->i_size, bs) - off;
  3163. if (len == 0) {
  3164. ret = 0;
  3165. goto out_unlock;
  3166. }
  3167. /* verify the end result is block aligned */
  3168. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
  3169. !IS_ALIGNED(destoff, bs))
  3170. goto out_unlock;
  3171. /* verify if ranges are overlapped within the same file */
  3172. if (same_inode) {
  3173. if (destoff + len > off && destoff < off + len)
  3174. goto out_unlock;
  3175. }
  3176. if (destoff > inode->i_size) {
  3177. ret = btrfs_cont_expand(inode, inode->i_size, destoff);
  3178. if (ret)
  3179. goto out_unlock;
  3180. }
  3181. /*
  3182. * Lock the target range too. Right after we replace the file extent
  3183. * items in the fs tree (which now point to the cloned data), we might
  3184. * have a worker replace them with extent items relative to a write
  3185. * operation that was issued before this clone operation (i.e. confront
  3186. * with inode.c:btrfs_finish_ordered_io).
  3187. */
  3188. if (same_inode) {
  3189. u64 lock_start = min_t(u64, off, destoff);
  3190. u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
  3191. lock_extent_range(src, lock_start, lock_len);
  3192. } else {
  3193. lock_extent_range(src, off, len);
  3194. lock_extent_range(inode, destoff, len);
  3195. }
  3196. ret = btrfs_clone(src, inode, off, olen, len, destoff);
  3197. if (same_inode) {
  3198. u64 lock_start = min_t(u64, off, destoff);
  3199. u64 lock_end = max_t(u64, off, destoff) + len - 1;
  3200. unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
  3201. } else {
  3202. unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
  3203. unlock_extent(&BTRFS_I(inode)->io_tree, destoff,
  3204. destoff + len - 1);
  3205. }
  3206. /*
  3207. * Truncate page cache pages so that future reads will see the cloned
  3208. * data immediately and not the previous data.
  3209. */
  3210. truncate_inode_pages_range(&inode->i_data, destoff,
  3211. PAGE_CACHE_ALIGN(destoff + len) - 1);
  3212. out_unlock:
  3213. if (!same_inode) {
  3214. if (inode < src) {
  3215. mutex_unlock(&src->i_mutex);
  3216. mutex_unlock(&inode->i_mutex);
  3217. } else {
  3218. mutex_unlock(&inode->i_mutex);
  3219. mutex_unlock(&src->i_mutex);
  3220. }
  3221. } else {
  3222. mutex_unlock(&src->i_mutex);
  3223. }
  3224. out_fput:
  3225. fdput(src_file);
  3226. out_drop_write:
  3227. mnt_drop_write_file(file);
  3228. return ret;
  3229. }
  3230. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  3231. {
  3232. struct btrfs_ioctl_clone_range_args args;
  3233. if (copy_from_user(&args, argp, sizeof(args)))
  3234. return -EFAULT;
  3235. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  3236. args.src_length, args.dest_offset);
  3237. }
  3238. /*
  3239. * there are many ways the trans_start and trans_end ioctls can lead
  3240. * to deadlocks. They should only be used by applications that
  3241. * basically own the machine, and have a very in depth understanding
  3242. * of all the possible deadlocks and enospc problems.
  3243. */
  3244. static long btrfs_ioctl_trans_start(struct file *file)
  3245. {
  3246. struct inode *inode = file_inode(file);
  3247. struct btrfs_root *root = BTRFS_I(inode)->root;
  3248. struct btrfs_trans_handle *trans;
  3249. int ret;
  3250. ret = -EPERM;
  3251. if (!capable(CAP_SYS_ADMIN))
  3252. goto out;
  3253. ret = -EINPROGRESS;
  3254. if (file->private_data)
  3255. goto out;
  3256. ret = -EROFS;
  3257. if (btrfs_root_readonly(root))
  3258. goto out;
  3259. ret = mnt_want_write_file(file);
  3260. if (ret)
  3261. goto out;
  3262. atomic_inc(&root->fs_info->open_ioctl_trans);
  3263. ret = -ENOMEM;
  3264. trans = btrfs_start_ioctl_transaction(root);
  3265. if (IS_ERR(trans))
  3266. goto out_drop;
  3267. file->private_data = trans;
  3268. return 0;
  3269. out_drop:
  3270. atomic_dec(&root->fs_info->open_ioctl_trans);
  3271. mnt_drop_write_file(file);
  3272. out:
  3273. return ret;
  3274. }
  3275. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  3276. {
  3277. struct inode *inode = file_inode(file);
  3278. struct btrfs_root *root = BTRFS_I(inode)->root;
  3279. struct btrfs_root *new_root;
  3280. struct btrfs_dir_item *di;
  3281. struct btrfs_trans_handle *trans;
  3282. struct btrfs_path *path;
  3283. struct btrfs_key location;
  3284. struct btrfs_disk_key disk_key;
  3285. u64 objectid = 0;
  3286. u64 dir_id;
  3287. int ret;
  3288. if (!capable(CAP_SYS_ADMIN))
  3289. return -EPERM;
  3290. ret = mnt_want_write_file(file);
  3291. if (ret)
  3292. return ret;
  3293. if (copy_from_user(&objectid, argp, sizeof(objectid))) {
  3294. ret = -EFAULT;
  3295. goto out;
  3296. }
  3297. if (!objectid)
  3298. objectid = BTRFS_FS_TREE_OBJECTID;
  3299. location.objectid = objectid;
  3300. location.type = BTRFS_ROOT_ITEM_KEY;
  3301. location.offset = (u64)-1;
  3302. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  3303. if (IS_ERR(new_root)) {
  3304. ret = PTR_ERR(new_root);
  3305. goto out;
  3306. }
  3307. path = btrfs_alloc_path();
  3308. if (!path) {
  3309. ret = -ENOMEM;
  3310. goto out;
  3311. }
  3312. path->leave_spinning = 1;
  3313. trans = btrfs_start_transaction(root, 1);
  3314. if (IS_ERR(trans)) {
  3315. btrfs_free_path(path);
  3316. ret = PTR_ERR(trans);
  3317. goto out;
  3318. }
  3319. dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
  3320. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  3321. dir_id, "default", 7, 1);
  3322. if (IS_ERR_OR_NULL(di)) {
  3323. btrfs_free_path(path);
  3324. btrfs_end_transaction(trans, root);
  3325. btrfs_err(new_root->fs_info, "Umm, you don't have the default dir"
  3326. "item, this isn't going to work");
  3327. ret = -ENOENT;
  3328. goto out;
  3329. }
  3330. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  3331. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  3332. btrfs_mark_buffer_dirty(path->nodes[0]);
  3333. btrfs_free_path(path);
  3334. btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
  3335. btrfs_end_transaction(trans, root);
  3336. out:
  3337. mnt_drop_write_file(file);
  3338. return ret;
  3339. }
  3340. void btrfs_get_block_group_info(struct list_head *groups_list,
  3341. struct btrfs_ioctl_space_info *space)
  3342. {
  3343. struct btrfs_block_group_cache *block_group;
  3344. space->total_bytes = 0;
  3345. space->used_bytes = 0;
  3346. space->flags = 0;
  3347. list_for_each_entry(block_group, groups_list, list) {
  3348. space->flags = block_group->flags;
  3349. space->total_bytes += block_group->key.offset;
  3350. space->used_bytes +=
  3351. btrfs_block_group_used(&block_group->item);
  3352. }
  3353. }
  3354. static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
  3355. {
  3356. struct btrfs_ioctl_space_args space_args;
  3357. struct btrfs_ioctl_space_info space;
  3358. struct btrfs_ioctl_space_info *dest;
  3359. struct btrfs_ioctl_space_info *dest_orig;
  3360. struct btrfs_ioctl_space_info __user *user_dest;
  3361. struct btrfs_space_info *info;
  3362. u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
  3363. BTRFS_BLOCK_GROUP_SYSTEM,
  3364. BTRFS_BLOCK_GROUP_METADATA,
  3365. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
  3366. int num_types = 4;
  3367. int alloc_size;
  3368. int ret = 0;
  3369. u64 slot_count = 0;
  3370. int i, c;
  3371. if (copy_from_user(&space_args,
  3372. (struct btrfs_ioctl_space_args __user *)arg,
  3373. sizeof(space_args)))
  3374. return -EFAULT;
  3375. for (i = 0; i < num_types; i++) {
  3376. struct btrfs_space_info *tmp;
  3377. info = NULL;
  3378. rcu_read_lock();
  3379. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  3380. list) {
  3381. if (tmp->flags == types[i]) {
  3382. info = tmp;
  3383. break;
  3384. }
  3385. }
  3386. rcu_read_unlock();
  3387. if (!info)
  3388. continue;
  3389. down_read(&info->groups_sem);
  3390. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  3391. if (!list_empty(&info->block_groups[c]))
  3392. slot_count++;
  3393. }
  3394. up_read(&info->groups_sem);
  3395. }
  3396. /*
  3397. * Global block reserve, exported as a space_info
  3398. */
  3399. slot_count++;
  3400. /* space_slots == 0 means they are asking for a count */
  3401. if (space_args.space_slots == 0) {
  3402. space_args.total_spaces = slot_count;
  3403. goto out;
  3404. }
  3405. slot_count = min_t(u64, space_args.space_slots, slot_count);
  3406. alloc_size = sizeof(*dest) * slot_count;
  3407. /* we generally have at most 6 or so space infos, one for each raid
  3408. * level. So, a whole page should be more than enough for everyone
  3409. */
  3410. if (alloc_size > PAGE_CACHE_SIZE)
  3411. return -ENOMEM;
  3412. space_args.total_spaces = 0;
  3413. dest = kmalloc(alloc_size, GFP_NOFS);
  3414. if (!dest)
  3415. return -ENOMEM;
  3416. dest_orig = dest;
  3417. /* now we have a buffer to copy into */
  3418. for (i = 0; i < num_types; i++) {
  3419. struct btrfs_space_info *tmp;
  3420. if (!slot_count)
  3421. break;
  3422. info = NULL;
  3423. rcu_read_lock();
  3424. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  3425. list) {
  3426. if (tmp->flags == types[i]) {
  3427. info = tmp;
  3428. break;
  3429. }
  3430. }
  3431. rcu_read_unlock();
  3432. if (!info)
  3433. continue;
  3434. down_read(&info->groups_sem);
  3435. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  3436. if (!list_empty(&info->block_groups[c])) {
  3437. btrfs_get_block_group_info(
  3438. &info->block_groups[c], &space);
  3439. memcpy(dest, &space, sizeof(space));
  3440. dest++;
  3441. space_args.total_spaces++;
  3442. slot_count--;
  3443. }
  3444. if (!slot_count)
  3445. break;
  3446. }
  3447. up_read(&info->groups_sem);
  3448. }
  3449. /*
  3450. * Add global block reserve
  3451. */
  3452. if (slot_count) {
  3453. struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv;
  3454. spin_lock(&block_rsv->lock);
  3455. space.total_bytes = block_rsv->size;
  3456. space.used_bytes = block_rsv->size - block_rsv->reserved;
  3457. spin_unlock(&block_rsv->lock);
  3458. space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
  3459. memcpy(dest, &space, sizeof(space));
  3460. space_args.total_spaces++;
  3461. }
  3462. user_dest = (struct btrfs_ioctl_space_info __user *)
  3463. (arg + sizeof(struct btrfs_ioctl_space_args));
  3464. if (copy_to_user(user_dest, dest_orig, alloc_size))
  3465. ret = -EFAULT;
  3466. kfree(dest_orig);
  3467. out:
  3468. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  3469. ret = -EFAULT;
  3470. return ret;
  3471. }
  3472. /*
  3473. * there are many ways the trans_start and trans_end ioctls can lead
  3474. * to deadlocks. They should only be used by applications that
  3475. * basically own the machine, and have a very in depth understanding
  3476. * of all the possible deadlocks and enospc problems.
  3477. */
  3478. long btrfs_ioctl_trans_end(struct file *file)
  3479. {
  3480. struct inode *inode = file_inode(file);
  3481. struct btrfs_root *root = BTRFS_I(inode)->root;
  3482. struct btrfs_trans_handle *trans;
  3483. trans = file->private_data;
  3484. if (!trans)
  3485. return -EINVAL;
  3486. file->private_data = NULL;
  3487. btrfs_end_transaction(trans, root);
  3488. atomic_dec(&root->fs_info->open_ioctl_trans);
  3489. mnt_drop_write_file(file);
  3490. return 0;
  3491. }
  3492. static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
  3493. void __user *argp)
  3494. {
  3495. struct btrfs_trans_handle *trans;
  3496. u64 transid;
  3497. int ret;
  3498. trans = btrfs_attach_transaction_barrier(root);
  3499. if (IS_ERR(trans)) {
  3500. if (PTR_ERR(trans) != -ENOENT)
  3501. return PTR_ERR(trans);
  3502. /* No running transaction, don't bother */
  3503. transid = root->fs_info->last_trans_committed;
  3504. goto out;
  3505. }
  3506. transid = trans->transid;
  3507. ret = btrfs_commit_transaction_async(trans, root, 0);
  3508. if (ret) {
  3509. btrfs_end_transaction(trans, root);
  3510. return ret;
  3511. }
  3512. out:
  3513. if (argp)
  3514. if (copy_to_user(argp, &transid, sizeof(transid)))
  3515. return -EFAULT;
  3516. return 0;
  3517. }
  3518. static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
  3519. void __user *argp)
  3520. {
  3521. u64 transid;
  3522. if (argp) {
  3523. if (copy_from_user(&transid, argp, sizeof(transid)))
  3524. return -EFAULT;
  3525. } else {
  3526. transid = 0; /* current trans */
  3527. }
  3528. return btrfs_wait_for_commit(root, transid);
  3529. }
  3530. static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
  3531. {
  3532. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3533. struct btrfs_ioctl_scrub_args *sa;
  3534. int ret;
  3535. if (!capable(CAP_SYS_ADMIN))
  3536. return -EPERM;
  3537. sa = memdup_user(arg, sizeof(*sa));
  3538. if (IS_ERR(sa))
  3539. return PTR_ERR(sa);
  3540. if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
  3541. ret = mnt_want_write_file(file);
  3542. if (ret)
  3543. goto out;
  3544. }
  3545. ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
  3546. &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
  3547. 0);
  3548. if (copy_to_user(arg, sa, sizeof(*sa)))
  3549. ret = -EFAULT;
  3550. if (!(sa->flags & BTRFS_SCRUB_READONLY))
  3551. mnt_drop_write_file(file);
  3552. out:
  3553. kfree(sa);
  3554. return ret;
  3555. }
  3556. static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
  3557. {
  3558. if (!capable(CAP_SYS_ADMIN))
  3559. return -EPERM;
  3560. return btrfs_scrub_cancel(root->fs_info);
  3561. }
  3562. static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
  3563. void __user *arg)
  3564. {
  3565. struct btrfs_ioctl_scrub_args *sa;
  3566. int ret;
  3567. if (!capable(CAP_SYS_ADMIN))
  3568. return -EPERM;
  3569. sa = memdup_user(arg, sizeof(*sa));
  3570. if (IS_ERR(sa))
  3571. return PTR_ERR(sa);
  3572. ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
  3573. if (copy_to_user(arg, sa, sizeof(*sa)))
  3574. ret = -EFAULT;
  3575. kfree(sa);
  3576. return ret;
  3577. }
  3578. static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
  3579. void __user *arg)
  3580. {
  3581. struct btrfs_ioctl_get_dev_stats *sa;
  3582. int ret;
  3583. sa = memdup_user(arg, sizeof(*sa));
  3584. if (IS_ERR(sa))
  3585. return PTR_ERR(sa);
  3586. if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
  3587. kfree(sa);
  3588. return -EPERM;
  3589. }
  3590. ret = btrfs_get_dev_stats(root, sa);
  3591. if (copy_to_user(arg, sa, sizeof(*sa)))
  3592. ret = -EFAULT;
  3593. kfree(sa);
  3594. return ret;
  3595. }
  3596. static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
  3597. {
  3598. struct btrfs_ioctl_dev_replace_args *p;
  3599. int ret;
  3600. if (!capable(CAP_SYS_ADMIN))
  3601. return -EPERM;
  3602. p = memdup_user(arg, sizeof(*p));
  3603. if (IS_ERR(p))
  3604. return PTR_ERR(p);
  3605. switch (p->cmd) {
  3606. case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
  3607. if (root->fs_info->sb->s_flags & MS_RDONLY) {
  3608. ret = -EROFS;
  3609. goto out;
  3610. }
  3611. if (atomic_xchg(
  3612. &root->fs_info->mutually_exclusive_operation_running,
  3613. 1)) {
  3614. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  3615. } else {
  3616. ret = btrfs_dev_replace_start(root, p);
  3617. atomic_set(
  3618. &root->fs_info->mutually_exclusive_operation_running,
  3619. 0);
  3620. }
  3621. break;
  3622. case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
  3623. btrfs_dev_replace_status(root->fs_info, p);
  3624. ret = 0;
  3625. break;
  3626. case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
  3627. ret = btrfs_dev_replace_cancel(root->fs_info, p);
  3628. break;
  3629. default:
  3630. ret = -EINVAL;
  3631. break;
  3632. }
  3633. if (copy_to_user(arg, p, sizeof(*p)))
  3634. ret = -EFAULT;
  3635. out:
  3636. kfree(p);
  3637. return ret;
  3638. }
  3639. static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
  3640. {
  3641. int ret = 0;
  3642. int i;
  3643. u64 rel_ptr;
  3644. int size;
  3645. struct btrfs_ioctl_ino_path_args *ipa = NULL;
  3646. struct inode_fs_paths *ipath = NULL;
  3647. struct btrfs_path *path;
  3648. if (!capable(CAP_DAC_READ_SEARCH))
  3649. return -EPERM;
  3650. path = btrfs_alloc_path();
  3651. if (!path) {
  3652. ret = -ENOMEM;
  3653. goto out;
  3654. }
  3655. ipa = memdup_user(arg, sizeof(*ipa));
  3656. if (IS_ERR(ipa)) {
  3657. ret = PTR_ERR(ipa);
  3658. ipa = NULL;
  3659. goto out;
  3660. }
  3661. size = min_t(u32, ipa->size, 4096);
  3662. ipath = init_ipath(size, root, path);
  3663. if (IS_ERR(ipath)) {
  3664. ret = PTR_ERR(ipath);
  3665. ipath = NULL;
  3666. goto out;
  3667. }
  3668. ret = paths_from_inode(ipa->inum, ipath);
  3669. if (ret < 0)
  3670. goto out;
  3671. for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
  3672. rel_ptr = ipath->fspath->val[i] -
  3673. (u64)(unsigned long)ipath->fspath->val;
  3674. ipath->fspath->val[i] = rel_ptr;
  3675. }
  3676. ret = copy_to_user((void *)(unsigned long)ipa->fspath,
  3677. (void *)(unsigned long)ipath->fspath, size);
  3678. if (ret) {
  3679. ret = -EFAULT;
  3680. goto out;
  3681. }
  3682. out:
  3683. btrfs_free_path(path);
  3684. free_ipath(ipath);
  3685. kfree(ipa);
  3686. return ret;
  3687. }
  3688. static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
  3689. {
  3690. struct btrfs_data_container *inodes = ctx;
  3691. const size_t c = 3 * sizeof(u64);
  3692. if (inodes->bytes_left >= c) {
  3693. inodes->bytes_left -= c;
  3694. inodes->val[inodes->elem_cnt] = inum;
  3695. inodes->val[inodes->elem_cnt + 1] = offset;
  3696. inodes->val[inodes->elem_cnt + 2] = root;
  3697. inodes->elem_cnt += 3;
  3698. } else {
  3699. inodes->bytes_missing += c - inodes->bytes_left;
  3700. inodes->bytes_left = 0;
  3701. inodes->elem_missed += 3;
  3702. }
  3703. return 0;
  3704. }
  3705. static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
  3706. void __user *arg)
  3707. {
  3708. int ret = 0;
  3709. int size;
  3710. struct btrfs_ioctl_logical_ino_args *loi;
  3711. struct btrfs_data_container *inodes = NULL;
  3712. struct btrfs_path *path = NULL;
  3713. if (!capable(CAP_SYS_ADMIN))
  3714. return -EPERM;
  3715. loi = memdup_user(arg, sizeof(*loi));
  3716. if (IS_ERR(loi)) {
  3717. ret = PTR_ERR(loi);
  3718. loi = NULL;
  3719. goto out;
  3720. }
  3721. path = btrfs_alloc_path();
  3722. if (!path) {
  3723. ret = -ENOMEM;
  3724. goto out;
  3725. }
  3726. size = min_t(u32, loi->size, 64 * 1024);
  3727. inodes = init_data_container(size);
  3728. if (IS_ERR(inodes)) {
  3729. ret = PTR_ERR(inodes);
  3730. inodes = NULL;
  3731. goto out;
  3732. }
  3733. ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
  3734. build_ino_list, inodes);
  3735. if (ret == -EINVAL)
  3736. ret = -ENOENT;
  3737. if (ret < 0)
  3738. goto out;
  3739. ret = copy_to_user((void *)(unsigned long)loi->inodes,
  3740. (void *)(unsigned long)inodes, size);
  3741. if (ret)
  3742. ret = -EFAULT;
  3743. out:
  3744. btrfs_free_path(path);
  3745. vfree(inodes);
  3746. kfree(loi);
  3747. return ret;
  3748. }
  3749. void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
  3750. struct btrfs_ioctl_balance_args *bargs)
  3751. {
  3752. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  3753. bargs->flags = bctl->flags;
  3754. if (atomic_read(&fs_info->balance_running))
  3755. bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
  3756. if (atomic_read(&fs_info->balance_pause_req))
  3757. bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
  3758. if (atomic_read(&fs_info->balance_cancel_req))
  3759. bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
  3760. memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
  3761. memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
  3762. memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
  3763. if (lock) {
  3764. spin_lock(&fs_info->balance_lock);
  3765. memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
  3766. spin_unlock(&fs_info->balance_lock);
  3767. } else {
  3768. memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
  3769. }
  3770. }
  3771. static long btrfs_ioctl_balance(struct file *file, void __user *arg)
  3772. {
  3773. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3774. struct btrfs_fs_info *fs_info = root->fs_info;
  3775. struct btrfs_ioctl_balance_args *bargs;
  3776. struct btrfs_balance_control *bctl;
  3777. bool need_unlock; /* for mut. excl. ops lock */
  3778. int ret;
  3779. if (!capable(CAP_SYS_ADMIN))
  3780. return -EPERM;
  3781. ret = mnt_want_write_file(file);
  3782. if (ret)
  3783. return ret;
  3784. again:
  3785. if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
  3786. mutex_lock(&fs_info->volume_mutex);
  3787. mutex_lock(&fs_info->balance_mutex);
  3788. need_unlock = true;
  3789. goto locked;
  3790. }
  3791. /*
  3792. * mut. excl. ops lock is locked. Three possibilites:
  3793. * (1) some other op is running
  3794. * (2) balance is running
  3795. * (3) balance is paused -- special case (think resume)
  3796. */
  3797. mutex_lock(&fs_info->balance_mutex);
  3798. if (fs_info->balance_ctl) {
  3799. /* this is either (2) or (3) */
  3800. if (!atomic_read(&fs_info->balance_running)) {
  3801. mutex_unlock(&fs_info->balance_mutex);
  3802. if (!mutex_trylock(&fs_info->volume_mutex))
  3803. goto again;
  3804. mutex_lock(&fs_info->balance_mutex);
  3805. if (fs_info->balance_ctl &&
  3806. !atomic_read(&fs_info->balance_running)) {
  3807. /* this is (3) */
  3808. need_unlock = false;
  3809. goto locked;
  3810. }
  3811. mutex_unlock(&fs_info->balance_mutex);
  3812. mutex_unlock(&fs_info->volume_mutex);
  3813. goto again;
  3814. } else {
  3815. /* this is (2) */
  3816. mutex_unlock(&fs_info->balance_mutex);
  3817. ret = -EINPROGRESS;
  3818. goto out;
  3819. }
  3820. } else {
  3821. /* this is (1) */
  3822. mutex_unlock(&fs_info->balance_mutex);
  3823. ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
  3824. goto out;
  3825. }
  3826. locked:
  3827. BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
  3828. if (arg) {
  3829. bargs = memdup_user(arg, sizeof(*bargs));
  3830. if (IS_ERR(bargs)) {
  3831. ret = PTR_ERR(bargs);
  3832. goto out_unlock;
  3833. }
  3834. if (bargs->flags & BTRFS_BALANCE_RESUME) {
  3835. if (!fs_info->balance_ctl) {
  3836. ret = -ENOTCONN;
  3837. goto out_bargs;
  3838. }
  3839. bctl = fs_info->balance_ctl;
  3840. spin_lock(&fs_info->balance_lock);
  3841. bctl->flags |= BTRFS_BALANCE_RESUME;
  3842. spin_unlock(&fs_info->balance_lock);
  3843. goto do_balance;
  3844. }
  3845. } else {
  3846. bargs = NULL;
  3847. }
  3848. if (fs_info->balance_ctl) {
  3849. ret = -EINPROGRESS;
  3850. goto out_bargs;
  3851. }
  3852. bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
  3853. if (!bctl) {
  3854. ret = -ENOMEM;
  3855. goto out_bargs;
  3856. }
  3857. bctl->fs_info = fs_info;
  3858. if (arg) {
  3859. memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
  3860. memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
  3861. memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
  3862. bctl->flags = bargs->flags;
  3863. } else {
  3864. /* balance everything - no filters */
  3865. bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
  3866. }
  3867. do_balance:
  3868. /*
  3869. * Ownership of bctl and mutually_exclusive_operation_running
  3870. * goes to to btrfs_balance. bctl is freed in __cancel_balance,
  3871. * or, if restriper was paused all the way until unmount, in
  3872. * free_fs_info. mutually_exclusive_operation_running is
  3873. * cleared in __cancel_balance.
  3874. */
  3875. need_unlock = false;
  3876. ret = btrfs_balance(bctl, bargs);
  3877. if (arg) {
  3878. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  3879. ret = -EFAULT;
  3880. }
  3881. out_bargs:
  3882. kfree(bargs);
  3883. out_unlock:
  3884. mutex_unlock(&fs_info->balance_mutex);
  3885. mutex_unlock(&fs_info->volume_mutex);
  3886. if (need_unlock)
  3887. atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
  3888. out:
  3889. mnt_drop_write_file(file);
  3890. return ret;
  3891. }
  3892. static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
  3893. {
  3894. if (!capable(CAP_SYS_ADMIN))
  3895. return -EPERM;
  3896. switch (cmd) {
  3897. case BTRFS_BALANCE_CTL_PAUSE:
  3898. return btrfs_pause_balance(root->fs_info);
  3899. case BTRFS_BALANCE_CTL_CANCEL:
  3900. return btrfs_cancel_balance(root->fs_info);
  3901. }
  3902. return -EINVAL;
  3903. }
  3904. static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
  3905. void __user *arg)
  3906. {
  3907. struct btrfs_fs_info *fs_info = root->fs_info;
  3908. struct btrfs_ioctl_balance_args *bargs;
  3909. int ret = 0;
  3910. if (!capable(CAP_SYS_ADMIN))
  3911. return -EPERM;
  3912. mutex_lock(&fs_info->balance_mutex);
  3913. if (!fs_info->balance_ctl) {
  3914. ret = -ENOTCONN;
  3915. goto out;
  3916. }
  3917. bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
  3918. if (!bargs) {
  3919. ret = -ENOMEM;
  3920. goto out;
  3921. }
  3922. update_ioctl_balance_args(fs_info, 1, bargs);
  3923. if (copy_to_user(arg, bargs, sizeof(*bargs)))
  3924. ret = -EFAULT;
  3925. kfree(bargs);
  3926. out:
  3927. mutex_unlock(&fs_info->balance_mutex);
  3928. return ret;
  3929. }
  3930. static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
  3931. {
  3932. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3933. struct btrfs_ioctl_quota_ctl_args *sa;
  3934. struct btrfs_trans_handle *trans = NULL;
  3935. int ret;
  3936. int err;
  3937. if (!capable(CAP_SYS_ADMIN))
  3938. return -EPERM;
  3939. ret = mnt_want_write_file(file);
  3940. if (ret)
  3941. return ret;
  3942. sa = memdup_user(arg, sizeof(*sa));
  3943. if (IS_ERR(sa)) {
  3944. ret = PTR_ERR(sa);
  3945. goto drop_write;
  3946. }
  3947. down_write(&root->fs_info->subvol_sem);
  3948. trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
  3949. if (IS_ERR(trans)) {
  3950. ret = PTR_ERR(trans);
  3951. goto out;
  3952. }
  3953. switch (sa->cmd) {
  3954. case BTRFS_QUOTA_CTL_ENABLE:
  3955. ret = btrfs_quota_enable(trans, root->fs_info);
  3956. break;
  3957. case BTRFS_QUOTA_CTL_DISABLE:
  3958. ret = btrfs_quota_disable(trans, root->fs_info);
  3959. break;
  3960. default:
  3961. ret = -EINVAL;
  3962. break;
  3963. }
  3964. err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
  3965. if (err && !ret)
  3966. ret = err;
  3967. out:
  3968. kfree(sa);
  3969. up_write(&root->fs_info->subvol_sem);
  3970. drop_write:
  3971. mnt_drop_write_file(file);
  3972. return ret;
  3973. }
  3974. static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
  3975. {
  3976. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  3977. struct btrfs_ioctl_qgroup_assign_args *sa;
  3978. struct btrfs_trans_handle *trans;
  3979. int ret;
  3980. int err;
  3981. if (!capable(CAP_SYS_ADMIN))
  3982. return -EPERM;
  3983. ret = mnt_want_write_file(file);
  3984. if (ret)
  3985. return ret;
  3986. sa = memdup_user(arg, sizeof(*sa));
  3987. if (IS_ERR(sa)) {
  3988. ret = PTR_ERR(sa);
  3989. goto drop_write;
  3990. }
  3991. trans = btrfs_join_transaction(root);
  3992. if (IS_ERR(trans)) {
  3993. ret = PTR_ERR(trans);
  3994. goto out;
  3995. }
  3996. /* FIXME: check if the IDs really exist */
  3997. if (sa->assign) {
  3998. ret = btrfs_add_qgroup_relation(trans, root->fs_info,
  3999. sa->src, sa->dst);
  4000. } else {
  4001. ret = btrfs_del_qgroup_relation(trans, root->fs_info,
  4002. sa->src, sa->dst);
  4003. }
  4004. err = btrfs_end_transaction(trans, root);
  4005. if (err && !ret)
  4006. ret = err;
  4007. out:
  4008. kfree(sa);
  4009. drop_write:
  4010. mnt_drop_write_file(file);
  4011. return ret;
  4012. }
  4013. static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
  4014. {
  4015. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4016. struct btrfs_ioctl_qgroup_create_args *sa;
  4017. struct btrfs_trans_handle *trans;
  4018. int ret;
  4019. int err;
  4020. if (!capable(CAP_SYS_ADMIN))
  4021. return -EPERM;
  4022. ret = mnt_want_write_file(file);
  4023. if (ret)
  4024. return ret;
  4025. sa = memdup_user(arg, sizeof(*sa));
  4026. if (IS_ERR(sa)) {
  4027. ret = PTR_ERR(sa);
  4028. goto drop_write;
  4029. }
  4030. if (!sa->qgroupid) {
  4031. ret = -EINVAL;
  4032. goto out;
  4033. }
  4034. trans = btrfs_join_transaction(root);
  4035. if (IS_ERR(trans)) {
  4036. ret = PTR_ERR(trans);
  4037. goto out;
  4038. }
  4039. /* FIXME: check if the IDs really exist */
  4040. if (sa->create) {
  4041. ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
  4042. NULL);
  4043. } else {
  4044. ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
  4045. }
  4046. err = btrfs_end_transaction(trans, root);
  4047. if (err && !ret)
  4048. ret = err;
  4049. out:
  4050. kfree(sa);
  4051. drop_write:
  4052. mnt_drop_write_file(file);
  4053. return ret;
  4054. }
  4055. static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
  4056. {
  4057. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4058. struct btrfs_ioctl_qgroup_limit_args *sa;
  4059. struct btrfs_trans_handle *trans;
  4060. int ret;
  4061. int err;
  4062. u64 qgroupid;
  4063. if (!capable(CAP_SYS_ADMIN))
  4064. return -EPERM;
  4065. ret = mnt_want_write_file(file);
  4066. if (ret)
  4067. return ret;
  4068. sa = memdup_user(arg, sizeof(*sa));
  4069. if (IS_ERR(sa)) {
  4070. ret = PTR_ERR(sa);
  4071. goto drop_write;
  4072. }
  4073. trans = btrfs_join_transaction(root);
  4074. if (IS_ERR(trans)) {
  4075. ret = PTR_ERR(trans);
  4076. goto out;
  4077. }
  4078. qgroupid = sa->qgroupid;
  4079. if (!qgroupid) {
  4080. /* take the current subvol as qgroup */
  4081. qgroupid = root->root_key.objectid;
  4082. }
  4083. /* FIXME: check if the IDs really exist */
  4084. ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
  4085. err = btrfs_end_transaction(trans, root);
  4086. if (err && !ret)
  4087. ret = err;
  4088. out:
  4089. kfree(sa);
  4090. drop_write:
  4091. mnt_drop_write_file(file);
  4092. return ret;
  4093. }
  4094. static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
  4095. {
  4096. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4097. struct btrfs_ioctl_quota_rescan_args *qsa;
  4098. int ret;
  4099. if (!capable(CAP_SYS_ADMIN))
  4100. return -EPERM;
  4101. ret = mnt_want_write_file(file);
  4102. if (ret)
  4103. return ret;
  4104. qsa = memdup_user(arg, sizeof(*qsa));
  4105. if (IS_ERR(qsa)) {
  4106. ret = PTR_ERR(qsa);
  4107. goto drop_write;
  4108. }
  4109. if (qsa->flags) {
  4110. ret = -EINVAL;
  4111. goto out;
  4112. }
  4113. ret = btrfs_qgroup_rescan(root->fs_info);
  4114. out:
  4115. kfree(qsa);
  4116. drop_write:
  4117. mnt_drop_write_file(file);
  4118. return ret;
  4119. }
  4120. static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
  4121. {
  4122. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4123. struct btrfs_ioctl_quota_rescan_args *qsa;
  4124. int ret = 0;
  4125. if (!capable(CAP_SYS_ADMIN))
  4126. return -EPERM;
  4127. qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
  4128. if (!qsa)
  4129. return -ENOMEM;
  4130. if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
  4131. qsa->flags = 1;
  4132. qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
  4133. }
  4134. if (copy_to_user(arg, qsa, sizeof(*qsa)))
  4135. ret = -EFAULT;
  4136. kfree(qsa);
  4137. return ret;
  4138. }
  4139. static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
  4140. {
  4141. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4142. if (!capable(CAP_SYS_ADMIN))
  4143. return -EPERM;
  4144. return btrfs_qgroup_wait_for_completion(root->fs_info);
  4145. }
  4146. static long _btrfs_ioctl_set_received_subvol(struct file *file,
  4147. struct btrfs_ioctl_received_subvol_args *sa)
  4148. {
  4149. struct inode *inode = file_inode(file);
  4150. struct btrfs_root *root = BTRFS_I(inode)->root;
  4151. struct btrfs_root_item *root_item = &root->root_item;
  4152. struct btrfs_trans_handle *trans;
  4153. struct timespec ct = CURRENT_TIME;
  4154. int ret = 0;
  4155. int received_uuid_changed;
  4156. if (!inode_owner_or_capable(inode))
  4157. return -EPERM;
  4158. ret = mnt_want_write_file(file);
  4159. if (ret < 0)
  4160. return ret;
  4161. down_write(&root->fs_info->subvol_sem);
  4162. if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
  4163. ret = -EINVAL;
  4164. goto out;
  4165. }
  4166. if (btrfs_root_readonly(root)) {
  4167. ret = -EROFS;
  4168. goto out;
  4169. }
  4170. /*
  4171. * 1 - root item
  4172. * 2 - uuid items (received uuid + subvol uuid)
  4173. */
  4174. trans = btrfs_start_transaction(root, 3);
  4175. if (IS_ERR(trans)) {
  4176. ret = PTR_ERR(trans);
  4177. trans = NULL;
  4178. goto out;
  4179. }
  4180. sa->rtransid = trans->transid;
  4181. sa->rtime.sec = ct.tv_sec;
  4182. sa->rtime.nsec = ct.tv_nsec;
  4183. received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
  4184. BTRFS_UUID_SIZE);
  4185. if (received_uuid_changed &&
  4186. !btrfs_is_empty_uuid(root_item->received_uuid))
  4187. btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
  4188. root_item->received_uuid,
  4189. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  4190. root->root_key.objectid);
  4191. memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
  4192. btrfs_set_root_stransid(root_item, sa->stransid);
  4193. btrfs_set_root_rtransid(root_item, sa->rtransid);
  4194. btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
  4195. btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
  4196. btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
  4197. btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
  4198. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  4199. &root->root_key, &root->root_item);
  4200. if (ret < 0) {
  4201. btrfs_end_transaction(trans, root);
  4202. goto out;
  4203. }
  4204. if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
  4205. ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
  4206. sa->uuid,
  4207. BTRFS_UUID_KEY_RECEIVED_SUBVOL,
  4208. root->root_key.objectid);
  4209. if (ret < 0 && ret != -EEXIST) {
  4210. btrfs_abort_transaction(trans, root, ret);
  4211. goto out;
  4212. }
  4213. }
  4214. ret = btrfs_commit_transaction(trans, root);
  4215. if (ret < 0) {
  4216. btrfs_abort_transaction(trans, root, ret);
  4217. goto out;
  4218. }
  4219. out:
  4220. up_write(&root->fs_info->subvol_sem);
  4221. mnt_drop_write_file(file);
  4222. return ret;
  4223. }
  4224. #ifdef CONFIG_64BIT
  4225. static long btrfs_ioctl_set_received_subvol_32(struct file *file,
  4226. void __user *arg)
  4227. {
  4228. struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
  4229. struct btrfs_ioctl_received_subvol_args *args64 = NULL;
  4230. int ret = 0;
  4231. args32 = memdup_user(arg, sizeof(*args32));
  4232. if (IS_ERR(args32)) {
  4233. ret = PTR_ERR(args32);
  4234. args32 = NULL;
  4235. goto out;
  4236. }
  4237. args64 = kmalloc(sizeof(*args64), GFP_NOFS);
  4238. if (!args64) {
  4239. ret = -ENOMEM;
  4240. goto out;
  4241. }
  4242. memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
  4243. args64->stransid = args32->stransid;
  4244. args64->rtransid = args32->rtransid;
  4245. args64->stime.sec = args32->stime.sec;
  4246. args64->stime.nsec = args32->stime.nsec;
  4247. args64->rtime.sec = args32->rtime.sec;
  4248. args64->rtime.nsec = args32->rtime.nsec;
  4249. args64->flags = args32->flags;
  4250. ret = _btrfs_ioctl_set_received_subvol(file, args64);
  4251. if (ret)
  4252. goto out;
  4253. memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
  4254. args32->stransid = args64->stransid;
  4255. args32->rtransid = args64->rtransid;
  4256. args32->stime.sec = args64->stime.sec;
  4257. args32->stime.nsec = args64->stime.nsec;
  4258. args32->rtime.sec = args64->rtime.sec;
  4259. args32->rtime.nsec = args64->rtime.nsec;
  4260. args32->flags = args64->flags;
  4261. ret = copy_to_user(arg, args32, sizeof(*args32));
  4262. if (ret)
  4263. ret = -EFAULT;
  4264. out:
  4265. kfree(args32);
  4266. kfree(args64);
  4267. return ret;
  4268. }
  4269. #endif
  4270. static long btrfs_ioctl_set_received_subvol(struct file *file,
  4271. void __user *arg)
  4272. {
  4273. struct btrfs_ioctl_received_subvol_args *sa = NULL;
  4274. int ret = 0;
  4275. sa = memdup_user(arg, sizeof(*sa));
  4276. if (IS_ERR(sa)) {
  4277. ret = PTR_ERR(sa);
  4278. sa = NULL;
  4279. goto out;
  4280. }
  4281. ret = _btrfs_ioctl_set_received_subvol(file, sa);
  4282. if (ret)
  4283. goto out;
  4284. ret = copy_to_user(arg, sa, sizeof(*sa));
  4285. if (ret)
  4286. ret = -EFAULT;
  4287. out:
  4288. kfree(sa);
  4289. return ret;
  4290. }
  4291. static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
  4292. {
  4293. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4294. size_t len;
  4295. int ret;
  4296. char label[BTRFS_LABEL_SIZE];
  4297. spin_lock(&root->fs_info->super_lock);
  4298. memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
  4299. spin_unlock(&root->fs_info->super_lock);
  4300. len = strnlen(label, BTRFS_LABEL_SIZE);
  4301. if (len == BTRFS_LABEL_SIZE) {
  4302. btrfs_warn(root->fs_info,
  4303. "label is too long, return the first %zu bytes", --len);
  4304. }
  4305. ret = copy_to_user(arg, label, len);
  4306. return ret ? -EFAULT : 0;
  4307. }
  4308. static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
  4309. {
  4310. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4311. struct btrfs_super_block *super_block = root->fs_info->super_copy;
  4312. struct btrfs_trans_handle *trans;
  4313. char label[BTRFS_LABEL_SIZE];
  4314. int ret;
  4315. if (!capable(CAP_SYS_ADMIN))
  4316. return -EPERM;
  4317. if (copy_from_user(label, arg, sizeof(label)))
  4318. return -EFAULT;
  4319. if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
  4320. btrfs_err(root->fs_info, "unable to set label with more than %d bytes",
  4321. BTRFS_LABEL_SIZE - 1);
  4322. return -EINVAL;
  4323. }
  4324. ret = mnt_want_write_file(file);
  4325. if (ret)
  4326. return ret;
  4327. trans = btrfs_start_transaction(root, 0);
  4328. if (IS_ERR(trans)) {
  4329. ret = PTR_ERR(trans);
  4330. goto out_unlock;
  4331. }
  4332. spin_lock(&root->fs_info->super_lock);
  4333. strcpy(super_block->label, label);
  4334. spin_unlock(&root->fs_info->super_lock);
  4335. ret = btrfs_commit_transaction(trans, root);
  4336. out_unlock:
  4337. mnt_drop_write_file(file);
  4338. return ret;
  4339. }
  4340. #define INIT_FEATURE_FLAGS(suffix) \
  4341. { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
  4342. .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
  4343. .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
  4344. static int btrfs_ioctl_get_supported_features(struct file *file,
  4345. void __user *arg)
  4346. {
  4347. static struct btrfs_ioctl_feature_flags features[3] = {
  4348. INIT_FEATURE_FLAGS(SUPP),
  4349. INIT_FEATURE_FLAGS(SAFE_SET),
  4350. INIT_FEATURE_FLAGS(SAFE_CLEAR)
  4351. };
  4352. if (copy_to_user(arg, &features, sizeof(features)))
  4353. return -EFAULT;
  4354. return 0;
  4355. }
  4356. static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
  4357. {
  4358. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4359. struct btrfs_super_block *super_block = root->fs_info->super_copy;
  4360. struct btrfs_ioctl_feature_flags features;
  4361. features.compat_flags = btrfs_super_compat_flags(super_block);
  4362. features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
  4363. features.incompat_flags = btrfs_super_incompat_flags(super_block);
  4364. if (copy_to_user(arg, &features, sizeof(features)))
  4365. return -EFAULT;
  4366. return 0;
  4367. }
  4368. static int check_feature_bits(struct btrfs_root *root,
  4369. enum btrfs_feature_set set,
  4370. u64 change_mask, u64 flags, u64 supported_flags,
  4371. u64 safe_set, u64 safe_clear)
  4372. {
  4373. const char *type = btrfs_feature_set_names[set];
  4374. char *names;
  4375. u64 disallowed, unsupported;
  4376. u64 set_mask = flags & change_mask;
  4377. u64 clear_mask = ~flags & change_mask;
  4378. unsupported = set_mask & ~supported_flags;
  4379. if (unsupported) {
  4380. names = btrfs_printable_features(set, unsupported);
  4381. if (names) {
  4382. btrfs_warn(root->fs_info,
  4383. "this kernel does not support the %s feature bit%s",
  4384. names, strchr(names, ',') ? "s" : "");
  4385. kfree(names);
  4386. } else
  4387. btrfs_warn(root->fs_info,
  4388. "this kernel does not support %s bits 0x%llx",
  4389. type, unsupported);
  4390. return -EOPNOTSUPP;
  4391. }
  4392. disallowed = set_mask & ~safe_set;
  4393. if (disallowed) {
  4394. names = btrfs_printable_features(set, disallowed);
  4395. if (names) {
  4396. btrfs_warn(root->fs_info,
  4397. "can't set the %s feature bit%s while mounted",
  4398. names, strchr(names, ',') ? "s" : "");
  4399. kfree(names);
  4400. } else
  4401. btrfs_warn(root->fs_info,
  4402. "can't set %s bits 0x%llx while mounted",
  4403. type, disallowed);
  4404. return -EPERM;
  4405. }
  4406. disallowed = clear_mask & ~safe_clear;
  4407. if (disallowed) {
  4408. names = btrfs_printable_features(set, disallowed);
  4409. if (names) {
  4410. btrfs_warn(root->fs_info,
  4411. "can't clear the %s feature bit%s while mounted",
  4412. names, strchr(names, ',') ? "s" : "");
  4413. kfree(names);
  4414. } else
  4415. btrfs_warn(root->fs_info,
  4416. "can't clear %s bits 0x%llx while mounted",
  4417. type, disallowed);
  4418. return -EPERM;
  4419. }
  4420. return 0;
  4421. }
  4422. #define check_feature(root, change_mask, flags, mask_base) \
  4423. check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \
  4424. BTRFS_FEATURE_ ## mask_base ## _SUPP, \
  4425. BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
  4426. BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
  4427. static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
  4428. {
  4429. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4430. struct btrfs_super_block *super_block = root->fs_info->super_copy;
  4431. struct btrfs_ioctl_feature_flags flags[2];
  4432. struct btrfs_trans_handle *trans;
  4433. u64 newflags;
  4434. int ret;
  4435. if (!capable(CAP_SYS_ADMIN))
  4436. return -EPERM;
  4437. if (copy_from_user(flags, arg, sizeof(flags)))
  4438. return -EFAULT;
  4439. /* Nothing to do */
  4440. if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
  4441. !flags[0].incompat_flags)
  4442. return 0;
  4443. ret = check_feature(root, flags[0].compat_flags,
  4444. flags[1].compat_flags, COMPAT);
  4445. if (ret)
  4446. return ret;
  4447. ret = check_feature(root, flags[0].compat_ro_flags,
  4448. flags[1].compat_ro_flags, COMPAT_RO);
  4449. if (ret)
  4450. return ret;
  4451. ret = check_feature(root, flags[0].incompat_flags,
  4452. flags[1].incompat_flags, INCOMPAT);
  4453. if (ret)
  4454. return ret;
  4455. trans = btrfs_start_transaction(root, 0);
  4456. if (IS_ERR(trans))
  4457. return PTR_ERR(trans);
  4458. spin_lock(&root->fs_info->super_lock);
  4459. newflags = btrfs_super_compat_flags(super_block);
  4460. newflags |= flags[0].compat_flags & flags[1].compat_flags;
  4461. newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
  4462. btrfs_set_super_compat_flags(super_block, newflags);
  4463. newflags = btrfs_super_compat_ro_flags(super_block);
  4464. newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
  4465. newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
  4466. btrfs_set_super_compat_ro_flags(super_block, newflags);
  4467. newflags = btrfs_super_incompat_flags(super_block);
  4468. newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
  4469. newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
  4470. btrfs_set_super_incompat_flags(super_block, newflags);
  4471. spin_unlock(&root->fs_info->super_lock);
  4472. return btrfs_commit_transaction(trans, root);
  4473. }
  4474. long btrfs_ioctl(struct file *file, unsigned int
  4475. cmd, unsigned long arg)
  4476. {
  4477. struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
  4478. void __user *argp = (void __user *)arg;
  4479. switch (cmd) {
  4480. case FS_IOC_GETFLAGS:
  4481. return btrfs_ioctl_getflags(file, argp);
  4482. case FS_IOC_SETFLAGS:
  4483. return btrfs_ioctl_setflags(file, argp);
  4484. case FS_IOC_GETVERSION:
  4485. return btrfs_ioctl_getversion(file, argp);
  4486. case FITRIM:
  4487. return btrfs_ioctl_fitrim(file, argp);
  4488. case BTRFS_IOC_SNAP_CREATE:
  4489. return btrfs_ioctl_snap_create(file, argp, 0);
  4490. case BTRFS_IOC_SNAP_CREATE_V2:
  4491. return btrfs_ioctl_snap_create_v2(file, argp, 0);
  4492. case BTRFS_IOC_SUBVOL_CREATE:
  4493. return btrfs_ioctl_snap_create(file, argp, 1);
  4494. case BTRFS_IOC_SUBVOL_CREATE_V2:
  4495. return btrfs_ioctl_snap_create_v2(file, argp, 1);
  4496. case BTRFS_IOC_SNAP_DESTROY:
  4497. return btrfs_ioctl_snap_destroy(file, argp);
  4498. case BTRFS_IOC_SUBVOL_GETFLAGS:
  4499. return btrfs_ioctl_subvol_getflags(file, argp);
  4500. case BTRFS_IOC_SUBVOL_SETFLAGS:
  4501. return btrfs_ioctl_subvol_setflags(file, argp);
  4502. case BTRFS_IOC_DEFAULT_SUBVOL:
  4503. return btrfs_ioctl_default_subvol(file, argp);
  4504. case BTRFS_IOC_DEFRAG:
  4505. return btrfs_ioctl_defrag(file, NULL);
  4506. case BTRFS_IOC_DEFRAG_RANGE:
  4507. return btrfs_ioctl_defrag(file, argp);
  4508. case BTRFS_IOC_RESIZE:
  4509. return btrfs_ioctl_resize(file, argp);
  4510. case BTRFS_IOC_ADD_DEV:
  4511. return btrfs_ioctl_add_dev(root, argp);
  4512. case BTRFS_IOC_RM_DEV:
  4513. return btrfs_ioctl_rm_dev(file, argp);
  4514. case BTRFS_IOC_FS_INFO:
  4515. return btrfs_ioctl_fs_info(root, argp);
  4516. case BTRFS_IOC_DEV_INFO:
  4517. return btrfs_ioctl_dev_info(root, argp);
  4518. case BTRFS_IOC_BALANCE:
  4519. return btrfs_ioctl_balance(file, NULL);
  4520. case BTRFS_IOC_CLONE:
  4521. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  4522. case BTRFS_IOC_CLONE_RANGE:
  4523. return btrfs_ioctl_clone_range(file, argp);
  4524. case BTRFS_IOC_TRANS_START:
  4525. return btrfs_ioctl_trans_start(file);
  4526. case BTRFS_IOC_TRANS_END:
  4527. return btrfs_ioctl_trans_end(file);
  4528. case BTRFS_IOC_TREE_SEARCH:
  4529. return btrfs_ioctl_tree_search(file, argp);
  4530. case BTRFS_IOC_TREE_SEARCH_V2:
  4531. return btrfs_ioctl_tree_search_v2(file, argp);
  4532. case BTRFS_IOC_INO_LOOKUP:
  4533. return btrfs_ioctl_ino_lookup(file, argp);
  4534. case BTRFS_IOC_INO_PATHS:
  4535. return btrfs_ioctl_ino_to_path(root, argp);
  4536. case BTRFS_IOC_LOGICAL_INO:
  4537. return btrfs_ioctl_logical_to_ino(root, argp);
  4538. case BTRFS_IOC_SPACE_INFO:
  4539. return btrfs_ioctl_space_info(root, argp);
  4540. case BTRFS_IOC_SYNC: {
  4541. int ret;
  4542. ret = btrfs_start_delalloc_roots(root->fs_info, 0, -1);
  4543. if (ret)
  4544. return ret;
  4545. ret = btrfs_sync_fs(file->f_dentry->d_sb, 1);
  4546. /*
  4547. * The transaction thread may want to do more work,
  4548. * namely it pokes the cleaner ktread that will start
  4549. * processing uncleaned subvols.
  4550. */
  4551. wake_up_process(root->fs_info->transaction_kthread);
  4552. return ret;
  4553. }
  4554. case BTRFS_IOC_START_SYNC:
  4555. return btrfs_ioctl_start_sync(root, argp);
  4556. case BTRFS_IOC_WAIT_SYNC:
  4557. return btrfs_ioctl_wait_sync(root, argp);
  4558. case BTRFS_IOC_SCRUB:
  4559. return btrfs_ioctl_scrub(file, argp);
  4560. case BTRFS_IOC_SCRUB_CANCEL:
  4561. return btrfs_ioctl_scrub_cancel(root, argp);
  4562. case BTRFS_IOC_SCRUB_PROGRESS:
  4563. return btrfs_ioctl_scrub_progress(root, argp);
  4564. case BTRFS_IOC_BALANCE_V2:
  4565. return btrfs_ioctl_balance(file, argp);
  4566. case BTRFS_IOC_BALANCE_CTL:
  4567. return btrfs_ioctl_balance_ctl(root, arg);
  4568. case BTRFS_IOC_BALANCE_PROGRESS:
  4569. return btrfs_ioctl_balance_progress(root, argp);
  4570. case BTRFS_IOC_SET_RECEIVED_SUBVOL:
  4571. return btrfs_ioctl_set_received_subvol(file, argp);
  4572. #ifdef CONFIG_64BIT
  4573. case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
  4574. return btrfs_ioctl_set_received_subvol_32(file, argp);
  4575. #endif
  4576. case BTRFS_IOC_SEND:
  4577. return btrfs_ioctl_send(file, argp);
  4578. case BTRFS_IOC_GET_DEV_STATS:
  4579. return btrfs_ioctl_get_dev_stats(root, argp);
  4580. case BTRFS_IOC_QUOTA_CTL:
  4581. return btrfs_ioctl_quota_ctl(file, argp);
  4582. case BTRFS_IOC_QGROUP_ASSIGN:
  4583. return btrfs_ioctl_qgroup_assign(file, argp);
  4584. case BTRFS_IOC_QGROUP_CREATE:
  4585. return btrfs_ioctl_qgroup_create(file, argp);
  4586. case BTRFS_IOC_QGROUP_LIMIT:
  4587. return btrfs_ioctl_qgroup_limit(file, argp);
  4588. case BTRFS_IOC_QUOTA_RESCAN:
  4589. return btrfs_ioctl_quota_rescan(file, argp);
  4590. case BTRFS_IOC_QUOTA_RESCAN_STATUS:
  4591. return btrfs_ioctl_quota_rescan_status(file, argp);
  4592. case BTRFS_IOC_QUOTA_RESCAN_WAIT:
  4593. return btrfs_ioctl_quota_rescan_wait(file, argp);
  4594. case BTRFS_IOC_DEV_REPLACE:
  4595. return btrfs_ioctl_dev_replace(root, argp);
  4596. case BTRFS_IOC_GET_FSLABEL:
  4597. return btrfs_ioctl_get_fslabel(file, argp);
  4598. case BTRFS_IOC_SET_FSLABEL:
  4599. return btrfs_ioctl_set_fslabel(file, argp);
  4600. case BTRFS_IOC_FILE_EXTENT_SAME:
  4601. return btrfs_ioctl_file_extent_same(file, argp);
  4602. case BTRFS_IOC_GET_SUPPORTED_FEATURES:
  4603. return btrfs_ioctl_get_supported_features(file, argp);
  4604. case BTRFS_IOC_GET_FEATURES:
  4605. return btrfs_ioctl_get_features(file, argp);
  4606. case BTRFS_IOC_SET_FEATURES:
  4607. return btrfs_ioctl_set_features(file, argp);
  4608. }
  4609. return -ENOTTY;
  4610. }