inode.c 134 KB

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