inode.c 135 KB

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