ioctl.c 138 KB

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