ioctl.c 135 KB

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