inode.c 133 KB

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