inode.c 133 KB

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