ioctl.c 137 KB

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