inode.c 142 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297
  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/pagemap.h>
  24. #include <linux/highmem.h>
  25. #include <linux/time.h>
  26. #include <linux/init.h>
  27. #include <linux/string.h>
  28. #include <linux/smp_lock.h>
  29. #include <linux/backing-dev.h>
  30. #include <linux/mpage.h>
  31. #include <linux/swap.h>
  32. #include <linux/writeback.h>
  33. #include <linux/statfs.h>
  34. #include <linux/compat.h>
  35. #include <linux/bit_spinlock.h>
  36. #include <linux/xattr.h>
  37. #include <linux/posix_acl.h>
  38. #include <linux/falloc.h>
  39. #include "compat.h"
  40. #include "ctree.h"
  41. #include "disk-io.h"
  42. #include "transaction.h"
  43. #include "btrfs_inode.h"
  44. #include "ioctl.h"
  45. #include "print-tree.h"
  46. #include "volumes.h"
  47. #include "ordered-data.h"
  48. #include "xattr.h"
  49. #include "tree-log.h"
  50. #include "ref-cache.h"
  51. #include "compression.h"
  52. #include "locking.h"
  53. struct btrfs_iget_args {
  54. u64 ino;
  55. struct btrfs_root *root;
  56. };
  57. static struct inode_operations btrfs_dir_inode_operations;
  58. static struct inode_operations btrfs_symlink_inode_operations;
  59. static struct inode_operations btrfs_dir_ro_inode_operations;
  60. static struct inode_operations btrfs_special_inode_operations;
  61. static struct inode_operations btrfs_file_inode_operations;
  62. static struct address_space_operations btrfs_aops;
  63. static struct address_space_operations btrfs_symlink_aops;
  64. static struct file_operations btrfs_dir_file_operations;
  65. static struct extent_io_ops btrfs_extent_io_ops;
  66. static struct kmem_cache *btrfs_inode_cachep;
  67. struct kmem_cache *btrfs_trans_handle_cachep;
  68. struct kmem_cache *btrfs_transaction_cachep;
  69. struct kmem_cache *btrfs_path_cachep;
  70. #define S_SHIFT 12
  71. static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
  72. [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
  73. [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
  74. [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
  75. [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
  76. [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
  77. [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
  78. [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
  79. };
  80. static void btrfs_truncate(struct inode *inode);
  81. static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
  82. static noinline int cow_file_range(struct inode *inode,
  83. struct page *locked_page,
  84. u64 start, u64 end, int *page_started,
  85. unsigned long *nr_written, int unlock);
  86. static int btrfs_init_inode_security(struct inode *inode, struct inode *dir)
  87. {
  88. int err;
  89. err = btrfs_init_acl(inode, dir);
  90. if (!err)
  91. err = btrfs_xattr_security_init(inode, dir);
  92. return err;
  93. }
  94. /*
  95. * this does all the hard work for inserting an inline extent into
  96. * the btree. The caller should have done a btrfs_drop_extents so that
  97. * no overlapping inline items exist in the btree
  98. */
  99. static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
  100. struct btrfs_root *root, struct inode *inode,
  101. u64 start, size_t size, size_t compressed_size,
  102. struct page **compressed_pages)
  103. {
  104. struct btrfs_key key;
  105. struct btrfs_path *path;
  106. struct extent_buffer *leaf;
  107. struct page *page = NULL;
  108. char *kaddr;
  109. unsigned long ptr;
  110. struct btrfs_file_extent_item *ei;
  111. int err = 0;
  112. int ret;
  113. size_t cur_size = size;
  114. size_t datasize;
  115. unsigned long offset;
  116. int use_compress = 0;
  117. if (compressed_size && compressed_pages) {
  118. use_compress = 1;
  119. cur_size = compressed_size;
  120. }
  121. path = btrfs_alloc_path();
  122. if (!path)
  123. return -ENOMEM;
  124. path->leave_spinning = 1;
  125. btrfs_set_trans_block_group(trans, inode);
  126. key.objectid = inode->i_ino;
  127. key.offset = start;
  128. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  129. datasize = btrfs_file_extent_calc_inline_size(cur_size);
  130. inode_add_bytes(inode, size);
  131. ret = btrfs_insert_empty_item(trans, root, path, &key,
  132. datasize);
  133. BUG_ON(ret);
  134. if (ret) {
  135. err = ret;
  136. goto fail;
  137. }
  138. leaf = path->nodes[0];
  139. ei = btrfs_item_ptr(leaf, path->slots[0],
  140. struct btrfs_file_extent_item);
  141. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  142. btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
  143. btrfs_set_file_extent_encryption(leaf, ei, 0);
  144. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  145. btrfs_set_file_extent_ram_bytes(leaf, ei, size);
  146. ptr = btrfs_file_extent_inline_start(ei);
  147. if (use_compress) {
  148. struct page *cpage;
  149. int i = 0;
  150. while (compressed_size > 0) {
  151. cpage = compressed_pages[i];
  152. cur_size = min_t(unsigned long, compressed_size,
  153. PAGE_CACHE_SIZE);
  154. kaddr = kmap_atomic(cpage, KM_USER0);
  155. write_extent_buffer(leaf, kaddr, ptr, cur_size);
  156. kunmap_atomic(kaddr, KM_USER0);
  157. i++;
  158. ptr += cur_size;
  159. compressed_size -= cur_size;
  160. }
  161. btrfs_set_file_extent_compression(leaf, ei,
  162. BTRFS_COMPRESS_ZLIB);
  163. } else {
  164. page = find_get_page(inode->i_mapping,
  165. start >> PAGE_CACHE_SHIFT);
  166. btrfs_set_file_extent_compression(leaf, ei, 0);
  167. kaddr = kmap_atomic(page, KM_USER0);
  168. offset = start & (PAGE_CACHE_SIZE - 1);
  169. write_extent_buffer(leaf, kaddr + offset, ptr, size);
  170. kunmap_atomic(kaddr, KM_USER0);
  171. page_cache_release(page);
  172. }
  173. btrfs_mark_buffer_dirty(leaf);
  174. btrfs_free_path(path);
  175. BTRFS_I(inode)->disk_i_size = inode->i_size;
  176. btrfs_update_inode(trans, root, inode);
  177. return 0;
  178. fail:
  179. btrfs_free_path(path);
  180. return err;
  181. }
  182. /*
  183. * conditionally insert an inline extent into the file. This
  184. * does the checks required to make sure the data is small enough
  185. * to fit as an inline extent.
  186. */
  187. static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
  188. struct btrfs_root *root,
  189. struct inode *inode, u64 start, u64 end,
  190. size_t compressed_size,
  191. struct page **compressed_pages)
  192. {
  193. u64 isize = i_size_read(inode);
  194. u64 actual_end = min(end + 1, isize);
  195. u64 inline_len = actual_end - start;
  196. u64 aligned_end = (end + root->sectorsize - 1) &
  197. ~((u64)root->sectorsize - 1);
  198. u64 hint_byte;
  199. u64 data_len = inline_len;
  200. int ret;
  201. if (compressed_size)
  202. data_len = compressed_size;
  203. if (start > 0 ||
  204. actual_end >= PAGE_CACHE_SIZE ||
  205. data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
  206. (!compressed_size &&
  207. (actual_end & (root->sectorsize - 1)) == 0) ||
  208. end + 1 < isize ||
  209. data_len > root->fs_info->max_inline) {
  210. return 1;
  211. }
  212. ret = btrfs_drop_extents(trans, root, inode, start,
  213. aligned_end, aligned_end, start, &hint_byte);
  214. BUG_ON(ret);
  215. if (isize > actual_end)
  216. inline_len = min_t(u64, isize, actual_end);
  217. ret = insert_inline_extent(trans, root, inode, start,
  218. inline_len, compressed_size,
  219. compressed_pages);
  220. BUG_ON(ret);
  221. btrfs_drop_extent_cache(inode, start, aligned_end, 0);
  222. return 0;
  223. }
  224. struct async_extent {
  225. u64 start;
  226. u64 ram_size;
  227. u64 compressed_size;
  228. struct page **pages;
  229. unsigned long nr_pages;
  230. struct list_head list;
  231. };
  232. struct async_cow {
  233. struct inode *inode;
  234. struct btrfs_root *root;
  235. struct page *locked_page;
  236. u64 start;
  237. u64 end;
  238. struct list_head extents;
  239. struct btrfs_work work;
  240. };
  241. static noinline int add_async_extent(struct async_cow *cow,
  242. u64 start, u64 ram_size,
  243. u64 compressed_size,
  244. struct page **pages,
  245. unsigned long nr_pages)
  246. {
  247. struct async_extent *async_extent;
  248. async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
  249. async_extent->start = start;
  250. async_extent->ram_size = ram_size;
  251. async_extent->compressed_size = compressed_size;
  252. async_extent->pages = pages;
  253. async_extent->nr_pages = nr_pages;
  254. list_add_tail(&async_extent->list, &cow->extents);
  255. return 0;
  256. }
  257. /*
  258. * we create compressed extents in two phases. The first
  259. * phase compresses a range of pages that have already been
  260. * locked (both pages and state bits are locked).
  261. *
  262. * This is done inside an ordered work queue, and the compression
  263. * is spread across many cpus. The actual IO submission is step
  264. * two, and the ordered work queue takes care of making sure that
  265. * happens in the same order things were put onto the queue by
  266. * writepages and friends.
  267. *
  268. * If this code finds it can't get good compression, it puts an
  269. * entry onto the work queue to write the uncompressed bytes. This
  270. * makes sure that both compressed inodes and uncompressed inodes
  271. * are written in the same order that pdflush sent them down.
  272. */
  273. static noinline int compress_file_range(struct inode *inode,
  274. struct page *locked_page,
  275. u64 start, u64 end,
  276. struct async_cow *async_cow,
  277. int *num_added)
  278. {
  279. struct btrfs_root *root = BTRFS_I(inode)->root;
  280. struct btrfs_trans_handle *trans;
  281. u64 num_bytes;
  282. u64 orig_start;
  283. u64 disk_num_bytes;
  284. u64 blocksize = root->sectorsize;
  285. u64 actual_end;
  286. u64 isize = i_size_read(inode);
  287. int ret = 0;
  288. struct page **pages = NULL;
  289. unsigned long nr_pages;
  290. unsigned long nr_pages_ret = 0;
  291. unsigned long total_compressed = 0;
  292. unsigned long total_in = 0;
  293. unsigned long max_compressed = 128 * 1024;
  294. unsigned long max_uncompressed = 128 * 1024;
  295. int i;
  296. int will_compress;
  297. orig_start = start;
  298. actual_end = min_t(u64, isize, end + 1);
  299. again:
  300. will_compress = 0;
  301. nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
  302. nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
  303. /*
  304. * we don't want to send crud past the end of i_size through
  305. * compression, that's just a waste of CPU time. So, if the
  306. * end of the file is before the start of our current
  307. * requested range of bytes, we bail out to the uncompressed
  308. * cleanup code that can deal with all of this.
  309. *
  310. * It isn't really the fastest way to fix things, but this is a
  311. * very uncommon corner.
  312. */
  313. if (actual_end <= start)
  314. goto cleanup_and_bail_uncompressed;
  315. total_compressed = actual_end - start;
  316. /* we want to make sure that amount of ram required to uncompress
  317. * an extent is reasonable, so we limit the total size in ram
  318. * of a compressed extent to 128k. This is a crucial number
  319. * because it also controls how easily we can spread reads across
  320. * cpus for decompression.
  321. *
  322. * We also want to make sure the amount of IO required to do
  323. * a random read is reasonably small, so we limit the size of
  324. * a compressed extent to 128k.
  325. */
  326. total_compressed = min(total_compressed, max_uncompressed);
  327. num_bytes = (end - start + blocksize) & ~(blocksize - 1);
  328. num_bytes = max(blocksize, num_bytes);
  329. disk_num_bytes = num_bytes;
  330. total_in = 0;
  331. ret = 0;
  332. /*
  333. * we do compression for mount -o compress and when the
  334. * inode has not been flagged as nocompress. This flag can
  335. * change at any time if we discover bad compression ratios.
  336. */
  337. if (!btrfs_test_flag(inode, NOCOMPRESS) &&
  338. btrfs_test_opt(root, COMPRESS)) {
  339. WARN_ON(pages);
  340. pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
  341. ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
  342. total_compressed, pages,
  343. nr_pages, &nr_pages_ret,
  344. &total_in,
  345. &total_compressed,
  346. max_compressed);
  347. if (!ret) {
  348. unsigned long offset = total_compressed &
  349. (PAGE_CACHE_SIZE - 1);
  350. struct page *page = pages[nr_pages_ret - 1];
  351. char *kaddr;
  352. /* zero the tail end of the last page, we might be
  353. * sending it down to disk
  354. */
  355. if (offset) {
  356. kaddr = kmap_atomic(page, KM_USER0);
  357. memset(kaddr + offset, 0,
  358. PAGE_CACHE_SIZE - offset);
  359. kunmap_atomic(kaddr, KM_USER0);
  360. }
  361. will_compress = 1;
  362. }
  363. }
  364. if (start == 0) {
  365. trans = btrfs_join_transaction(root, 1);
  366. BUG_ON(!trans);
  367. btrfs_set_trans_block_group(trans, inode);
  368. /* lets try to make an inline extent */
  369. if (ret || total_in < (actual_end - start)) {
  370. /* we didn't compress the entire range, try
  371. * to make an uncompressed inline extent.
  372. */
  373. ret = cow_file_range_inline(trans, root, inode,
  374. start, end, 0, NULL);
  375. } else {
  376. /* try making a compressed inline extent */
  377. ret = cow_file_range_inline(trans, root, inode,
  378. start, end,
  379. total_compressed, pages);
  380. }
  381. btrfs_end_transaction(trans, root);
  382. if (ret == 0) {
  383. /*
  384. * inline extent creation worked, we don't need
  385. * to create any more async work items. Unlock
  386. * and free up our temp pages.
  387. */
  388. extent_clear_unlock_delalloc(inode,
  389. &BTRFS_I(inode)->io_tree,
  390. start, end, NULL, 1, 0,
  391. 0, 1, 1, 1);
  392. ret = 0;
  393. goto free_pages_out;
  394. }
  395. }
  396. if (will_compress) {
  397. /*
  398. * we aren't doing an inline extent round the compressed size
  399. * up to a block size boundary so the allocator does sane
  400. * things
  401. */
  402. total_compressed = (total_compressed + blocksize - 1) &
  403. ~(blocksize - 1);
  404. /*
  405. * one last check to make sure the compression is really a
  406. * win, compare the page count read with the blocks on disk
  407. */
  408. total_in = (total_in + PAGE_CACHE_SIZE - 1) &
  409. ~(PAGE_CACHE_SIZE - 1);
  410. if (total_compressed >= total_in) {
  411. will_compress = 0;
  412. } else {
  413. disk_num_bytes = total_compressed;
  414. num_bytes = total_in;
  415. }
  416. }
  417. if (!will_compress && pages) {
  418. /*
  419. * the compression code ran but failed to make things smaller,
  420. * free any pages it allocated and our page pointer array
  421. */
  422. for (i = 0; i < nr_pages_ret; i++) {
  423. WARN_ON(pages[i]->mapping);
  424. page_cache_release(pages[i]);
  425. }
  426. kfree(pages);
  427. pages = NULL;
  428. total_compressed = 0;
  429. nr_pages_ret = 0;
  430. /* flag the file so we don't compress in the future */
  431. btrfs_set_flag(inode, NOCOMPRESS);
  432. }
  433. if (will_compress) {
  434. *num_added += 1;
  435. /* the async work queues will take care of doing actual
  436. * allocation on disk for these compressed pages,
  437. * and will submit them to the elevator.
  438. */
  439. add_async_extent(async_cow, start, num_bytes,
  440. total_compressed, pages, nr_pages_ret);
  441. if (start + num_bytes < end && start + num_bytes < actual_end) {
  442. start += num_bytes;
  443. pages = NULL;
  444. cond_resched();
  445. goto again;
  446. }
  447. } else {
  448. cleanup_and_bail_uncompressed:
  449. /*
  450. * No compression, but we still need to write the pages in
  451. * the file we've been given so far. redirty the locked
  452. * page if it corresponds to our extent and set things up
  453. * for the async work queue to run cow_file_range to do
  454. * the normal delalloc dance
  455. */
  456. if (page_offset(locked_page) >= start &&
  457. page_offset(locked_page) <= end) {
  458. __set_page_dirty_nobuffers(locked_page);
  459. /* unlocked later on in the async handlers */
  460. }
  461. add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
  462. *num_added += 1;
  463. }
  464. out:
  465. return 0;
  466. free_pages_out:
  467. for (i = 0; i < nr_pages_ret; i++) {
  468. WARN_ON(pages[i]->mapping);
  469. page_cache_release(pages[i]);
  470. }
  471. kfree(pages);
  472. goto out;
  473. }
  474. /*
  475. * phase two of compressed writeback. This is the ordered portion
  476. * of the code, which only gets called in the order the work was
  477. * queued. We walk all the async extents created by compress_file_range
  478. * and send them down to the disk.
  479. */
  480. static noinline int submit_compressed_extents(struct inode *inode,
  481. struct async_cow *async_cow)
  482. {
  483. struct async_extent *async_extent;
  484. u64 alloc_hint = 0;
  485. struct btrfs_trans_handle *trans;
  486. struct btrfs_key ins;
  487. struct extent_map *em;
  488. struct btrfs_root *root = BTRFS_I(inode)->root;
  489. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  490. struct extent_io_tree *io_tree;
  491. int ret;
  492. if (list_empty(&async_cow->extents))
  493. return 0;
  494. trans = btrfs_join_transaction(root, 1);
  495. while (!list_empty(&async_cow->extents)) {
  496. async_extent = list_entry(async_cow->extents.next,
  497. struct async_extent, list);
  498. list_del(&async_extent->list);
  499. io_tree = &BTRFS_I(inode)->io_tree;
  500. /* did the compression code fall back to uncompressed IO? */
  501. if (!async_extent->pages) {
  502. int page_started = 0;
  503. unsigned long nr_written = 0;
  504. lock_extent(io_tree, async_extent->start,
  505. async_extent->start +
  506. async_extent->ram_size - 1, GFP_NOFS);
  507. /* allocate blocks */
  508. cow_file_range(inode, async_cow->locked_page,
  509. async_extent->start,
  510. async_extent->start +
  511. async_extent->ram_size - 1,
  512. &page_started, &nr_written, 0);
  513. /*
  514. * if page_started, cow_file_range inserted an
  515. * inline extent and took care of all the unlocking
  516. * and IO for us. Otherwise, we need to submit
  517. * all those pages down to the drive.
  518. */
  519. if (!page_started)
  520. extent_write_locked_range(io_tree,
  521. inode, async_extent->start,
  522. async_extent->start +
  523. async_extent->ram_size - 1,
  524. btrfs_get_extent,
  525. WB_SYNC_ALL);
  526. kfree(async_extent);
  527. cond_resched();
  528. continue;
  529. }
  530. lock_extent(io_tree, async_extent->start,
  531. async_extent->start + async_extent->ram_size - 1,
  532. GFP_NOFS);
  533. /*
  534. * here we're doing allocation and writeback of the
  535. * compressed pages
  536. */
  537. btrfs_drop_extent_cache(inode, async_extent->start,
  538. async_extent->start +
  539. async_extent->ram_size - 1, 0);
  540. ret = btrfs_reserve_extent(trans, root,
  541. async_extent->compressed_size,
  542. async_extent->compressed_size,
  543. 0, alloc_hint,
  544. (u64)-1, &ins, 1);
  545. BUG_ON(ret);
  546. em = alloc_extent_map(GFP_NOFS);
  547. em->start = async_extent->start;
  548. em->len = async_extent->ram_size;
  549. em->orig_start = em->start;
  550. em->block_start = ins.objectid;
  551. em->block_len = ins.offset;
  552. em->bdev = root->fs_info->fs_devices->latest_bdev;
  553. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  554. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  555. while (1) {
  556. spin_lock(&em_tree->lock);
  557. ret = add_extent_mapping(em_tree, em);
  558. spin_unlock(&em_tree->lock);
  559. if (ret != -EEXIST) {
  560. free_extent_map(em);
  561. break;
  562. }
  563. btrfs_drop_extent_cache(inode, async_extent->start,
  564. async_extent->start +
  565. async_extent->ram_size - 1, 0);
  566. }
  567. ret = btrfs_add_ordered_extent(inode, async_extent->start,
  568. ins.objectid,
  569. async_extent->ram_size,
  570. ins.offset,
  571. BTRFS_ORDERED_COMPRESSED);
  572. BUG_ON(ret);
  573. btrfs_end_transaction(trans, root);
  574. /*
  575. * clear dirty, set writeback and unlock the pages.
  576. */
  577. extent_clear_unlock_delalloc(inode,
  578. &BTRFS_I(inode)->io_tree,
  579. async_extent->start,
  580. async_extent->start +
  581. async_extent->ram_size - 1,
  582. NULL, 1, 1, 0, 1, 1, 0);
  583. ret = btrfs_submit_compressed_write(inode,
  584. async_extent->start,
  585. async_extent->ram_size,
  586. ins.objectid,
  587. ins.offset, async_extent->pages,
  588. async_extent->nr_pages);
  589. BUG_ON(ret);
  590. trans = btrfs_join_transaction(root, 1);
  591. alloc_hint = ins.objectid + ins.offset;
  592. kfree(async_extent);
  593. cond_resched();
  594. }
  595. btrfs_end_transaction(trans, root);
  596. return 0;
  597. }
  598. /*
  599. * when extent_io.c finds a delayed allocation range in the file,
  600. * the call backs end up in this code. The basic idea is to
  601. * allocate extents on disk for the range, and create ordered data structs
  602. * in ram to track those extents.
  603. *
  604. * locked_page is the page that writepage had locked already. We use
  605. * it to make sure we don't do extra locks or unlocks.
  606. *
  607. * *page_started is set to one if we unlock locked_page and do everything
  608. * required to start IO on it. It may be clean and already done with
  609. * IO when we return.
  610. */
  611. static noinline int cow_file_range(struct inode *inode,
  612. struct page *locked_page,
  613. u64 start, u64 end, int *page_started,
  614. unsigned long *nr_written,
  615. int unlock)
  616. {
  617. struct btrfs_root *root = BTRFS_I(inode)->root;
  618. struct btrfs_trans_handle *trans;
  619. u64 alloc_hint = 0;
  620. u64 num_bytes;
  621. unsigned long ram_size;
  622. u64 disk_num_bytes;
  623. u64 cur_alloc_size;
  624. u64 blocksize = root->sectorsize;
  625. u64 actual_end;
  626. u64 isize = i_size_read(inode);
  627. struct btrfs_key ins;
  628. struct extent_map *em;
  629. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  630. int ret = 0;
  631. trans = btrfs_join_transaction(root, 1);
  632. BUG_ON(!trans);
  633. btrfs_set_trans_block_group(trans, inode);
  634. actual_end = min_t(u64, isize, end + 1);
  635. num_bytes = (end - start + blocksize) & ~(blocksize - 1);
  636. num_bytes = max(blocksize, num_bytes);
  637. disk_num_bytes = num_bytes;
  638. ret = 0;
  639. if (start == 0) {
  640. /* lets try to make an inline extent */
  641. ret = cow_file_range_inline(trans, root, inode,
  642. start, end, 0, NULL);
  643. if (ret == 0) {
  644. extent_clear_unlock_delalloc(inode,
  645. &BTRFS_I(inode)->io_tree,
  646. start, end, NULL, 1, 1,
  647. 1, 1, 1, 1);
  648. *nr_written = *nr_written +
  649. (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
  650. *page_started = 1;
  651. ret = 0;
  652. goto out;
  653. }
  654. }
  655. BUG_ON(disk_num_bytes >
  656. btrfs_super_total_bytes(&root->fs_info->super_copy));
  657. btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
  658. while (disk_num_bytes > 0) {
  659. cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
  660. ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
  661. root->sectorsize, 0, alloc_hint,
  662. (u64)-1, &ins, 1);
  663. BUG_ON(ret);
  664. em = alloc_extent_map(GFP_NOFS);
  665. em->start = start;
  666. em->orig_start = em->start;
  667. ram_size = ins.offset;
  668. em->len = ins.offset;
  669. em->block_start = ins.objectid;
  670. em->block_len = ins.offset;
  671. em->bdev = root->fs_info->fs_devices->latest_bdev;
  672. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  673. while (1) {
  674. spin_lock(&em_tree->lock);
  675. ret = add_extent_mapping(em_tree, em);
  676. spin_unlock(&em_tree->lock);
  677. if (ret != -EEXIST) {
  678. free_extent_map(em);
  679. break;
  680. }
  681. btrfs_drop_extent_cache(inode, start,
  682. start + ram_size - 1, 0);
  683. }
  684. cur_alloc_size = ins.offset;
  685. ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
  686. ram_size, cur_alloc_size, 0);
  687. BUG_ON(ret);
  688. if (root->root_key.objectid ==
  689. BTRFS_DATA_RELOC_TREE_OBJECTID) {
  690. ret = btrfs_reloc_clone_csums(inode, start,
  691. cur_alloc_size);
  692. BUG_ON(ret);
  693. }
  694. if (disk_num_bytes < cur_alloc_size)
  695. break;
  696. /* we're not doing compressed IO, don't unlock the first
  697. * page (which the caller expects to stay locked), don't
  698. * clear any dirty bits and don't set any writeback bits
  699. */
  700. extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
  701. start, start + ram_size - 1,
  702. locked_page, unlock, 1,
  703. 1, 0, 0, 0);
  704. disk_num_bytes -= cur_alloc_size;
  705. num_bytes -= cur_alloc_size;
  706. alloc_hint = ins.objectid + ins.offset;
  707. start += cur_alloc_size;
  708. }
  709. out:
  710. ret = 0;
  711. btrfs_end_transaction(trans, root);
  712. return ret;
  713. }
  714. /*
  715. * work queue call back to started compression on a file and pages
  716. */
  717. static noinline void async_cow_start(struct btrfs_work *work)
  718. {
  719. struct async_cow *async_cow;
  720. int num_added = 0;
  721. async_cow = container_of(work, struct async_cow, work);
  722. compress_file_range(async_cow->inode, async_cow->locked_page,
  723. async_cow->start, async_cow->end, async_cow,
  724. &num_added);
  725. if (num_added == 0)
  726. async_cow->inode = NULL;
  727. }
  728. /*
  729. * work queue call back to submit previously compressed pages
  730. */
  731. static noinline void async_cow_submit(struct btrfs_work *work)
  732. {
  733. struct async_cow *async_cow;
  734. struct btrfs_root *root;
  735. unsigned long nr_pages;
  736. async_cow = container_of(work, struct async_cow, work);
  737. root = async_cow->root;
  738. nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
  739. PAGE_CACHE_SHIFT;
  740. atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
  741. if (atomic_read(&root->fs_info->async_delalloc_pages) <
  742. 5 * 1042 * 1024 &&
  743. waitqueue_active(&root->fs_info->async_submit_wait))
  744. wake_up(&root->fs_info->async_submit_wait);
  745. if (async_cow->inode)
  746. submit_compressed_extents(async_cow->inode, async_cow);
  747. }
  748. static noinline void async_cow_free(struct btrfs_work *work)
  749. {
  750. struct async_cow *async_cow;
  751. async_cow = container_of(work, struct async_cow, work);
  752. kfree(async_cow);
  753. }
  754. static int cow_file_range_async(struct inode *inode, struct page *locked_page,
  755. u64 start, u64 end, int *page_started,
  756. unsigned long *nr_written)
  757. {
  758. struct async_cow *async_cow;
  759. struct btrfs_root *root = BTRFS_I(inode)->root;
  760. unsigned long nr_pages;
  761. u64 cur_end;
  762. int limit = 10 * 1024 * 1042;
  763. clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED |
  764. EXTENT_DELALLOC, 1, 0, GFP_NOFS);
  765. while (start < end) {
  766. async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
  767. async_cow->inode = inode;
  768. async_cow->root = root;
  769. async_cow->locked_page = locked_page;
  770. async_cow->start = start;
  771. if (btrfs_test_flag(inode, NOCOMPRESS))
  772. cur_end = end;
  773. else
  774. cur_end = min(end, start + 512 * 1024 - 1);
  775. async_cow->end = cur_end;
  776. INIT_LIST_HEAD(&async_cow->extents);
  777. async_cow->work.func = async_cow_start;
  778. async_cow->work.ordered_func = async_cow_submit;
  779. async_cow->work.ordered_free = async_cow_free;
  780. async_cow->work.flags = 0;
  781. nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
  782. PAGE_CACHE_SHIFT;
  783. atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
  784. btrfs_queue_worker(&root->fs_info->delalloc_workers,
  785. &async_cow->work);
  786. if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
  787. wait_event(root->fs_info->async_submit_wait,
  788. (atomic_read(&root->fs_info->async_delalloc_pages) <
  789. limit));
  790. }
  791. while (atomic_read(&root->fs_info->async_submit_draining) &&
  792. atomic_read(&root->fs_info->async_delalloc_pages)) {
  793. wait_event(root->fs_info->async_submit_wait,
  794. (atomic_read(&root->fs_info->async_delalloc_pages) ==
  795. 0));
  796. }
  797. *nr_written += nr_pages;
  798. start = cur_end + 1;
  799. }
  800. *page_started = 1;
  801. return 0;
  802. }
  803. static noinline int csum_exist_in_range(struct btrfs_root *root,
  804. u64 bytenr, u64 num_bytes)
  805. {
  806. int ret;
  807. struct btrfs_ordered_sum *sums;
  808. LIST_HEAD(list);
  809. ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
  810. bytenr + num_bytes - 1, &list);
  811. if (ret == 0 && list_empty(&list))
  812. return 0;
  813. while (!list_empty(&list)) {
  814. sums = list_entry(list.next, struct btrfs_ordered_sum, list);
  815. list_del(&sums->list);
  816. kfree(sums);
  817. }
  818. return 1;
  819. }
  820. /*
  821. * when nowcow writeback call back. This checks for snapshots or COW copies
  822. * of the extents that exist in the file, and COWs the file as required.
  823. *
  824. * If no cow copies or snapshots exist, we write directly to the existing
  825. * blocks on disk
  826. */
  827. static noinline int run_delalloc_nocow(struct inode *inode,
  828. struct page *locked_page,
  829. u64 start, u64 end, int *page_started, int force,
  830. unsigned long *nr_written)
  831. {
  832. struct btrfs_root *root = BTRFS_I(inode)->root;
  833. struct btrfs_trans_handle *trans;
  834. struct extent_buffer *leaf;
  835. struct btrfs_path *path;
  836. struct btrfs_file_extent_item *fi;
  837. struct btrfs_key found_key;
  838. u64 cow_start;
  839. u64 cur_offset;
  840. u64 extent_end;
  841. u64 disk_bytenr;
  842. u64 num_bytes;
  843. int extent_type;
  844. int ret;
  845. int type;
  846. int nocow;
  847. int check_prev = 1;
  848. path = btrfs_alloc_path();
  849. BUG_ON(!path);
  850. trans = btrfs_join_transaction(root, 1);
  851. BUG_ON(!trans);
  852. cow_start = (u64)-1;
  853. cur_offset = start;
  854. while (1) {
  855. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  856. cur_offset, 0);
  857. BUG_ON(ret < 0);
  858. if (ret > 0 && path->slots[0] > 0 && check_prev) {
  859. leaf = path->nodes[0];
  860. btrfs_item_key_to_cpu(leaf, &found_key,
  861. path->slots[0] - 1);
  862. if (found_key.objectid == inode->i_ino &&
  863. found_key.type == BTRFS_EXTENT_DATA_KEY)
  864. path->slots[0]--;
  865. }
  866. check_prev = 0;
  867. next_slot:
  868. leaf = path->nodes[0];
  869. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  870. ret = btrfs_next_leaf(root, path);
  871. if (ret < 0)
  872. BUG_ON(1);
  873. if (ret > 0)
  874. break;
  875. leaf = path->nodes[0];
  876. }
  877. nocow = 0;
  878. disk_bytenr = 0;
  879. num_bytes = 0;
  880. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  881. if (found_key.objectid > inode->i_ino ||
  882. found_key.type > BTRFS_EXTENT_DATA_KEY ||
  883. found_key.offset > end)
  884. break;
  885. if (found_key.offset > cur_offset) {
  886. extent_end = found_key.offset;
  887. goto out_check;
  888. }
  889. fi = btrfs_item_ptr(leaf, path->slots[0],
  890. struct btrfs_file_extent_item);
  891. extent_type = btrfs_file_extent_type(leaf, fi);
  892. if (extent_type == BTRFS_FILE_EXTENT_REG ||
  893. extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  894. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  895. extent_end = found_key.offset +
  896. btrfs_file_extent_num_bytes(leaf, fi);
  897. if (extent_end <= start) {
  898. path->slots[0]++;
  899. goto next_slot;
  900. }
  901. if (disk_bytenr == 0)
  902. goto out_check;
  903. if (btrfs_file_extent_compression(leaf, fi) ||
  904. btrfs_file_extent_encryption(leaf, fi) ||
  905. btrfs_file_extent_other_encoding(leaf, fi))
  906. goto out_check;
  907. if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
  908. goto out_check;
  909. if (btrfs_extent_readonly(root, disk_bytenr))
  910. goto out_check;
  911. if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
  912. disk_bytenr))
  913. goto out_check;
  914. disk_bytenr += btrfs_file_extent_offset(leaf, fi);
  915. disk_bytenr += cur_offset - found_key.offset;
  916. num_bytes = min(end + 1, extent_end) - cur_offset;
  917. /*
  918. * force cow if csum exists in the range.
  919. * this ensure that csum for a given extent are
  920. * either valid or do not exist.
  921. */
  922. if (csum_exist_in_range(root, disk_bytenr, num_bytes))
  923. goto out_check;
  924. nocow = 1;
  925. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  926. extent_end = found_key.offset +
  927. btrfs_file_extent_inline_len(leaf, fi);
  928. extent_end = ALIGN(extent_end, root->sectorsize);
  929. } else {
  930. BUG_ON(1);
  931. }
  932. out_check:
  933. if (extent_end <= start) {
  934. path->slots[0]++;
  935. goto next_slot;
  936. }
  937. if (!nocow) {
  938. if (cow_start == (u64)-1)
  939. cow_start = cur_offset;
  940. cur_offset = extent_end;
  941. if (cur_offset > end)
  942. break;
  943. path->slots[0]++;
  944. goto next_slot;
  945. }
  946. btrfs_release_path(root, path);
  947. if (cow_start != (u64)-1) {
  948. ret = cow_file_range(inode, locked_page, cow_start,
  949. found_key.offset - 1, page_started,
  950. nr_written, 1);
  951. BUG_ON(ret);
  952. cow_start = (u64)-1;
  953. }
  954. if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  955. struct extent_map *em;
  956. struct extent_map_tree *em_tree;
  957. em_tree = &BTRFS_I(inode)->extent_tree;
  958. em = alloc_extent_map(GFP_NOFS);
  959. em->start = cur_offset;
  960. em->orig_start = em->start;
  961. em->len = num_bytes;
  962. em->block_len = num_bytes;
  963. em->block_start = disk_bytenr;
  964. em->bdev = root->fs_info->fs_devices->latest_bdev;
  965. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  966. while (1) {
  967. spin_lock(&em_tree->lock);
  968. ret = add_extent_mapping(em_tree, em);
  969. spin_unlock(&em_tree->lock);
  970. if (ret != -EEXIST) {
  971. free_extent_map(em);
  972. break;
  973. }
  974. btrfs_drop_extent_cache(inode, em->start,
  975. em->start + em->len - 1, 0);
  976. }
  977. type = BTRFS_ORDERED_PREALLOC;
  978. } else {
  979. type = BTRFS_ORDERED_NOCOW;
  980. }
  981. ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
  982. num_bytes, num_bytes, type);
  983. BUG_ON(ret);
  984. extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
  985. cur_offset, cur_offset + num_bytes - 1,
  986. locked_page, 1, 1, 1, 0, 0, 0);
  987. cur_offset = extent_end;
  988. if (cur_offset > end)
  989. break;
  990. }
  991. btrfs_release_path(root, path);
  992. if (cur_offset <= end && cow_start == (u64)-1)
  993. cow_start = cur_offset;
  994. if (cow_start != (u64)-1) {
  995. ret = cow_file_range(inode, locked_page, cow_start, end,
  996. page_started, nr_written, 1);
  997. BUG_ON(ret);
  998. }
  999. ret = btrfs_end_transaction(trans, root);
  1000. BUG_ON(ret);
  1001. btrfs_free_path(path);
  1002. return 0;
  1003. }
  1004. /*
  1005. * extent_io.c call back to do delayed allocation processing
  1006. */
  1007. static int run_delalloc_range(struct inode *inode, struct page *locked_page,
  1008. u64 start, u64 end, int *page_started,
  1009. unsigned long *nr_written)
  1010. {
  1011. int ret;
  1012. struct btrfs_root *root = BTRFS_I(inode)->root;
  1013. if (btrfs_test_flag(inode, NODATACOW))
  1014. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1015. page_started, 1, nr_written);
  1016. else if (btrfs_test_flag(inode, PREALLOC))
  1017. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1018. page_started, 0, nr_written);
  1019. else if (!btrfs_test_opt(root, COMPRESS))
  1020. ret = cow_file_range(inode, locked_page, start, end,
  1021. page_started, nr_written, 1);
  1022. else
  1023. ret = cow_file_range_async(inode, locked_page, start, end,
  1024. page_started, nr_written);
  1025. return ret;
  1026. }
  1027. /*
  1028. * extent_io.c set_bit_hook, used to track delayed allocation
  1029. * bytes in this file, and to maintain the list of inodes that
  1030. * have pending delalloc work to be done.
  1031. */
  1032. static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
  1033. unsigned long old, unsigned long bits)
  1034. {
  1035. /*
  1036. * set_bit and clear bit hooks normally require _irqsave/restore
  1037. * but in this case, we are only testeing for the DELALLOC
  1038. * bit, which is only set or cleared with irqs on
  1039. */
  1040. if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
  1041. struct btrfs_root *root = BTRFS_I(inode)->root;
  1042. btrfs_delalloc_reserve_space(root, inode, end - start + 1);
  1043. spin_lock(&root->fs_info->delalloc_lock);
  1044. BTRFS_I(inode)->delalloc_bytes += end - start + 1;
  1045. root->fs_info->delalloc_bytes += end - start + 1;
  1046. if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1047. list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
  1048. &root->fs_info->delalloc_inodes);
  1049. }
  1050. spin_unlock(&root->fs_info->delalloc_lock);
  1051. }
  1052. return 0;
  1053. }
  1054. /*
  1055. * extent_io.c clear_bit_hook, see set_bit_hook for why
  1056. */
  1057. static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
  1058. unsigned long old, unsigned long bits)
  1059. {
  1060. /*
  1061. * set_bit and clear bit hooks normally require _irqsave/restore
  1062. * but in this case, we are only testeing for the DELALLOC
  1063. * bit, which is only set or cleared with irqs on
  1064. */
  1065. if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
  1066. struct btrfs_root *root = BTRFS_I(inode)->root;
  1067. spin_lock(&root->fs_info->delalloc_lock);
  1068. if (end - start + 1 > root->fs_info->delalloc_bytes) {
  1069. printk(KERN_INFO "btrfs warning: delalloc account "
  1070. "%llu %llu\n",
  1071. (unsigned long long)end - start + 1,
  1072. (unsigned long long)
  1073. root->fs_info->delalloc_bytes);
  1074. btrfs_delalloc_free_space(root, inode, (u64)-1);
  1075. root->fs_info->delalloc_bytes = 0;
  1076. BTRFS_I(inode)->delalloc_bytes = 0;
  1077. } else {
  1078. btrfs_delalloc_free_space(root, inode,
  1079. end - start + 1);
  1080. root->fs_info->delalloc_bytes -= end - start + 1;
  1081. BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
  1082. }
  1083. if (BTRFS_I(inode)->delalloc_bytes == 0 &&
  1084. !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1085. list_del_init(&BTRFS_I(inode)->delalloc_inodes);
  1086. }
  1087. spin_unlock(&root->fs_info->delalloc_lock);
  1088. }
  1089. return 0;
  1090. }
  1091. /*
  1092. * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
  1093. * we don't create bios that span stripes or chunks
  1094. */
  1095. int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
  1096. size_t size, struct bio *bio,
  1097. unsigned long bio_flags)
  1098. {
  1099. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  1100. struct btrfs_mapping_tree *map_tree;
  1101. u64 logical = (u64)bio->bi_sector << 9;
  1102. u64 length = 0;
  1103. u64 map_length;
  1104. int ret;
  1105. if (bio_flags & EXTENT_BIO_COMPRESSED)
  1106. return 0;
  1107. length = bio->bi_size;
  1108. map_tree = &root->fs_info->mapping_tree;
  1109. map_length = length;
  1110. ret = btrfs_map_block(map_tree, READ, logical,
  1111. &map_length, NULL, 0);
  1112. if (map_length < length + size)
  1113. return 1;
  1114. return 0;
  1115. }
  1116. /*
  1117. * in order to insert checksums into the metadata in large chunks,
  1118. * we wait until bio submission time. All the pages in the bio are
  1119. * checksummed and sums are attached onto the ordered extent record.
  1120. *
  1121. * At IO completion time the cums attached on the ordered extent record
  1122. * are inserted into the btree
  1123. */
  1124. static int __btrfs_submit_bio_start(struct inode *inode, int rw,
  1125. struct bio *bio, int mirror_num,
  1126. unsigned long bio_flags)
  1127. {
  1128. struct btrfs_root *root = BTRFS_I(inode)->root;
  1129. int ret = 0;
  1130. ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
  1131. BUG_ON(ret);
  1132. return 0;
  1133. }
  1134. /*
  1135. * in order to insert checksums into the metadata in large chunks,
  1136. * we wait until bio submission time. All the pages in the bio are
  1137. * checksummed and sums are attached onto the ordered extent record.
  1138. *
  1139. * At IO completion time the cums attached on the ordered extent record
  1140. * are inserted into the btree
  1141. */
  1142. static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
  1143. int mirror_num, unsigned long bio_flags)
  1144. {
  1145. struct btrfs_root *root = BTRFS_I(inode)->root;
  1146. return btrfs_map_bio(root, rw, bio, mirror_num, 1);
  1147. }
  1148. /*
  1149. * extent_io.c submission hook. This does the right thing for csum calculation
  1150. * on write, or reading the csums from the tree before a read
  1151. */
  1152. static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  1153. int mirror_num, unsigned long bio_flags)
  1154. {
  1155. struct btrfs_root *root = BTRFS_I(inode)->root;
  1156. int ret = 0;
  1157. int skip_sum;
  1158. skip_sum = btrfs_test_flag(inode, NODATASUM);
  1159. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  1160. BUG_ON(ret);
  1161. if (!(rw & (1 << BIO_RW))) {
  1162. if (bio_flags & EXTENT_BIO_COMPRESSED) {
  1163. return btrfs_submit_compressed_read(inode, bio,
  1164. mirror_num, bio_flags);
  1165. } else if (!skip_sum)
  1166. btrfs_lookup_bio_sums(root, inode, bio, NULL);
  1167. goto mapit;
  1168. } else if (!skip_sum) {
  1169. /* csum items have already been cloned */
  1170. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
  1171. goto mapit;
  1172. /* we're doing a write, do the async checksumming */
  1173. return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
  1174. inode, rw, bio, mirror_num,
  1175. bio_flags, __btrfs_submit_bio_start,
  1176. __btrfs_submit_bio_done);
  1177. }
  1178. mapit:
  1179. return btrfs_map_bio(root, rw, bio, mirror_num, 0);
  1180. }
  1181. /*
  1182. * given a list of ordered sums record them in the inode. This happens
  1183. * at IO completion time based on sums calculated at bio submission time.
  1184. */
  1185. static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
  1186. struct inode *inode, u64 file_offset,
  1187. struct list_head *list)
  1188. {
  1189. struct btrfs_ordered_sum *sum;
  1190. btrfs_set_trans_block_group(trans, inode);
  1191. list_for_each_entry(sum, list, list) {
  1192. btrfs_csum_file_blocks(trans,
  1193. BTRFS_I(inode)->root->fs_info->csum_root, sum);
  1194. }
  1195. return 0;
  1196. }
  1197. int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
  1198. {
  1199. if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
  1200. WARN_ON(1);
  1201. return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
  1202. GFP_NOFS);
  1203. }
  1204. /* see btrfs_writepage_start_hook for details on why this is required */
  1205. struct btrfs_writepage_fixup {
  1206. struct page *page;
  1207. struct btrfs_work work;
  1208. };
  1209. static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
  1210. {
  1211. struct btrfs_writepage_fixup *fixup;
  1212. struct btrfs_ordered_extent *ordered;
  1213. struct page *page;
  1214. struct inode *inode;
  1215. u64 page_start;
  1216. u64 page_end;
  1217. fixup = container_of(work, struct btrfs_writepage_fixup, work);
  1218. page = fixup->page;
  1219. again:
  1220. lock_page(page);
  1221. if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
  1222. ClearPageChecked(page);
  1223. goto out_page;
  1224. }
  1225. inode = page->mapping->host;
  1226. page_start = page_offset(page);
  1227. page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
  1228. lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
  1229. /* already ordered? We're done */
  1230. if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  1231. EXTENT_ORDERED, 0)) {
  1232. goto out;
  1233. }
  1234. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  1235. if (ordered) {
  1236. unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
  1237. page_end, GFP_NOFS);
  1238. unlock_page(page);
  1239. btrfs_start_ordered_extent(inode, ordered, 1);
  1240. goto again;
  1241. }
  1242. btrfs_set_extent_delalloc(inode, page_start, page_end);
  1243. ClearPageChecked(page);
  1244. out:
  1245. unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
  1246. out_page:
  1247. unlock_page(page);
  1248. page_cache_release(page);
  1249. }
  1250. /*
  1251. * There are a few paths in the higher layers of the kernel that directly
  1252. * set the page dirty bit without asking the filesystem if it is a
  1253. * good idea. This causes problems because we want to make sure COW
  1254. * properly happens and the data=ordered rules are followed.
  1255. *
  1256. * In our case any range that doesn't have the ORDERED bit set
  1257. * hasn't been properly setup for IO. We kick off an async process
  1258. * to fix it up. The async helper will wait for ordered extents, set
  1259. * the delalloc bit and make it safe to write the page.
  1260. */
  1261. static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
  1262. {
  1263. struct inode *inode = page->mapping->host;
  1264. struct btrfs_writepage_fixup *fixup;
  1265. struct btrfs_root *root = BTRFS_I(inode)->root;
  1266. int ret;
  1267. ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
  1268. EXTENT_ORDERED, 0);
  1269. if (ret)
  1270. return 0;
  1271. if (PageChecked(page))
  1272. return -EAGAIN;
  1273. fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
  1274. if (!fixup)
  1275. return -EAGAIN;
  1276. SetPageChecked(page);
  1277. page_cache_get(page);
  1278. fixup->work.func = btrfs_writepage_fixup_worker;
  1279. fixup->page = page;
  1280. btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
  1281. return -EAGAIN;
  1282. }
  1283. static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
  1284. struct inode *inode, u64 file_pos,
  1285. u64 disk_bytenr, u64 disk_num_bytes,
  1286. u64 num_bytes, u64 ram_bytes,
  1287. u64 locked_end,
  1288. u8 compression, u8 encryption,
  1289. u16 other_encoding, int extent_type)
  1290. {
  1291. struct btrfs_root *root = BTRFS_I(inode)->root;
  1292. struct btrfs_file_extent_item *fi;
  1293. struct btrfs_path *path;
  1294. struct extent_buffer *leaf;
  1295. struct btrfs_key ins;
  1296. u64 hint;
  1297. int ret;
  1298. path = btrfs_alloc_path();
  1299. BUG_ON(!path);
  1300. path->leave_spinning = 1;
  1301. ret = btrfs_drop_extents(trans, root, inode, file_pos,
  1302. file_pos + num_bytes, locked_end,
  1303. file_pos, &hint);
  1304. BUG_ON(ret);
  1305. ins.objectid = inode->i_ino;
  1306. ins.offset = file_pos;
  1307. ins.type = BTRFS_EXTENT_DATA_KEY;
  1308. ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
  1309. BUG_ON(ret);
  1310. leaf = path->nodes[0];
  1311. fi = btrfs_item_ptr(leaf, path->slots[0],
  1312. struct btrfs_file_extent_item);
  1313. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  1314. btrfs_set_file_extent_type(leaf, fi, extent_type);
  1315. btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
  1316. btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
  1317. btrfs_set_file_extent_offset(leaf, fi, 0);
  1318. btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
  1319. btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
  1320. btrfs_set_file_extent_compression(leaf, fi, compression);
  1321. btrfs_set_file_extent_encryption(leaf, fi, encryption);
  1322. btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
  1323. btrfs_unlock_up_safe(path, 1);
  1324. btrfs_set_lock_blocking(leaf);
  1325. btrfs_mark_buffer_dirty(leaf);
  1326. inode_add_bytes(inode, num_bytes);
  1327. btrfs_drop_extent_cache(inode, file_pos, file_pos + num_bytes - 1, 0);
  1328. ins.objectid = disk_bytenr;
  1329. ins.offset = disk_num_bytes;
  1330. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1331. ret = btrfs_alloc_reserved_extent(trans, root, leaf->start,
  1332. root->root_key.objectid,
  1333. trans->transid, inode->i_ino, &ins);
  1334. BUG_ON(ret);
  1335. btrfs_free_path(path);
  1336. return 0;
  1337. }
  1338. /*
  1339. * helper function for btrfs_finish_ordered_io, this
  1340. * just reads in some of the csum leaves to prime them into ram
  1341. * before we start the transaction. It limits the amount of btree
  1342. * reads required while inside the transaction.
  1343. */
  1344. static noinline void reada_csum(struct btrfs_root *root,
  1345. struct btrfs_path *path,
  1346. struct btrfs_ordered_extent *ordered_extent)
  1347. {
  1348. struct btrfs_ordered_sum *sum;
  1349. u64 bytenr;
  1350. sum = list_entry(ordered_extent->list.next, struct btrfs_ordered_sum,
  1351. list);
  1352. bytenr = sum->sums[0].bytenr;
  1353. /*
  1354. * we don't care about the results, the point of this search is
  1355. * just to get the btree leaves into ram
  1356. */
  1357. btrfs_lookup_csum(NULL, root->fs_info->csum_root, path, bytenr, 0);
  1358. }
  1359. /* as ordered data IO finishes, this gets called so we can finish
  1360. * an ordered extent if the range of bytes in the file it covers are
  1361. * fully written.
  1362. */
  1363. static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
  1364. {
  1365. struct btrfs_root *root = BTRFS_I(inode)->root;
  1366. struct btrfs_trans_handle *trans;
  1367. struct btrfs_ordered_extent *ordered_extent = NULL;
  1368. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1369. struct btrfs_path *path;
  1370. int compressed = 0;
  1371. int ret;
  1372. ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
  1373. if (!ret)
  1374. return 0;
  1375. /*
  1376. * before we join the transaction, try to do some of our IO.
  1377. * This will limit the amount of IO that we have to do with
  1378. * the transaction running. We're unlikely to need to do any
  1379. * IO if the file extents are new, the disk_i_size checks
  1380. * covers the most common case.
  1381. */
  1382. if (start < BTRFS_I(inode)->disk_i_size) {
  1383. path = btrfs_alloc_path();
  1384. if (path) {
  1385. ret = btrfs_lookup_file_extent(NULL, root, path,
  1386. inode->i_ino,
  1387. start, 0);
  1388. ordered_extent = btrfs_lookup_ordered_extent(inode,
  1389. start);
  1390. if (!list_empty(&ordered_extent->list)) {
  1391. btrfs_release_path(root, path);
  1392. reada_csum(root, path, ordered_extent);
  1393. }
  1394. btrfs_free_path(path);
  1395. }
  1396. }
  1397. trans = btrfs_join_transaction(root, 1);
  1398. if (!ordered_extent)
  1399. ordered_extent = btrfs_lookup_ordered_extent(inode, start);
  1400. BUG_ON(!ordered_extent);
  1401. if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
  1402. goto nocow;
  1403. lock_extent(io_tree, ordered_extent->file_offset,
  1404. ordered_extent->file_offset + ordered_extent->len - 1,
  1405. GFP_NOFS);
  1406. if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
  1407. compressed = 1;
  1408. if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
  1409. BUG_ON(compressed);
  1410. ret = btrfs_mark_extent_written(trans, root, inode,
  1411. ordered_extent->file_offset,
  1412. ordered_extent->file_offset +
  1413. ordered_extent->len);
  1414. BUG_ON(ret);
  1415. } else {
  1416. ret = insert_reserved_file_extent(trans, inode,
  1417. ordered_extent->file_offset,
  1418. ordered_extent->start,
  1419. ordered_extent->disk_len,
  1420. ordered_extent->len,
  1421. ordered_extent->len,
  1422. ordered_extent->file_offset +
  1423. ordered_extent->len,
  1424. compressed, 0, 0,
  1425. BTRFS_FILE_EXTENT_REG);
  1426. BUG_ON(ret);
  1427. }
  1428. unlock_extent(io_tree, ordered_extent->file_offset,
  1429. ordered_extent->file_offset + ordered_extent->len - 1,
  1430. GFP_NOFS);
  1431. nocow:
  1432. add_pending_csums(trans, inode, ordered_extent->file_offset,
  1433. &ordered_extent->list);
  1434. mutex_lock(&BTRFS_I(inode)->extent_mutex);
  1435. btrfs_ordered_update_i_size(inode, ordered_extent);
  1436. btrfs_update_inode(trans, root, inode);
  1437. btrfs_remove_ordered_extent(inode, ordered_extent);
  1438. mutex_unlock(&BTRFS_I(inode)->extent_mutex);
  1439. /* once for us */
  1440. btrfs_put_ordered_extent(ordered_extent);
  1441. /* once for the tree */
  1442. btrfs_put_ordered_extent(ordered_extent);
  1443. btrfs_end_transaction(trans, root);
  1444. return 0;
  1445. }
  1446. static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
  1447. struct extent_state *state, int uptodate)
  1448. {
  1449. return btrfs_finish_ordered_io(page->mapping->host, start, end);
  1450. }
  1451. /*
  1452. * When IO fails, either with EIO or csum verification fails, we
  1453. * try other mirrors that might have a good copy of the data. This
  1454. * io_failure_record is used to record state as we go through all the
  1455. * mirrors. If another mirror has good data, the page is set up to date
  1456. * and things continue. If a good mirror can't be found, the original
  1457. * bio end_io callback is called to indicate things have failed.
  1458. */
  1459. struct io_failure_record {
  1460. struct page *page;
  1461. u64 start;
  1462. u64 len;
  1463. u64 logical;
  1464. unsigned long bio_flags;
  1465. int last_mirror;
  1466. };
  1467. static int btrfs_io_failed_hook(struct bio *failed_bio,
  1468. struct page *page, u64 start, u64 end,
  1469. struct extent_state *state)
  1470. {
  1471. struct io_failure_record *failrec = NULL;
  1472. u64 private;
  1473. struct extent_map *em;
  1474. struct inode *inode = page->mapping->host;
  1475. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1476. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1477. struct bio *bio;
  1478. int num_copies;
  1479. int ret;
  1480. int rw;
  1481. u64 logical;
  1482. ret = get_state_private(failure_tree, start, &private);
  1483. if (ret) {
  1484. failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
  1485. if (!failrec)
  1486. return -ENOMEM;
  1487. failrec->start = start;
  1488. failrec->len = end - start + 1;
  1489. failrec->last_mirror = 0;
  1490. failrec->bio_flags = 0;
  1491. spin_lock(&em_tree->lock);
  1492. em = lookup_extent_mapping(em_tree, start, failrec->len);
  1493. if (em->start > start || em->start + em->len < start) {
  1494. free_extent_map(em);
  1495. em = NULL;
  1496. }
  1497. spin_unlock(&em_tree->lock);
  1498. if (!em || IS_ERR(em)) {
  1499. kfree(failrec);
  1500. return -EIO;
  1501. }
  1502. logical = start - em->start;
  1503. logical = em->block_start + logical;
  1504. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  1505. logical = em->block_start;
  1506. failrec->bio_flags = EXTENT_BIO_COMPRESSED;
  1507. }
  1508. failrec->logical = logical;
  1509. free_extent_map(em);
  1510. set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
  1511. EXTENT_DIRTY, GFP_NOFS);
  1512. set_state_private(failure_tree, start,
  1513. (u64)(unsigned long)failrec);
  1514. } else {
  1515. failrec = (struct io_failure_record *)(unsigned long)private;
  1516. }
  1517. num_copies = btrfs_num_copies(
  1518. &BTRFS_I(inode)->root->fs_info->mapping_tree,
  1519. failrec->logical, failrec->len);
  1520. failrec->last_mirror++;
  1521. if (!state) {
  1522. spin_lock(&BTRFS_I(inode)->io_tree.lock);
  1523. state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
  1524. failrec->start,
  1525. EXTENT_LOCKED);
  1526. if (state && state->start != failrec->start)
  1527. state = NULL;
  1528. spin_unlock(&BTRFS_I(inode)->io_tree.lock);
  1529. }
  1530. if (!state || failrec->last_mirror > num_copies) {
  1531. set_state_private(failure_tree, failrec->start, 0);
  1532. clear_extent_bits(failure_tree, failrec->start,
  1533. failrec->start + failrec->len - 1,
  1534. EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
  1535. kfree(failrec);
  1536. return -EIO;
  1537. }
  1538. bio = bio_alloc(GFP_NOFS, 1);
  1539. bio->bi_private = state;
  1540. bio->bi_end_io = failed_bio->bi_end_io;
  1541. bio->bi_sector = failrec->logical >> 9;
  1542. bio->bi_bdev = failed_bio->bi_bdev;
  1543. bio->bi_size = 0;
  1544. bio_add_page(bio, page, failrec->len, start - page_offset(page));
  1545. if (failed_bio->bi_rw & (1 << BIO_RW))
  1546. rw = WRITE;
  1547. else
  1548. rw = READ;
  1549. BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
  1550. failrec->last_mirror,
  1551. failrec->bio_flags);
  1552. return 0;
  1553. }
  1554. /*
  1555. * each time an IO finishes, we do a fast check in the IO failure tree
  1556. * to see if we need to process or clean up an io_failure_record
  1557. */
  1558. static int btrfs_clean_io_failures(struct inode *inode, u64 start)
  1559. {
  1560. u64 private;
  1561. u64 private_failure;
  1562. struct io_failure_record *failure;
  1563. int ret;
  1564. private = 0;
  1565. if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
  1566. (u64)-1, 1, EXTENT_DIRTY)) {
  1567. ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
  1568. start, &private_failure);
  1569. if (ret == 0) {
  1570. failure = (struct io_failure_record *)(unsigned long)
  1571. private_failure;
  1572. set_state_private(&BTRFS_I(inode)->io_failure_tree,
  1573. failure->start, 0);
  1574. clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
  1575. failure->start,
  1576. failure->start + failure->len - 1,
  1577. EXTENT_DIRTY | EXTENT_LOCKED,
  1578. GFP_NOFS);
  1579. kfree(failure);
  1580. }
  1581. }
  1582. return 0;
  1583. }
  1584. /*
  1585. * when reads are done, we need to check csums to verify the data is correct
  1586. * if there's a match, we allow the bio to finish. If not, we go through
  1587. * the io_failure_record routines to find good copies
  1588. */
  1589. static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
  1590. struct extent_state *state)
  1591. {
  1592. size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
  1593. struct inode *inode = page->mapping->host;
  1594. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1595. char *kaddr;
  1596. u64 private = ~(u32)0;
  1597. int ret;
  1598. struct btrfs_root *root = BTRFS_I(inode)->root;
  1599. u32 csum = ~(u32)0;
  1600. if (PageChecked(page)) {
  1601. ClearPageChecked(page);
  1602. goto good;
  1603. }
  1604. if (btrfs_test_flag(inode, NODATASUM))
  1605. return 0;
  1606. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
  1607. test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1)) {
  1608. clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
  1609. GFP_NOFS);
  1610. return 0;
  1611. }
  1612. if (state && state->start == start) {
  1613. private = state->private;
  1614. ret = 0;
  1615. } else {
  1616. ret = get_state_private(io_tree, start, &private);
  1617. }
  1618. kaddr = kmap_atomic(page, KM_USER0);
  1619. if (ret)
  1620. goto zeroit;
  1621. csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
  1622. btrfs_csum_final(csum, (char *)&csum);
  1623. if (csum != private)
  1624. goto zeroit;
  1625. kunmap_atomic(kaddr, KM_USER0);
  1626. good:
  1627. /* if the io failure tree for this inode is non-empty,
  1628. * check to see if we've recovered from a failed IO
  1629. */
  1630. btrfs_clean_io_failures(inode, start);
  1631. return 0;
  1632. zeroit:
  1633. if (printk_ratelimit()) {
  1634. printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
  1635. "private %llu\n", page->mapping->host->i_ino,
  1636. (unsigned long long)start, csum,
  1637. (unsigned long long)private);
  1638. }
  1639. memset(kaddr + offset, 1, end - start + 1);
  1640. flush_dcache_page(page);
  1641. kunmap_atomic(kaddr, KM_USER0);
  1642. if (private == 0)
  1643. return 0;
  1644. return -EIO;
  1645. }
  1646. /*
  1647. * This creates an orphan entry for the given inode in case something goes
  1648. * wrong in the middle of an unlink/truncate.
  1649. */
  1650. int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
  1651. {
  1652. struct btrfs_root *root = BTRFS_I(inode)->root;
  1653. int ret = 0;
  1654. spin_lock(&root->list_lock);
  1655. /* already on the orphan list, we're good */
  1656. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  1657. spin_unlock(&root->list_lock);
  1658. return 0;
  1659. }
  1660. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  1661. spin_unlock(&root->list_lock);
  1662. /*
  1663. * insert an orphan item to track this unlinked/truncated file
  1664. */
  1665. ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
  1666. return ret;
  1667. }
  1668. /*
  1669. * We have done the truncate/delete so we can go ahead and remove the orphan
  1670. * item for this particular inode.
  1671. */
  1672. int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
  1673. {
  1674. struct btrfs_root *root = BTRFS_I(inode)->root;
  1675. int ret = 0;
  1676. spin_lock(&root->list_lock);
  1677. if (list_empty(&BTRFS_I(inode)->i_orphan)) {
  1678. spin_unlock(&root->list_lock);
  1679. return 0;
  1680. }
  1681. list_del_init(&BTRFS_I(inode)->i_orphan);
  1682. if (!trans) {
  1683. spin_unlock(&root->list_lock);
  1684. return 0;
  1685. }
  1686. spin_unlock(&root->list_lock);
  1687. ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
  1688. return ret;
  1689. }
  1690. /*
  1691. * this cleans up any orphans that may be left on the list from the last use
  1692. * of this root.
  1693. */
  1694. void btrfs_orphan_cleanup(struct btrfs_root *root)
  1695. {
  1696. struct btrfs_path *path;
  1697. struct extent_buffer *leaf;
  1698. struct btrfs_item *item;
  1699. struct btrfs_key key, found_key;
  1700. struct btrfs_trans_handle *trans;
  1701. struct inode *inode;
  1702. int ret = 0, nr_unlink = 0, nr_truncate = 0;
  1703. path = btrfs_alloc_path();
  1704. if (!path)
  1705. return;
  1706. path->reada = -1;
  1707. key.objectid = BTRFS_ORPHAN_OBJECTID;
  1708. btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
  1709. key.offset = (u64)-1;
  1710. while (1) {
  1711. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1712. if (ret < 0) {
  1713. printk(KERN_ERR "Error searching slot for orphan: %d"
  1714. "\n", ret);
  1715. break;
  1716. }
  1717. /*
  1718. * if ret == 0 means we found what we were searching for, which
  1719. * is weird, but possible, so only screw with path if we didnt
  1720. * find the key and see if we have stuff that matches
  1721. */
  1722. if (ret > 0) {
  1723. if (path->slots[0] == 0)
  1724. break;
  1725. path->slots[0]--;
  1726. }
  1727. /* pull out the item */
  1728. leaf = path->nodes[0];
  1729. item = btrfs_item_nr(leaf, path->slots[0]);
  1730. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1731. /* make sure the item matches what we want */
  1732. if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
  1733. break;
  1734. if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
  1735. break;
  1736. /* release the path since we're done with it */
  1737. btrfs_release_path(root, path);
  1738. /*
  1739. * this is where we are basically btrfs_lookup, without the
  1740. * crossing root thing. we store the inode number in the
  1741. * offset of the orphan item.
  1742. */
  1743. inode = btrfs_iget_locked(root->fs_info->sb,
  1744. found_key.offset, root);
  1745. if (!inode)
  1746. break;
  1747. if (inode->i_state & I_NEW) {
  1748. BTRFS_I(inode)->root = root;
  1749. /* have to set the location manually */
  1750. BTRFS_I(inode)->location.objectid = inode->i_ino;
  1751. BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
  1752. BTRFS_I(inode)->location.offset = 0;
  1753. btrfs_read_locked_inode(inode);
  1754. unlock_new_inode(inode);
  1755. }
  1756. /*
  1757. * add this inode to the orphan list so btrfs_orphan_del does
  1758. * the proper thing when we hit it
  1759. */
  1760. spin_lock(&root->list_lock);
  1761. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  1762. spin_unlock(&root->list_lock);
  1763. /*
  1764. * if this is a bad inode, means we actually succeeded in
  1765. * removing the inode, but not the orphan record, which means
  1766. * we need to manually delete the orphan since iput will just
  1767. * do a destroy_inode
  1768. */
  1769. if (is_bad_inode(inode)) {
  1770. trans = btrfs_start_transaction(root, 1);
  1771. btrfs_orphan_del(trans, inode);
  1772. btrfs_end_transaction(trans, root);
  1773. iput(inode);
  1774. continue;
  1775. }
  1776. /* if we have links, this was a truncate, lets do that */
  1777. if (inode->i_nlink) {
  1778. nr_truncate++;
  1779. btrfs_truncate(inode);
  1780. } else {
  1781. nr_unlink++;
  1782. }
  1783. /* this will do delete_inode and everything for us */
  1784. iput(inode);
  1785. }
  1786. if (nr_unlink)
  1787. printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
  1788. if (nr_truncate)
  1789. printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
  1790. btrfs_free_path(path);
  1791. }
  1792. /*
  1793. * very simple check to peek ahead in the leaf looking for xattrs. If we
  1794. * don't find any xattrs, we know there can't be any acls.
  1795. *
  1796. * slot is the slot the inode is in, objectid is the objectid of the inode
  1797. */
  1798. static noinline int acls_after_inode_item(struct extent_buffer *leaf,
  1799. int slot, u64 objectid)
  1800. {
  1801. u32 nritems = btrfs_header_nritems(leaf);
  1802. struct btrfs_key found_key;
  1803. int scanned = 0;
  1804. slot++;
  1805. while (slot < nritems) {
  1806. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  1807. /* we found a different objectid, there must not be acls */
  1808. if (found_key.objectid != objectid)
  1809. return 0;
  1810. /* we found an xattr, assume we've got an acl */
  1811. if (found_key.type == BTRFS_XATTR_ITEM_KEY)
  1812. return 1;
  1813. /*
  1814. * we found a key greater than an xattr key, there can't
  1815. * be any acls later on
  1816. */
  1817. if (found_key.type > BTRFS_XATTR_ITEM_KEY)
  1818. return 0;
  1819. slot++;
  1820. scanned++;
  1821. /*
  1822. * it goes inode, inode backrefs, xattrs, extents,
  1823. * so if there are a ton of hard links to an inode there can
  1824. * be a lot of backrefs. Don't waste time searching too hard,
  1825. * this is just an optimization
  1826. */
  1827. if (scanned >= 8)
  1828. break;
  1829. }
  1830. /* we hit the end of the leaf before we found an xattr or
  1831. * something larger than an xattr. We have to assume the inode
  1832. * has acls
  1833. */
  1834. return 1;
  1835. }
  1836. /*
  1837. * read an inode from the btree into the in-memory inode
  1838. */
  1839. void btrfs_read_locked_inode(struct inode *inode)
  1840. {
  1841. struct btrfs_path *path;
  1842. struct extent_buffer *leaf;
  1843. struct btrfs_inode_item *inode_item;
  1844. struct btrfs_timespec *tspec;
  1845. struct btrfs_root *root = BTRFS_I(inode)->root;
  1846. struct btrfs_key location;
  1847. int maybe_acls;
  1848. u64 alloc_group_block;
  1849. u32 rdev;
  1850. int ret;
  1851. path = btrfs_alloc_path();
  1852. BUG_ON(!path);
  1853. memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
  1854. ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
  1855. if (ret)
  1856. goto make_bad;
  1857. leaf = path->nodes[0];
  1858. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  1859. struct btrfs_inode_item);
  1860. inode->i_mode = btrfs_inode_mode(leaf, inode_item);
  1861. inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
  1862. inode->i_uid = btrfs_inode_uid(leaf, inode_item);
  1863. inode->i_gid = btrfs_inode_gid(leaf, inode_item);
  1864. btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
  1865. tspec = btrfs_inode_atime(inode_item);
  1866. inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  1867. inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  1868. tspec = btrfs_inode_mtime(inode_item);
  1869. inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  1870. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  1871. tspec = btrfs_inode_ctime(inode_item);
  1872. inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  1873. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  1874. inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
  1875. BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
  1876. BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
  1877. inode->i_generation = BTRFS_I(inode)->generation;
  1878. inode->i_rdev = 0;
  1879. rdev = btrfs_inode_rdev(leaf, inode_item);
  1880. BTRFS_I(inode)->index_cnt = (u64)-1;
  1881. BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
  1882. alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
  1883. /*
  1884. * try to precache a NULL acl entry for files that don't have
  1885. * any xattrs or acls
  1886. */
  1887. maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
  1888. if (!maybe_acls) {
  1889. BTRFS_I(inode)->i_acl = NULL;
  1890. BTRFS_I(inode)->i_default_acl = NULL;
  1891. }
  1892. BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
  1893. alloc_group_block, 0);
  1894. btrfs_free_path(path);
  1895. inode_item = NULL;
  1896. switch (inode->i_mode & S_IFMT) {
  1897. case S_IFREG:
  1898. inode->i_mapping->a_ops = &btrfs_aops;
  1899. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  1900. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  1901. inode->i_fop = &btrfs_file_operations;
  1902. inode->i_op = &btrfs_file_inode_operations;
  1903. break;
  1904. case S_IFDIR:
  1905. inode->i_fop = &btrfs_dir_file_operations;
  1906. if (root == root->fs_info->tree_root)
  1907. inode->i_op = &btrfs_dir_ro_inode_operations;
  1908. else
  1909. inode->i_op = &btrfs_dir_inode_operations;
  1910. break;
  1911. case S_IFLNK:
  1912. inode->i_op = &btrfs_symlink_inode_operations;
  1913. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  1914. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  1915. break;
  1916. default:
  1917. inode->i_op = &btrfs_special_inode_operations;
  1918. init_special_inode(inode, inode->i_mode, rdev);
  1919. break;
  1920. }
  1921. return;
  1922. make_bad:
  1923. btrfs_free_path(path);
  1924. make_bad_inode(inode);
  1925. }
  1926. /*
  1927. * given a leaf and an inode, copy the inode fields into the leaf
  1928. */
  1929. static void fill_inode_item(struct btrfs_trans_handle *trans,
  1930. struct extent_buffer *leaf,
  1931. struct btrfs_inode_item *item,
  1932. struct inode *inode)
  1933. {
  1934. btrfs_set_inode_uid(leaf, item, inode->i_uid);
  1935. btrfs_set_inode_gid(leaf, item, inode->i_gid);
  1936. btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
  1937. btrfs_set_inode_mode(leaf, item, inode->i_mode);
  1938. btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
  1939. btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
  1940. inode->i_atime.tv_sec);
  1941. btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
  1942. inode->i_atime.tv_nsec);
  1943. btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
  1944. inode->i_mtime.tv_sec);
  1945. btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
  1946. inode->i_mtime.tv_nsec);
  1947. btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
  1948. inode->i_ctime.tv_sec);
  1949. btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
  1950. inode->i_ctime.tv_nsec);
  1951. btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
  1952. btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
  1953. btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
  1954. btrfs_set_inode_transid(leaf, item, trans->transid);
  1955. btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
  1956. btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
  1957. btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
  1958. }
  1959. /*
  1960. * copy everything in the in-memory inode into the btree.
  1961. */
  1962. noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
  1963. struct btrfs_root *root, struct inode *inode)
  1964. {
  1965. struct btrfs_inode_item *inode_item;
  1966. struct btrfs_path *path;
  1967. struct extent_buffer *leaf;
  1968. int ret;
  1969. path = btrfs_alloc_path();
  1970. BUG_ON(!path);
  1971. path->leave_spinning = 1;
  1972. ret = btrfs_lookup_inode(trans, root, path,
  1973. &BTRFS_I(inode)->location, 1);
  1974. if (ret) {
  1975. if (ret > 0)
  1976. ret = -ENOENT;
  1977. goto failed;
  1978. }
  1979. btrfs_unlock_up_safe(path, 1);
  1980. leaf = path->nodes[0];
  1981. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  1982. struct btrfs_inode_item);
  1983. fill_inode_item(trans, leaf, inode_item, inode);
  1984. btrfs_mark_buffer_dirty(leaf);
  1985. btrfs_set_inode_last_trans(trans, inode);
  1986. ret = 0;
  1987. failed:
  1988. btrfs_free_path(path);
  1989. return ret;
  1990. }
  1991. /*
  1992. * unlink helper that gets used here in inode.c and in the tree logging
  1993. * recovery code. It remove a link in a directory with a given name, and
  1994. * also drops the back refs in the inode to the directory
  1995. */
  1996. int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  1997. struct btrfs_root *root,
  1998. struct inode *dir, struct inode *inode,
  1999. const char *name, int name_len)
  2000. {
  2001. struct btrfs_path *path;
  2002. int ret = 0;
  2003. struct extent_buffer *leaf;
  2004. struct btrfs_dir_item *di;
  2005. struct btrfs_key key;
  2006. u64 index;
  2007. path = btrfs_alloc_path();
  2008. if (!path) {
  2009. ret = -ENOMEM;
  2010. goto err;
  2011. }
  2012. path->leave_spinning = 1;
  2013. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  2014. name, name_len, -1);
  2015. if (IS_ERR(di)) {
  2016. ret = PTR_ERR(di);
  2017. goto err;
  2018. }
  2019. if (!di) {
  2020. ret = -ENOENT;
  2021. goto err;
  2022. }
  2023. leaf = path->nodes[0];
  2024. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2025. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2026. if (ret)
  2027. goto err;
  2028. btrfs_release_path(root, path);
  2029. ret = btrfs_del_inode_ref(trans, root, name, name_len,
  2030. inode->i_ino,
  2031. dir->i_ino, &index);
  2032. if (ret) {
  2033. printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
  2034. "inode %lu parent %lu\n", name_len, name,
  2035. inode->i_ino, dir->i_ino);
  2036. goto err;
  2037. }
  2038. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  2039. index, name, name_len, -1);
  2040. if (IS_ERR(di)) {
  2041. ret = PTR_ERR(di);
  2042. goto err;
  2043. }
  2044. if (!di) {
  2045. ret = -ENOENT;
  2046. goto err;
  2047. }
  2048. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2049. btrfs_release_path(root, path);
  2050. ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
  2051. inode, dir->i_ino);
  2052. BUG_ON(ret != 0 && ret != -ENOENT);
  2053. ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
  2054. dir, index);
  2055. BUG_ON(ret);
  2056. err:
  2057. btrfs_free_path(path);
  2058. if (ret)
  2059. goto out;
  2060. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  2061. inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  2062. btrfs_update_inode(trans, root, dir);
  2063. btrfs_drop_nlink(inode);
  2064. ret = btrfs_update_inode(trans, root, inode);
  2065. dir->i_sb->s_dirt = 1;
  2066. out:
  2067. return ret;
  2068. }
  2069. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  2070. {
  2071. struct btrfs_root *root;
  2072. struct btrfs_trans_handle *trans;
  2073. struct inode *inode = dentry->d_inode;
  2074. int ret;
  2075. unsigned long nr = 0;
  2076. root = BTRFS_I(dir)->root;
  2077. trans = btrfs_start_transaction(root, 1);
  2078. btrfs_set_trans_block_group(trans, dir);
  2079. btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
  2080. ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  2081. dentry->d_name.name, dentry->d_name.len);
  2082. if (inode->i_nlink == 0)
  2083. ret = btrfs_orphan_add(trans, inode);
  2084. nr = trans->blocks_used;
  2085. btrfs_end_transaction_throttle(trans, root);
  2086. btrfs_btree_balance_dirty(root, nr);
  2087. return ret;
  2088. }
  2089. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  2090. {
  2091. struct inode *inode = dentry->d_inode;
  2092. int err = 0;
  2093. int ret;
  2094. struct btrfs_root *root = BTRFS_I(dir)->root;
  2095. struct btrfs_trans_handle *trans;
  2096. unsigned long nr = 0;
  2097. /*
  2098. * the FIRST_FREE_OBJECTID check makes sure we don't try to rmdir
  2099. * the root of a subvolume or snapshot
  2100. */
  2101. if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
  2102. inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
  2103. return -ENOTEMPTY;
  2104. }
  2105. trans = btrfs_start_transaction(root, 1);
  2106. btrfs_set_trans_block_group(trans, dir);
  2107. err = btrfs_orphan_add(trans, inode);
  2108. if (err)
  2109. goto fail_trans;
  2110. /* now the directory is empty */
  2111. err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  2112. dentry->d_name.name, dentry->d_name.len);
  2113. if (!err)
  2114. btrfs_i_size_write(inode, 0);
  2115. fail_trans:
  2116. nr = trans->blocks_used;
  2117. ret = btrfs_end_transaction_throttle(trans, root);
  2118. btrfs_btree_balance_dirty(root, nr);
  2119. if (ret && !err)
  2120. err = ret;
  2121. return err;
  2122. }
  2123. #if 0
  2124. /*
  2125. * when truncating bytes in a file, it is possible to avoid reading
  2126. * the leaves that contain only checksum items. This can be the
  2127. * majority of the IO required to delete a large file, but it must
  2128. * be done carefully.
  2129. *
  2130. * The keys in the level just above the leaves are checked to make sure
  2131. * the lowest key in a given leaf is a csum key, and starts at an offset
  2132. * after the new size.
  2133. *
  2134. * Then the key for the next leaf is checked to make sure it also has
  2135. * a checksum item for the same file. If it does, we know our target leaf
  2136. * contains only checksum items, and it can be safely freed without reading
  2137. * it.
  2138. *
  2139. * This is just an optimization targeted at large files. It may do
  2140. * nothing. It will return 0 unless things went badly.
  2141. */
  2142. static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
  2143. struct btrfs_root *root,
  2144. struct btrfs_path *path,
  2145. struct inode *inode, u64 new_size)
  2146. {
  2147. struct btrfs_key key;
  2148. int ret;
  2149. int nritems;
  2150. struct btrfs_key found_key;
  2151. struct btrfs_key other_key;
  2152. struct btrfs_leaf_ref *ref;
  2153. u64 leaf_gen;
  2154. u64 leaf_start;
  2155. path->lowest_level = 1;
  2156. key.objectid = inode->i_ino;
  2157. key.type = BTRFS_CSUM_ITEM_KEY;
  2158. key.offset = new_size;
  2159. again:
  2160. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2161. if (ret < 0)
  2162. goto out;
  2163. if (path->nodes[1] == NULL) {
  2164. ret = 0;
  2165. goto out;
  2166. }
  2167. ret = 0;
  2168. btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
  2169. nritems = btrfs_header_nritems(path->nodes[1]);
  2170. if (!nritems)
  2171. goto out;
  2172. if (path->slots[1] >= nritems)
  2173. goto next_node;
  2174. /* did we find a key greater than anything we want to delete? */
  2175. if (found_key.objectid > inode->i_ino ||
  2176. (found_key.objectid == inode->i_ino && found_key.type > key.type))
  2177. goto out;
  2178. /* we check the next key in the node to make sure the leave contains
  2179. * only checksum items. This comparison doesn't work if our
  2180. * leaf is the last one in the node
  2181. */
  2182. if (path->slots[1] + 1 >= nritems) {
  2183. next_node:
  2184. /* search forward from the last key in the node, this
  2185. * will bring us into the next node in the tree
  2186. */
  2187. btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
  2188. /* unlikely, but we inc below, so check to be safe */
  2189. if (found_key.offset == (u64)-1)
  2190. goto out;
  2191. /* search_forward needs a path with locks held, do the
  2192. * search again for the original key. It is possible
  2193. * this will race with a balance and return a path that
  2194. * we could modify, but this drop is just an optimization
  2195. * and is allowed to miss some leaves.
  2196. */
  2197. btrfs_release_path(root, path);
  2198. found_key.offset++;
  2199. /* setup a max key for search_forward */
  2200. other_key.offset = (u64)-1;
  2201. other_key.type = key.type;
  2202. other_key.objectid = key.objectid;
  2203. path->keep_locks = 1;
  2204. ret = btrfs_search_forward(root, &found_key, &other_key,
  2205. path, 0, 0);
  2206. path->keep_locks = 0;
  2207. if (ret || found_key.objectid != key.objectid ||
  2208. found_key.type != key.type) {
  2209. ret = 0;
  2210. goto out;
  2211. }
  2212. key.offset = found_key.offset;
  2213. btrfs_release_path(root, path);
  2214. cond_resched();
  2215. goto again;
  2216. }
  2217. /* we know there's one more slot after us in the tree,
  2218. * read that key so we can verify it is also a checksum item
  2219. */
  2220. btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
  2221. if (found_key.objectid < inode->i_ino)
  2222. goto next_key;
  2223. if (found_key.type != key.type || found_key.offset < new_size)
  2224. goto next_key;
  2225. /*
  2226. * if the key for the next leaf isn't a csum key from this objectid,
  2227. * we can't be sure there aren't good items inside this leaf.
  2228. * Bail out
  2229. */
  2230. if (other_key.objectid != inode->i_ino || other_key.type != key.type)
  2231. goto out;
  2232. leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
  2233. leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
  2234. /*
  2235. * it is safe to delete this leaf, it contains only
  2236. * csum items from this inode at an offset >= new_size
  2237. */
  2238. ret = btrfs_del_leaf(trans, root, path, leaf_start);
  2239. BUG_ON(ret);
  2240. if (root->ref_cows && leaf_gen < trans->transid) {
  2241. ref = btrfs_alloc_leaf_ref(root, 0);
  2242. if (ref) {
  2243. ref->root_gen = root->root_key.offset;
  2244. ref->bytenr = leaf_start;
  2245. ref->owner = 0;
  2246. ref->generation = leaf_gen;
  2247. ref->nritems = 0;
  2248. btrfs_sort_leaf_ref(ref);
  2249. ret = btrfs_add_leaf_ref(root, ref, 0);
  2250. WARN_ON(ret);
  2251. btrfs_free_leaf_ref(root, ref);
  2252. } else {
  2253. WARN_ON(1);
  2254. }
  2255. }
  2256. next_key:
  2257. btrfs_release_path(root, path);
  2258. if (other_key.objectid == inode->i_ino &&
  2259. other_key.type == key.type && other_key.offset > key.offset) {
  2260. key.offset = other_key.offset;
  2261. cond_resched();
  2262. goto again;
  2263. }
  2264. ret = 0;
  2265. out:
  2266. /* fixup any changes we've made to the path */
  2267. path->lowest_level = 0;
  2268. path->keep_locks = 0;
  2269. btrfs_release_path(root, path);
  2270. return ret;
  2271. }
  2272. #endif
  2273. /*
  2274. * this can truncate away extent items, csum items and directory items.
  2275. * It starts at a high offset and removes keys until it can't find
  2276. * any higher than new_size
  2277. *
  2278. * csum items that cross the new i_size are truncated to the new size
  2279. * as well.
  2280. *
  2281. * min_type is the minimum key type to truncate down to. If set to 0, this
  2282. * will kill all the items on this inode, including the INODE_ITEM_KEY.
  2283. */
  2284. noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
  2285. struct btrfs_root *root,
  2286. struct inode *inode,
  2287. u64 new_size, u32 min_type)
  2288. {
  2289. int ret;
  2290. struct btrfs_path *path;
  2291. struct btrfs_key key;
  2292. struct btrfs_key found_key;
  2293. u32 found_type = (u8)-1;
  2294. struct extent_buffer *leaf;
  2295. struct btrfs_file_extent_item *fi;
  2296. u64 extent_start = 0;
  2297. u64 extent_num_bytes = 0;
  2298. u64 item_end = 0;
  2299. u64 root_gen = 0;
  2300. u64 root_owner = 0;
  2301. int found_extent;
  2302. int del_item;
  2303. int pending_del_nr = 0;
  2304. int pending_del_slot = 0;
  2305. int extent_type = -1;
  2306. int encoding;
  2307. u64 mask = root->sectorsize - 1;
  2308. if (root->ref_cows)
  2309. btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
  2310. path = btrfs_alloc_path();
  2311. path->reada = -1;
  2312. BUG_ON(!path);
  2313. /* FIXME, add redo link to tree so we don't leak on crash */
  2314. key.objectid = inode->i_ino;
  2315. key.offset = (u64)-1;
  2316. key.type = (u8)-1;
  2317. search_again:
  2318. path->leave_spinning = 1;
  2319. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2320. if (ret < 0)
  2321. goto error;
  2322. if (ret > 0) {
  2323. /* there are no items in the tree for us to truncate, we're
  2324. * done
  2325. */
  2326. if (path->slots[0] == 0) {
  2327. ret = 0;
  2328. goto error;
  2329. }
  2330. path->slots[0]--;
  2331. }
  2332. while (1) {
  2333. fi = NULL;
  2334. leaf = path->nodes[0];
  2335. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2336. found_type = btrfs_key_type(&found_key);
  2337. encoding = 0;
  2338. if (found_key.objectid != inode->i_ino)
  2339. break;
  2340. if (found_type < min_type)
  2341. break;
  2342. item_end = found_key.offset;
  2343. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  2344. fi = btrfs_item_ptr(leaf, path->slots[0],
  2345. struct btrfs_file_extent_item);
  2346. extent_type = btrfs_file_extent_type(leaf, fi);
  2347. encoding = btrfs_file_extent_compression(leaf, fi);
  2348. encoding |= btrfs_file_extent_encryption(leaf, fi);
  2349. encoding |= btrfs_file_extent_other_encoding(leaf, fi);
  2350. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2351. item_end +=
  2352. btrfs_file_extent_num_bytes(leaf, fi);
  2353. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2354. item_end += btrfs_file_extent_inline_len(leaf,
  2355. fi);
  2356. }
  2357. item_end--;
  2358. }
  2359. if (item_end < new_size) {
  2360. if (found_type == BTRFS_DIR_ITEM_KEY)
  2361. found_type = BTRFS_INODE_ITEM_KEY;
  2362. else if (found_type == BTRFS_EXTENT_ITEM_KEY)
  2363. found_type = BTRFS_EXTENT_DATA_KEY;
  2364. else if (found_type == BTRFS_EXTENT_DATA_KEY)
  2365. found_type = BTRFS_XATTR_ITEM_KEY;
  2366. else if (found_type == BTRFS_XATTR_ITEM_KEY)
  2367. found_type = BTRFS_INODE_REF_KEY;
  2368. else if (found_type)
  2369. found_type--;
  2370. else
  2371. break;
  2372. btrfs_set_key_type(&key, found_type);
  2373. goto next;
  2374. }
  2375. if (found_key.offset >= new_size)
  2376. del_item = 1;
  2377. else
  2378. del_item = 0;
  2379. found_extent = 0;
  2380. /* FIXME, shrink the extent if the ref count is only 1 */
  2381. if (found_type != BTRFS_EXTENT_DATA_KEY)
  2382. goto delete;
  2383. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2384. u64 num_dec;
  2385. extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
  2386. if (!del_item && !encoding) {
  2387. u64 orig_num_bytes =
  2388. btrfs_file_extent_num_bytes(leaf, fi);
  2389. extent_num_bytes = new_size -
  2390. found_key.offset + root->sectorsize - 1;
  2391. extent_num_bytes = extent_num_bytes &
  2392. ~((u64)root->sectorsize - 1);
  2393. btrfs_set_file_extent_num_bytes(leaf, fi,
  2394. extent_num_bytes);
  2395. num_dec = (orig_num_bytes -
  2396. extent_num_bytes);
  2397. if (root->ref_cows && extent_start != 0)
  2398. inode_sub_bytes(inode, num_dec);
  2399. btrfs_mark_buffer_dirty(leaf);
  2400. } else {
  2401. extent_num_bytes =
  2402. btrfs_file_extent_disk_num_bytes(leaf,
  2403. fi);
  2404. /* FIXME blocksize != 4096 */
  2405. num_dec = btrfs_file_extent_num_bytes(leaf, fi);
  2406. if (extent_start != 0) {
  2407. found_extent = 1;
  2408. if (root->ref_cows)
  2409. inode_sub_bytes(inode, num_dec);
  2410. }
  2411. root_gen = btrfs_header_generation(leaf);
  2412. root_owner = btrfs_header_owner(leaf);
  2413. }
  2414. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2415. /*
  2416. * we can't truncate inline items that have had
  2417. * special encodings
  2418. */
  2419. if (!del_item &&
  2420. btrfs_file_extent_compression(leaf, fi) == 0 &&
  2421. btrfs_file_extent_encryption(leaf, fi) == 0 &&
  2422. btrfs_file_extent_other_encoding(leaf, fi) == 0) {
  2423. u32 size = new_size - found_key.offset;
  2424. if (root->ref_cows) {
  2425. inode_sub_bytes(inode, item_end + 1 -
  2426. new_size);
  2427. }
  2428. size =
  2429. btrfs_file_extent_calc_inline_size(size);
  2430. ret = btrfs_truncate_item(trans, root, path,
  2431. size, 1);
  2432. BUG_ON(ret);
  2433. } else if (root->ref_cows) {
  2434. inode_sub_bytes(inode, item_end + 1 -
  2435. found_key.offset);
  2436. }
  2437. }
  2438. delete:
  2439. if (del_item) {
  2440. if (!pending_del_nr) {
  2441. /* no pending yet, add ourselves */
  2442. pending_del_slot = path->slots[0];
  2443. pending_del_nr = 1;
  2444. } else if (pending_del_nr &&
  2445. path->slots[0] + 1 == pending_del_slot) {
  2446. /* hop on the pending chunk */
  2447. pending_del_nr++;
  2448. pending_del_slot = path->slots[0];
  2449. } else {
  2450. BUG();
  2451. }
  2452. } else {
  2453. break;
  2454. }
  2455. if (found_extent) {
  2456. btrfs_set_path_blocking(path);
  2457. ret = btrfs_free_extent(trans, root, extent_start,
  2458. extent_num_bytes,
  2459. leaf->start, root_owner,
  2460. root_gen, inode->i_ino, 0);
  2461. BUG_ON(ret);
  2462. }
  2463. next:
  2464. if (path->slots[0] == 0) {
  2465. if (pending_del_nr)
  2466. goto del_pending;
  2467. btrfs_release_path(root, path);
  2468. if (found_type == BTRFS_INODE_ITEM_KEY)
  2469. break;
  2470. goto search_again;
  2471. }
  2472. path->slots[0]--;
  2473. if (pending_del_nr &&
  2474. path->slots[0] + 1 != pending_del_slot) {
  2475. struct btrfs_key debug;
  2476. del_pending:
  2477. btrfs_item_key_to_cpu(path->nodes[0], &debug,
  2478. pending_del_slot);
  2479. ret = btrfs_del_items(trans, root, path,
  2480. pending_del_slot,
  2481. pending_del_nr);
  2482. BUG_ON(ret);
  2483. pending_del_nr = 0;
  2484. btrfs_release_path(root, path);
  2485. if (found_type == BTRFS_INODE_ITEM_KEY)
  2486. break;
  2487. goto search_again;
  2488. }
  2489. }
  2490. ret = 0;
  2491. error:
  2492. if (pending_del_nr) {
  2493. ret = btrfs_del_items(trans, root, path, pending_del_slot,
  2494. pending_del_nr);
  2495. }
  2496. btrfs_free_path(path);
  2497. inode->i_sb->s_dirt = 1;
  2498. return ret;
  2499. }
  2500. /*
  2501. * taken from block_truncate_page, but does cow as it zeros out
  2502. * any bytes left in the last page in the file.
  2503. */
  2504. static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
  2505. {
  2506. struct inode *inode = mapping->host;
  2507. struct btrfs_root *root = BTRFS_I(inode)->root;
  2508. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2509. struct btrfs_ordered_extent *ordered;
  2510. char *kaddr;
  2511. u32 blocksize = root->sectorsize;
  2512. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  2513. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  2514. struct page *page;
  2515. int ret = 0;
  2516. u64 page_start;
  2517. u64 page_end;
  2518. if ((offset & (blocksize - 1)) == 0)
  2519. goto out;
  2520. ret = -ENOMEM;
  2521. again:
  2522. page = grab_cache_page(mapping, index);
  2523. if (!page)
  2524. goto out;
  2525. page_start = page_offset(page);
  2526. page_end = page_start + PAGE_CACHE_SIZE - 1;
  2527. if (!PageUptodate(page)) {
  2528. ret = btrfs_readpage(NULL, page);
  2529. lock_page(page);
  2530. if (page->mapping != mapping) {
  2531. unlock_page(page);
  2532. page_cache_release(page);
  2533. goto again;
  2534. }
  2535. if (!PageUptodate(page)) {
  2536. ret = -EIO;
  2537. goto out_unlock;
  2538. }
  2539. }
  2540. wait_on_page_writeback(page);
  2541. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2542. set_page_extent_mapped(page);
  2543. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  2544. if (ordered) {
  2545. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2546. unlock_page(page);
  2547. page_cache_release(page);
  2548. btrfs_start_ordered_extent(inode, ordered, 1);
  2549. btrfs_put_ordered_extent(ordered);
  2550. goto again;
  2551. }
  2552. btrfs_set_extent_delalloc(inode, page_start, page_end);
  2553. ret = 0;
  2554. if (offset != PAGE_CACHE_SIZE) {
  2555. kaddr = kmap(page);
  2556. memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
  2557. flush_dcache_page(page);
  2558. kunmap(page);
  2559. }
  2560. ClearPageChecked(page);
  2561. set_page_dirty(page);
  2562. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2563. out_unlock:
  2564. unlock_page(page);
  2565. page_cache_release(page);
  2566. out:
  2567. return ret;
  2568. }
  2569. int btrfs_cont_expand(struct inode *inode, loff_t size)
  2570. {
  2571. struct btrfs_trans_handle *trans;
  2572. struct btrfs_root *root = BTRFS_I(inode)->root;
  2573. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2574. struct extent_map *em;
  2575. u64 mask = root->sectorsize - 1;
  2576. u64 hole_start = (inode->i_size + mask) & ~mask;
  2577. u64 block_end = (size + mask) & ~mask;
  2578. u64 last_byte;
  2579. u64 cur_offset;
  2580. u64 hole_size;
  2581. int err;
  2582. if (size <= hole_start)
  2583. return 0;
  2584. err = btrfs_check_metadata_free_space(root);
  2585. if (err)
  2586. return err;
  2587. btrfs_truncate_page(inode->i_mapping, inode->i_size);
  2588. while (1) {
  2589. struct btrfs_ordered_extent *ordered;
  2590. btrfs_wait_ordered_range(inode, hole_start,
  2591. block_end - hole_start);
  2592. lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  2593. ordered = btrfs_lookup_ordered_extent(inode, hole_start);
  2594. if (!ordered)
  2595. break;
  2596. unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  2597. btrfs_put_ordered_extent(ordered);
  2598. }
  2599. trans = btrfs_start_transaction(root, 1);
  2600. btrfs_set_trans_block_group(trans, inode);
  2601. cur_offset = hole_start;
  2602. while (1) {
  2603. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  2604. block_end - cur_offset, 0);
  2605. BUG_ON(IS_ERR(em) || !em);
  2606. last_byte = min(extent_map_end(em), block_end);
  2607. last_byte = (last_byte + mask) & ~mask;
  2608. if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
  2609. u64 hint_byte = 0;
  2610. hole_size = last_byte - cur_offset;
  2611. err = btrfs_drop_extents(trans, root, inode,
  2612. cur_offset,
  2613. cur_offset + hole_size,
  2614. block_end,
  2615. cur_offset, &hint_byte);
  2616. if (err)
  2617. break;
  2618. err = btrfs_insert_file_extent(trans, root,
  2619. inode->i_ino, cur_offset, 0,
  2620. 0, hole_size, 0, hole_size,
  2621. 0, 0, 0);
  2622. btrfs_drop_extent_cache(inode, hole_start,
  2623. last_byte - 1, 0);
  2624. }
  2625. free_extent_map(em);
  2626. cur_offset = last_byte;
  2627. if (err || cur_offset >= block_end)
  2628. break;
  2629. }
  2630. btrfs_end_transaction(trans, root);
  2631. unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  2632. return err;
  2633. }
  2634. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  2635. {
  2636. struct inode *inode = dentry->d_inode;
  2637. int err;
  2638. err = inode_change_ok(inode, attr);
  2639. if (err)
  2640. return err;
  2641. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  2642. if (attr->ia_size > inode->i_size) {
  2643. err = btrfs_cont_expand(inode, attr->ia_size);
  2644. if (err)
  2645. return err;
  2646. } else if (inode->i_size > 0 &&
  2647. attr->ia_size == 0) {
  2648. /* we're truncating a file that used to have good
  2649. * data down to zero. Make sure it gets into
  2650. * the ordered flush list so that any new writes
  2651. * get down to disk quickly.
  2652. */
  2653. BTRFS_I(inode)->ordered_data_close = 1;
  2654. }
  2655. }
  2656. err = inode_setattr(inode, attr);
  2657. if (!err && ((attr->ia_valid & ATTR_MODE)))
  2658. err = btrfs_acl_chmod(inode);
  2659. return err;
  2660. }
  2661. void btrfs_delete_inode(struct inode *inode)
  2662. {
  2663. struct btrfs_trans_handle *trans;
  2664. struct btrfs_root *root = BTRFS_I(inode)->root;
  2665. unsigned long nr;
  2666. int ret;
  2667. truncate_inode_pages(&inode->i_data, 0);
  2668. if (is_bad_inode(inode)) {
  2669. btrfs_orphan_del(NULL, inode);
  2670. goto no_delete;
  2671. }
  2672. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  2673. btrfs_i_size_write(inode, 0);
  2674. trans = btrfs_join_transaction(root, 1);
  2675. btrfs_set_trans_block_group(trans, inode);
  2676. ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
  2677. if (ret) {
  2678. btrfs_orphan_del(NULL, inode);
  2679. goto no_delete_lock;
  2680. }
  2681. btrfs_orphan_del(trans, inode);
  2682. nr = trans->blocks_used;
  2683. clear_inode(inode);
  2684. btrfs_end_transaction(trans, root);
  2685. btrfs_btree_balance_dirty(root, nr);
  2686. return;
  2687. no_delete_lock:
  2688. nr = trans->blocks_used;
  2689. btrfs_end_transaction(trans, root);
  2690. btrfs_btree_balance_dirty(root, nr);
  2691. no_delete:
  2692. clear_inode(inode);
  2693. }
  2694. /*
  2695. * this returns the key found in the dir entry in the location pointer.
  2696. * If no dir entries were found, location->objectid is 0.
  2697. */
  2698. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  2699. struct btrfs_key *location)
  2700. {
  2701. const char *name = dentry->d_name.name;
  2702. int namelen = dentry->d_name.len;
  2703. struct btrfs_dir_item *di;
  2704. struct btrfs_path *path;
  2705. struct btrfs_root *root = BTRFS_I(dir)->root;
  2706. int ret = 0;
  2707. path = btrfs_alloc_path();
  2708. BUG_ON(!path);
  2709. di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
  2710. namelen, 0);
  2711. if (IS_ERR(di))
  2712. ret = PTR_ERR(di);
  2713. if (!di || IS_ERR(di))
  2714. goto out_err;
  2715. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  2716. out:
  2717. btrfs_free_path(path);
  2718. return ret;
  2719. out_err:
  2720. location->objectid = 0;
  2721. goto out;
  2722. }
  2723. /*
  2724. * when we hit a tree root in a directory, the btrfs part of the inode
  2725. * needs to be changed to reflect the root directory of the tree root. This
  2726. * is kind of like crossing a mount point.
  2727. */
  2728. static int fixup_tree_root_location(struct btrfs_root *root,
  2729. struct btrfs_key *location,
  2730. struct btrfs_root **sub_root,
  2731. struct dentry *dentry)
  2732. {
  2733. struct btrfs_root_item *ri;
  2734. if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
  2735. return 0;
  2736. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  2737. return 0;
  2738. *sub_root = btrfs_read_fs_root(root->fs_info, location,
  2739. dentry->d_name.name,
  2740. dentry->d_name.len);
  2741. if (IS_ERR(*sub_root))
  2742. return PTR_ERR(*sub_root);
  2743. ri = &(*sub_root)->root_item;
  2744. location->objectid = btrfs_root_dirid(ri);
  2745. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  2746. location->offset = 0;
  2747. return 0;
  2748. }
  2749. static noinline void init_btrfs_i(struct inode *inode)
  2750. {
  2751. struct btrfs_inode *bi = BTRFS_I(inode);
  2752. bi->i_acl = BTRFS_ACL_NOT_CACHED;
  2753. bi->i_default_acl = BTRFS_ACL_NOT_CACHED;
  2754. bi->generation = 0;
  2755. bi->sequence = 0;
  2756. bi->last_trans = 0;
  2757. bi->logged_trans = 0;
  2758. bi->delalloc_bytes = 0;
  2759. bi->reserved_bytes = 0;
  2760. bi->disk_i_size = 0;
  2761. bi->flags = 0;
  2762. bi->index_cnt = (u64)-1;
  2763. bi->last_unlink_trans = 0;
  2764. bi->ordered_data_close = 0;
  2765. extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
  2766. extent_io_tree_init(&BTRFS_I(inode)->io_tree,
  2767. inode->i_mapping, GFP_NOFS);
  2768. extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
  2769. inode->i_mapping, GFP_NOFS);
  2770. INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
  2771. INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations);
  2772. btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
  2773. mutex_init(&BTRFS_I(inode)->extent_mutex);
  2774. mutex_init(&BTRFS_I(inode)->log_mutex);
  2775. }
  2776. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  2777. {
  2778. struct btrfs_iget_args *args = p;
  2779. inode->i_ino = args->ino;
  2780. init_btrfs_i(inode);
  2781. BTRFS_I(inode)->root = args->root;
  2782. btrfs_set_inode_space_info(args->root, inode);
  2783. return 0;
  2784. }
  2785. static int btrfs_find_actor(struct inode *inode, void *opaque)
  2786. {
  2787. struct btrfs_iget_args *args = opaque;
  2788. return args->ino == inode->i_ino &&
  2789. args->root == BTRFS_I(inode)->root;
  2790. }
  2791. struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
  2792. struct btrfs_root *root, int wait)
  2793. {
  2794. struct inode *inode;
  2795. struct btrfs_iget_args args;
  2796. args.ino = objectid;
  2797. args.root = root;
  2798. if (wait) {
  2799. inode = ilookup5(s, objectid, btrfs_find_actor,
  2800. (void *)&args);
  2801. } else {
  2802. inode = ilookup5_nowait(s, objectid, btrfs_find_actor,
  2803. (void *)&args);
  2804. }
  2805. return inode;
  2806. }
  2807. struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
  2808. struct btrfs_root *root)
  2809. {
  2810. struct inode *inode;
  2811. struct btrfs_iget_args args;
  2812. args.ino = objectid;
  2813. args.root = root;
  2814. inode = iget5_locked(s, objectid, btrfs_find_actor,
  2815. btrfs_init_locked_inode,
  2816. (void *)&args);
  2817. return inode;
  2818. }
  2819. /* Get an inode object given its location and corresponding root.
  2820. * Returns in *is_new if the inode was read from disk
  2821. */
  2822. struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
  2823. struct btrfs_root *root, int *is_new)
  2824. {
  2825. struct inode *inode;
  2826. inode = btrfs_iget_locked(s, location->objectid, root);
  2827. if (!inode)
  2828. return ERR_PTR(-EACCES);
  2829. if (inode->i_state & I_NEW) {
  2830. BTRFS_I(inode)->root = root;
  2831. memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
  2832. btrfs_read_locked_inode(inode);
  2833. unlock_new_inode(inode);
  2834. if (is_new)
  2835. *is_new = 1;
  2836. } else {
  2837. if (is_new)
  2838. *is_new = 0;
  2839. }
  2840. return inode;
  2841. }
  2842. struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
  2843. {
  2844. struct inode *inode;
  2845. struct btrfs_inode *bi = BTRFS_I(dir);
  2846. struct btrfs_root *root = bi->root;
  2847. struct btrfs_root *sub_root = root;
  2848. struct btrfs_key location;
  2849. int ret, new;
  2850. if (dentry->d_name.len > BTRFS_NAME_LEN)
  2851. return ERR_PTR(-ENAMETOOLONG);
  2852. ret = btrfs_inode_by_name(dir, dentry, &location);
  2853. if (ret < 0)
  2854. return ERR_PTR(ret);
  2855. inode = NULL;
  2856. if (location.objectid) {
  2857. ret = fixup_tree_root_location(root, &location, &sub_root,
  2858. dentry);
  2859. if (ret < 0)
  2860. return ERR_PTR(ret);
  2861. if (ret > 0)
  2862. return ERR_PTR(-ENOENT);
  2863. inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
  2864. if (IS_ERR(inode))
  2865. return ERR_CAST(inode);
  2866. }
  2867. return inode;
  2868. }
  2869. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  2870. struct nameidata *nd)
  2871. {
  2872. struct inode *inode;
  2873. if (dentry->d_name.len > BTRFS_NAME_LEN)
  2874. return ERR_PTR(-ENAMETOOLONG);
  2875. inode = btrfs_lookup_dentry(dir, dentry);
  2876. if (IS_ERR(inode))
  2877. return ERR_CAST(inode);
  2878. return d_splice_alias(inode, dentry);
  2879. }
  2880. static unsigned char btrfs_filetype_table[] = {
  2881. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  2882. };
  2883. static int btrfs_real_readdir(struct file *filp, void *dirent,
  2884. filldir_t filldir)
  2885. {
  2886. struct inode *inode = filp->f_dentry->d_inode;
  2887. struct btrfs_root *root = BTRFS_I(inode)->root;
  2888. struct btrfs_item *item;
  2889. struct btrfs_dir_item *di;
  2890. struct btrfs_key key;
  2891. struct btrfs_key found_key;
  2892. struct btrfs_path *path;
  2893. int ret;
  2894. u32 nritems;
  2895. struct extent_buffer *leaf;
  2896. int slot;
  2897. int advance;
  2898. unsigned char d_type;
  2899. int over = 0;
  2900. u32 di_cur;
  2901. u32 di_total;
  2902. u32 di_len;
  2903. int key_type = BTRFS_DIR_INDEX_KEY;
  2904. char tmp_name[32];
  2905. char *name_ptr;
  2906. int name_len;
  2907. /* FIXME, use a real flag for deciding about the key type */
  2908. if (root->fs_info->tree_root == root)
  2909. key_type = BTRFS_DIR_ITEM_KEY;
  2910. /* special case for "." */
  2911. if (filp->f_pos == 0) {
  2912. over = filldir(dirent, ".", 1,
  2913. 1, inode->i_ino,
  2914. DT_DIR);
  2915. if (over)
  2916. return 0;
  2917. filp->f_pos = 1;
  2918. }
  2919. /* special case for .., just use the back ref */
  2920. if (filp->f_pos == 1) {
  2921. u64 pino = parent_ino(filp->f_path.dentry);
  2922. over = filldir(dirent, "..", 2,
  2923. 2, pino, DT_DIR);
  2924. if (over)
  2925. return 0;
  2926. filp->f_pos = 2;
  2927. }
  2928. path = btrfs_alloc_path();
  2929. path->reada = 2;
  2930. btrfs_set_key_type(&key, key_type);
  2931. key.offset = filp->f_pos;
  2932. key.objectid = inode->i_ino;
  2933. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2934. if (ret < 0)
  2935. goto err;
  2936. advance = 0;
  2937. while (1) {
  2938. leaf = path->nodes[0];
  2939. nritems = btrfs_header_nritems(leaf);
  2940. slot = path->slots[0];
  2941. if (advance || slot >= nritems) {
  2942. if (slot >= nritems - 1) {
  2943. ret = btrfs_next_leaf(root, path);
  2944. if (ret)
  2945. break;
  2946. leaf = path->nodes[0];
  2947. nritems = btrfs_header_nritems(leaf);
  2948. slot = path->slots[0];
  2949. } else {
  2950. slot++;
  2951. path->slots[0]++;
  2952. }
  2953. }
  2954. advance = 1;
  2955. item = btrfs_item_nr(leaf, slot);
  2956. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2957. if (found_key.objectid != key.objectid)
  2958. break;
  2959. if (btrfs_key_type(&found_key) != key_type)
  2960. break;
  2961. if (found_key.offset < filp->f_pos)
  2962. continue;
  2963. filp->f_pos = found_key.offset;
  2964. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  2965. di_cur = 0;
  2966. di_total = btrfs_item_size(leaf, item);
  2967. while (di_cur < di_total) {
  2968. struct btrfs_key location;
  2969. name_len = btrfs_dir_name_len(leaf, di);
  2970. if (name_len <= sizeof(tmp_name)) {
  2971. name_ptr = tmp_name;
  2972. } else {
  2973. name_ptr = kmalloc(name_len, GFP_NOFS);
  2974. if (!name_ptr) {
  2975. ret = -ENOMEM;
  2976. goto err;
  2977. }
  2978. }
  2979. read_extent_buffer(leaf, name_ptr,
  2980. (unsigned long)(di + 1), name_len);
  2981. d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
  2982. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  2983. /* is this a reference to our own snapshot? If so
  2984. * skip it
  2985. */
  2986. if (location.type == BTRFS_ROOT_ITEM_KEY &&
  2987. location.objectid == root->root_key.objectid) {
  2988. over = 0;
  2989. goto skip;
  2990. }
  2991. over = filldir(dirent, name_ptr, name_len,
  2992. found_key.offset, location.objectid,
  2993. d_type);
  2994. skip:
  2995. if (name_ptr != tmp_name)
  2996. kfree(name_ptr);
  2997. if (over)
  2998. goto nopos;
  2999. di_len = btrfs_dir_name_len(leaf, di) +
  3000. btrfs_dir_data_len(leaf, di) + sizeof(*di);
  3001. di_cur += di_len;
  3002. di = (struct btrfs_dir_item *)((char *)di + di_len);
  3003. }
  3004. }
  3005. /* Reached end of directory/root. Bump pos past the last item. */
  3006. if (key_type == BTRFS_DIR_INDEX_KEY)
  3007. filp->f_pos = INT_LIMIT(off_t);
  3008. else
  3009. filp->f_pos++;
  3010. nopos:
  3011. ret = 0;
  3012. err:
  3013. btrfs_free_path(path);
  3014. return ret;
  3015. }
  3016. int btrfs_write_inode(struct inode *inode, int wait)
  3017. {
  3018. struct btrfs_root *root = BTRFS_I(inode)->root;
  3019. struct btrfs_trans_handle *trans;
  3020. int ret = 0;
  3021. if (root->fs_info->btree_inode == inode)
  3022. return 0;
  3023. if (wait) {
  3024. trans = btrfs_join_transaction(root, 1);
  3025. btrfs_set_trans_block_group(trans, inode);
  3026. ret = btrfs_commit_transaction(trans, root);
  3027. }
  3028. return ret;
  3029. }
  3030. /*
  3031. * This is somewhat expensive, updating the tree every time the
  3032. * inode changes. But, it is most likely to find the inode in cache.
  3033. * FIXME, needs more benchmarking...there are no reasons other than performance
  3034. * to keep or drop this code.
  3035. */
  3036. void btrfs_dirty_inode(struct inode *inode)
  3037. {
  3038. struct btrfs_root *root = BTRFS_I(inode)->root;
  3039. struct btrfs_trans_handle *trans;
  3040. trans = btrfs_join_transaction(root, 1);
  3041. btrfs_set_trans_block_group(trans, inode);
  3042. btrfs_update_inode(trans, root, inode);
  3043. btrfs_end_transaction(trans, root);
  3044. }
  3045. /*
  3046. * find the highest existing sequence number in a directory
  3047. * and then set the in-memory index_cnt variable to reflect
  3048. * free sequence numbers
  3049. */
  3050. static int btrfs_set_inode_index_count(struct inode *inode)
  3051. {
  3052. struct btrfs_root *root = BTRFS_I(inode)->root;
  3053. struct btrfs_key key, found_key;
  3054. struct btrfs_path *path;
  3055. struct extent_buffer *leaf;
  3056. int ret;
  3057. key.objectid = inode->i_ino;
  3058. btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
  3059. key.offset = (u64)-1;
  3060. path = btrfs_alloc_path();
  3061. if (!path)
  3062. return -ENOMEM;
  3063. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3064. if (ret < 0)
  3065. goto out;
  3066. /* FIXME: we should be able to handle this */
  3067. if (ret == 0)
  3068. goto out;
  3069. ret = 0;
  3070. /*
  3071. * MAGIC NUMBER EXPLANATION:
  3072. * since we search a directory based on f_pos we have to start at 2
  3073. * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
  3074. * else has to start at 2
  3075. */
  3076. if (path->slots[0] == 0) {
  3077. BTRFS_I(inode)->index_cnt = 2;
  3078. goto out;
  3079. }
  3080. path->slots[0]--;
  3081. leaf = path->nodes[0];
  3082. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3083. if (found_key.objectid != inode->i_ino ||
  3084. btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
  3085. BTRFS_I(inode)->index_cnt = 2;
  3086. goto out;
  3087. }
  3088. BTRFS_I(inode)->index_cnt = found_key.offset + 1;
  3089. out:
  3090. btrfs_free_path(path);
  3091. return ret;
  3092. }
  3093. /*
  3094. * helper to find a free sequence number in a given directory. This current
  3095. * code is very simple, later versions will do smarter things in the btree
  3096. */
  3097. int btrfs_set_inode_index(struct inode *dir, u64 *index)
  3098. {
  3099. int ret = 0;
  3100. if (BTRFS_I(dir)->index_cnt == (u64)-1) {
  3101. ret = btrfs_set_inode_index_count(dir);
  3102. if (ret)
  3103. return ret;
  3104. }
  3105. *index = BTRFS_I(dir)->index_cnt;
  3106. BTRFS_I(dir)->index_cnt++;
  3107. return ret;
  3108. }
  3109. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  3110. struct btrfs_root *root,
  3111. struct inode *dir,
  3112. const char *name, int name_len,
  3113. u64 ref_objectid, u64 objectid,
  3114. u64 alloc_hint, int mode, u64 *index)
  3115. {
  3116. struct inode *inode;
  3117. struct btrfs_inode_item *inode_item;
  3118. struct btrfs_key *location;
  3119. struct btrfs_path *path;
  3120. struct btrfs_inode_ref *ref;
  3121. struct btrfs_key key[2];
  3122. u32 sizes[2];
  3123. unsigned long ptr;
  3124. int ret;
  3125. int owner;
  3126. path = btrfs_alloc_path();
  3127. BUG_ON(!path);
  3128. inode = new_inode(root->fs_info->sb);
  3129. if (!inode)
  3130. return ERR_PTR(-ENOMEM);
  3131. if (dir) {
  3132. ret = btrfs_set_inode_index(dir, index);
  3133. if (ret) {
  3134. iput(inode);
  3135. return ERR_PTR(ret);
  3136. }
  3137. }
  3138. /*
  3139. * index_cnt is ignored for everything but a dir,
  3140. * btrfs_get_inode_index_count has an explanation for the magic
  3141. * number
  3142. */
  3143. init_btrfs_i(inode);
  3144. BTRFS_I(inode)->index_cnt = 2;
  3145. BTRFS_I(inode)->root = root;
  3146. BTRFS_I(inode)->generation = trans->transid;
  3147. btrfs_set_inode_space_info(root, inode);
  3148. if (mode & S_IFDIR)
  3149. owner = 0;
  3150. else
  3151. owner = 1;
  3152. BTRFS_I(inode)->block_group =
  3153. btrfs_find_block_group(root, 0, alloc_hint, owner);
  3154. if ((mode & S_IFREG)) {
  3155. if (btrfs_test_opt(root, NODATASUM))
  3156. btrfs_set_flag(inode, NODATASUM);
  3157. if (btrfs_test_opt(root, NODATACOW))
  3158. btrfs_set_flag(inode, NODATACOW);
  3159. }
  3160. key[0].objectid = objectid;
  3161. btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
  3162. key[0].offset = 0;
  3163. key[1].objectid = objectid;
  3164. btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
  3165. key[1].offset = ref_objectid;
  3166. sizes[0] = sizeof(struct btrfs_inode_item);
  3167. sizes[1] = name_len + sizeof(*ref);
  3168. path->leave_spinning = 1;
  3169. ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
  3170. if (ret != 0)
  3171. goto fail;
  3172. if (objectid > root->highest_inode)
  3173. root->highest_inode = objectid;
  3174. inode->i_uid = current_fsuid();
  3175. if (dir && (dir->i_mode & S_ISGID)) {
  3176. inode->i_gid = dir->i_gid;
  3177. if (S_ISDIR(mode))
  3178. mode |= S_ISGID;
  3179. } else
  3180. inode->i_gid = current_fsgid();
  3181. inode->i_mode = mode;
  3182. inode->i_ino = objectid;
  3183. inode_set_bytes(inode, 0);
  3184. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  3185. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3186. struct btrfs_inode_item);
  3187. fill_inode_item(trans, path->nodes[0], inode_item, inode);
  3188. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  3189. struct btrfs_inode_ref);
  3190. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  3191. btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
  3192. ptr = (unsigned long)(ref + 1);
  3193. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  3194. btrfs_mark_buffer_dirty(path->nodes[0]);
  3195. btrfs_free_path(path);
  3196. location = &BTRFS_I(inode)->location;
  3197. location->objectid = objectid;
  3198. location->offset = 0;
  3199. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  3200. insert_inode_hash(inode);
  3201. return inode;
  3202. fail:
  3203. if (dir)
  3204. BTRFS_I(dir)->index_cnt--;
  3205. btrfs_free_path(path);
  3206. iput(inode);
  3207. return ERR_PTR(ret);
  3208. }
  3209. static inline u8 btrfs_inode_type(struct inode *inode)
  3210. {
  3211. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  3212. }
  3213. /*
  3214. * utility function to add 'inode' into 'parent_inode' with
  3215. * a give name and a given sequence number.
  3216. * if 'add_backref' is true, also insert a backref from the
  3217. * inode to the parent directory.
  3218. */
  3219. int btrfs_add_link(struct btrfs_trans_handle *trans,
  3220. struct inode *parent_inode, struct inode *inode,
  3221. const char *name, int name_len, int add_backref, u64 index)
  3222. {
  3223. int ret;
  3224. struct btrfs_key key;
  3225. struct btrfs_root *root = BTRFS_I(parent_inode)->root;
  3226. key.objectid = inode->i_ino;
  3227. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  3228. key.offset = 0;
  3229. ret = btrfs_insert_dir_item(trans, root, name, name_len,
  3230. parent_inode->i_ino,
  3231. &key, btrfs_inode_type(inode),
  3232. index);
  3233. if (ret == 0) {
  3234. if (add_backref) {
  3235. ret = btrfs_insert_inode_ref(trans, root,
  3236. name, name_len,
  3237. inode->i_ino,
  3238. parent_inode->i_ino,
  3239. index);
  3240. }
  3241. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  3242. name_len * 2);
  3243. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  3244. ret = btrfs_update_inode(trans, root, parent_inode);
  3245. }
  3246. return ret;
  3247. }
  3248. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  3249. struct dentry *dentry, struct inode *inode,
  3250. int backref, u64 index)
  3251. {
  3252. int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
  3253. inode, dentry->d_name.name,
  3254. dentry->d_name.len, backref, index);
  3255. if (!err) {
  3256. d_instantiate(dentry, inode);
  3257. return 0;
  3258. }
  3259. if (err > 0)
  3260. err = -EEXIST;
  3261. return err;
  3262. }
  3263. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  3264. int mode, dev_t rdev)
  3265. {
  3266. struct btrfs_trans_handle *trans;
  3267. struct btrfs_root *root = BTRFS_I(dir)->root;
  3268. struct inode *inode = NULL;
  3269. int err;
  3270. int drop_inode = 0;
  3271. u64 objectid;
  3272. unsigned long nr = 0;
  3273. u64 index = 0;
  3274. if (!new_valid_dev(rdev))
  3275. return -EINVAL;
  3276. err = btrfs_check_metadata_free_space(root);
  3277. if (err)
  3278. goto fail;
  3279. trans = btrfs_start_transaction(root, 1);
  3280. btrfs_set_trans_block_group(trans, dir);
  3281. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  3282. if (err) {
  3283. err = -ENOSPC;
  3284. goto out_unlock;
  3285. }
  3286. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  3287. dentry->d_name.len,
  3288. dentry->d_parent->d_inode->i_ino, objectid,
  3289. BTRFS_I(dir)->block_group, mode, &index);
  3290. err = PTR_ERR(inode);
  3291. if (IS_ERR(inode))
  3292. goto out_unlock;
  3293. err = btrfs_init_inode_security(inode, dir);
  3294. if (err) {
  3295. drop_inode = 1;
  3296. goto out_unlock;
  3297. }
  3298. btrfs_set_trans_block_group(trans, inode);
  3299. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  3300. if (err)
  3301. drop_inode = 1;
  3302. else {
  3303. inode->i_op = &btrfs_special_inode_operations;
  3304. init_special_inode(inode, inode->i_mode, rdev);
  3305. btrfs_update_inode(trans, root, inode);
  3306. }
  3307. dir->i_sb->s_dirt = 1;
  3308. btrfs_update_inode_block_group(trans, inode);
  3309. btrfs_update_inode_block_group(trans, dir);
  3310. out_unlock:
  3311. nr = trans->blocks_used;
  3312. btrfs_end_transaction_throttle(trans, root);
  3313. fail:
  3314. if (drop_inode) {
  3315. inode_dec_link_count(inode);
  3316. iput(inode);
  3317. }
  3318. btrfs_btree_balance_dirty(root, nr);
  3319. return err;
  3320. }
  3321. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  3322. int mode, struct nameidata *nd)
  3323. {
  3324. struct btrfs_trans_handle *trans;
  3325. struct btrfs_root *root = BTRFS_I(dir)->root;
  3326. struct inode *inode = NULL;
  3327. int err;
  3328. int drop_inode = 0;
  3329. unsigned long nr = 0;
  3330. u64 objectid;
  3331. u64 index = 0;
  3332. err = btrfs_check_metadata_free_space(root);
  3333. if (err)
  3334. goto fail;
  3335. trans = btrfs_start_transaction(root, 1);
  3336. btrfs_set_trans_block_group(trans, dir);
  3337. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  3338. if (err) {
  3339. err = -ENOSPC;
  3340. goto out_unlock;
  3341. }
  3342. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  3343. dentry->d_name.len,
  3344. dentry->d_parent->d_inode->i_ino,
  3345. objectid, BTRFS_I(dir)->block_group, mode,
  3346. &index);
  3347. err = PTR_ERR(inode);
  3348. if (IS_ERR(inode))
  3349. goto out_unlock;
  3350. err = btrfs_init_inode_security(inode, dir);
  3351. if (err) {
  3352. drop_inode = 1;
  3353. goto out_unlock;
  3354. }
  3355. btrfs_set_trans_block_group(trans, inode);
  3356. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  3357. if (err)
  3358. drop_inode = 1;
  3359. else {
  3360. inode->i_mapping->a_ops = &btrfs_aops;
  3361. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  3362. inode->i_fop = &btrfs_file_operations;
  3363. inode->i_op = &btrfs_file_inode_operations;
  3364. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  3365. }
  3366. dir->i_sb->s_dirt = 1;
  3367. btrfs_update_inode_block_group(trans, inode);
  3368. btrfs_update_inode_block_group(trans, dir);
  3369. out_unlock:
  3370. nr = trans->blocks_used;
  3371. btrfs_end_transaction_throttle(trans, root);
  3372. fail:
  3373. if (drop_inode) {
  3374. inode_dec_link_count(inode);
  3375. iput(inode);
  3376. }
  3377. btrfs_btree_balance_dirty(root, nr);
  3378. return err;
  3379. }
  3380. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  3381. struct dentry *dentry)
  3382. {
  3383. struct btrfs_trans_handle *trans;
  3384. struct btrfs_root *root = BTRFS_I(dir)->root;
  3385. struct inode *inode = old_dentry->d_inode;
  3386. u64 index;
  3387. unsigned long nr = 0;
  3388. int err;
  3389. int drop_inode = 0;
  3390. if (inode->i_nlink == 0)
  3391. return -ENOENT;
  3392. btrfs_inc_nlink(inode);
  3393. err = btrfs_check_metadata_free_space(root);
  3394. if (err)
  3395. goto fail;
  3396. err = btrfs_set_inode_index(dir, &index);
  3397. if (err)
  3398. goto fail;
  3399. trans = btrfs_start_transaction(root, 1);
  3400. btrfs_set_trans_block_group(trans, dir);
  3401. atomic_inc(&inode->i_count);
  3402. err = btrfs_add_nondir(trans, dentry, inode, 1, index);
  3403. if (err)
  3404. drop_inode = 1;
  3405. dir->i_sb->s_dirt = 1;
  3406. btrfs_update_inode_block_group(trans, dir);
  3407. err = btrfs_update_inode(trans, root, inode);
  3408. if (err)
  3409. drop_inode = 1;
  3410. nr = trans->blocks_used;
  3411. btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
  3412. btrfs_end_transaction_throttle(trans, root);
  3413. fail:
  3414. if (drop_inode) {
  3415. inode_dec_link_count(inode);
  3416. iput(inode);
  3417. }
  3418. btrfs_btree_balance_dirty(root, nr);
  3419. return err;
  3420. }
  3421. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  3422. {
  3423. struct inode *inode = NULL;
  3424. struct btrfs_trans_handle *trans;
  3425. struct btrfs_root *root = BTRFS_I(dir)->root;
  3426. int err = 0;
  3427. int drop_on_err = 0;
  3428. u64 objectid = 0;
  3429. u64 index = 0;
  3430. unsigned long nr = 1;
  3431. err = btrfs_check_metadata_free_space(root);
  3432. if (err)
  3433. goto out_unlock;
  3434. trans = btrfs_start_transaction(root, 1);
  3435. btrfs_set_trans_block_group(trans, dir);
  3436. if (IS_ERR(trans)) {
  3437. err = PTR_ERR(trans);
  3438. goto out_unlock;
  3439. }
  3440. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  3441. if (err) {
  3442. err = -ENOSPC;
  3443. goto out_unlock;
  3444. }
  3445. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  3446. dentry->d_name.len,
  3447. dentry->d_parent->d_inode->i_ino, objectid,
  3448. BTRFS_I(dir)->block_group, S_IFDIR | mode,
  3449. &index);
  3450. if (IS_ERR(inode)) {
  3451. err = PTR_ERR(inode);
  3452. goto out_fail;
  3453. }
  3454. drop_on_err = 1;
  3455. err = btrfs_init_inode_security(inode, dir);
  3456. if (err)
  3457. goto out_fail;
  3458. inode->i_op = &btrfs_dir_inode_operations;
  3459. inode->i_fop = &btrfs_dir_file_operations;
  3460. btrfs_set_trans_block_group(trans, inode);
  3461. btrfs_i_size_write(inode, 0);
  3462. err = btrfs_update_inode(trans, root, inode);
  3463. if (err)
  3464. goto out_fail;
  3465. err = btrfs_add_link(trans, dentry->d_parent->d_inode,
  3466. inode, dentry->d_name.name,
  3467. dentry->d_name.len, 0, index);
  3468. if (err)
  3469. goto out_fail;
  3470. d_instantiate(dentry, inode);
  3471. drop_on_err = 0;
  3472. dir->i_sb->s_dirt = 1;
  3473. btrfs_update_inode_block_group(trans, inode);
  3474. btrfs_update_inode_block_group(trans, dir);
  3475. out_fail:
  3476. nr = trans->blocks_used;
  3477. btrfs_end_transaction_throttle(trans, root);
  3478. out_unlock:
  3479. if (drop_on_err)
  3480. iput(inode);
  3481. btrfs_btree_balance_dirty(root, nr);
  3482. return err;
  3483. }
  3484. /* helper for btfs_get_extent. Given an existing extent in the tree,
  3485. * and an extent that you want to insert, deal with overlap and insert
  3486. * the new extent into the tree.
  3487. */
  3488. static int merge_extent_mapping(struct extent_map_tree *em_tree,
  3489. struct extent_map *existing,
  3490. struct extent_map *em,
  3491. u64 map_start, u64 map_len)
  3492. {
  3493. u64 start_diff;
  3494. BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
  3495. start_diff = map_start - em->start;
  3496. em->start = map_start;
  3497. em->len = map_len;
  3498. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  3499. !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  3500. em->block_start += start_diff;
  3501. em->block_len -= start_diff;
  3502. }
  3503. return add_extent_mapping(em_tree, em);
  3504. }
  3505. static noinline int uncompress_inline(struct btrfs_path *path,
  3506. struct inode *inode, struct page *page,
  3507. size_t pg_offset, u64 extent_offset,
  3508. struct btrfs_file_extent_item *item)
  3509. {
  3510. int ret;
  3511. struct extent_buffer *leaf = path->nodes[0];
  3512. char *tmp;
  3513. size_t max_size;
  3514. unsigned long inline_size;
  3515. unsigned long ptr;
  3516. WARN_ON(pg_offset != 0);
  3517. max_size = btrfs_file_extent_ram_bytes(leaf, item);
  3518. inline_size = btrfs_file_extent_inline_item_len(leaf,
  3519. btrfs_item_nr(leaf, path->slots[0]));
  3520. tmp = kmalloc(inline_size, GFP_NOFS);
  3521. ptr = btrfs_file_extent_inline_start(item);
  3522. read_extent_buffer(leaf, tmp, ptr, inline_size);
  3523. max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
  3524. ret = btrfs_zlib_decompress(tmp, page, extent_offset,
  3525. inline_size, max_size);
  3526. if (ret) {
  3527. char *kaddr = kmap_atomic(page, KM_USER0);
  3528. unsigned long copy_size = min_t(u64,
  3529. PAGE_CACHE_SIZE - pg_offset,
  3530. max_size - extent_offset);
  3531. memset(kaddr + pg_offset, 0, copy_size);
  3532. kunmap_atomic(kaddr, KM_USER0);
  3533. }
  3534. kfree(tmp);
  3535. return 0;
  3536. }
  3537. /*
  3538. * a bit scary, this does extent mapping from logical file offset to the disk.
  3539. * the ugly parts come from merging extents from the disk with the in-ram
  3540. * representation. This gets more complex because of the data=ordered code,
  3541. * where the in-ram extents might be locked pending data=ordered completion.
  3542. *
  3543. * This also copies inline extents directly into the page.
  3544. */
  3545. struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
  3546. size_t pg_offset, u64 start, u64 len,
  3547. int create)
  3548. {
  3549. int ret;
  3550. int err = 0;
  3551. u64 bytenr;
  3552. u64 extent_start = 0;
  3553. u64 extent_end = 0;
  3554. u64 objectid = inode->i_ino;
  3555. u32 found_type;
  3556. struct btrfs_path *path = NULL;
  3557. struct btrfs_root *root = BTRFS_I(inode)->root;
  3558. struct btrfs_file_extent_item *item;
  3559. struct extent_buffer *leaf;
  3560. struct btrfs_key found_key;
  3561. struct extent_map *em = NULL;
  3562. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  3563. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3564. struct btrfs_trans_handle *trans = NULL;
  3565. int compressed;
  3566. again:
  3567. spin_lock(&em_tree->lock);
  3568. em = lookup_extent_mapping(em_tree, start, len);
  3569. if (em)
  3570. em->bdev = root->fs_info->fs_devices->latest_bdev;
  3571. spin_unlock(&em_tree->lock);
  3572. if (em) {
  3573. if (em->start > start || em->start + em->len <= start)
  3574. free_extent_map(em);
  3575. else if (em->block_start == EXTENT_MAP_INLINE && page)
  3576. free_extent_map(em);
  3577. else
  3578. goto out;
  3579. }
  3580. em = alloc_extent_map(GFP_NOFS);
  3581. if (!em) {
  3582. err = -ENOMEM;
  3583. goto out;
  3584. }
  3585. em->bdev = root->fs_info->fs_devices->latest_bdev;
  3586. em->start = EXTENT_MAP_HOLE;
  3587. em->orig_start = EXTENT_MAP_HOLE;
  3588. em->len = (u64)-1;
  3589. em->block_len = (u64)-1;
  3590. if (!path) {
  3591. path = btrfs_alloc_path();
  3592. BUG_ON(!path);
  3593. }
  3594. ret = btrfs_lookup_file_extent(trans, root, path,
  3595. objectid, start, trans != NULL);
  3596. if (ret < 0) {
  3597. err = ret;
  3598. goto out;
  3599. }
  3600. if (ret != 0) {
  3601. if (path->slots[0] == 0)
  3602. goto not_found;
  3603. path->slots[0]--;
  3604. }
  3605. leaf = path->nodes[0];
  3606. item = btrfs_item_ptr(leaf, path->slots[0],
  3607. struct btrfs_file_extent_item);
  3608. /* are we inside the extent that was found? */
  3609. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3610. found_type = btrfs_key_type(&found_key);
  3611. if (found_key.objectid != objectid ||
  3612. found_type != BTRFS_EXTENT_DATA_KEY) {
  3613. goto not_found;
  3614. }
  3615. found_type = btrfs_file_extent_type(leaf, item);
  3616. extent_start = found_key.offset;
  3617. compressed = btrfs_file_extent_compression(leaf, item);
  3618. if (found_type == BTRFS_FILE_EXTENT_REG ||
  3619. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  3620. extent_end = extent_start +
  3621. btrfs_file_extent_num_bytes(leaf, item);
  3622. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  3623. size_t size;
  3624. size = btrfs_file_extent_inline_len(leaf, item);
  3625. extent_end = (extent_start + size + root->sectorsize - 1) &
  3626. ~((u64)root->sectorsize - 1);
  3627. }
  3628. if (start >= extent_end) {
  3629. path->slots[0]++;
  3630. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  3631. ret = btrfs_next_leaf(root, path);
  3632. if (ret < 0) {
  3633. err = ret;
  3634. goto out;
  3635. }
  3636. if (ret > 0)
  3637. goto not_found;
  3638. leaf = path->nodes[0];
  3639. }
  3640. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3641. if (found_key.objectid != objectid ||
  3642. found_key.type != BTRFS_EXTENT_DATA_KEY)
  3643. goto not_found;
  3644. if (start + len <= found_key.offset)
  3645. goto not_found;
  3646. em->start = start;
  3647. em->len = found_key.offset - start;
  3648. goto not_found_em;
  3649. }
  3650. if (found_type == BTRFS_FILE_EXTENT_REG ||
  3651. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  3652. em->start = extent_start;
  3653. em->len = extent_end - extent_start;
  3654. em->orig_start = extent_start -
  3655. btrfs_file_extent_offset(leaf, item);
  3656. bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
  3657. if (bytenr == 0) {
  3658. em->block_start = EXTENT_MAP_HOLE;
  3659. goto insert;
  3660. }
  3661. if (compressed) {
  3662. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  3663. em->block_start = bytenr;
  3664. em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
  3665. item);
  3666. } else {
  3667. bytenr += btrfs_file_extent_offset(leaf, item);
  3668. em->block_start = bytenr;
  3669. em->block_len = em->len;
  3670. if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
  3671. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  3672. }
  3673. goto insert;
  3674. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  3675. unsigned long ptr;
  3676. char *map;
  3677. size_t size;
  3678. size_t extent_offset;
  3679. size_t copy_size;
  3680. em->block_start = EXTENT_MAP_INLINE;
  3681. if (!page || create) {
  3682. em->start = extent_start;
  3683. em->len = extent_end - extent_start;
  3684. goto out;
  3685. }
  3686. size = btrfs_file_extent_inline_len(leaf, item);
  3687. extent_offset = page_offset(page) + pg_offset - extent_start;
  3688. copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
  3689. size - extent_offset);
  3690. em->start = extent_start + extent_offset;
  3691. em->len = (copy_size + root->sectorsize - 1) &
  3692. ~((u64)root->sectorsize - 1);
  3693. em->orig_start = EXTENT_MAP_INLINE;
  3694. if (compressed)
  3695. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  3696. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  3697. if (create == 0 && !PageUptodate(page)) {
  3698. if (btrfs_file_extent_compression(leaf, item) ==
  3699. BTRFS_COMPRESS_ZLIB) {
  3700. ret = uncompress_inline(path, inode, page,
  3701. pg_offset,
  3702. extent_offset, item);
  3703. BUG_ON(ret);
  3704. } else {
  3705. map = kmap(page);
  3706. read_extent_buffer(leaf, map + pg_offset, ptr,
  3707. copy_size);
  3708. kunmap(page);
  3709. }
  3710. flush_dcache_page(page);
  3711. } else if (create && PageUptodate(page)) {
  3712. if (!trans) {
  3713. kunmap(page);
  3714. free_extent_map(em);
  3715. em = NULL;
  3716. btrfs_release_path(root, path);
  3717. trans = btrfs_join_transaction(root, 1);
  3718. goto again;
  3719. }
  3720. map = kmap(page);
  3721. write_extent_buffer(leaf, map + pg_offset, ptr,
  3722. copy_size);
  3723. kunmap(page);
  3724. btrfs_mark_buffer_dirty(leaf);
  3725. }
  3726. set_extent_uptodate(io_tree, em->start,
  3727. extent_map_end(em) - 1, GFP_NOFS);
  3728. goto insert;
  3729. } else {
  3730. printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
  3731. WARN_ON(1);
  3732. }
  3733. not_found:
  3734. em->start = start;
  3735. em->len = len;
  3736. not_found_em:
  3737. em->block_start = EXTENT_MAP_HOLE;
  3738. set_bit(EXTENT_FLAG_VACANCY, &em->flags);
  3739. insert:
  3740. btrfs_release_path(root, path);
  3741. if (em->start > start || extent_map_end(em) <= start) {
  3742. printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
  3743. "[%llu %llu]\n", (unsigned long long)em->start,
  3744. (unsigned long long)em->len,
  3745. (unsigned long long)start,
  3746. (unsigned long long)len);
  3747. err = -EIO;
  3748. goto out;
  3749. }
  3750. err = 0;
  3751. spin_lock(&em_tree->lock);
  3752. ret = add_extent_mapping(em_tree, em);
  3753. /* it is possible that someone inserted the extent into the tree
  3754. * while we had the lock dropped. It is also possible that
  3755. * an overlapping map exists in the tree
  3756. */
  3757. if (ret == -EEXIST) {
  3758. struct extent_map *existing;
  3759. ret = 0;
  3760. existing = lookup_extent_mapping(em_tree, start, len);
  3761. if (existing && (existing->start > start ||
  3762. existing->start + existing->len <= start)) {
  3763. free_extent_map(existing);
  3764. existing = NULL;
  3765. }
  3766. if (!existing) {
  3767. existing = lookup_extent_mapping(em_tree, em->start,
  3768. em->len);
  3769. if (existing) {
  3770. err = merge_extent_mapping(em_tree, existing,
  3771. em, start,
  3772. root->sectorsize);
  3773. free_extent_map(existing);
  3774. if (err) {
  3775. free_extent_map(em);
  3776. em = NULL;
  3777. }
  3778. } else {
  3779. err = -EIO;
  3780. free_extent_map(em);
  3781. em = NULL;
  3782. }
  3783. } else {
  3784. free_extent_map(em);
  3785. em = existing;
  3786. err = 0;
  3787. }
  3788. }
  3789. spin_unlock(&em_tree->lock);
  3790. out:
  3791. if (path)
  3792. btrfs_free_path(path);
  3793. if (trans) {
  3794. ret = btrfs_end_transaction(trans, root);
  3795. if (!err)
  3796. err = ret;
  3797. }
  3798. if (err) {
  3799. free_extent_map(em);
  3800. return ERR_PTR(err);
  3801. }
  3802. return em;
  3803. }
  3804. static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
  3805. const struct iovec *iov, loff_t offset,
  3806. unsigned long nr_segs)
  3807. {
  3808. return -EINVAL;
  3809. }
  3810. static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  3811. __u64 start, __u64 len)
  3812. {
  3813. return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
  3814. }
  3815. int btrfs_readpage(struct file *file, struct page *page)
  3816. {
  3817. struct extent_io_tree *tree;
  3818. tree = &BTRFS_I(page->mapping->host)->io_tree;
  3819. return extent_read_full_page(tree, page, btrfs_get_extent);
  3820. }
  3821. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  3822. {
  3823. struct extent_io_tree *tree;
  3824. if (current->flags & PF_MEMALLOC) {
  3825. redirty_page_for_writepage(wbc, page);
  3826. unlock_page(page);
  3827. return 0;
  3828. }
  3829. tree = &BTRFS_I(page->mapping->host)->io_tree;
  3830. return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
  3831. }
  3832. int btrfs_writepages(struct address_space *mapping,
  3833. struct writeback_control *wbc)
  3834. {
  3835. struct extent_io_tree *tree;
  3836. tree = &BTRFS_I(mapping->host)->io_tree;
  3837. return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
  3838. }
  3839. static int
  3840. btrfs_readpages(struct file *file, struct address_space *mapping,
  3841. struct list_head *pages, unsigned nr_pages)
  3842. {
  3843. struct extent_io_tree *tree;
  3844. tree = &BTRFS_I(mapping->host)->io_tree;
  3845. return extent_readpages(tree, mapping, pages, nr_pages,
  3846. btrfs_get_extent);
  3847. }
  3848. static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  3849. {
  3850. struct extent_io_tree *tree;
  3851. struct extent_map_tree *map;
  3852. int ret;
  3853. tree = &BTRFS_I(page->mapping->host)->io_tree;
  3854. map = &BTRFS_I(page->mapping->host)->extent_tree;
  3855. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  3856. if (ret == 1) {
  3857. ClearPagePrivate(page);
  3858. set_page_private(page, 0);
  3859. page_cache_release(page);
  3860. }
  3861. return ret;
  3862. }
  3863. static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  3864. {
  3865. if (PageWriteback(page) || PageDirty(page))
  3866. return 0;
  3867. return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
  3868. }
  3869. static void btrfs_invalidatepage(struct page *page, unsigned long offset)
  3870. {
  3871. struct extent_io_tree *tree;
  3872. struct btrfs_ordered_extent *ordered;
  3873. u64 page_start = page_offset(page);
  3874. u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
  3875. wait_on_page_writeback(page);
  3876. tree = &BTRFS_I(page->mapping->host)->io_tree;
  3877. if (offset) {
  3878. btrfs_releasepage(page, GFP_NOFS);
  3879. return;
  3880. }
  3881. lock_extent(tree, page_start, page_end, GFP_NOFS);
  3882. ordered = btrfs_lookup_ordered_extent(page->mapping->host,
  3883. page_offset(page));
  3884. if (ordered) {
  3885. /*
  3886. * IO on this page will never be started, so we need
  3887. * to account for any ordered extents now
  3888. */
  3889. clear_extent_bit(tree, page_start, page_end,
  3890. EXTENT_DIRTY | EXTENT_DELALLOC |
  3891. EXTENT_LOCKED, 1, 0, GFP_NOFS);
  3892. btrfs_finish_ordered_io(page->mapping->host,
  3893. page_start, page_end);
  3894. btrfs_put_ordered_extent(ordered);
  3895. lock_extent(tree, page_start, page_end, GFP_NOFS);
  3896. }
  3897. clear_extent_bit(tree, page_start, page_end,
  3898. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  3899. EXTENT_ORDERED,
  3900. 1, 1, GFP_NOFS);
  3901. __btrfs_releasepage(page, GFP_NOFS);
  3902. ClearPageChecked(page);
  3903. if (PagePrivate(page)) {
  3904. ClearPagePrivate(page);
  3905. set_page_private(page, 0);
  3906. page_cache_release(page);
  3907. }
  3908. }
  3909. /*
  3910. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  3911. * called from a page fault handler when a page is first dirtied. Hence we must
  3912. * be careful to check for EOF conditions here. We set the page up correctly
  3913. * for a written page which means we get ENOSPC checking when writing into
  3914. * holes and correct delalloc and unwritten extent mapping on filesystems that
  3915. * support these features.
  3916. *
  3917. * We are not allowed to take the i_mutex here so we have to play games to
  3918. * protect against truncate races as the page could now be beyond EOF. Because
  3919. * vmtruncate() writes the inode size before removing pages, once we have the
  3920. * page lock we can determine safely if the page is beyond EOF. If it is not
  3921. * beyond EOF, then the page is guaranteed safe against truncation until we
  3922. * unlock the page.
  3923. */
  3924. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  3925. {
  3926. struct page *page = vmf->page;
  3927. struct inode *inode = fdentry(vma->vm_file)->d_inode;
  3928. struct btrfs_root *root = BTRFS_I(inode)->root;
  3929. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3930. struct btrfs_ordered_extent *ordered;
  3931. char *kaddr;
  3932. unsigned long zero_start;
  3933. loff_t size;
  3934. int ret;
  3935. u64 page_start;
  3936. u64 page_end;
  3937. ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
  3938. if (ret) {
  3939. if (ret == -ENOMEM)
  3940. ret = VM_FAULT_OOM;
  3941. else /* -ENOSPC, -EIO, etc */
  3942. ret = VM_FAULT_SIGBUS;
  3943. goto out;
  3944. }
  3945. ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
  3946. again:
  3947. lock_page(page);
  3948. size = i_size_read(inode);
  3949. page_start = page_offset(page);
  3950. page_end = page_start + PAGE_CACHE_SIZE - 1;
  3951. if ((page->mapping != inode->i_mapping) ||
  3952. (page_start >= size)) {
  3953. btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
  3954. /* page got truncated out from underneath us */
  3955. goto out_unlock;
  3956. }
  3957. wait_on_page_writeback(page);
  3958. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  3959. set_page_extent_mapped(page);
  3960. /*
  3961. * we can't set the delalloc bits if there are pending ordered
  3962. * extents. Drop our locks and wait for them to finish
  3963. */
  3964. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  3965. if (ordered) {
  3966. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  3967. unlock_page(page);
  3968. btrfs_start_ordered_extent(inode, ordered, 1);
  3969. btrfs_put_ordered_extent(ordered);
  3970. goto again;
  3971. }
  3972. btrfs_set_extent_delalloc(inode, page_start, page_end);
  3973. ret = 0;
  3974. /* page is wholly or partially inside EOF */
  3975. if (page_start + PAGE_CACHE_SIZE > size)
  3976. zero_start = size & ~PAGE_CACHE_MASK;
  3977. else
  3978. zero_start = PAGE_CACHE_SIZE;
  3979. if (zero_start != PAGE_CACHE_SIZE) {
  3980. kaddr = kmap(page);
  3981. memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
  3982. flush_dcache_page(page);
  3983. kunmap(page);
  3984. }
  3985. ClearPageChecked(page);
  3986. set_page_dirty(page);
  3987. BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
  3988. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  3989. out_unlock:
  3990. unlock_page(page);
  3991. out:
  3992. return ret;
  3993. }
  3994. static void btrfs_truncate(struct inode *inode)
  3995. {
  3996. struct btrfs_root *root = BTRFS_I(inode)->root;
  3997. int ret;
  3998. struct btrfs_trans_handle *trans;
  3999. unsigned long nr;
  4000. u64 mask = root->sectorsize - 1;
  4001. if (!S_ISREG(inode->i_mode))
  4002. return;
  4003. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  4004. return;
  4005. btrfs_truncate_page(inode->i_mapping, inode->i_size);
  4006. btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
  4007. trans = btrfs_start_transaction(root, 1);
  4008. /*
  4009. * setattr is responsible for setting the ordered_data_close flag,
  4010. * but that is only tested during the last file release. That
  4011. * could happen well after the next commit, leaving a great big
  4012. * window where new writes may get lost if someone chooses to write
  4013. * to this file after truncating to zero
  4014. *
  4015. * The inode doesn't have any dirty data here, and so if we commit
  4016. * this is a noop. If someone immediately starts writing to the inode
  4017. * it is very likely we'll catch some of their writes in this
  4018. * transaction, and the commit will find this file on the ordered
  4019. * data list with good things to send down.
  4020. *
  4021. * This is a best effort solution, there is still a window where
  4022. * using truncate to replace the contents of the file will
  4023. * end up with a zero length file after a crash.
  4024. */
  4025. if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
  4026. btrfs_add_ordered_operation(trans, root, inode);
  4027. btrfs_set_trans_block_group(trans, inode);
  4028. btrfs_i_size_write(inode, inode->i_size);
  4029. ret = btrfs_orphan_add(trans, inode);
  4030. if (ret)
  4031. goto out;
  4032. /* FIXME, add redo link to tree so we don't leak on crash */
  4033. ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
  4034. BTRFS_EXTENT_DATA_KEY);
  4035. btrfs_update_inode(trans, root, inode);
  4036. ret = btrfs_orphan_del(trans, inode);
  4037. BUG_ON(ret);
  4038. out:
  4039. nr = trans->blocks_used;
  4040. ret = btrfs_end_transaction_throttle(trans, root);
  4041. BUG_ON(ret);
  4042. btrfs_btree_balance_dirty(root, nr);
  4043. }
  4044. /*
  4045. * create a new subvolume directory/inode (helper for the ioctl).
  4046. */
  4047. int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
  4048. struct btrfs_root *new_root, struct dentry *dentry,
  4049. u64 new_dirid, u64 alloc_hint)
  4050. {
  4051. struct inode *inode;
  4052. int error;
  4053. u64 index = 0;
  4054. inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
  4055. new_dirid, alloc_hint, S_IFDIR | 0700, &index);
  4056. if (IS_ERR(inode))
  4057. return PTR_ERR(inode);
  4058. inode->i_op = &btrfs_dir_inode_operations;
  4059. inode->i_fop = &btrfs_dir_file_operations;
  4060. inode->i_nlink = 1;
  4061. btrfs_i_size_write(inode, 0);
  4062. error = btrfs_update_inode(trans, new_root, inode);
  4063. if (error)
  4064. return error;
  4065. d_instantiate(dentry, inode);
  4066. return 0;
  4067. }
  4068. /* helper function for file defrag and space balancing. This
  4069. * forces readahead on a given range of bytes in an inode
  4070. */
  4071. unsigned long btrfs_force_ra(struct address_space *mapping,
  4072. struct file_ra_state *ra, struct file *file,
  4073. pgoff_t offset, pgoff_t last_index)
  4074. {
  4075. pgoff_t req_size = last_index - offset + 1;
  4076. page_cache_sync_readahead(mapping, ra, file, offset, req_size);
  4077. return offset + req_size;
  4078. }
  4079. struct inode *btrfs_alloc_inode(struct super_block *sb)
  4080. {
  4081. struct btrfs_inode *ei;
  4082. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  4083. if (!ei)
  4084. return NULL;
  4085. ei->last_trans = 0;
  4086. ei->logged_trans = 0;
  4087. btrfs_ordered_inode_tree_init(&ei->ordered_tree);
  4088. ei->i_acl = BTRFS_ACL_NOT_CACHED;
  4089. ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
  4090. INIT_LIST_HEAD(&ei->i_orphan);
  4091. INIT_LIST_HEAD(&ei->ordered_operations);
  4092. return &ei->vfs_inode;
  4093. }
  4094. void btrfs_destroy_inode(struct inode *inode)
  4095. {
  4096. struct btrfs_ordered_extent *ordered;
  4097. struct btrfs_root *root = BTRFS_I(inode)->root;
  4098. WARN_ON(!list_empty(&inode->i_dentry));
  4099. WARN_ON(inode->i_data.nrpages);
  4100. if (BTRFS_I(inode)->i_acl &&
  4101. BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
  4102. posix_acl_release(BTRFS_I(inode)->i_acl);
  4103. if (BTRFS_I(inode)->i_default_acl &&
  4104. BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
  4105. posix_acl_release(BTRFS_I(inode)->i_default_acl);
  4106. /*
  4107. * Make sure we're properly removed from the ordered operation
  4108. * lists.
  4109. */
  4110. smp_mb();
  4111. if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
  4112. spin_lock(&root->fs_info->ordered_extent_lock);
  4113. list_del_init(&BTRFS_I(inode)->ordered_operations);
  4114. spin_unlock(&root->fs_info->ordered_extent_lock);
  4115. }
  4116. spin_lock(&root->list_lock);
  4117. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  4118. printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
  4119. " list\n", inode->i_ino);
  4120. dump_stack();
  4121. }
  4122. spin_unlock(&root->list_lock);
  4123. while (1) {
  4124. ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
  4125. if (!ordered)
  4126. break;
  4127. else {
  4128. printk(KERN_ERR "btrfs found ordered "
  4129. "extent %llu %llu on inode cleanup\n",
  4130. (unsigned long long)ordered->file_offset,
  4131. (unsigned long long)ordered->len);
  4132. btrfs_remove_ordered_extent(inode, ordered);
  4133. btrfs_put_ordered_extent(ordered);
  4134. btrfs_put_ordered_extent(ordered);
  4135. }
  4136. }
  4137. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  4138. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  4139. }
  4140. static void init_once(void *foo)
  4141. {
  4142. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  4143. inode_init_once(&ei->vfs_inode);
  4144. }
  4145. void btrfs_destroy_cachep(void)
  4146. {
  4147. if (btrfs_inode_cachep)
  4148. kmem_cache_destroy(btrfs_inode_cachep);
  4149. if (btrfs_trans_handle_cachep)
  4150. kmem_cache_destroy(btrfs_trans_handle_cachep);
  4151. if (btrfs_transaction_cachep)
  4152. kmem_cache_destroy(btrfs_transaction_cachep);
  4153. if (btrfs_path_cachep)
  4154. kmem_cache_destroy(btrfs_path_cachep);
  4155. }
  4156. int btrfs_init_cachep(void)
  4157. {
  4158. btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
  4159. sizeof(struct btrfs_inode), 0,
  4160. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
  4161. if (!btrfs_inode_cachep)
  4162. goto fail;
  4163. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
  4164. sizeof(struct btrfs_trans_handle), 0,
  4165. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  4166. if (!btrfs_trans_handle_cachep)
  4167. goto fail;
  4168. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
  4169. sizeof(struct btrfs_transaction), 0,
  4170. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  4171. if (!btrfs_transaction_cachep)
  4172. goto fail;
  4173. btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
  4174. sizeof(struct btrfs_path), 0,
  4175. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  4176. if (!btrfs_path_cachep)
  4177. goto fail;
  4178. return 0;
  4179. fail:
  4180. btrfs_destroy_cachep();
  4181. return -ENOMEM;
  4182. }
  4183. static int btrfs_getattr(struct vfsmount *mnt,
  4184. struct dentry *dentry, struct kstat *stat)
  4185. {
  4186. struct inode *inode = dentry->d_inode;
  4187. generic_fillattr(inode, stat);
  4188. stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
  4189. stat->blksize = PAGE_CACHE_SIZE;
  4190. stat->blocks = (inode_get_bytes(inode) +
  4191. BTRFS_I(inode)->delalloc_bytes) >> 9;
  4192. return 0;
  4193. }
  4194. static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  4195. struct inode *new_dir, struct dentry *new_dentry)
  4196. {
  4197. struct btrfs_trans_handle *trans;
  4198. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  4199. struct inode *new_inode = new_dentry->d_inode;
  4200. struct inode *old_inode = old_dentry->d_inode;
  4201. struct timespec ctime = CURRENT_TIME;
  4202. u64 index = 0;
  4203. int ret;
  4204. /* we're not allowed to rename between subvolumes */
  4205. if (BTRFS_I(old_inode)->root->root_key.objectid !=
  4206. BTRFS_I(new_dir)->root->root_key.objectid)
  4207. return -EXDEV;
  4208. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  4209. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
  4210. return -ENOTEMPTY;
  4211. }
  4212. /* to rename a snapshot or subvolume, we need to juggle the
  4213. * backrefs. This isn't coded yet
  4214. */
  4215. if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  4216. return -EXDEV;
  4217. ret = btrfs_check_metadata_free_space(root);
  4218. if (ret)
  4219. goto out_unlock;
  4220. /*
  4221. * we're using rename to replace one file with another.
  4222. * and the replacement file is large. Start IO on it now so
  4223. * we don't add too much work to the end of the transaction
  4224. */
  4225. if (new_inode && old_inode && S_ISREG(old_inode->i_mode) &&
  4226. new_inode->i_size &&
  4227. old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  4228. filemap_flush(old_inode->i_mapping);
  4229. trans = btrfs_start_transaction(root, 1);
  4230. /*
  4231. * make sure the inode gets flushed if it is replacing
  4232. * something.
  4233. */
  4234. if (new_inode && new_inode->i_size &&
  4235. old_inode && S_ISREG(old_inode->i_mode)) {
  4236. btrfs_add_ordered_operation(trans, root, old_inode);
  4237. }
  4238. /*
  4239. * this is an ugly little race, but the rename is required to make
  4240. * sure that if we crash, the inode is either at the old name
  4241. * or the new one. pinning the log transaction lets us make sure
  4242. * we don't allow a log commit to come in after we unlink the
  4243. * name but before we add the new name back in.
  4244. */
  4245. btrfs_pin_log_trans(root);
  4246. btrfs_set_trans_block_group(trans, new_dir);
  4247. btrfs_inc_nlink(old_dentry->d_inode);
  4248. old_dir->i_ctime = old_dir->i_mtime = ctime;
  4249. new_dir->i_ctime = new_dir->i_mtime = ctime;
  4250. old_inode->i_ctime = ctime;
  4251. if (old_dentry->d_parent != new_dentry->d_parent)
  4252. btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
  4253. ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
  4254. old_dentry->d_name.name,
  4255. old_dentry->d_name.len);
  4256. if (ret)
  4257. goto out_fail;
  4258. if (new_inode) {
  4259. new_inode->i_ctime = CURRENT_TIME;
  4260. ret = btrfs_unlink_inode(trans, root, new_dir,
  4261. new_dentry->d_inode,
  4262. new_dentry->d_name.name,
  4263. new_dentry->d_name.len);
  4264. if (ret)
  4265. goto out_fail;
  4266. if (new_inode->i_nlink == 0) {
  4267. ret = btrfs_orphan_add(trans, new_dentry->d_inode);
  4268. if (ret)
  4269. goto out_fail;
  4270. }
  4271. }
  4272. ret = btrfs_set_inode_index(new_dir, &index);
  4273. if (ret)
  4274. goto out_fail;
  4275. ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
  4276. old_inode, new_dentry->d_name.name,
  4277. new_dentry->d_name.len, 1, index);
  4278. if (ret)
  4279. goto out_fail;
  4280. btrfs_log_new_name(trans, old_inode, old_dir,
  4281. new_dentry->d_parent);
  4282. out_fail:
  4283. /* this btrfs_end_log_trans just allows the current
  4284. * log-sub transaction to complete
  4285. */
  4286. btrfs_end_log_trans(root);
  4287. btrfs_end_transaction_throttle(trans, root);
  4288. out_unlock:
  4289. return ret;
  4290. }
  4291. /*
  4292. * some fairly slow code that needs optimization. This walks the list
  4293. * of all the inodes with pending delalloc and forces them to disk.
  4294. */
  4295. int btrfs_start_delalloc_inodes(struct btrfs_root *root)
  4296. {
  4297. struct list_head *head = &root->fs_info->delalloc_inodes;
  4298. struct btrfs_inode *binode;
  4299. struct inode *inode;
  4300. if (root->fs_info->sb->s_flags & MS_RDONLY)
  4301. return -EROFS;
  4302. spin_lock(&root->fs_info->delalloc_lock);
  4303. while (!list_empty(head)) {
  4304. binode = list_entry(head->next, struct btrfs_inode,
  4305. delalloc_inodes);
  4306. inode = igrab(&binode->vfs_inode);
  4307. if (!inode)
  4308. list_del_init(&binode->delalloc_inodes);
  4309. spin_unlock(&root->fs_info->delalloc_lock);
  4310. if (inode) {
  4311. filemap_flush(inode->i_mapping);
  4312. iput(inode);
  4313. }
  4314. cond_resched();
  4315. spin_lock(&root->fs_info->delalloc_lock);
  4316. }
  4317. spin_unlock(&root->fs_info->delalloc_lock);
  4318. /* the filemap_flush will queue IO into the worker threads, but
  4319. * we have to make sure the IO is actually started and that
  4320. * ordered extents get created before we return
  4321. */
  4322. atomic_inc(&root->fs_info->async_submit_draining);
  4323. while (atomic_read(&root->fs_info->nr_async_submits) ||
  4324. atomic_read(&root->fs_info->async_delalloc_pages)) {
  4325. wait_event(root->fs_info->async_submit_wait,
  4326. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  4327. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  4328. }
  4329. atomic_dec(&root->fs_info->async_submit_draining);
  4330. return 0;
  4331. }
  4332. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  4333. const char *symname)
  4334. {
  4335. struct btrfs_trans_handle *trans;
  4336. struct btrfs_root *root = BTRFS_I(dir)->root;
  4337. struct btrfs_path *path;
  4338. struct btrfs_key key;
  4339. struct inode *inode = NULL;
  4340. int err;
  4341. int drop_inode = 0;
  4342. u64 objectid;
  4343. u64 index = 0 ;
  4344. int name_len;
  4345. int datasize;
  4346. unsigned long ptr;
  4347. struct btrfs_file_extent_item *ei;
  4348. struct extent_buffer *leaf;
  4349. unsigned long nr = 0;
  4350. name_len = strlen(symname) + 1;
  4351. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
  4352. return -ENAMETOOLONG;
  4353. err = btrfs_check_metadata_free_space(root);
  4354. if (err)
  4355. goto out_fail;
  4356. trans = btrfs_start_transaction(root, 1);
  4357. btrfs_set_trans_block_group(trans, dir);
  4358. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  4359. if (err) {
  4360. err = -ENOSPC;
  4361. goto out_unlock;
  4362. }
  4363. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4364. dentry->d_name.len,
  4365. dentry->d_parent->d_inode->i_ino, objectid,
  4366. BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
  4367. &index);
  4368. err = PTR_ERR(inode);
  4369. if (IS_ERR(inode))
  4370. goto out_unlock;
  4371. err = btrfs_init_inode_security(inode, dir);
  4372. if (err) {
  4373. drop_inode = 1;
  4374. goto out_unlock;
  4375. }
  4376. btrfs_set_trans_block_group(trans, inode);
  4377. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  4378. if (err)
  4379. drop_inode = 1;
  4380. else {
  4381. inode->i_mapping->a_ops = &btrfs_aops;
  4382. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  4383. inode->i_fop = &btrfs_file_operations;
  4384. inode->i_op = &btrfs_file_inode_operations;
  4385. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  4386. }
  4387. dir->i_sb->s_dirt = 1;
  4388. btrfs_update_inode_block_group(trans, inode);
  4389. btrfs_update_inode_block_group(trans, dir);
  4390. if (drop_inode)
  4391. goto out_unlock;
  4392. path = btrfs_alloc_path();
  4393. BUG_ON(!path);
  4394. key.objectid = inode->i_ino;
  4395. key.offset = 0;
  4396. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  4397. datasize = btrfs_file_extent_calc_inline_size(name_len);
  4398. err = btrfs_insert_empty_item(trans, root, path, &key,
  4399. datasize);
  4400. if (err) {
  4401. drop_inode = 1;
  4402. goto out_unlock;
  4403. }
  4404. leaf = path->nodes[0];
  4405. ei = btrfs_item_ptr(leaf, path->slots[0],
  4406. struct btrfs_file_extent_item);
  4407. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  4408. btrfs_set_file_extent_type(leaf, ei,
  4409. BTRFS_FILE_EXTENT_INLINE);
  4410. btrfs_set_file_extent_encryption(leaf, ei, 0);
  4411. btrfs_set_file_extent_compression(leaf, ei, 0);
  4412. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  4413. btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
  4414. ptr = btrfs_file_extent_inline_start(ei);
  4415. write_extent_buffer(leaf, symname, ptr, name_len);
  4416. btrfs_mark_buffer_dirty(leaf);
  4417. btrfs_free_path(path);
  4418. inode->i_op = &btrfs_symlink_inode_operations;
  4419. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  4420. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  4421. inode_set_bytes(inode, name_len);
  4422. btrfs_i_size_write(inode, name_len - 1);
  4423. err = btrfs_update_inode(trans, root, inode);
  4424. if (err)
  4425. drop_inode = 1;
  4426. out_unlock:
  4427. nr = trans->blocks_used;
  4428. btrfs_end_transaction_throttle(trans, root);
  4429. out_fail:
  4430. if (drop_inode) {
  4431. inode_dec_link_count(inode);
  4432. iput(inode);
  4433. }
  4434. btrfs_btree_balance_dirty(root, nr);
  4435. return err;
  4436. }
  4437. static int prealloc_file_range(struct btrfs_trans_handle *trans,
  4438. struct inode *inode, u64 start, u64 end,
  4439. u64 locked_end, u64 alloc_hint, int mode)
  4440. {
  4441. struct btrfs_root *root = BTRFS_I(inode)->root;
  4442. struct btrfs_key ins;
  4443. u64 alloc_size;
  4444. u64 cur_offset = start;
  4445. u64 num_bytes = end - start;
  4446. int ret = 0;
  4447. while (num_bytes > 0) {
  4448. alloc_size = min(num_bytes, root->fs_info->max_extent);
  4449. ret = btrfs_reserve_extent(trans, root, alloc_size,
  4450. root->sectorsize, 0, alloc_hint,
  4451. (u64)-1, &ins, 1);
  4452. if (ret) {
  4453. WARN_ON(1);
  4454. goto out;
  4455. }
  4456. ret = insert_reserved_file_extent(trans, inode,
  4457. cur_offset, ins.objectid,
  4458. ins.offset, ins.offset,
  4459. ins.offset, locked_end,
  4460. 0, 0, 0,
  4461. BTRFS_FILE_EXTENT_PREALLOC);
  4462. BUG_ON(ret);
  4463. num_bytes -= ins.offset;
  4464. cur_offset += ins.offset;
  4465. alloc_hint = ins.objectid + ins.offset;
  4466. }
  4467. out:
  4468. if (cur_offset > start) {
  4469. inode->i_ctime = CURRENT_TIME;
  4470. btrfs_set_flag(inode, PREALLOC);
  4471. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  4472. cur_offset > i_size_read(inode))
  4473. btrfs_i_size_write(inode, cur_offset);
  4474. ret = btrfs_update_inode(trans, root, inode);
  4475. BUG_ON(ret);
  4476. }
  4477. return ret;
  4478. }
  4479. static long btrfs_fallocate(struct inode *inode, int mode,
  4480. loff_t offset, loff_t len)
  4481. {
  4482. u64 cur_offset;
  4483. u64 last_byte;
  4484. u64 alloc_start;
  4485. u64 alloc_end;
  4486. u64 alloc_hint = 0;
  4487. u64 locked_end;
  4488. u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
  4489. struct extent_map *em;
  4490. struct btrfs_trans_handle *trans;
  4491. int ret;
  4492. alloc_start = offset & ~mask;
  4493. alloc_end = (offset + len + mask) & ~mask;
  4494. /*
  4495. * wait for ordered IO before we have any locks. We'll loop again
  4496. * below with the locks held.
  4497. */
  4498. btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
  4499. mutex_lock(&inode->i_mutex);
  4500. if (alloc_start > inode->i_size) {
  4501. ret = btrfs_cont_expand(inode, alloc_start);
  4502. if (ret)
  4503. goto out;
  4504. }
  4505. locked_end = alloc_end - 1;
  4506. while (1) {
  4507. struct btrfs_ordered_extent *ordered;
  4508. trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
  4509. if (!trans) {
  4510. ret = -EIO;
  4511. goto out;
  4512. }
  4513. /* the extent lock is ordered inside the running
  4514. * transaction
  4515. */
  4516. lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
  4517. GFP_NOFS);
  4518. ordered = btrfs_lookup_first_ordered_extent(inode,
  4519. alloc_end - 1);
  4520. if (ordered &&
  4521. ordered->file_offset + ordered->len > alloc_start &&
  4522. ordered->file_offset < alloc_end) {
  4523. btrfs_put_ordered_extent(ordered);
  4524. unlock_extent(&BTRFS_I(inode)->io_tree,
  4525. alloc_start, locked_end, GFP_NOFS);
  4526. btrfs_end_transaction(trans, BTRFS_I(inode)->root);
  4527. /*
  4528. * we can't wait on the range with the transaction
  4529. * running or with the extent lock held
  4530. */
  4531. btrfs_wait_ordered_range(inode, alloc_start,
  4532. alloc_end - alloc_start);
  4533. } else {
  4534. if (ordered)
  4535. btrfs_put_ordered_extent(ordered);
  4536. break;
  4537. }
  4538. }
  4539. cur_offset = alloc_start;
  4540. while (1) {
  4541. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  4542. alloc_end - cur_offset, 0);
  4543. BUG_ON(IS_ERR(em) || !em);
  4544. last_byte = min(extent_map_end(em), alloc_end);
  4545. last_byte = (last_byte + mask) & ~mask;
  4546. if (em->block_start == EXTENT_MAP_HOLE) {
  4547. ret = prealloc_file_range(trans, inode, cur_offset,
  4548. last_byte, locked_end + 1,
  4549. alloc_hint, mode);
  4550. if (ret < 0) {
  4551. free_extent_map(em);
  4552. break;
  4553. }
  4554. }
  4555. if (em->block_start <= EXTENT_MAP_LAST_BYTE)
  4556. alloc_hint = em->block_start;
  4557. free_extent_map(em);
  4558. cur_offset = last_byte;
  4559. if (cur_offset >= alloc_end) {
  4560. ret = 0;
  4561. break;
  4562. }
  4563. }
  4564. unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
  4565. GFP_NOFS);
  4566. btrfs_end_transaction(trans, BTRFS_I(inode)->root);
  4567. out:
  4568. mutex_unlock(&inode->i_mutex);
  4569. return ret;
  4570. }
  4571. static int btrfs_set_page_dirty(struct page *page)
  4572. {
  4573. return __set_page_dirty_nobuffers(page);
  4574. }
  4575. static int btrfs_permission(struct inode *inode, int mask)
  4576. {
  4577. if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
  4578. return -EACCES;
  4579. return generic_permission(inode, mask, btrfs_check_acl);
  4580. }
  4581. static struct inode_operations btrfs_dir_inode_operations = {
  4582. .getattr = btrfs_getattr,
  4583. .lookup = btrfs_lookup,
  4584. .create = btrfs_create,
  4585. .unlink = btrfs_unlink,
  4586. .link = btrfs_link,
  4587. .mkdir = btrfs_mkdir,
  4588. .rmdir = btrfs_rmdir,
  4589. .rename = btrfs_rename,
  4590. .symlink = btrfs_symlink,
  4591. .setattr = btrfs_setattr,
  4592. .mknod = btrfs_mknod,
  4593. .setxattr = btrfs_setxattr,
  4594. .getxattr = btrfs_getxattr,
  4595. .listxattr = btrfs_listxattr,
  4596. .removexattr = btrfs_removexattr,
  4597. .permission = btrfs_permission,
  4598. };
  4599. static struct inode_operations btrfs_dir_ro_inode_operations = {
  4600. .lookup = btrfs_lookup,
  4601. .permission = btrfs_permission,
  4602. };
  4603. static struct file_operations btrfs_dir_file_operations = {
  4604. .llseek = generic_file_llseek,
  4605. .read = generic_read_dir,
  4606. .readdir = btrfs_real_readdir,
  4607. .unlocked_ioctl = btrfs_ioctl,
  4608. #ifdef CONFIG_COMPAT
  4609. .compat_ioctl = btrfs_ioctl,
  4610. #endif
  4611. .release = btrfs_release_file,
  4612. .fsync = btrfs_sync_file,
  4613. };
  4614. static struct extent_io_ops btrfs_extent_io_ops = {
  4615. .fill_delalloc = run_delalloc_range,
  4616. .submit_bio_hook = btrfs_submit_bio_hook,
  4617. .merge_bio_hook = btrfs_merge_bio_hook,
  4618. .readpage_end_io_hook = btrfs_readpage_end_io_hook,
  4619. .writepage_end_io_hook = btrfs_writepage_end_io_hook,
  4620. .writepage_start_hook = btrfs_writepage_start_hook,
  4621. .readpage_io_failed_hook = btrfs_io_failed_hook,
  4622. .set_bit_hook = btrfs_set_bit_hook,
  4623. .clear_bit_hook = btrfs_clear_bit_hook,
  4624. };
  4625. /*
  4626. * btrfs doesn't support the bmap operation because swapfiles
  4627. * use bmap to make a mapping of extents in the file. They assume
  4628. * these extents won't change over the life of the file and they
  4629. * use the bmap result to do IO directly to the drive.
  4630. *
  4631. * the btrfs bmap call would return logical addresses that aren't
  4632. * suitable for IO and they also will change frequently as COW
  4633. * operations happen. So, swapfile + btrfs == corruption.
  4634. *
  4635. * For now we're avoiding this by dropping bmap.
  4636. */
  4637. static struct address_space_operations btrfs_aops = {
  4638. .readpage = btrfs_readpage,
  4639. .writepage = btrfs_writepage,
  4640. .writepages = btrfs_writepages,
  4641. .readpages = btrfs_readpages,
  4642. .sync_page = block_sync_page,
  4643. .direct_IO = btrfs_direct_IO,
  4644. .invalidatepage = btrfs_invalidatepage,
  4645. .releasepage = btrfs_releasepage,
  4646. .set_page_dirty = btrfs_set_page_dirty,
  4647. };
  4648. static struct address_space_operations btrfs_symlink_aops = {
  4649. .readpage = btrfs_readpage,
  4650. .writepage = btrfs_writepage,
  4651. .invalidatepage = btrfs_invalidatepage,
  4652. .releasepage = btrfs_releasepage,
  4653. };
  4654. static struct inode_operations btrfs_file_inode_operations = {
  4655. .truncate = btrfs_truncate,
  4656. .getattr = btrfs_getattr,
  4657. .setattr = btrfs_setattr,
  4658. .setxattr = btrfs_setxattr,
  4659. .getxattr = btrfs_getxattr,
  4660. .listxattr = btrfs_listxattr,
  4661. .removexattr = btrfs_removexattr,
  4662. .permission = btrfs_permission,
  4663. .fallocate = btrfs_fallocate,
  4664. .fiemap = btrfs_fiemap,
  4665. };
  4666. static struct inode_operations btrfs_special_inode_operations = {
  4667. .getattr = btrfs_getattr,
  4668. .setattr = btrfs_setattr,
  4669. .permission = btrfs_permission,
  4670. .setxattr = btrfs_setxattr,
  4671. .getxattr = btrfs_getxattr,
  4672. .listxattr = btrfs_listxattr,
  4673. .removexattr = btrfs_removexattr,
  4674. };
  4675. static struct inode_operations btrfs_symlink_inode_operations = {
  4676. .readlink = generic_readlink,
  4677. .follow_link = page_follow_link_light,
  4678. .put_link = page_put_link,
  4679. .permission = btrfs_permission,
  4680. .setxattr = btrfs_setxattr,
  4681. .getxattr = btrfs_getxattr,
  4682. .listxattr = btrfs_listxattr,
  4683. .removexattr = btrfs_removexattr,
  4684. };