ioctl.c 133 KB

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