inode.c 139 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823
  1. /*
  2. * linux/fs/ext4/inode.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/inode.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * 64-bit file support on 64-bit platforms by Jakub Jelinek
  16. * (jj@sunsite.ms.mff.cuni.cz)
  17. *
  18. * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000
  19. */
  20. #include <linux/fs.h>
  21. #include <linux/time.h>
  22. #include <linux/jbd2.h>
  23. #include <linux/highuid.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/quotaops.h>
  26. #include <linux/string.h>
  27. #include <linux/buffer_head.h>
  28. #include <linux/writeback.h>
  29. #include <linux/pagevec.h>
  30. #include <linux/mpage.h>
  31. #include <linux/namei.h>
  32. #include <linux/uio.h>
  33. #include <linux/bio.h>
  34. #include <linux/workqueue.h>
  35. #include <linux/kernel.h>
  36. #include <linux/printk.h>
  37. #include <linux/slab.h>
  38. #include <linux/ratelimit.h>
  39. #include "ext4_jbd2.h"
  40. #include "xattr.h"
  41. #include "acl.h"
  42. #include "truncate.h"
  43. #include <trace/events/ext4.h>
  44. #define MPAGE_DA_EXTENT_TAIL 0x01
  45. static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw,
  46. struct ext4_inode_info *ei)
  47. {
  48. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  49. __u16 csum_lo;
  50. __u16 csum_hi = 0;
  51. __u32 csum;
  52. csum_lo = raw->i_checksum_lo;
  53. raw->i_checksum_lo = 0;
  54. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
  55. EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) {
  56. csum_hi = raw->i_checksum_hi;
  57. raw->i_checksum_hi = 0;
  58. }
  59. csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw,
  60. EXT4_INODE_SIZE(inode->i_sb));
  61. raw->i_checksum_lo = csum_lo;
  62. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
  63. EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
  64. raw->i_checksum_hi = csum_hi;
  65. return csum;
  66. }
  67. static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw,
  68. struct ext4_inode_info *ei)
  69. {
  70. __u32 provided, calculated;
  71. if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
  72. cpu_to_le32(EXT4_OS_LINUX) ||
  73. !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
  74. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
  75. return 1;
  76. provided = le16_to_cpu(raw->i_checksum_lo);
  77. calculated = ext4_inode_csum(inode, raw, ei);
  78. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
  79. EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
  80. provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16;
  81. else
  82. calculated &= 0xFFFF;
  83. return provided == calculated;
  84. }
  85. static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
  86. struct ext4_inode_info *ei)
  87. {
  88. __u32 csum;
  89. if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
  90. cpu_to_le32(EXT4_OS_LINUX) ||
  91. !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
  92. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
  93. return;
  94. csum = ext4_inode_csum(inode, raw, ei);
  95. raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF);
  96. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
  97. EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
  98. raw->i_checksum_hi = cpu_to_le16(csum >> 16);
  99. }
  100. static inline int ext4_begin_ordered_truncate(struct inode *inode,
  101. loff_t new_size)
  102. {
  103. trace_ext4_begin_ordered_truncate(inode, new_size);
  104. /*
  105. * If jinode is zero, then we never opened the file for
  106. * writing, so there's no need to call
  107. * jbd2_journal_begin_ordered_truncate() since there's no
  108. * outstanding writes we need to flush.
  109. */
  110. if (!EXT4_I(inode)->jinode)
  111. return 0;
  112. return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode),
  113. EXT4_I(inode)->jinode,
  114. new_size);
  115. }
  116. static void ext4_invalidatepage(struct page *page, unsigned long offset);
  117. static int __ext4_journalled_writepage(struct page *page, unsigned int len);
  118. static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
  119. static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle,
  120. struct inode *inode, struct page *page, loff_t from,
  121. loff_t length, int flags);
  122. /*
  123. * Test whether an inode is a fast symlink.
  124. */
  125. static int ext4_inode_is_fast_symlink(struct inode *inode)
  126. {
  127. int ea_blocks = EXT4_I(inode)->i_file_acl ?
  128. (inode->i_sb->s_blocksize >> 9) : 0;
  129. return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
  130. }
  131. /*
  132. * Restart the transaction associated with *handle. This does a commit,
  133. * so before we call here everything must be consistently dirtied against
  134. * this transaction.
  135. */
  136. int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode,
  137. int nblocks)
  138. {
  139. int ret;
  140. /*
  141. * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
  142. * moment, get_block can be called only for blocks inside i_size since
  143. * page cache has been already dropped and writes are blocked by
  144. * i_mutex. So we can safely drop the i_data_sem here.
  145. */
  146. BUG_ON(EXT4_JOURNAL(inode) == NULL);
  147. jbd_debug(2, "restarting handle %p\n", handle);
  148. up_write(&EXT4_I(inode)->i_data_sem);
  149. ret = ext4_journal_restart(handle, nblocks);
  150. down_write(&EXT4_I(inode)->i_data_sem);
  151. ext4_discard_preallocations(inode);
  152. return ret;
  153. }
  154. /*
  155. * Called at the last iput() if i_nlink is zero.
  156. */
  157. void ext4_evict_inode(struct inode *inode)
  158. {
  159. handle_t *handle;
  160. int err;
  161. trace_ext4_evict_inode(inode);
  162. ext4_ioend_wait(inode);
  163. if (inode->i_nlink) {
  164. /*
  165. * When journalling data dirty buffers are tracked only in the
  166. * journal. So although mm thinks everything is clean and
  167. * ready for reaping the inode might still have some pages to
  168. * write in the running transaction or waiting to be
  169. * checkpointed. Thus calling jbd2_journal_invalidatepage()
  170. * (via truncate_inode_pages()) to discard these buffers can
  171. * cause data loss. Also even if we did not discard these
  172. * buffers, we would have no way to find them after the inode
  173. * is reaped and thus user could see stale data if he tries to
  174. * read them before the transaction is checkpointed. So be
  175. * careful and force everything to disk here... We use
  176. * ei->i_datasync_tid to store the newest transaction
  177. * containing inode's data.
  178. *
  179. * Note that directories do not have this problem because they
  180. * don't use page cache.
  181. */
  182. if (ext4_should_journal_data(inode) &&
  183. (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode))) {
  184. journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
  185. tid_t commit_tid = EXT4_I(inode)->i_datasync_tid;
  186. jbd2_log_start_commit(journal, commit_tid);
  187. jbd2_log_wait_commit(journal, commit_tid);
  188. filemap_write_and_wait(&inode->i_data);
  189. }
  190. truncate_inode_pages(&inode->i_data, 0);
  191. goto no_delete;
  192. }
  193. if (!is_bad_inode(inode))
  194. dquot_initialize(inode);
  195. if (ext4_should_order_data(inode))
  196. ext4_begin_ordered_truncate(inode, 0);
  197. truncate_inode_pages(&inode->i_data, 0);
  198. if (is_bad_inode(inode))
  199. goto no_delete;
  200. /*
  201. * Protect us against freezing - iput() caller didn't have to have any
  202. * protection against it
  203. */
  204. sb_start_intwrite(inode->i_sb);
  205. handle = ext4_journal_start(inode, ext4_blocks_for_truncate(inode)+3);
  206. if (IS_ERR(handle)) {
  207. ext4_std_error(inode->i_sb, PTR_ERR(handle));
  208. /*
  209. * If we're going to skip the normal cleanup, we still need to
  210. * make sure that the in-core orphan linked list is properly
  211. * cleaned up.
  212. */
  213. ext4_orphan_del(NULL, inode);
  214. sb_end_intwrite(inode->i_sb);
  215. goto no_delete;
  216. }
  217. if (IS_SYNC(inode))
  218. ext4_handle_sync(handle);
  219. inode->i_size = 0;
  220. err = ext4_mark_inode_dirty(handle, inode);
  221. if (err) {
  222. ext4_warning(inode->i_sb,
  223. "couldn't mark inode dirty (err %d)", err);
  224. goto stop_handle;
  225. }
  226. if (inode->i_blocks)
  227. ext4_truncate(inode);
  228. /*
  229. * ext4_ext_truncate() doesn't reserve any slop when it
  230. * restarts journal transactions; therefore there may not be
  231. * enough credits left in the handle to remove the inode from
  232. * the orphan list and set the dtime field.
  233. */
  234. if (!ext4_handle_has_enough_credits(handle, 3)) {
  235. err = ext4_journal_extend(handle, 3);
  236. if (err > 0)
  237. err = ext4_journal_restart(handle, 3);
  238. if (err != 0) {
  239. ext4_warning(inode->i_sb,
  240. "couldn't extend journal (err %d)", err);
  241. stop_handle:
  242. ext4_journal_stop(handle);
  243. ext4_orphan_del(NULL, inode);
  244. sb_end_intwrite(inode->i_sb);
  245. goto no_delete;
  246. }
  247. }
  248. /*
  249. * Kill off the orphan record which ext4_truncate created.
  250. * AKPM: I think this can be inside the above `if'.
  251. * Note that ext4_orphan_del() has to be able to cope with the
  252. * deletion of a non-existent orphan - this is because we don't
  253. * know if ext4_truncate() actually created an orphan record.
  254. * (Well, we could do this if we need to, but heck - it works)
  255. */
  256. ext4_orphan_del(handle, inode);
  257. EXT4_I(inode)->i_dtime = get_seconds();
  258. /*
  259. * One subtle ordering requirement: if anything has gone wrong
  260. * (transaction abort, IO errors, whatever), then we can still
  261. * do these next steps (the fs will already have been marked as
  262. * having errors), but we can't free the inode if the mark_dirty
  263. * fails.
  264. */
  265. if (ext4_mark_inode_dirty(handle, inode))
  266. /* If that failed, just do the required in-core inode clear. */
  267. ext4_clear_inode(inode);
  268. else
  269. ext4_free_inode(handle, inode);
  270. ext4_journal_stop(handle);
  271. sb_end_intwrite(inode->i_sb);
  272. return;
  273. no_delete:
  274. ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
  275. }
  276. #ifdef CONFIG_QUOTA
  277. qsize_t *ext4_get_reserved_space(struct inode *inode)
  278. {
  279. return &EXT4_I(inode)->i_reserved_quota;
  280. }
  281. #endif
  282. /*
  283. * Calculate the number of metadata blocks need to reserve
  284. * to allocate a block located at @lblock
  285. */
  286. static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
  287. {
  288. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  289. return ext4_ext_calc_metadata_amount(inode, lblock);
  290. return ext4_ind_calc_metadata_amount(inode, lblock);
  291. }
  292. /*
  293. * Called with i_data_sem down, which is important since we can call
  294. * ext4_discard_preallocations() from here.
  295. */
  296. void ext4_da_update_reserve_space(struct inode *inode,
  297. int used, int quota_claim)
  298. {
  299. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  300. struct ext4_inode_info *ei = EXT4_I(inode);
  301. spin_lock(&ei->i_block_reservation_lock);
  302. trace_ext4_da_update_reserve_space(inode, used, quota_claim);
  303. if (unlikely(used > ei->i_reserved_data_blocks)) {
  304. ext4_msg(inode->i_sb, KERN_NOTICE, "%s: ino %lu, used %d "
  305. "with only %d reserved data blocks",
  306. __func__, inode->i_ino, used,
  307. ei->i_reserved_data_blocks);
  308. WARN_ON(1);
  309. used = ei->i_reserved_data_blocks;
  310. }
  311. if (unlikely(ei->i_allocated_meta_blocks > ei->i_reserved_meta_blocks)) {
  312. ext4_msg(inode->i_sb, KERN_NOTICE, "%s: ino %lu, allocated %d "
  313. "with only %d reserved metadata blocks\n", __func__,
  314. inode->i_ino, ei->i_allocated_meta_blocks,
  315. ei->i_reserved_meta_blocks);
  316. WARN_ON(1);
  317. ei->i_allocated_meta_blocks = ei->i_reserved_meta_blocks;
  318. }
  319. /* Update per-inode reservations */
  320. ei->i_reserved_data_blocks -= used;
  321. ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks;
  322. percpu_counter_sub(&sbi->s_dirtyclusters_counter,
  323. used + ei->i_allocated_meta_blocks);
  324. ei->i_allocated_meta_blocks = 0;
  325. if (ei->i_reserved_data_blocks == 0) {
  326. /*
  327. * We can release all of the reserved metadata blocks
  328. * only when we have written all of the delayed
  329. * allocation blocks.
  330. */
  331. percpu_counter_sub(&sbi->s_dirtyclusters_counter,
  332. ei->i_reserved_meta_blocks);
  333. ei->i_reserved_meta_blocks = 0;
  334. ei->i_da_metadata_calc_len = 0;
  335. }
  336. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  337. /* Update quota subsystem for data blocks */
  338. if (quota_claim)
  339. dquot_claim_block(inode, EXT4_C2B(sbi, used));
  340. else {
  341. /*
  342. * We did fallocate with an offset that is already delayed
  343. * allocated. So on delayed allocated writeback we should
  344. * not re-claim the quota for fallocated blocks.
  345. */
  346. dquot_release_reservation_block(inode, EXT4_C2B(sbi, used));
  347. }
  348. /*
  349. * If we have done all the pending block allocations and if
  350. * there aren't any writers on the inode, we can discard the
  351. * inode's preallocations.
  352. */
  353. if ((ei->i_reserved_data_blocks == 0) &&
  354. (atomic_read(&inode->i_writecount) == 0))
  355. ext4_discard_preallocations(inode);
  356. }
  357. static int __check_block_validity(struct inode *inode, const char *func,
  358. unsigned int line,
  359. struct ext4_map_blocks *map)
  360. {
  361. if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
  362. map->m_len)) {
  363. ext4_error_inode(inode, func, line, map->m_pblk,
  364. "lblock %lu mapped to illegal pblock "
  365. "(length %d)", (unsigned long) map->m_lblk,
  366. map->m_len);
  367. return -EIO;
  368. }
  369. return 0;
  370. }
  371. #define check_block_validity(inode, map) \
  372. __check_block_validity((inode), __func__, __LINE__, (map))
  373. /*
  374. * Return the number of contiguous dirty pages in a given inode
  375. * starting at page frame idx.
  376. */
  377. static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx,
  378. unsigned int max_pages)
  379. {
  380. struct address_space *mapping = inode->i_mapping;
  381. pgoff_t index;
  382. struct pagevec pvec;
  383. pgoff_t num = 0;
  384. int i, nr_pages, done = 0;
  385. if (max_pages == 0)
  386. return 0;
  387. pagevec_init(&pvec, 0);
  388. while (!done) {
  389. index = idx;
  390. nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  391. PAGECACHE_TAG_DIRTY,
  392. (pgoff_t)PAGEVEC_SIZE);
  393. if (nr_pages == 0)
  394. break;
  395. for (i = 0; i < nr_pages; i++) {
  396. struct page *page = pvec.pages[i];
  397. struct buffer_head *bh, *head;
  398. lock_page(page);
  399. if (unlikely(page->mapping != mapping) ||
  400. !PageDirty(page) ||
  401. PageWriteback(page) ||
  402. page->index != idx) {
  403. done = 1;
  404. unlock_page(page);
  405. break;
  406. }
  407. if (page_has_buffers(page)) {
  408. bh = head = page_buffers(page);
  409. do {
  410. if (!buffer_delay(bh) &&
  411. !buffer_unwritten(bh))
  412. done = 1;
  413. bh = bh->b_this_page;
  414. } while (!done && (bh != head));
  415. }
  416. unlock_page(page);
  417. if (done)
  418. break;
  419. idx++;
  420. num++;
  421. if (num >= max_pages) {
  422. done = 1;
  423. break;
  424. }
  425. }
  426. pagevec_release(&pvec);
  427. }
  428. return num;
  429. }
  430. /*
  431. * The ext4_map_blocks() function tries to look up the requested blocks,
  432. * and returns if the blocks are already mapped.
  433. *
  434. * Otherwise it takes the write lock of the i_data_sem and allocate blocks
  435. * and store the allocated blocks in the result buffer head and mark it
  436. * mapped.
  437. *
  438. * If file type is extents based, it will call ext4_ext_map_blocks(),
  439. * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
  440. * based files
  441. *
  442. * On success, it returns the number of blocks being mapped or allocate.
  443. * if create==0 and the blocks are pre-allocated and uninitialized block,
  444. * the result buffer head is unmapped. If the create ==1, it will make sure
  445. * the buffer head is mapped.
  446. *
  447. * It returns 0 if plain look up failed (blocks have not been allocated), in
  448. * that case, buffer head is unmapped
  449. *
  450. * It returns the error in case of allocation failure.
  451. */
  452. int ext4_map_blocks(handle_t *handle, struct inode *inode,
  453. struct ext4_map_blocks *map, int flags)
  454. {
  455. int retval;
  456. map->m_flags = 0;
  457. ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
  458. "logical block %lu\n", inode->i_ino, flags, map->m_len,
  459. (unsigned long) map->m_lblk);
  460. /*
  461. * Try to see if we can get the block without requesting a new
  462. * file system block.
  463. */
  464. if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
  465. down_read((&EXT4_I(inode)->i_data_sem));
  466. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
  467. retval = ext4_ext_map_blocks(handle, inode, map, flags &
  468. EXT4_GET_BLOCKS_KEEP_SIZE);
  469. } else {
  470. retval = ext4_ind_map_blocks(handle, inode, map, flags &
  471. EXT4_GET_BLOCKS_KEEP_SIZE);
  472. }
  473. if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
  474. up_read((&EXT4_I(inode)->i_data_sem));
  475. if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
  476. int ret;
  477. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
  478. /* delayed alloc may be allocated by fallocate and
  479. * coverted to initialized by directIO.
  480. * we need to handle delayed extent here.
  481. */
  482. down_write((&EXT4_I(inode)->i_data_sem));
  483. goto delayed_mapped;
  484. }
  485. ret = check_block_validity(inode, map);
  486. if (ret != 0)
  487. return ret;
  488. }
  489. /* If it is only a block(s) look up */
  490. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
  491. return retval;
  492. /*
  493. * Returns if the blocks have already allocated
  494. *
  495. * Note that if blocks have been preallocated
  496. * ext4_ext_get_block() returns the create = 0
  497. * with buffer head unmapped.
  498. */
  499. if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
  500. return retval;
  501. /*
  502. * When we call get_blocks without the create flag, the
  503. * BH_Unwritten flag could have gotten set if the blocks
  504. * requested were part of a uninitialized extent. We need to
  505. * clear this flag now that we are committed to convert all or
  506. * part of the uninitialized extent to be an initialized
  507. * extent. This is because we need to avoid the combination
  508. * of BH_Unwritten and BH_Mapped flags being simultaneously
  509. * set on the buffer_head.
  510. */
  511. map->m_flags &= ~EXT4_MAP_UNWRITTEN;
  512. /*
  513. * New blocks allocate and/or writing to uninitialized extent
  514. * will possibly result in updating i_data, so we take
  515. * the write lock of i_data_sem, and call get_blocks()
  516. * with create == 1 flag.
  517. */
  518. down_write((&EXT4_I(inode)->i_data_sem));
  519. /*
  520. * if the caller is from delayed allocation writeout path
  521. * we have already reserved fs blocks for allocation
  522. * let the underlying get_block() function know to
  523. * avoid double accounting
  524. */
  525. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
  526. ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED);
  527. /*
  528. * We need to check for EXT4 here because migrate
  529. * could have changed the inode type in between
  530. */
  531. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
  532. retval = ext4_ext_map_blocks(handle, inode, map, flags);
  533. } else {
  534. retval = ext4_ind_map_blocks(handle, inode, map, flags);
  535. if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
  536. /*
  537. * We allocated new blocks which will result in
  538. * i_data's format changing. Force the migrate
  539. * to fail by clearing migrate flags
  540. */
  541. ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
  542. }
  543. /*
  544. * Update reserved blocks/metadata blocks after successful
  545. * block allocation which had been deferred till now. We don't
  546. * support fallocate for non extent files. So we can update
  547. * reserve space here.
  548. */
  549. if ((retval > 0) &&
  550. (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
  551. ext4_da_update_reserve_space(inode, retval, 1);
  552. }
  553. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
  554. ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED);
  555. if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
  556. int ret;
  557. delayed_mapped:
  558. /* delayed allocation blocks has been allocated */
  559. ret = ext4_es_remove_extent(inode, map->m_lblk,
  560. map->m_len);
  561. if (ret < 0)
  562. retval = ret;
  563. }
  564. }
  565. up_write((&EXT4_I(inode)->i_data_sem));
  566. if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
  567. int ret = check_block_validity(inode, map);
  568. if (ret != 0)
  569. return ret;
  570. }
  571. return retval;
  572. }
  573. /* Maximum number of blocks we map for direct IO at once. */
  574. #define DIO_MAX_BLOCKS 4096
  575. static int _ext4_get_block(struct inode *inode, sector_t iblock,
  576. struct buffer_head *bh, int flags)
  577. {
  578. handle_t *handle = ext4_journal_current_handle();
  579. struct ext4_map_blocks map;
  580. int ret = 0, started = 0;
  581. int dio_credits;
  582. if (ext4_has_inline_data(inode))
  583. return -ERANGE;
  584. map.m_lblk = iblock;
  585. map.m_len = bh->b_size >> inode->i_blkbits;
  586. if (flags && !(flags & EXT4_GET_BLOCKS_NO_LOCK) && !handle) {
  587. /* Direct IO write... */
  588. if (map.m_len > DIO_MAX_BLOCKS)
  589. map.m_len = DIO_MAX_BLOCKS;
  590. dio_credits = ext4_chunk_trans_blocks(inode, map.m_len);
  591. handle = ext4_journal_start(inode, dio_credits);
  592. if (IS_ERR(handle)) {
  593. ret = PTR_ERR(handle);
  594. return ret;
  595. }
  596. started = 1;
  597. }
  598. ret = ext4_map_blocks(handle, inode, &map, flags);
  599. if (ret > 0) {
  600. map_bh(bh, inode->i_sb, map.m_pblk);
  601. bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
  602. bh->b_size = inode->i_sb->s_blocksize * map.m_len;
  603. ret = 0;
  604. }
  605. if (started)
  606. ext4_journal_stop(handle);
  607. return ret;
  608. }
  609. int ext4_get_block(struct inode *inode, sector_t iblock,
  610. struct buffer_head *bh, int create)
  611. {
  612. return _ext4_get_block(inode, iblock, bh,
  613. create ? EXT4_GET_BLOCKS_CREATE : 0);
  614. }
  615. /*
  616. * `handle' can be NULL if create is zero
  617. */
  618. struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
  619. ext4_lblk_t block, int create, int *errp)
  620. {
  621. struct ext4_map_blocks map;
  622. struct buffer_head *bh;
  623. int fatal = 0, err;
  624. J_ASSERT(handle != NULL || create == 0);
  625. map.m_lblk = block;
  626. map.m_len = 1;
  627. err = ext4_map_blocks(handle, inode, &map,
  628. create ? EXT4_GET_BLOCKS_CREATE : 0);
  629. /* ensure we send some value back into *errp */
  630. *errp = 0;
  631. if (err < 0)
  632. *errp = err;
  633. if (err <= 0)
  634. return NULL;
  635. bh = sb_getblk(inode->i_sb, map.m_pblk);
  636. if (unlikely(!bh)) {
  637. *errp = -ENOMEM;
  638. return NULL;
  639. }
  640. if (map.m_flags & EXT4_MAP_NEW) {
  641. J_ASSERT(create != 0);
  642. J_ASSERT(handle != NULL);
  643. /*
  644. * Now that we do not always journal data, we should
  645. * keep in mind whether this should always journal the
  646. * new buffer as metadata. For now, regular file
  647. * writes use ext4_get_block instead, so it's not a
  648. * problem.
  649. */
  650. lock_buffer(bh);
  651. BUFFER_TRACE(bh, "call get_create_access");
  652. fatal = ext4_journal_get_create_access(handle, bh);
  653. if (!fatal && !buffer_uptodate(bh)) {
  654. memset(bh->b_data, 0, inode->i_sb->s_blocksize);
  655. set_buffer_uptodate(bh);
  656. }
  657. unlock_buffer(bh);
  658. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  659. err = ext4_handle_dirty_metadata(handle, inode, bh);
  660. if (!fatal)
  661. fatal = err;
  662. } else {
  663. BUFFER_TRACE(bh, "not a new buffer");
  664. }
  665. if (fatal) {
  666. *errp = fatal;
  667. brelse(bh);
  668. bh = NULL;
  669. }
  670. return bh;
  671. }
  672. struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
  673. ext4_lblk_t block, int create, int *err)
  674. {
  675. struct buffer_head *bh;
  676. bh = ext4_getblk(handle, inode, block, create, err);
  677. if (!bh)
  678. return bh;
  679. if (buffer_uptodate(bh))
  680. return bh;
  681. ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &bh);
  682. wait_on_buffer(bh);
  683. if (buffer_uptodate(bh))
  684. return bh;
  685. put_bh(bh);
  686. *err = -EIO;
  687. return NULL;
  688. }
  689. int ext4_walk_page_buffers(handle_t *handle,
  690. struct buffer_head *head,
  691. unsigned from,
  692. unsigned to,
  693. int *partial,
  694. int (*fn)(handle_t *handle,
  695. struct buffer_head *bh))
  696. {
  697. struct buffer_head *bh;
  698. unsigned block_start, block_end;
  699. unsigned blocksize = head->b_size;
  700. int err, ret = 0;
  701. struct buffer_head *next;
  702. for (bh = head, block_start = 0;
  703. ret == 0 && (bh != head || !block_start);
  704. block_start = block_end, bh = next) {
  705. next = bh->b_this_page;
  706. block_end = block_start + blocksize;
  707. if (block_end <= from || block_start >= to) {
  708. if (partial && !buffer_uptodate(bh))
  709. *partial = 1;
  710. continue;
  711. }
  712. err = (*fn)(handle, bh);
  713. if (!ret)
  714. ret = err;
  715. }
  716. return ret;
  717. }
  718. /*
  719. * To preserve ordering, it is essential that the hole instantiation and
  720. * the data write be encapsulated in a single transaction. We cannot
  721. * close off a transaction and start a new one between the ext4_get_block()
  722. * and the commit_write(). So doing the jbd2_journal_start at the start of
  723. * prepare_write() is the right place.
  724. *
  725. * Also, this function can nest inside ext4_writepage(). In that case, we
  726. * *know* that ext4_writepage() has generated enough buffer credits to do the
  727. * whole page. So we won't block on the journal in that case, which is good,
  728. * because the caller may be PF_MEMALLOC.
  729. *
  730. * By accident, ext4 can be reentered when a transaction is open via
  731. * quota file writes. If we were to commit the transaction while thus
  732. * reentered, there can be a deadlock - we would be holding a quota
  733. * lock, and the commit would never complete if another thread had a
  734. * transaction open and was blocking on the quota lock - a ranking
  735. * violation.
  736. *
  737. * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
  738. * will _not_ run commit under these circumstances because handle->h_ref
  739. * is elevated. We'll still have enough credits for the tiny quotafile
  740. * write.
  741. */
  742. int do_journal_get_write_access(handle_t *handle,
  743. struct buffer_head *bh)
  744. {
  745. int dirty = buffer_dirty(bh);
  746. int ret;
  747. if (!buffer_mapped(bh) || buffer_freed(bh))
  748. return 0;
  749. /*
  750. * __block_write_begin() could have dirtied some buffers. Clean
  751. * the dirty bit as jbd2_journal_get_write_access() could complain
  752. * otherwise about fs integrity issues. Setting of the dirty bit
  753. * by __block_write_begin() isn't a real problem here as we clear
  754. * the bit before releasing a page lock and thus writeback cannot
  755. * ever write the buffer.
  756. */
  757. if (dirty)
  758. clear_buffer_dirty(bh);
  759. ret = ext4_journal_get_write_access(handle, bh);
  760. if (!ret && dirty)
  761. ret = ext4_handle_dirty_metadata(handle, NULL, bh);
  762. return ret;
  763. }
  764. static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock,
  765. struct buffer_head *bh_result, int create);
  766. static int ext4_write_begin(struct file *file, struct address_space *mapping,
  767. loff_t pos, unsigned len, unsigned flags,
  768. struct page **pagep, void **fsdata)
  769. {
  770. struct inode *inode = mapping->host;
  771. int ret, needed_blocks;
  772. handle_t *handle;
  773. int retries = 0;
  774. struct page *page;
  775. pgoff_t index;
  776. unsigned from, to;
  777. trace_ext4_write_begin(inode, pos, len, flags);
  778. /*
  779. * Reserve one block more for addition to orphan list in case
  780. * we allocate blocks but write fails for some reason
  781. */
  782. needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
  783. index = pos >> PAGE_CACHE_SHIFT;
  784. from = pos & (PAGE_CACHE_SIZE - 1);
  785. to = from + len;
  786. if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
  787. ret = ext4_try_to_write_inline_data(mapping, inode, pos, len,
  788. flags, pagep);
  789. if (ret < 0)
  790. goto out;
  791. if (ret == 1) {
  792. ret = 0;
  793. goto out;
  794. }
  795. }
  796. retry:
  797. handle = ext4_journal_start(inode, needed_blocks);
  798. if (IS_ERR(handle)) {
  799. ret = PTR_ERR(handle);
  800. goto out;
  801. }
  802. /* We cannot recurse into the filesystem as the transaction is already
  803. * started */
  804. flags |= AOP_FLAG_NOFS;
  805. page = grab_cache_page_write_begin(mapping, index, flags);
  806. if (!page) {
  807. ext4_journal_stop(handle);
  808. ret = -ENOMEM;
  809. goto out;
  810. }
  811. *pagep = page;
  812. if (ext4_should_dioread_nolock(inode))
  813. ret = __block_write_begin(page, pos, len, ext4_get_block_write);
  814. else
  815. ret = __block_write_begin(page, pos, len, ext4_get_block);
  816. if (!ret && ext4_should_journal_data(inode)) {
  817. ret = ext4_walk_page_buffers(handle, page_buffers(page),
  818. from, to, NULL,
  819. do_journal_get_write_access);
  820. }
  821. if (ret) {
  822. unlock_page(page);
  823. page_cache_release(page);
  824. /*
  825. * __block_write_begin may have instantiated a few blocks
  826. * outside i_size. Trim these off again. Don't need
  827. * i_size_read because we hold i_mutex.
  828. *
  829. * Add inode to orphan list in case we crash before
  830. * truncate finishes
  831. */
  832. if (pos + len > inode->i_size && ext4_can_truncate(inode))
  833. ext4_orphan_add(handle, inode);
  834. ext4_journal_stop(handle);
  835. if (pos + len > inode->i_size) {
  836. ext4_truncate_failed_write(inode);
  837. /*
  838. * If truncate failed early the inode might
  839. * still be on the orphan list; we need to
  840. * make sure the inode is removed from the
  841. * orphan list in that case.
  842. */
  843. if (inode->i_nlink)
  844. ext4_orphan_del(NULL, inode);
  845. }
  846. }
  847. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  848. goto retry;
  849. out:
  850. return ret;
  851. }
  852. /* For write_end() in data=journal mode */
  853. static int write_end_fn(handle_t *handle, struct buffer_head *bh)
  854. {
  855. if (!buffer_mapped(bh) || buffer_freed(bh))
  856. return 0;
  857. set_buffer_uptodate(bh);
  858. return ext4_handle_dirty_metadata(handle, NULL, bh);
  859. }
  860. static int ext4_generic_write_end(struct file *file,
  861. struct address_space *mapping,
  862. loff_t pos, unsigned len, unsigned copied,
  863. struct page *page, void *fsdata)
  864. {
  865. int i_size_changed = 0;
  866. struct inode *inode = mapping->host;
  867. handle_t *handle = ext4_journal_current_handle();
  868. if (ext4_has_inline_data(inode))
  869. copied = ext4_write_inline_data_end(inode, pos, len,
  870. copied, page);
  871. else
  872. copied = block_write_end(file, mapping, pos,
  873. len, copied, page, fsdata);
  874. /*
  875. * No need to use i_size_read() here, the i_size
  876. * cannot change under us because we hold i_mutex.
  877. *
  878. * But it's important to update i_size while still holding page lock:
  879. * page writeout could otherwise come in and zero beyond i_size.
  880. */
  881. if (pos + copied > inode->i_size) {
  882. i_size_write(inode, pos + copied);
  883. i_size_changed = 1;
  884. }
  885. if (pos + copied > EXT4_I(inode)->i_disksize) {
  886. /* We need to mark inode dirty even if
  887. * new_i_size is less that inode->i_size
  888. * bu greater than i_disksize.(hint delalloc)
  889. */
  890. ext4_update_i_disksize(inode, (pos + copied));
  891. i_size_changed = 1;
  892. }
  893. unlock_page(page);
  894. page_cache_release(page);
  895. /*
  896. * Don't mark the inode dirty under page lock. First, it unnecessarily
  897. * makes the holding time of page lock longer. Second, it forces lock
  898. * ordering of page lock and transaction start for journaling
  899. * filesystems.
  900. */
  901. if (i_size_changed)
  902. ext4_mark_inode_dirty(handle, inode);
  903. return copied;
  904. }
  905. /*
  906. * We need to pick up the new inode size which generic_commit_write gave us
  907. * `file' can be NULL - eg, when called from page_symlink().
  908. *
  909. * ext4 never places buffers on inode->i_mapping->private_list. metadata
  910. * buffers are managed internally.
  911. */
  912. static int ext4_ordered_write_end(struct file *file,
  913. struct address_space *mapping,
  914. loff_t pos, unsigned len, unsigned copied,
  915. struct page *page, void *fsdata)
  916. {
  917. handle_t *handle = ext4_journal_current_handle();
  918. struct inode *inode = mapping->host;
  919. int ret = 0, ret2;
  920. trace_ext4_ordered_write_end(inode, pos, len, copied);
  921. ret = ext4_jbd2_file_inode(handle, inode);
  922. if (ret == 0) {
  923. ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
  924. page, fsdata);
  925. copied = ret2;
  926. if (pos + len > inode->i_size && ext4_can_truncate(inode))
  927. /* if we have allocated more blocks and copied
  928. * less. We will have blocks allocated outside
  929. * inode->i_size. So truncate them
  930. */
  931. ext4_orphan_add(handle, inode);
  932. if (ret2 < 0)
  933. ret = ret2;
  934. } else {
  935. unlock_page(page);
  936. page_cache_release(page);
  937. }
  938. ret2 = ext4_journal_stop(handle);
  939. if (!ret)
  940. ret = ret2;
  941. if (pos + len > inode->i_size) {
  942. ext4_truncate_failed_write(inode);
  943. /*
  944. * If truncate failed early the inode might still be
  945. * on the orphan list; we need to make sure the inode
  946. * is removed from the orphan list in that case.
  947. */
  948. if (inode->i_nlink)
  949. ext4_orphan_del(NULL, inode);
  950. }
  951. return ret ? ret : copied;
  952. }
  953. static int ext4_writeback_write_end(struct file *file,
  954. struct address_space *mapping,
  955. loff_t pos, unsigned len, unsigned copied,
  956. struct page *page, void *fsdata)
  957. {
  958. handle_t *handle = ext4_journal_current_handle();
  959. struct inode *inode = mapping->host;
  960. int ret = 0, ret2;
  961. trace_ext4_writeback_write_end(inode, pos, len, copied);
  962. ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
  963. page, fsdata);
  964. copied = ret2;
  965. if (pos + len > inode->i_size && ext4_can_truncate(inode))
  966. /* if we have allocated more blocks and copied
  967. * less. We will have blocks allocated outside
  968. * inode->i_size. So truncate them
  969. */
  970. ext4_orphan_add(handle, inode);
  971. if (ret2 < 0)
  972. ret = ret2;
  973. ret2 = ext4_journal_stop(handle);
  974. if (!ret)
  975. ret = ret2;
  976. if (pos + len > inode->i_size) {
  977. ext4_truncate_failed_write(inode);
  978. /*
  979. * If truncate failed early the inode might still be
  980. * on the orphan list; we need to make sure the inode
  981. * is removed from the orphan list in that case.
  982. */
  983. if (inode->i_nlink)
  984. ext4_orphan_del(NULL, inode);
  985. }
  986. return ret ? ret : copied;
  987. }
  988. static int ext4_journalled_write_end(struct file *file,
  989. struct address_space *mapping,
  990. loff_t pos, unsigned len, unsigned copied,
  991. struct page *page, void *fsdata)
  992. {
  993. handle_t *handle = ext4_journal_current_handle();
  994. struct inode *inode = mapping->host;
  995. int ret = 0, ret2;
  996. int partial = 0;
  997. unsigned from, to;
  998. loff_t new_i_size;
  999. trace_ext4_journalled_write_end(inode, pos, len, copied);
  1000. from = pos & (PAGE_CACHE_SIZE - 1);
  1001. to = from + len;
  1002. BUG_ON(!ext4_handle_valid(handle));
  1003. if (ext4_has_inline_data(inode))
  1004. copied = ext4_write_inline_data_end(inode, pos, len,
  1005. copied, page);
  1006. else {
  1007. if (copied < len) {
  1008. if (!PageUptodate(page))
  1009. copied = 0;
  1010. page_zero_new_buffers(page, from+copied, to);
  1011. }
  1012. ret = ext4_walk_page_buffers(handle, page_buffers(page), from,
  1013. to, &partial, write_end_fn);
  1014. if (!partial)
  1015. SetPageUptodate(page);
  1016. }
  1017. new_i_size = pos + copied;
  1018. if (new_i_size > inode->i_size)
  1019. i_size_write(inode, pos+copied);
  1020. ext4_set_inode_state(inode, EXT4_STATE_JDATA);
  1021. EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
  1022. if (new_i_size > EXT4_I(inode)->i_disksize) {
  1023. ext4_update_i_disksize(inode, new_i_size);
  1024. ret2 = ext4_mark_inode_dirty(handle, inode);
  1025. if (!ret)
  1026. ret = ret2;
  1027. }
  1028. unlock_page(page);
  1029. page_cache_release(page);
  1030. if (pos + len > inode->i_size && ext4_can_truncate(inode))
  1031. /* if we have allocated more blocks and copied
  1032. * less. We will have blocks allocated outside
  1033. * inode->i_size. So truncate them
  1034. */
  1035. ext4_orphan_add(handle, inode);
  1036. ret2 = ext4_journal_stop(handle);
  1037. if (!ret)
  1038. ret = ret2;
  1039. if (pos + len > inode->i_size) {
  1040. ext4_truncate_failed_write(inode);
  1041. /*
  1042. * If truncate failed early the inode might still be
  1043. * on the orphan list; we need to make sure the inode
  1044. * is removed from the orphan list in that case.
  1045. */
  1046. if (inode->i_nlink)
  1047. ext4_orphan_del(NULL, inode);
  1048. }
  1049. return ret ? ret : copied;
  1050. }
  1051. /*
  1052. * Reserve a single cluster located at lblock
  1053. */
  1054. static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock)
  1055. {
  1056. int retries = 0;
  1057. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1058. struct ext4_inode_info *ei = EXT4_I(inode);
  1059. unsigned int md_needed;
  1060. int ret;
  1061. ext4_lblk_t save_last_lblock;
  1062. int save_len;
  1063. /*
  1064. * We will charge metadata quota at writeout time; this saves
  1065. * us from metadata over-estimation, though we may go over by
  1066. * a small amount in the end. Here we just reserve for data.
  1067. */
  1068. ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1));
  1069. if (ret)
  1070. return ret;
  1071. /*
  1072. * recalculate the amount of metadata blocks to reserve
  1073. * in order to allocate nrblocks
  1074. * worse case is one extent per block
  1075. */
  1076. repeat:
  1077. spin_lock(&ei->i_block_reservation_lock);
  1078. /*
  1079. * ext4_calc_metadata_amount() has side effects, which we have
  1080. * to be prepared undo if we fail to claim space.
  1081. */
  1082. save_len = ei->i_da_metadata_calc_len;
  1083. save_last_lblock = ei->i_da_metadata_calc_last_lblock;
  1084. md_needed = EXT4_NUM_B2C(sbi,
  1085. ext4_calc_metadata_amount(inode, lblock));
  1086. trace_ext4_da_reserve_space(inode, md_needed);
  1087. /*
  1088. * We do still charge estimated metadata to the sb though;
  1089. * we cannot afford to run out of free blocks.
  1090. */
  1091. if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) {
  1092. ei->i_da_metadata_calc_len = save_len;
  1093. ei->i_da_metadata_calc_last_lblock = save_last_lblock;
  1094. spin_unlock(&ei->i_block_reservation_lock);
  1095. if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
  1096. yield();
  1097. goto repeat;
  1098. }
  1099. dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1));
  1100. return -ENOSPC;
  1101. }
  1102. ei->i_reserved_data_blocks++;
  1103. ei->i_reserved_meta_blocks += md_needed;
  1104. spin_unlock(&ei->i_block_reservation_lock);
  1105. return 0; /* success */
  1106. }
  1107. static void ext4_da_release_space(struct inode *inode, int to_free)
  1108. {
  1109. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1110. struct ext4_inode_info *ei = EXT4_I(inode);
  1111. if (!to_free)
  1112. return; /* Nothing to release, exit */
  1113. spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
  1114. trace_ext4_da_release_space(inode, to_free);
  1115. if (unlikely(to_free > ei->i_reserved_data_blocks)) {
  1116. /*
  1117. * if there aren't enough reserved blocks, then the
  1118. * counter is messed up somewhere. Since this
  1119. * function is called from invalidate page, it's
  1120. * harmless to return without any action.
  1121. */
  1122. ext4_msg(inode->i_sb, KERN_NOTICE, "ext4_da_release_space: "
  1123. "ino %lu, to_free %d with only %d reserved "
  1124. "data blocks", inode->i_ino, to_free,
  1125. ei->i_reserved_data_blocks);
  1126. WARN_ON(1);
  1127. to_free = ei->i_reserved_data_blocks;
  1128. }
  1129. ei->i_reserved_data_blocks -= to_free;
  1130. if (ei->i_reserved_data_blocks == 0) {
  1131. /*
  1132. * We can release all of the reserved metadata blocks
  1133. * only when we have written all of the delayed
  1134. * allocation blocks.
  1135. * Note that in case of bigalloc, i_reserved_meta_blocks,
  1136. * i_reserved_data_blocks, etc. refer to number of clusters.
  1137. */
  1138. percpu_counter_sub(&sbi->s_dirtyclusters_counter,
  1139. ei->i_reserved_meta_blocks);
  1140. ei->i_reserved_meta_blocks = 0;
  1141. ei->i_da_metadata_calc_len = 0;
  1142. }
  1143. /* update fs dirty data blocks counter */
  1144. percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free);
  1145. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1146. dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free));
  1147. }
  1148. static void ext4_da_page_release_reservation(struct page *page,
  1149. unsigned long offset)
  1150. {
  1151. int to_release = 0;
  1152. struct buffer_head *head, *bh;
  1153. unsigned int curr_off = 0;
  1154. struct inode *inode = page->mapping->host;
  1155. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1156. int num_clusters;
  1157. ext4_fsblk_t lblk;
  1158. head = page_buffers(page);
  1159. bh = head;
  1160. do {
  1161. unsigned int next_off = curr_off + bh->b_size;
  1162. if ((offset <= curr_off) && (buffer_delay(bh))) {
  1163. to_release++;
  1164. clear_buffer_delay(bh);
  1165. }
  1166. curr_off = next_off;
  1167. } while ((bh = bh->b_this_page) != head);
  1168. if (to_release) {
  1169. lblk = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1170. ext4_es_remove_extent(inode, lblk, to_release);
  1171. }
  1172. /* If we have released all the blocks belonging to a cluster, then we
  1173. * need to release the reserved space for that cluster. */
  1174. num_clusters = EXT4_NUM_B2C(sbi, to_release);
  1175. while (num_clusters > 0) {
  1176. lblk = (page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits)) +
  1177. ((num_clusters - 1) << sbi->s_cluster_bits);
  1178. if (sbi->s_cluster_ratio == 1 ||
  1179. !ext4_find_delalloc_cluster(inode, lblk))
  1180. ext4_da_release_space(inode, 1);
  1181. num_clusters--;
  1182. }
  1183. }
  1184. /*
  1185. * Delayed allocation stuff
  1186. */
  1187. /*
  1188. * mpage_da_submit_io - walks through extent of pages and try to write
  1189. * them with writepage() call back
  1190. *
  1191. * @mpd->inode: inode
  1192. * @mpd->first_page: first page of the extent
  1193. * @mpd->next_page: page after the last page of the extent
  1194. *
  1195. * By the time mpage_da_submit_io() is called we expect all blocks
  1196. * to be allocated. this may be wrong if allocation failed.
  1197. *
  1198. * As pages are already locked by write_cache_pages(), we can't use it
  1199. */
  1200. static int mpage_da_submit_io(struct mpage_da_data *mpd,
  1201. struct ext4_map_blocks *map)
  1202. {
  1203. struct pagevec pvec;
  1204. unsigned long index, end;
  1205. int ret = 0, err, nr_pages, i;
  1206. struct inode *inode = mpd->inode;
  1207. struct address_space *mapping = inode->i_mapping;
  1208. loff_t size = i_size_read(inode);
  1209. unsigned int len, block_start;
  1210. struct buffer_head *bh, *page_bufs = NULL;
  1211. sector_t pblock = 0, cur_logical = 0;
  1212. struct ext4_io_submit io_submit;
  1213. BUG_ON(mpd->next_page <= mpd->first_page);
  1214. memset(&io_submit, 0, sizeof(io_submit));
  1215. /*
  1216. * We need to start from the first_page to the next_page - 1
  1217. * to make sure we also write the mapped dirty buffer_heads.
  1218. * If we look at mpd->b_blocknr we would only be looking
  1219. * at the currently mapped buffer_heads.
  1220. */
  1221. index = mpd->first_page;
  1222. end = mpd->next_page - 1;
  1223. pagevec_init(&pvec, 0);
  1224. while (index <= end) {
  1225. nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
  1226. if (nr_pages == 0)
  1227. break;
  1228. for (i = 0; i < nr_pages; i++) {
  1229. int skip_page = 0;
  1230. struct page *page = pvec.pages[i];
  1231. index = page->index;
  1232. if (index > end)
  1233. break;
  1234. if (index == size >> PAGE_CACHE_SHIFT)
  1235. len = size & ~PAGE_CACHE_MASK;
  1236. else
  1237. len = PAGE_CACHE_SIZE;
  1238. if (map) {
  1239. cur_logical = index << (PAGE_CACHE_SHIFT -
  1240. inode->i_blkbits);
  1241. pblock = map->m_pblk + (cur_logical -
  1242. map->m_lblk);
  1243. }
  1244. index++;
  1245. BUG_ON(!PageLocked(page));
  1246. BUG_ON(PageWriteback(page));
  1247. bh = page_bufs = page_buffers(page);
  1248. block_start = 0;
  1249. do {
  1250. if (map && (cur_logical >= map->m_lblk) &&
  1251. (cur_logical <= (map->m_lblk +
  1252. (map->m_len - 1)))) {
  1253. if (buffer_delay(bh)) {
  1254. clear_buffer_delay(bh);
  1255. bh->b_blocknr = pblock;
  1256. }
  1257. if (buffer_unwritten(bh) ||
  1258. buffer_mapped(bh))
  1259. BUG_ON(bh->b_blocknr != pblock);
  1260. if (map->m_flags & EXT4_MAP_UNINIT)
  1261. set_buffer_uninit(bh);
  1262. clear_buffer_unwritten(bh);
  1263. }
  1264. /*
  1265. * skip page if block allocation undone and
  1266. * block is dirty
  1267. */
  1268. if (ext4_bh_delay_or_unwritten(NULL, bh))
  1269. skip_page = 1;
  1270. bh = bh->b_this_page;
  1271. block_start += bh->b_size;
  1272. cur_logical++;
  1273. pblock++;
  1274. } while (bh != page_bufs);
  1275. if (skip_page) {
  1276. unlock_page(page);
  1277. continue;
  1278. }
  1279. clear_page_dirty_for_io(page);
  1280. err = ext4_bio_write_page(&io_submit, page, len,
  1281. mpd->wbc);
  1282. if (!err)
  1283. mpd->pages_written++;
  1284. /*
  1285. * In error case, we have to continue because
  1286. * remaining pages are still locked
  1287. */
  1288. if (ret == 0)
  1289. ret = err;
  1290. }
  1291. pagevec_release(&pvec);
  1292. }
  1293. ext4_io_submit(&io_submit);
  1294. return ret;
  1295. }
  1296. static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd)
  1297. {
  1298. int nr_pages, i;
  1299. pgoff_t index, end;
  1300. struct pagevec pvec;
  1301. struct inode *inode = mpd->inode;
  1302. struct address_space *mapping = inode->i_mapping;
  1303. ext4_lblk_t start, last;
  1304. index = mpd->first_page;
  1305. end = mpd->next_page - 1;
  1306. start = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1307. last = end << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1308. ext4_es_remove_extent(inode, start, last - start + 1);
  1309. pagevec_init(&pvec, 0);
  1310. while (index <= end) {
  1311. nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
  1312. if (nr_pages == 0)
  1313. break;
  1314. for (i = 0; i < nr_pages; i++) {
  1315. struct page *page = pvec.pages[i];
  1316. if (page->index > end)
  1317. break;
  1318. BUG_ON(!PageLocked(page));
  1319. BUG_ON(PageWriteback(page));
  1320. block_invalidatepage(page, 0);
  1321. ClearPageUptodate(page);
  1322. unlock_page(page);
  1323. }
  1324. index = pvec.pages[nr_pages - 1]->index + 1;
  1325. pagevec_release(&pvec);
  1326. }
  1327. return;
  1328. }
  1329. static void ext4_print_free_blocks(struct inode *inode)
  1330. {
  1331. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1332. struct super_block *sb = inode->i_sb;
  1333. ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld",
  1334. EXT4_C2B(EXT4_SB(inode->i_sb),
  1335. ext4_count_free_clusters(inode->i_sb)));
  1336. ext4_msg(sb, KERN_CRIT, "Free/Dirty block details");
  1337. ext4_msg(sb, KERN_CRIT, "free_blocks=%lld",
  1338. (long long) EXT4_C2B(EXT4_SB(inode->i_sb),
  1339. percpu_counter_sum(&sbi->s_freeclusters_counter)));
  1340. ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld",
  1341. (long long) EXT4_C2B(EXT4_SB(inode->i_sb),
  1342. percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
  1343. ext4_msg(sb, KERN_CRIT, "Block reservation details");
  1344. ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u",
  1345. EXT4_I(inode)->i_reserved_data_blocks);
  1346. ext4_msg(sb, KERN_CRIT, "i_reserved_meta_blocks=%u",
  1347. EXT4_I(inode)->i_reserved_meta_blocks);
  1348. return;
  1349. }
  1350. /*
  1351. * mpage_da_map_and_submit - go through given space, map them
  1352. * if necessary, and then submit them for I/O
  1353. *
  1354. * @mpd - bh describing space
  1355. *
  1356. * The function skips space we know is already mapped to disk blocks.
  1357. *
  1358. */
  1359. static void mpage_da_map_and_submit(struct mpage_da_data *mpd)
  1360. {
  1361. int err, blks, get_blocks_flags;
  1362. struct ext4_map_blocks map, *mapp = NULL;
  1363. sector_t next = mpd->b_blocknr;
  1364. unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits;
  1365. loff_t disksize = EXT4_I(mpd->inode)->i_disksize;
  1366. handle_t *handle = NULL;
  1367. /*
  1368. * If the blocks are mapped already, or we couldn't accumulate
  1369. * any blocks, then proceed immediately to the submission stage.
  1370. */
  1371. if ((mpd->b_size == 0) ||
  1372. ((mpd->b_state & (1 << BH_Mapped)) &&
  1373. !(mpd->b_state & (1 << BH_Delay)) &&
  1374. !(mpd->b_state & (1 << BH_Unwritten))))
  1375. goto submit_io;
  1376. handle = ext4_journal_current_handle();
  1377. BUG_ON(!handle);
  1378. /*
  1379. * Call ext4_map_blocks() to allocate any delayed allocation
  1380. * blocks, or to convert an uninitialized extent to be
  1381. * initialized (in the case where we have written into
  1382. * one or more preallocated blocks).
  1383. *
  1384. * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to
  1385. * indicate that we are on the delayed allocation path. This
  1386. * affects functions in many different parts of the allocation
  1387. * call path. This flag exists primarily because we don't
  1388. * want to change *many* call functions, so ext4_map_blocks()
  1389. * will set the EXT4_STATE_DELALLOC_RESERVED flag once the
  1390. * inode's allocation semaphore is taken.
  1391. *
  1392. * If the blocks in questions were delalloc blocks, set
  1393. * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting
  1394. * variables are updated after the blocks have been allocated.
  1395. */
  1396. map.m_lblk = next;
  1397. map.m_len = max_blocks;
  1398. get_blocks_flags = EXT4_GET_BLOCKS_CREATE;
  1399. if (ext4_should_dioread_nolock(mpd->inode))
  1400. get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
  1401. if (mpd->b_state & (1 << BH_Delay))
  1402. get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
  1403. blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags);
  1404. if (blks < 0) {
  1405. struct super_block *sb = mpd->inode->i_sb;
  1406. err = blks;
  1407. /*
  1408. * If get block returns EAGAIN or ENOSPC and there
  1409. * appears to be free blocks we will just let
  1410. * mpage_da_submit_io() unlock all of the pages.
  1411. */
  1412. if (err == -EAGAIN)
  1413. goto submit_io;
  1414. if (err == -ENOSPC && ext4_count_free_clusters(sb)) {
  1415. mpd->retval = err;
  1416. goto submit_io;
  1417. }
  1418. /*
  1419. * get block failure will cause us to loop in
  1420. * writepages, because a_ops->writepage won't be able
  1421. * to make progress. The page will be redirtied by
  1422. * writepage and writepages will again try to write
  1423. * the same.
  1424. */
  1425. if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) {
  1426. ext4_msg(sb, KERN_CRIT,
  1427. "delayed block allocation failed for inode %lu "
  1428. "at logical offset %llu with max blocks %zd "
  1429. "with error %d", mpd->inode->i_ino,
  1430. (unsigned long long) next,
  1431. mpd->b_size >> mpd->inode->i_blkbits, err);
  1432. ext4_msg(sb, KERN_CRIT,
  1433. "This should not happen!! Data will be lost\n");
  1434. if (err == -ENOSPC)
  1435. ext4_print_free_blocks(mpd->inode);
  1436. }
  1437. /* invalidate all the pages */
  1438. ext4_da_block_invalidatepages(mpd);
  1439. /* Mark this page range as having been completed */
  1440. mpd->io_done = 1;
  1441. return;
  1442. }
  1443. BUG_ON(blks == 0);
  1444. mapp = &map;
  1445. if (map.m_flags & EXT4_MAP_NEW) {
  1446. struct block_device *bdev = mpd->inode->i_sb->s_bdev;
  1447. int i;
  1448. for (i = 0; i < map.m_len; i++)
  1449. unmap_underlying_metadata(bdev, map.m_pblk + i);
  1450. }
  1451. /*
  1452. * Update on-disk size along with block allocation.
  1453. */
  1454. disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits;
  1455. if (disksize > i_size_read(mpd->inode))
  1456. disksize = i_size_read(mpd->inode);
  1457. if (disksize > EXT4_I(mpd->inode)->i_disksize) {
  1458. ext4_update_i_disksize(mpd->inode, disksize);
  1459. err = ext4_mark_inode_dirty(handle, mpd->inode);
  1460. if (err)
  1461. ext4_error(mpd->inode->i_sb,
  1462. "Failed to mark inode %lu dirty",
  1463. mpd->inode->i_ino);
  1464. }
  1465. submit_io:
  1466. mpage_da_submit_io(mpd, mapp);
  1467. mpd->io_done = 1;
  1468. }
  1469. #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
  1470. (1 << BH_Delay) | (1 << BH_Unwritten))
  1471. /*
  1472. * mpage_add_bh_to_extent - try to add one more block to extent of blocks
  1473. *
  1474. * @mpd->lbh - extent of blocks
  1475. * @logical - logical number of the block in the file
  1476. * @bh - bh of the block (used to access block's state)
  1477. *
  1478. * the function is used to collect contig. blocks in same state
  1479. */
  1480. static void mpage_add_bh_to_extent(struct mpage_da_data *mpd,
  1481. sector_t logical, size_t b_size,
  1482. unsigned long b_state)
  1483. {
  1484. sector_t next;
  1485. int nrblocks = mpd->b_size >> mpd->inode->i_blkbits;
  1486. /*
  1487. * XXX Don't go larger than mballoc is willing to allocate
  1488. * This is a stopgap solution. We eventually need to fold
  1489. * mpage_da_submit_io() into this function and then call
  1490. * ext4_map_blocks() multiple times in a loop
  1491. */
  1492. if (nrblocks >= 8*1024*1024/mpd->inode->i_sb->s_blocksize)
  1493. goto flush_it;
  1494. /* check if thereserved journal credits might overflow */
  1495. if (!(ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS))) {
  1496. if (nrblocks >= EXT4_MAX_TRANS_DATA) {
  1497. /*
  1498. * With non-extent format we are limited by the journal
  1499. * credit available. Total credit needed to insert
  1500. * nrblocks contiguous blocks is dependent on the
  1501. * nrblocks. So limit nrblocks.
  1502. */
  1503. goto flush_it;
  1504. } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) >
  1505. EXT4_MAX_TRANS_DATA) {
  1506. /*
  1507. * Adding the new buffer_head would make it cross the
  1508. * allowed limit for which we have journal credit
  1509. * reserved. So limit the new bh->b_size
  1510. */
  1511. b_size = (EXT4_MAX_TRANS_DATA - nrblocks) <<
  1512. mpd->inode->i_blkbits;
  1513. /* we will do mpage_da_submit_io in the next loop */
  1514. }
  1515. }
  1516. /*
  1517. * First block in the extent
  1518. */
  1519. if (mpd->b_size == 0) {
  1520. mpd->b_blocknr = logical;
  1521. mpd->b_size = b_size;
  1522. mpd->b_state = b_state & BH_FLAGS;
  1523. return;
  1524. }
  1525. next = mpd->b_blocknr + nrblocks;
  1526. /*
  1527. * Can we merge the block to our big extent?
  1528. */
  1529. if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) {
  1530. mpd->b_size += b_size;
  1531. return;
  1532. }
  1533. flush_it:
  1534. /*
  1535. * We couldn't merge the block to our extent, so we
  1536. * need to flush current extent and start new one
  1537. */
  1538. mpage_da_map_and_submit(mpd);
  1539. return;
  1540. }
  1541. static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
  1542. {
  1543. return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
  1544. }
  1545. /*
  1546. * This function is grabs code from the very beginning of
  1547. * ext4_map_blocks, but assumes that the caller is from delayed write
  1548. * time. This function looks up the requested blocks and sets the
  1549. * buffer delay bit under the protection of i_data_sem.
  1550. */
  1551. static int ext4_da_map_blocks(struct inode *inode, sector_t iblock,
  1552. struct ext4_map_blocks *map,
  1553. struct buffer_head *bh)
  1554. {
  1555. int retval;
  1556. sector_t invalid_block = ~((sector_t) 0xffff);
  1557. if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
  1558. invalid_block = ~0;
  1559. map->m_flags = 0;
  1560. ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u,"
  1561. "logical block %lu\n", inode->i_ino, map->m_len,
  1562. (unsigned long) map->m_lblk);
  1563. /*
  1564. * Try to see if we can get the block without requesting a new
  1565. * file system block.
  1566. */
  1567. down_read((&EXT4_I(inode)->i_data_sem));
  1568. if (ext4_has_inline_data(inode)) {
  1569. /*
  1570. * We will soon create blocks for this page, and let
  1571. * us pretend as if the blocks aren't allocated yet.
  1572. * In case of clusters, we have to handle the work
  1573. * of mapping from cluster so that the reserved space
  1574. * is calculated properly.
  1575. */
  1576. if ((EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) &&
  1577. ext4_find_delalloc_cluster(inode, map->m_lblk))
  1578. map->m_flags |= EXT4_MAP_FROM_CLUSTER;
  1579. retval = 0;
  1580. } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  1581. retval = ext4_ext_map_blocks(NULL, inode, map, 0);
  1582. else
  1583. retval = ext4_ind_map_blocks(NULL, inode, map, 0);
  1584. if (retval == 0) {
  1585. /*
  1586. * XXX: __block_prepare_write() unmaps passed block,
  1587. * is it OK?
  1588. */
  1589. /* If the block was allocated from previously allocated cluster,
  1590. * then we dont need to reserve it again. */
  1591. if (!(map->m_flags & EXT4_MAP_FROM_CLUSTER)) {
  1592. retval = ext4_da_reserve_space(inode, iblock);
  1593. if (retval)
  1594. /* not enough space to reserve */
  1595. goto out_unlock;
  1596. }
  1597. retval = ext4_es_insert_extent(inode, map->m_lblk, map->m_len);
  1598. if (retval)
  1599. goto out_unlock;
  1600. /* Clear EXT4_MAP_FROM_CLUSTER flag since its purpose is served
  1601. * and it should not appear on the bh->b_state.
  1602. */
  1603. map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;
  1604. map_bh(bh, inode->i_sb, invalid_block);
  1605. set_buffer_new(bh);
  1606. set_buffer_delay(bh);
  1607. }
  1608. out_unlock:
  1609. up_read((&EXT4_I(inode)->i_data_sem));
  1610. return retval;
  1611. }
  1612. /*
  1613. * This is a special get_blocks_t callback which is used by
  1614. * ext4_da_write_begin(). It will either return mapped block or
  1615. * reserve space for a single block.
  1616. *
  1617. * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
  1618. * We also have b_blocknr = -1 and b_bdev initialized properly
  1619. *
  1620. * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
  1621. * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
  1622. * initialized properly.
  1623. */
  1624. int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
  1625. struct buffer_head *bh, int create)
  1626. {
  1627. struct ext4_map_blocks map;
  1628. int ret = 0;
  1629. BUG_ON(create == 0);
  1630. BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
  1631. map.m_lblk = iblock;
  1632. map.m_len = 1;
  1633. /*
  1634. * first, we need to know whether the block is allocated already
  1635. * preallocated blocks are unmapped but should treated
  1636. * the same as allocated blocks.
  1637. */
  1638. ret = ext4_da_map_blocks(inode, iblock, &map, bh);
  1639. if (ret <= 0)
  1640. return ret;
  1641. map_bh(bh, inode->i_sb, map.m_pblk);
  1642. bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
  1643. if (buffer_unwritten(bh)) {
  1644. /* A delayed write to unwritten bh should be marked
  1645. * new and mapped. Mapped ensures that we don't do
  1646. * get_block multiple times when we write to the same
  1647. * offset and new ensures that we do proper zero out
  1648. * for partial write.
  1649. */
  1650. set_buffer_new(bh);
  1651. set_buffer_mapped(bh);
  1652. }
  1653. return 0;
  1654. }
  1655. static int bget_one(handle_t *handle, struct buffer_head *bh)
  1656. {
  1657. get_bh(bh);
  1658. return 0;
  1659. }
  1660. static int bput_one(handle_t *handle, struct buffer_head *bh)
  1661. {
  1662. put_bh(bh);
  1663. return 0;
  1664. }
  1665. static int __ext4_journalled_writepage(struct page *page,
  1666. unsigned int len)
  1667. {
  1668. struct address_space *mapping = page->mapping;
  1669. struct inode *inode = mapping->host;
  1670. struct buffer_head *page_bufs = NULL;
  1671. handle_t *handle = NULL;
  1672. int ret = 0, err = 0;
  1673. int inline_data = ext4_has_inline_data(inode);
  1674. struct buffer_head *inode_bh = NULL;
  1675. ClearPageChecked(page);
  1676. if (inline_data) {
  1677. BUG_ON(page->index != 0);
  1678. BUG_ON(len > ext4_get_max_inline_size(inode));
  1679. inode_bh = ext4_journalled_write_inline_data(inode, len, page);
  1680. if (inode_bh == NULL)
  1681. goto out;
  1682. } else {
  1683. page_bufs = page_buffers(page);
  1684. if (!page_bufs) {
  1685. BUG();
  1686. goto out;
  1687. }
  1688. ext4_walk_page_buffers(handle, page_bufs, 0, len,
  1689. NULL, bget_one);
  1690. }
  1691. /* As soon as we unlock the page, it can go away, but we have
  1692. * references to buffers so we are safe */
  1693. unlock_page(page);
  1694. handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
  1695. if (IS_ERR(handle)) {
  1696. ret = PTR_ERR(handle);
  1697. goto out;
  1698. }
  1699. BUG_ON(!ext4_handle_valid(handle));
  1700. if (inline_data) {
  1701. ret = ext4_journal_get_write_access(handle, inode_bh);
  1702. err = ext4_handle_dirty_metadata(handle, inode, inode_bh);
  1703. } else {
  1704. ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
  1705. do_journal_get_write_access);
  1706. err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
  1707. write_end_fn);
  1708. }
  1709. if (ret == 0)
  1710. ret = err;
  1711. EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
  1712. err = ext4_journal_stop(handle);
  1713. if (!ret)
  1714. ret = err;
  1715. if (!ext4_has_inline_data(inode))
  1716. ext4_walk_page_buffers(handle, page_bufs, 0, len,
  1717. NULL, bput_one);
  1718. ext4_set_inode_state(inode, EXT4_STATE_JDATA);
  1719. out:
  1720. brelse(inode_bh);
  1721. return ret;
  1722. }
  1723. /*
  1724. * Note that we don't need to start a transaction unless we're journaling data
  1725. * because we should have holes filled from ext4_page_mkwrite(). We even don't
  1726. * need to file the inode to the transaction's list in ordered mode because if
  1727. * we are writing back data added by write(), the inode is already there and if
  1728. * we are writing back data modified via mmap(), no one guarantees in which
  1729. * transaction the data will hit the disk. In case we are journaling data, we
  1730. * cannot start transaction directly because transaction start ranks above page
  1731. * lock so we have to do some magic.
  1732. *
  1733. * This function can get called via...
  1734. * - ext4_da_writepages after taking page lock (have journal handle)
  1735. * - journal_submit_inode_data_buffers (no journal handle)
  1736. * - shrink_page_list via the kswapd/direct reclaim (no journal handle)
  1737. * - grab_page_cache when doing write_begin (have journal handle)
  1738. *
  1739. * We don't do any block allocation in this function. If we have page with
  1740. * multiple blocks we need to write those buffer_heads that are mapped. This
  1741. * is important for mmaped based write. So if we do with blocksize 1K
  1742. * truncate(f, 1024);
  1743. * a = mmap(f, 0, 4096);
  1744. * a[0] = 'a';
  1745. * truncate(f, 4096);
  1746. * we have in the page first buffer_head mapped via page_mkwrite call back
  1747. * but other buffer_heads would be unmapped but dirty (dirty done via the
  1748. * do_wp_page). So writepage should write the first block. If we modify
  1749. * the mmap area beyond 1024 we will again get a page_fault and the
  1750. * page_mkwrite callback will do the block allocation and mark the
  1751. * buffer_heads mapped.
  1752. *
  1753. * We redirty the page if we have any buffer_heads that is either delay or
  1754. * unwritten in the page.
  1755. *
  1756. * We can get recursively called as show below.
  1757. *
  1758. * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
  1759. * ext4_writepage()
  1760. *
  1761. * But since we don't do any block allocation we should not deadlock.
  1762. * Page also have the dirty flag cleared so we don't get recurive page_lock.
  1763. */
  1764. static int ext4_writepage(struct page *page,
  1765. struct writeback_control *wbc)
  1766. {
  1767. int ret = 0;
  1768. loff_t size;
  1769. unsigned int len;
  1770. struct buffer_head *page_bufs = NULL;
  1771. struct inode *inode = page->mapping->host;
  1772. struct ext4_io_submit io_submit;
  1773. trace_ext4_writepage(page);
  1774. size = i_size_read(inode);
  1775. if (page->index == size >> PAGE_CACHE_SHIFT)
  1776. len = size & ~PAGE_CACHE_MASK;
  1777. else
  1778. len = PAGE_CACHE_SIZE;
  1779. page_bufs = page_buffers(page);
  1780. if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL,
  1781. ext4_bh_delay_or_unwritten)) {
  1782. /*
  1783. * We don't want to do block allocation, so redirty
  1784. * the page and return. We may reach here when we do
  1785. * a journal commit via journal_submit_inode_data_buffers.
  1786. * We can also reach here via shrink_page_list but it
  1787. * should never be for direct reclaim so warn if that
  1788. * happens
  1789. */
  1790. WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
  1791. PF_MEMALLOC);
  1792. redirty_page_for_writepage(wbc, page);
  1793. unlock_page(page);
  1794. return 0;
  1795. }
  1796. if (PageChecked(page) && ext4_should_journal_data(inode))
  1797. /*
  1798. * It's mmapped pagecache. Add buffers and journal it. There
  1799. * doesn't seem much point in redirtying the page here.
  1800. */
  1801. return __ext4_journalled_writepage(page, len);
  1802. memset(&io_submit, 0, sizeof(io_submit));
  1803. ret = ext4_bio_write_page(&io_submit, page, len, wbc);
  1804. ext4_io_submit(&io_submit);
  1805. return ret;
  1806. }
  1807. /*
  1808. * This is called via ext4_da_writepages() to
  1809. * calculate the total number of credits to reserve to fit
  1810. * a single extent allocation into a single transaction,
  1811. * ext4_da_writpeages() will loop calling this before
  1812. * the block allocation.
  1813. */
  1814. static int ext4_da_writepages_trans_blocks(struct inode *inode)
  1815. {
  1816. int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
  1817. /*
  1818. * With non-extent format the journal credit needed to
  1819. * insert nrblocks contiguous block is dependent on
  1820. * number of contiguous block. So we will limit
  1821. * number of contiguous block to a sane value
  1822. */
  1823. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) &&
  1824. (max_blocks > EXT4_MAX_TRANS_DATA))
  1825. max_blocks = EXT4_MAX_TRANS_DATA;
  1826. return ext4_chunk_trans_blocks(inode, max_blocks);
  1827. }
  1828. /*
  1829. * write_cache_pages_da - walk the list of dirty pages of the given
  1830. * address space and accumulate pages that need writing, and call
  1831. * mpage_da_map_and_submit to map a single contiguous memory region
  1832. * and then write them.
  1833. */
  1834. static int write_cache_pages_da(handle_t *handle,
  1835. struct address_space *mapping,
  1836. struct writeback_control *wbc,
  1837. struct mpage_da_data *mpd,
  1838. pgoff_t *done_index)
  1839. {
  1840. struct buffer_head *bh, *head;
  1841. struct inode *inode = mapping->host;
  1842. struct pagevec pvec;
  1843. unsigned int nr_pages;
  1844. sector_t logical;
  1845. pgoff_t index, end;
  1846. long nr_to_write = wbc->nr_to_write;
  1847. int i, tag, ret = 0;
  1848. memset(mpd, 0, sizeof(struct mpage_da_data));
  1849. mpd->wbc = wbc;
  1850. mpd->inode = inode;
  1851. pagevec_init(&pvec, 0);
  1852. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  1853. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  1854. if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
  1855. tag = PAGECACHE_TAG_TOWRITE;
  1856. else
  1857. tag = PAGECACHE_TAG_DIRTY;
  1858. *done_index = index;
  1859. while (index <= end) {
  1860. nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
  1861. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
  1862. if (nr_pages == 0)
  1863. return 0;
  1864. for (i = 0; i < nr_pages; i++) {
  1865. struct page *page = pvec.pages[i];
  1866. /*
  1867. * At this point, the page may be truncated or
  1868. * invalidated (changing page->mapping to NULL), or
  1869. * even swizzled back from swapper_space to tmpfs file
  1870. * mapping. However, page->index will not change
  1871. * because we have a reference on the page.
  1872. */
  1873. if (page->index > end)
  1874. goto out;
  1875. *done_index = page->index + 1;
  1876. /*
  1877. * If we can't merge this page, and we have
  1878. * accumulated an contiguous region, write it
  1879. */
  1880. if ((mpd->next_page != page->index) &&
  1881. (mpd->next_page != mpd->first_page)) {
  1882. mpage_da_map_and_submit(mpd);
  1883. goto ret_extent_tail;
  1884. }
  1885. lock_page(page);
  1886. /*
  1887. * If the page is no longer dirty, or its
  1888. * mapping no longer corresponds to inode we
  1889. * are writing (which means it has been
  1890. * truncated or invalidated), or the page is
  1891. * already under writeback and we are not
  1892. * doing a data integrity writeback, skip the page
  1893. */
  1894. if (!PageDirty(page) ||
  1895. (PageWriteback(page) &&
  1896. (wbc->sync_mode == WB_SYNC_NONE)) ||
  1897. unlikely(page->mapping != mapping)) {
  1898. unlock_page(page);
  1899. continue;
  1900. }
  1901. wait_on_page_writeback(page);
  1902. BUG_ON(PageWriteback(page));
  1903. /*
  1904. * If we have inline data and arrive here, it means that
  1905. * we will soon create the block for the 1st page, so
  1906. * we'd better clear the inline data here.
  1907. */
  1908. if (ext4_has_inline_data(inode)) {
  1909. BUG_ON(ext4_test_inode_state(inode,
  1910. EXT4_STATE_MAY_INLINE_DATA));
  1911. ext4_destroy_inline_data(handle, inode);
  1912. }
  1913. if (mpd->next_page != page->index)
  1914. mpd->first_page = page->index;
  1915. mpd->next_page = page->index + 1;
  1916. logical = (sector_t) page->index <<
  1917. (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1918. /* Add all dirty buffers to mpd */
  1919. head = page_buffers(page);
  1920. bh = head;
  1921. do {
  1922. BUG_ON(buffer_locked(bh));
  1923. /*
  1924. * We need to try to allocate unmapped blocks
  1925. * in the same page. Otherwise we won't make
  1926. * progress with the page in ext4_writepage
  1927. */
  1928. if (ext4_bh_delay_or_unwritten(NULL, bh)) {
  1929. mpage_add_bh_to_extent(mpd, logical,
  1930. bh->b_size,
  1931. bh->b_state);
  1932. if (mpd->io_done)
  1933. goto ret_extent_tail;
  1934. } else if (buffer_dirty(bh) &&
  1935. buffer_mapped(bh)) {
  1936. /*
  1937. * mapped dirty buffer. We need to
  1938. * update the b_state because we look
  1939. * at b_state in mpage_da_map_blocks.
  1940. * We don't update b_size because if we
  1941. * find an unmapped buffer_head later
  1942. * we need to use the b_state flag of
  1943. * that buffer_head.
  1944. */
  1945. if (mpd->b_size == 0)
  1946. mpd->b_state =
  1947. bh->b_state & BH_FLAGS;
  1948. }
  1949. logical++;
  1950. } while ((bh = bh->b_this_page) != head);
  1951. if (nr_to_write > 0) {
  1952. nr_to_write--;
  1953. if (nr_to_write == 0 &&
  1954. wbc->sync_mode == WB_SYNC_NONE)
  1955. /*
  1956. * We stop writing back only if we are
  1957. * not doing integrity sync. In case of
  1958. * integrity sync we have to keep going
  1959. * because someone may be concurrently
  1960. * dirtying pages, and we might have
  1961. * synced a lot of newly appeared dirty
  1962. * pages, but have not synced all of the
  1963. * old dirty pages.
  1964. */
  1965. goto out;
  1966. }
  1967. }
  1968. pagevec_release(&pvec);
  1969. cond_resched();
  1970. }
  1971. return 0;
  1972. ret_extent_tail:
  1973. ret = MPAGE_DA_EXTENT_TAIL;
  1974. out:
  1975. pagevec_release(&pvec);
  1976. cond_resched();
  1977. return ret;
  1978. }
  1979. static int ext4_da_writepages(struct address_space *mapping,
  1980. struct writeback_control *wbc)
  1981. {
  1982. pgoff_t index;
  1983. int range_whole = 0;
  1984. handle_t *handle = NULL;
  1985. struct mpage_da_data mpd;
  1986. struct inode *inode = mapping->host;
  1987. int pages_written = 0;
  1988. unsigned int max_pages;
  1989. int range_cyclic, cycled = 1, io_done = 0;
  1990. int needed_blocks, ret = 0;
  1991. long desired_nr_to_write, nr_to_writebump = 0;
  1992. loff_t range_start = wbc->range_start;
  1993. struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
  1994. pgoff_t done_index = 0;
  1995. pgoff_t end;
  1996. struct blk_plug plug;
  1997. trace_ext4_da_writepages(inode, wbc);
  1998. /*
  1999. * No pages to write? This is mainly a kludge to avoid starting
  2000. * a transaction for special inodes like journal inode on last iput()
  2001. * because that could violate lock ordering on umount
  2002. */
  2003. if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
  2004. return 0;
  2005. /*
  2006. * If the filesystem has aborted, it is read-only, so return
  2007. * right away instead of dumping stack traces later on that
  2008. * will obscure the real source of the problem. We test
  2009. * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
  2010. * the latter could be true if the filesystem is mounted
  2011. * read-only, and in that case, ext4_da_writepages should
  2012. * *never* be called, so if that ever happens, we would want
  2013. * the stack trace.
  2014. */
  2015. if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
  2016. return -EROFS;
  2017. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  2018. range_whole = 1;
  2019. range_cyclic = wbc->range_cyclic;
  2020. if (wbc->range_cyclic) {
  2021. index = mapping->writeback_index;
  2022. if (index)
  2023. cycled = 0;
  2024. wbc->range_start = index << PAGE_CACHE_SHIFT;
  2025. wbc->range_end = LLONG_MAX;
  2026. wbc->range_cyclic = 0;
  2027. end = -1;
  2028. } else {
  2029. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  2030. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  2031. }
  2032. /*
  2033. * This works around two forms of stupidity. The first is in
  2034. * the writeback code, which caps the maximum number of pages
  2035. * written to be 1024 pages. This is wrong on multiple
  2036. * levels; different architectues have a different page size,
  2037. * which changes the maximum amount of data which gets
  2038. * written. Secondly, 4 megabytes is way too small. XFS
  2039. * forces this value to be 16 megabytes by multiplying
  2040. * nr_to_write parameter by four, and then relies on its
  2041. * allocator to allocate larger extents to make them
  2042. * contiguous. Unfortunately this brings us to the second
  2043. * stupidity, which is that ext4's mballoc code only allocates
  2044. * at most 2048 blocks. So we force contiguous writes up to
  2045. * the number of dirty blocks in the inode, or
  2046. * sbi->max_writeback_mb_bump whichever is smaller.
  2047. */
  2048. max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT);
  2049. if (!range_cyclic && range_whole) {
  2050. if (wbc->nr_to_write == LONG_MAX)
  2051. desired_nr_to_write = wbc->nr_to_write;
  2052. else
  2053. desired_nr_to_write = wbc->nr_to_write * 8;
  2054. } else
  2055. desired_nr_to_write = ext4_num_dirty_pages(inode, index,
  2056. max_pages);
  2057. if (desired_nr_to_write > max_pages)
  2058. desired_nr_to_write = max_pages;
  2059. if (wbc->nr_to_write < desired_nr_to_write) {
  2060. nr_to_writebump = desired_nr_to_write - wbc->nr_to_write;
  2061. wbc->nr_to_write = desired_nr_to_write;
  2062. }
  2063. retry:
  2064. if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
  2065. tag_pages_for_writeback(mapping, index, end);
  2066. blk_start_plug(&plug);
  2067. while (!ret && wbc->nr_to_write > 0) {
  2068. /*
  2069. * we insert one extent at a time. So we need
  2070. * credit needed for single extent allocation.
  2071. * journalled mode is currently not supported
  2072. * by delalloc
  2073. */
  2074. BUG_ON(ext4_should_journal_data(inode));
  2075. needed_blocks = ext4_da_writepages_trans_blocks(inode);
  2076. /* start a new transaction*/
  2077. handle = ext4_journal_start(inode, needed_blocks);
  2078. if (IS_ERR(handle)) {
  2079. ret = PTR_ERR(handle);
  2080. ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
  2081. "%ld pages, ino %lu; err %d", __func__,
  2082. wbc->nr_to_write, inode->i_ino, ret);
  2083. blk_finish_plug(&plug);
  2084. goto out_writepages;
  2085. }
  2086. /*
  2087. * Now call write_cache_pages_da() to find the next
  2088. * contiguous region of logical blocks that need
  2089. * blocks to be allocated by ext4 and submit them.
  2090. */
  2091. ret = write_cache_pages_da(handle, mapping,
  2092. wbc, &mpd, &done_index);
  2093. /*
  2094. * If we have a contiguous extent of pages and we
  2095. * haven't done the I/O yet, map the blocks and submit
  2096. * them for I/O.
  2097. */
  2098. if (!mpd.io_done && mpd.next_page != mpd.first_page) {
  2099. mpage_da_map_and_submit(&mpd);
  2100. ret = MPAGE_DA_EXTENT_TAIL;
  2101. }
  2102. trace_ext4_da_write_pages(inode, &mpd);
  2103. wbc->nr_to_write -= mpd.pages_written;
  2104. ext4_journal_stop(handle);
  2105. if ((mpd.retval == -ENOSPC) && sbi->s_journal) {
  2106. /* commit the transaction which would
  2107. * free blocks released in the transaction
  2108. * and try again
  2109. */
  2110. jbd2_journal_force_commit_nested(sbi->s_journal);
  2111. ret = 0;
  2112. } else if (ret == MPAGE_DA_EXTENT_TAIL) {
  2113. /*
  2114. * Got one extent now try with rest of the pages.
  2115. * If mpd.retval is set -EIO, journal is aborted.
  2116. * So we don't need to write any more.
  2117. */
  2118. pages_written += mpd.pages_written;
  2119. ret = mpd.retval;
  2120. io_done = 1;
  2121. } else if (wbc->nr_to_write)
  2122. /*
  2123. * There is no more writeout needed
  2124. * or we requested for a noblocking writeout
  2125. * and we found the device congested
  2126. */
  2127. break;
  2128. }
  2129. blk_finish_plug(&plug);
  2130. if (!io_done && !cycled) {
  2131. cycled = 1;
  2132. index = 0;
  2133. wbc->range_start = index << PAGE_CACHE_SHIFT;
  2134. wbc->range_end = mapping->writeback_index - 1;
  2135. goto retry;
  2136. }
  2137. /* Update index */
  2138. wbc->range_cyclic = range_cyclic;
  2139. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  2140. /*
  2141. * set the writeback_index so that range_cyclic
  2142. * mode will write it back later
  2143. */
  2144. mapping->writeback_index = done_index;
  2145. out_writepages:
  2146. wbc->nr_to_write -= nr_to_writebump;
  2147. wbc->range_start = range_start;
  2148. trace_ext4_da_writepages_result(inode, wbc, ret, pages_written);
  2149. return ret;
  2150. }
  2151. static int ext4_nonda_switch(struct super_block *sb)
  2152. {
  2153. s64 free_blocks, dirty_blocks;
  2154. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2155. /*
  2156. * switch to non delalloc mode if we are running low
  2157. * on free block. The free block accounting via percpu
  2158. * counters can get slightly wrong with percpu_counter_batch getting
  2159. * accumulated on each CPU without updating global counters
  2160. * Delalloc need an accurate free block accounting. So switch
  2161. * to non delalloc when we are near to error range.
  2162. */
  2163. free_blocks = EXT4_C2B(sbi,
  2164. percpu_counter_read_positive(&sbi->s_freeclusters_counter));
  2165. dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyclusters_counter);
  2166. /*
  2167. * Start pushing delalloc when 1/2 of free blocks are dirty.
  2168. */
  2169. if (dirty_blocks && (free_blocks < 2 * dirty_blocks) &&
  2170. !writeback_in_progress(sb->s_bdi) &&
  2171. down_read_trylock(&sb->s_umount)) {
  2172. writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE);
  2173. up_read(&sb->s_umount);
  2174. }
  2175. if (2 * free_blocks < 3 * dirty_blocks ||
  2176. free_blocks < (dirty_blocks + EXT4_FREECLUSTERS_WATERMARK)) {
  2177. /*
  2178. * free block count is less than 150% of dirty blocks
  2179. * or free blocks is less than watermark
  2180. */
  2181. return 1;
  2182. }
  2183. return 0;
  2184. }
  2185. static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
  2186. loff_t pos, unsigned len, unsigned flags,
  2187. struct page **pagep, void **fsdata)
  2188. {
  2189. int ret, retries = 0;
  2190. struct page *page;
  2191. pgoff_t index;
  2192. struct inode *inode = mapping->host;
  2193. handle_t *handle;
  2194. index = pos >> PAGE_CACHE_SHIFT;
  2195. if (ext4_nonda_switch(inode->i_sb)) {
  2196. *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
  2197. return ext4_write_begin(file, mapping, pos,
  2198. len, flags, pagep, fsdata);
  2199. }
  2200. *fsdata = (void *)0;
  2201. trace_ext4_da_write_begin(inode, pos, len, flags);
  2202. if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
  2203. ret = ext4_da_write_inline_data_begin(mapping, inode,
  2204. pos, len, flags,
  2205. pagep, fsdata);
  2206. if (ret < 0)
  2207. goto out;
  2208. if (ret == 1) {
  2209. ret = 0;
  2210. goto out;
  2211. }
  2212. }
  2213. retry:
  2214. /*
  2215. * With delayed allocation, we don't log the i_disksize update
  2216. * if there is delayed block allocation. But we still need
  2217. * to journalling the i_disksize update if writes to the end
  2218. * of file which has an already mapped buffer.
  2219. */
  2220. handle = ext4_journal_start(inode, 1);
  2221. if (IS_ERR(handle)) {
  2222. ret = PTR_ERR(handle);
  2223. goto out;
  2224. }
  2225. /* We cannot recurse into the filesystem as the transaction is already
  2226. * started */
  2227. flags |= AOP_FLAG_NOFS;
  2228. page = grab_cache_page_write_begin(mapping, index, flags);
  2229. if (!page) {
  2230. ext4_journal_stop(handle);
  2231. ret = -ENOMEM;
  2232. goto out;
  2233. }
  2234. *pagep = page;
  2235. ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
  2236. if (ret < 0) {
  2237. unlock_page(page);
  2238. ext4_journal_stop(handle);
  2239. page_cache_release(page);
  2240. /*
  2241. * block_write_begin may have instantiated a few blocks
  2242. * outside i_size. Trim these off again. Don't need
  2243. * i_size_read because we hold i_mutex.
  2244. */
  2245. if (pos + len > inode->i_size)
  2246. ext4_truncate_failed_write(inode);
  2247. }
  2248. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  2249. goto retry;
  2250. out:
  2251. return ret;
  2252. }
  2253. /*
  2254. * Check if we should update i_disksize
  2255. * when write to the end of file but not require block allocation
  2256. */
  2257. static int ext4_da_should_update_i_disksize(struct page *page,
  2258. unsigned long offset)
  2259. {
  2260. struct buffer_head *bh;
  2261. struct inode *inode = page->mapping->host;
  2262. unsigned int idx;
  2263. int i;
  2264. bh = page_buffers(page);
  2265. idx = offset >> inode->i_blkbits;
  2266. for (i = 0; i < idx; i++)
  2267. bh = bh->b_this_page;
  2268. if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
  2269. return 0;
  2270. return 1;
  2271. }
  2272. static int ext4_da_write_end(struct file *file,
  2273. struct address_space *mapping,
  2274. loff_t pos, unsigned len, unsigned copied,
  2275. struct page *page, void *fsdata)
  2276. {
  2277. struct inode *inode = mapping->host;
  2278. int ret = 0, ret2;
  2279. handle_t *handle = ext4_journal_current_handle();
  2280. loff_t new_i_size;
  2281. unsigned long start, end;
  2282. int write_mode = (int)(unsigned long)fsdata;
  2283. if (write_mode == FALL_BACK_TO_NONDELALLOC) {
  2284. switch (ext4_inode_journal_mode(inode)) {
  2285. case EXT4_INODE_ORDERED_DATA_MODE:
  2286. return ext4_ordered_write_end(file, mapping, pos,
  2287. len, copied, page, fsdata);
  2288. case EXT4_INODE_WRITEBACK_DATA_MODE:
  2289. return ext4_writeback_write_end(file, mapping, pos,
  2290. len, copied, page, fsdata);
  2291. default:
  2292. BUG();
  2293. }
  2294. }
  2295. trace_ext4_da_write_end(inode, pos, len, copied);
  2296. start = pos & (PAGE_CACHE_SIZE - 1);
  2297. end = start + copied - 1;
  2298. /*
  2299. * generic_write_end() will run mark_inode_dirty() if i_size
  2300. * changes. So let's piggyback the i_disksize mark_inode_dirty
  2301. * into that.
  2302. */
  2303. new_i_size = pos + copied;
  2304. if (copied && new_i_size > EXT4_I(inode)->i_disksize) {
  2305. if (ext4_has_inline_data(inode) ||
  2306. ext4_da_should_update_i_disksize(page, end)) {
  2307. down_write(&EXT4_I(inode)->i_data_sem);
  2308. if (new_i_size > EXT4_I(inode)->i_disksize)
  2309. EXT4_I(inode)->i_disksize = new_i_size;
  2310. up_write(&EXT4_I(inode)->i_data_sem);
  2311. /* We need to mark inode dirty even if
  2312. * new_i_size is less that inode->i_size
  2313. * bu greater than i_disksize.(hint delalloc)
  2314. */
  2315. ext4_mark_inode_dirty(handle, inode);
  2316. }
  2317. }
  2318. if (write_mode != CONVERT_INLINE_DATA &&
  2319. ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) &&
  2320. ext4_has_inline_data(inode))
  2321. ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied,
  2322. page);
  2323. else
  2324. ret2 = generic_write_end(file, mapping, pos, len, copied,
  2325. page, fsdata);
  2326. copied = ret2;
  2327. if (ret2 < 0)
  2328. ret = ret2;
  2329. ret2 = ext4_journal_stop(handle);
  2330. if (!ret)
  2331. ret = ret2;
  2332. return ret ? ret : copied;
  2333. }
  2334. static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
  2335. {
  2336. /*
  2337. * Drop reserved blocks
  2338. */
  2339. BUG_ON(!PageLocked(page));
  2340. if (!page_has_buffers(page))
  2341. goto out;
  2342. ext4_da_page_release_reservation(page, offset);
  2343. out:
  2344. ext4_invalidatepage(page, offset);
  2345. return;
  2346. }
  2347. /*
  2348. * Force all delayed allocation blocks to be allocated for a given inode.
  2349. */
  2350. int ext4_alloc_da_blocks(struct inode *inode)
  2351. {
  2352. trace_ext4_alloc_da_blocks(inode);
  2353. if (!EXT4_I(inode)->i_reserved_data_blocks &&
  2354. !EXT4_I(inode)->i_reserved_meta_blocks)
  2355. return 0;
  2356. /*
  2357. * We do something simple for now. The filemap_flush() will
  2358. * also start triggering a write of the data blocks, which is
  2359. * not strictly speaking necessary (and for users of
  2360. * laptop_mode, not even desirable). However, to do otherwise
  2361. * would require replicating code paths in:
  2362. *
  2363. * ext4_da_writepages() ->
  2364. * write_cache_pages() ---> (via passed in callback function)
  2365. * __mpage_da_writepage() -->
  2366. * mpage_add_bh_to_extent()
  2367. * mpage_da_map_blocks()
  2368. *
  2369. * The problem is that write_cache_pages(), located in
  2370. * mm/page-writeback.c, marks pages clean in preparation for
  2371. * doing I/O, which is not desirable if we're not planning on
  2372. * doing I/O at all.
  2373. *
  2374. * We could call write_cache_pages(), and then redirty all of
  2375. * the pages by calling redirty_page_for_writepage() but that
  2376. * would be ugly in the extreme. So instead we would need to
  2377. * replicate parts of the code in the above functions,
  2378. * simplifying them because we wouldn't actually intend to
  2379. * write out the pages, but rather only collect contiguous
  2380. * logical block extents, call the multi-block allocator, and
  2381. * then update the buffer heads with the block allocations.
  2382. *
  2383. * For now, though, we'll cheat by calling filemap_flush(),
  2384. * which will map the blocks, and start the I/O, but not
  2385. * actually wait for the I/O to complete.
  2386. */
  2387. return filemap_flush(inode->i_mapping);
  2388. }
  2389. /*
  2390. * bmap() is special. It gets used by applications such as lilo and by
  2391. * the swapper to find the on-disk block of a specific piece of data.
  2392. *
  2393. * Naturally, this is dangerous if the block concerned is still in the
  2394. * journal. If somebody makes a swapfile on an ext4 data-journaling
  2395. * filesystem and enables swap, then they may get a nasty shock when the
  2396. * data getting swapped to that swapfile suddenly gets overwritten by
  2397. * the original zero's written out previously to the journal and
  2398. * awaiting writeback in the kernel's buffer cache.
  2399. *
  2400. * So, if we see any bmap calls here on a modified, data-journaled file,
  2401. * take extra steps to flush any blocks which might be in the cache.
  2402. */
  2403. static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
  2404. {
  2405. struct inode *inode = mapping->host;
  2406. journal_t *journal;
  2407. int err;
  2408. /*
  2409. * We can get here for an inline file via the FIBMAP ioctl
  2410. */
  2411. if (ext4_has_inline_data(inode))
  2412. return 0;
  2413. if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
  2414. test_opt(inode->i_sb, DELALLOC)) {
  2415. /*
  2416. * With delalloc we want to sync the file
  2417. * so that we can make sure we allocate
  2418. * blocks for file
  2419. */
  2420. filemap_write_and_wait(mapping);
  2421. }
  2422. if (EXT4_JOURNAL(inode) &&
  2423. ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
  2424. /*
  2425. * This is a REALLY heavyweight approach, but the use of
  2426. * bmap on dirty files is expected to be extremely rare:
  2427. * only if we run lilo or swapon on a freshly made file
  2428. * do we expect this to happen.
  2429. *
  2430. * (bmap requires CAP_SYS_RAWIO so this does not
  2431. * represent an unprivileged user DOS attack --- we'd be
  2432. * in trouble if mortal users could trigger this path at
  2433. * will.)
  2434. *
  2435. * NB. EXT4_STATE_JDATA is not set on files other than
  2436. * regular files. If somebody wants to bmap a directory
  2437. * or symlink and gets confused because the buffer
  2438. * hasn't yet been flushed to disk, they deserve
  2439. * everything they get.
  2440. */
  2441. ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
  2442. journal = EXT4_JOURNAL(inode);
  2443. jbd2_journal_lock_updates(journal);
  2444. err = jbd2_journal_flush(journal);
  2445. jbd2_journal_unlock_updates(journal);
  2446. if (err)
  2447. return 0;
  2448. }
  2449. return generic_block_bmap(mapping, block, ext4_get_block);
  2450. }
  2451. static int ext4_readpage(struct file *file, struct page *page)
  2452. {
  2453. int ret = -EAGAIN;
  2454. struct inode *inode = page->mapping->host;
  2455. trace_ext4_readpage(page);
  2456. if (ext4_has_inline_data(inode))
  2457. ret = ext4_readpage_inline(inode, page);
  2458. if (ret == -EAGAIN)
  2459. return mpage_readpage(page, ext4_get_block);
  2460. return ret;
  2461. }
  2462. static int
  2463. ext4_readpages(struct file *file, struct address_space *mapping,
  2464. struct list_head *pages, unsigned nr_pages)
  2465. {
  2466. struct inode *inode = mapping->host;
  2467. /* If the file has inline data, no need to do readpages. */
  2468. if (ext4_has_inline_data(inode))
  2469. return 0;
  2470. return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
  2471. }
  2472. static void ext4_invalidatepage(struct page *page, unsigned long offset)
  2473. {
  2474. trace_ext4_invalidatepage(page, offset);
  2475. /* No journalling happens on data buffers when this function is used */
  2476. WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page)));
  2477. block_invalidatepage(page, offset);
  2478. }
  2479. static int __ext4_journalled_invalidatepage(struct page *page,
  2480. unsigned long offset)
  2481. {
  2482. journal_t *journal = EXT4_JOURNAL(page->mapping->host);
  2483. trace_ext4_journalled_invalidatepage(page, offset);
  2484. /*
  2485. * If it's a full truncate we just forget about the pending dirtying
  2486. */
  2487. if (offset == 0)
  2488. ClearPageChecked(page);
  2489. return jbd2_journal_invalidatepage(journal, page, offset);
  2490. }
  2491. /* Wrapper for aops... */
  2492. static void ext4_journalled_invalidatepage(struct page *page,
  2493. unsigned long offset)
  2494. {
  2495. WARN_ON(__ext4_journalled_invalidatepage(page, offset) < 0);
  2496. }
  2497. static int ext4_releasepage(struct page *page, gfp_t wait)
  2498. {
  2499. journal_t *journal = EXT4_JOURNAL(page->mapping->host);
  2500. trace_ext4_releasepage(page);
  2501. WARN_ON(PageChecked(page));
  2502. if (!page_has_buffers(page))
  2503. return 0;
  2504. if (journal)
  2505. return jbd2_journal_try_to_free_buffers(journal, page, wait);
  2506. else
  2507. return try_to_free_buffers(page);
  2508. }
  2509. /*
  2510. * ext4_get_block used when preparing for a DIO write or buffer write.
  2511. * We allocate an uinitialized extent if blocks haven't been allocated.
  2512. * The extent will be converted to initialized after the IO is complete.
  2513. */
  2514. int ext4_get_block_write(struct inode *inode, sector_t iblock,
  2515. struct buffer_head *bh_result, int create)
  2516. {
  2517. ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n",
  2518. inode->i_ino, create);
  2519. return _ext4_get_block(inode, iblock, bh_result,
  2520. EXT4_GET_BLOCKS_IO_CREATE_EXT);
  2521. }
  2522. static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock,
  2523. struct buffer_head *bh_result, int create)
  2524. {
  2525. ext4_debug("ext4_get_block_write_nolock: inode %lu, create flag %d\n",
  2526. inode->i_ino, create);
  2527. return _ext4_get_block(inode, iblock, bh_result,
  2528. EXT4_GET_BLOCKS_NO_LOCK);
  2529. }
  2530. static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
  2531. ssize_t size, void *private, int ret,
  2532. bool is_async)
  2533. {
  2534. struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
  2535. ext4_io_end_t *io_end = iocb->private;
  2536. /* if not async direct IO or dio with 0 bytes write, just return */
  2537. if (!io_end || !size)
  2538. goto out;
  2539. ext_debug("ext4_end_io_dio(): io_end 0x%p "
  2540. "for inode %lu, iocb 0x%p, offset %llu, size %zd\n",
  2541. iocb->private, io_end->inode->i_ino, iocb, offset,
  2542. size);
  2543. iocb->private = NULL;
  2544. /* if not aio dio with unwritten extents, just free io and return */
  2545. if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
  2546. ext4_free_io_end(io_end);
  2547. out:
  2548. if (is_async)
  2549. aio_complete(iocb, ret, 0);
  2550. inode_dio_done(inode);
  2551. return;
  2552. }
  2553. io_end->offset = offset;
  2554. io_end->size = size;
  2555. if (is_async) {
  2556. io_end->iocb = iocb;
  2557. io_end->result = ret;
  2558. }
  2559. ext4_add_complete_io(io_end);
  2560. }
  2561. /*
  2562. * For ext4 extent files, ext4 will do direct-io write to holes,
  2563. * preallocated extents, and those write extend the file, no need to
  2564. * fall back to buffered IO.
  2565. *
  2566. * For holes, we fallocate those blocks, mark them as uninitialized
  2567. * If those blocks were preallocated, we mark sure they are split, but
  2568. * still keep the range to write as uninitialized.
  2569. *
  2570. * The unwritten extents will be converted to written when DIO is completed.
  2571. * For async direct IO, since the IO may still pending when return, we
  2572. * set up an end_io call back function, which will do the conversion
  2573. * when async direct IO completed.
  2574. *
  2575. * If the O_DIRECT write will extend the file then add this inode to the
  2576. * orphan list. So recovery will truncate it back to the original size
  2577. * if the machine crashes during the write.
  2578. *
  2579. */
  2580. static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
  2581. const struct iovec *iov, loff_t offset,
  2582. unsigned long nr_segs)
  2583. {
  2584. struct file *file = iocb->ki_filp;
  2585. struct inode *inode = file->f_mapping->host;
  2586. ssize_t ret;
  2587. size_t count = iov_length(iov, nr_segs);
  2588. int overwrite = 0;
  2589. get_block_t *get_block_func = NULL;
  2590. int dio_flags = 0;
  2591. loff_t final_size = offset + count;
  2592. /* Use the old path for reads and writes beyond i_size. */
  2593. if (rw != WRITE || final_size > inode->i_size)
  2594. return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
  2595. BUG_ON(iocb->private == NULL);
  2596. /* If we do a overwrite dio, i_mutex locking can be released */
  2597. overwrite = *((int *)iocb->private);
  2598. if (overwrite) {
  2599. atomic_inc(&inode->i_dio_count);
  2600. down_read(&EXT4_I(inode)->i_data_sem);
  2601. mutex_unlock(&inode->i_mutex);
  2602. }
  2603. /*
  2604. * We could direct write to holes and fallocate.
  2605. *
  2606. * Allocated blocks to fill the hole are marked as
  2607. * uninitialized to prevent parallel buffered read to expose
  2608. * the stale data before DIO complete the data IO.
  2609. *
  2610. * As to previously fallocated extents, ext4 get_block will
  2611. * just simply mark the buffer mapped but still keep the
  2612. * extents uninitialized.
  2613. *
  2614. * For non AIO case, we will convert those unwritten extents
  2615. * to written after return back from blockdev_direct_IO.
  2616. *
  2617. * For async DIO, the conversion needs to be deferred when the
  2618. * IO is completed. The ext4 end_io callback function will be
  2619. * called to take care of the conversion work. Here for async
  2620. * case, we allocate an io_end structure to hook to the iocb.
  2621. */
  2622. iocb->private = NULL;
  2623. ext4_inode_aio_set(inode, NULL);
  2624. if (!is_sync_kiocb(iocb)) {
  2625. ext4_io_end_t *io_end = ext4_init_io_end(inode, GFP_NOFS);
  2626. if (!io_end) {
  2627. ret = -ENOMEM;
  2628. goto retake_lock;
  2629. }
  2630. io_end->flag |= EXT4_IO_END_DIRECT;
  2631. iocb->private = io_end;
  2632. /*
  2633. * we save the io structure for current async direct
  2634. * IO, so that later ext4_map_blocks() could flag the
  2635. * io structure whether there is a unwritten extents
  2636. * needs to be converted when IO is completed.
  2637. */
  2638. ext4_inode_aio_set(inode, io_end);
  2639. }
  2640. if (overwrite) {
  2641. get_block_func = ext4_get_block_write_nolock;
  2642. } else {
  2643. get_block_func = ext4_get_block_write;
  2644. dio_flags = DIO_LOCKING;
  2645. }
  2646. ret = __blockdev_direct_IO(rw, iocb, inode,
  2647. inode->i_sb->s_bdev, iov,
  2648. offset, nr_segs,
  2649. get_block_func,
  2650. ext4_end_io_dio,
  2651. NULL,
  2652. dio_flags);
  2653. if (iocb->private)
  2654. ext4_inode_aio_set(inode, NULL);
  2655. /*
  2656. * The io_end structure takes a reference to the inode, that
  2657. * structure needs to be destroyed and the reference to the
  2658. * inode need to be dropped, when IO is complete, even with 0
  2659. * byte write, or failed.
  2660. *
  2661. * In the successful AIO DIO case, the io_end structure will
  2662. * be destroyed and the reference to the inode will be dropped
  2663. * after the end_io call back function is called.
  2664. *
  2665. * In the case there is 0 byte write, or error case, since VFS
  2666. * direct IO won't invoke the end_io call back function, we
  2667. * need to free the end_io structure here.
  2668. */
  2669. if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) {
  2670. ext4_free_io_end(iocb->private);
  2671. iocb->private = NULL;
  2672. } else if (ret > 0 && !overwrite && ext4_test_inode_state(inode,
  2673. EXT4_STATE_DIO_UNWRITTEN)) {
  2674. int err;
  2675. /*
  2676. * for non AIO case, since the IO is already
  2677. * completed, we could do the conversion right here
  2678. */
  2679. err = ext4_convert_unwritten_extents(inode,
  2680. offset, ret);
  2681. if (err < 0)
  2682. ret = err;
  2683. ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
  2684. }
  2685. retake_lock:
  2686. /* take i_mutex locking again if we do a ovewrite dio */
  2687. if (overwrite) {
  2688. inode_dio_done(inode);
  2689. up_read(&EXT4_I(inode)->i_data_sem);
  2690. mutex_lock(&inode->i_mutex);
  2691. }
  2692. return ret;
  2693. }
  2694. static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
  2695. const struct iovec *iov, loff_t offset,
  2696. unsigned long nr_segs)
  2697. {
  2698. struct file *file = iocb->ki_filp;
  2699. struct inode *inode = file->f_mapping->host;
  2700. ssize_t ret;
  2701. /*
  2702. * If we are doing data journalling we don't support O_DIRECT
  2703. */
  2704. if (ext4_should_journal_data(inode))
  2705. return 0;
  2706. /* Let buffer I/O handle the inline data case. */
  2707. if (ext4_has_inline_data(inode))
  2708. return 0;
  2709. trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw);
  2710. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  2711. ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs);
  2712. else
  2713. ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
  2714. trace_ext4_direct_IO_exit(inode, offset,
  2715. iov_length(iov, nr_segs), rw, ret);
  2716. return ret;
  2717. }
  2718. /*
  2719. * Pages can be marked dirty completely asynchronously from ext4's journalling
  2720. * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
  2721. * much here because ->set_page_dirty is called under VFS locks. The page is
  2722. * not necessarily locked.
  2723. *
  2724. * We cannot just dirty the page and leave attached buffers clean, because the
  2725. * buffers' dirty state is "definitive". We cannot just set the buffers dirty
  2726. * or jbddirty because all the journalling code will explode.
  2727. *
  2728. * So what we do is to mark the page "pending dirty" and next time writepage
  2729. * is called, propagate that into the buffers appropriately.
  2730. */
  2731. static int ext4_journalled_set_page_dirty(struct page *page)
  2732. {
  2733. SetPageChecked(page);
  2734. return __set_page_dirty_nobuffers(page);
  2735. }
  2736. static const struct address_space_operations ext4_ordered_aops = {
  2737. .readpage = ext4_readpage,
  2738. .readpages = ext4_readpages,
  2739. .writepage = ext4_writepage,
  2740. .write_begin = ext4_write_begin,
  2741. .write_end = ext4_ordered_write_end,
  2742. .bmap = ext4_bmap,
  2743. .invalidatepage = ext4_invalidatepage,
  2744. .releasepage = ext4_releasepage,
  2745. .direct_IO = ext4_direct_IO,
  2746. .migratepage = buffer_migrate_page,
  2747. .is_partially_uptodate = block_is_partially_uptodate,
  2748. .error_remove_page = generic_error_remove_page,
  2749. };
  2750. static const struct address_space_operations ext4_writeback_aops = {
  2751. .readpage = ext4_readpage,
  2752. .readpages = ext4_readpages,
  2753. .writepage = ext4_writepage,
  2754. .write_begin = ext4_write_begin,
  2755. .write_end = ext4_writeback_write_end,
  2756. .bmap = ext4_bmap,
  2757. .invalidatepage = ext4_invalidatepage,
  2758. .releasepage = ext4_releasepage,
  2759. .direct_IO = ext4_direct_IO,
  2760. .migratepage = buffer_migrate_page,
  2761. .is_partially_uptodate = block_is_partially_uptodate,
  2762. .error_remove_page = generic_error_remove_page,
  2763. };
  2764. static const struct address_space_operations ext4_journalled_aops = {
  2765. .readpage = ext4_readpage,
  2766. .readpages = ext4_readpages,
  2767. .writepage = ext4_writepage,
  2768. .write_begin = ext4_write_begin,
  2769. .write_end = ext4_journalled_write_end,
  2770. .set_page_dirty = ext4_journalled_set_page_dirty,
  2771. .bmap = ext4_bmap,
  2772. .invalidatepage = ext4_journalled_invalidatepage,
  2773. .releasepage = ext4_releasepage,
  2774. .direct_IO = ext4_direct_IO,
  2775. .is_partially_uptodate = block_is_partially_uptodate,
  2776. .error_remove_page = generic_error_remove_page,
  2777. };
  2778. static const struct address_space_operations ext4_da_aops = {
  2779. .readpage = ext4_readpage,
  2780. .readpages = ext4_readpages,
  2781. .writepage = ext4_writepage,
  2782. .writepages = ext4_da_writepages,
  2783. .write_begin = ext4_da_write_begin,
  2784. .write_end = ext4_da_write_end,
  2785. .bmap = ext4_bmap,
  2786. .invalidatepage = ext4_da_invalidatepage,
  2787. .releasepage = ext4_releasepage,
  2788. .direct_IO = ext4_direct_IO,
  2789. .migratepage = buffer_migrate_page,
  2790. .is_partially_uptodate = block_is_partially_uptodate,
  2791. .error_remove_page = generic_error_remove_page,
  2792. };
  2793. void ext4_set_aops(struct inode *inode)
  2794. {
  2795. switch (ext4_inode_journal_mode(inode)) {
  2796. case EXT4_INODE_ORDERED_DATA_MODE:
  2797. if (test_opt(inode->i_sb, DELALLOC))
  2798. inode->i_mapping->a_ops = &ext4_da_aops;
  2799. else
  2800. inode->i_mapping->a_ops = &ext4_ordered_aops;
  2801. break;
  2802. case EXT4_INODE_WRITEBACK_DATA_MODE:
  2803. if (test_opt(inode->i_sb, DELALLOC))
  2804. inode->i_mapping->a_ops = &ext4_da_aops;
  2805. else
  2806. inode->i_mapping->a_ops = &ext4_writeback_aops;
  2807. break;
  2808. case EXT4_INODE_JOURNAL_DATA_MODE:
  2809. inode->i_mapping->a_ops = &ext4_journalled_aops;
  2810. break;
  2811. default:
  2812. BUG();
  2813. }
  2814. }
  2815. /*
  2816. * ext4_discard_partial_page_buffers()
  2817. * Wrapper function for ext4_discard_partial_page_buffers_no_lock.
  2818. * This function finds and locks the page containing the offset
  2819. * "from" and passes it to ext4_discard_partial_page_buffers_no_lock.
  2820. * Calling functions that already have the page locked should call
  2821. * ext4_discard_partial_page_buffers_no_lock directly.
  2822. */
  2823. int ext4_discard_partial_page_buffers(handle_t *handle,
  2824. struct address_space *mapping, loff_t from,
  2825. loff_t length, int flags)
  2826. {
  2827. struct inode *inode = mapping->host;
  2828. struct page *page;
  2829. int err = 0;
  2830. page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
  2831. mapping_gfp_mask(mapping) & ~__GFP_FS);
  2832. if (!page)
  2833. return -ENOMEM;
  2834. err = ext4_discard_partial_page_buffers_no_lock(handle, inode, page,
  2835. from, length, flags);
  2836. unlock_page(page);
  2837. page_cache_release(page);
  2838. return err;
  2839. }
  2840. /*
  2841. * ext4_discard_partial_page_buffers_no_lock()
  2842. * Zeros a page range of length 'length' starting from offset 'from'.
  2843. * Buffer heads that correspond to the block aligned regions of the
  2844. * zeroed range will be unmapped. Unblock aligned regions
  2845. * will have the corresponding buffer head mapped if needed so that
  2846. * that region of the page can be updated with the partial zero out.
  2847. *
  2848. * This function assumes that the page has already been locked. The
  2849. * The range to be discarded must be contained with in the given page.
  2850. * If the specified range exceeds the end of the page it will be shortened
  2851. * to the end of the page that corresponds to 'from'. This function is
  2852. * appropriate for updating a page and it buffer heads to be unmapped and
  2853. * zeroed for blocks that have been either released, or are going to be
  2854. * released.
  2855. *
  2856. * handle: The journal handle
  2857. * inode: The files inode
  2858. * page: A locked page that contains the offset "from"
  2859. * from: The starting byte offset (from the beginning of the file)
  2860. * to begin discarding
  2861. * len: The length of bytes to discard
  2862. * flags: Optional flags that may be used:
  2863. *
  2864. * EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED
  2865. * Only zero the regions of the page whose buffer heads
  2866. * have already been unmapped. This flag is appropriate
  2867. * for updating the contents of a page whose blocks may
  2868. * have already been released, and we only want to zero
  2869. * out the regions that correspond to those released blocks.
  2870. *
  2871. * Returns zero on success or negative on failure.
  2872. */
  2873. static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle,
  2874. struct inode *inode, struct page *page, loff_t from,
  2875. loff_t length, int flags)
  2876. {
  2877. ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
  2878. unsigned int offset = from & (PAGE_CACHE_SIZE-1);
  2879. unsigned int blocksize, max, pos;
  2880. ext4_lblk_t iblock;
  2881. struct buffer_head *bh;
  2882. int err = 0;
  2883. blocksize = inode->i_sb->s_blocksize;
  2884. max = PAGE_CACHE_SIZE - offset;
  2885. if (index != page->index)
  2886. return -EINVAL;
  2887. /*
  2888. * correct length if it does not fall between
  2889. * 'from' and the end of the page
  2890. */
  2891. if (length > max || length < 0)
  2892. length = max;
  2893. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  2894. if (!page_has_buffers(page))
  2895. create_empty_buffers(page, blocksize, 0);
  2896. /* Find the buffer that contains "offset" */
  2897. bh = page_buffers(page);
  2898. pos = blocksize;
  2899. while (offset >= pos) {
  2900. bh = bh->b_this_page;
  2901. iblock++;
  2902. pos += blocksize;
  2903. }
  2904. pos = offset;
  2905. while (pos < offset + length) {
  2906. unsigned int end_of_block, range_to_discard;
  2907. err = 0;
  2908. /* The length of space left to zero and unmap */
  2909. range_to_discard = offset + length - pos;
  2910. /* The length of space until the end of the block */
  2911. end_of_block = blocksize - (pos & (blocksize-1));
  2912. /*
  2913. * Do not unmap or zero past end of block
  2914. * for this buffer head
  2915. */
  2916. if (range_to_discard > end_of_block)
  2917. range_to_discard = end_of_block;
  2918. /*
  2919. * Skip this buffer head if we are only zeroing unampped
  2920. * regions of the page
  2921. */
  2922. if (flags & EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED &&
  2923. buffer_mapped(bh))
  2924. goto next;
  2925. /* If the range is block aligned, unmap */
  2926. if (range_to_discard == blocksize) {
  2927. clear_buffer_dirty(bh);
  2928. bh->b_bdev = NULL;
  2929. clear_buffer_mapped(bh);
  2930. clear_buffer_req(bh);
  2931. clear_buffer_new(bh);
  2932. clear_buffer_delay(bh);
  2933. clear_buffer_unwritten(bh);
  2934. clear_buffer_uptodate(bh);
  2935. zero_user(page, pos, range_to_discard);
  2936. BUFFER_TRACE(bh, "Buffer discarded");
  2937. goto next;
  2938. }
  2939. /*
  2940. * If this block is not completely contained in the range
  2941. * to be discarded, then it is not going to be released. Because
  2942. * we need to keep this block, we need to make sure this part
  2943. * of the page is uptodate before we modify it by writeing
  2944. * partial zeros on it.
  2945. */
  2946. if (!buffer_mapped(bh)) {
  2947. /*
  2948. * Buffer head must be mapped before we can read
  2949. * from the block
  2950. */
  2951. BUFFER_TRACE(bh, "unmapped");
  2952. ext4_get_block(inode, iblock, bh, 0);
  2953. /* unmapped? It's a hole - nothing to do */
  2954. if (!buffer_mapped(bh)) {
  2955. BUFFER_TRACE(bh, "still unmapped");
  2956. goto next;
  2957. }
  2958. }
  2959. /* Ok, it's mapped. Make sure it's up-to-date */
  2960. if (PageUptodate(page))
  2961. set_buffer_uptodate(bh);
  2962. if (!buffer_uptodate(bh)) {
  2963. err = -EIO;
  2964. ll_rw_block(READ, 1, &bh);
  2965. wait_on_buffer(bh);
  2966. /* Uhhuh. Read error. Complain and punt.*/
  2967. if (!buffer_uptodate(bh))
  2968. goto next;
  2969. }
  2970. if (ext4_should_journal_data(inode)) {
  2971. BUFFER_TRACE(bh, "get write access");
  2972. err = ext4_journal_get_write_access(handle, bh);
  2973. if (err)
  2974. goto next;
  2975. }
  2976. zero_user(page, pos, range_to_discard);
  2977. err = 0;
  2978. if (ext4_should_journal_data(inode)) {
  2979. err = ext4_handle_dirty_metadata(handle, inode, bh);
  2980. } else
  2981. mark_buffer_dirty(bh);
  2982. BUFFER_TRACE(bh, "Partial buffer zeroed");
  2983. next:
  2984. bh = bh->b_this_page;
  2985. iblock++;
  2986. pos += range_to_discard;
  2987. }
  2988. return err;
  2989. }
  2990. int ext4_can_truncate(struct inode *inode)
  2991. {
  2992. if (S_ISREG(inode->i_mode))
  2993. return 1;
  2994. if (S_ISDIR(inode->i_mode))
  2995. return 1;
  2996. if (S_ISLNK(inode->i_mode))
  2997. return !ext4_inode_is_fast_symlink(inode);
  2998. return 0;
  2999. }
  3000. /*
  3001. * ext4_punch_hole: punches a hole in a file by releaseing the blocks
  3002. * associated with the given offset and length
  3003. *
  3004. * @inode: File inode
  3005. * @offset: The offset where the hole will begin
  3006. * @len: The length of the hole
  3007. *
  3008. * Returns: 0 on success or negative on failure
  3009. */
  3010. int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
  3011. {
  3012. struct inode *inode = file->f_path.dentry->d_inode;
  3013. if (!S_ISREG(inode->i_mode))
  3014. return -EOPNOTSUPP;
  3015. if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  3016. return ext4_ind_punch_hole(file, offset, length);
  3017. if (EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) {
  3018. /* TODO: Add support for bigalloc file systems */
  3019. return -EOPNOTSUPP;
  3020. }
  3021. trace_ext4_punch_hole(inode, offset, length);
  3022. return ext4_ext_punch_hole(file, offset, length);
  3023. }
  3024. /*
  3025. * ext4_truncate()
  3026. *
  3027. * We block out ext4_get_block() block instantiations across the entire
  3028. * transaction, and VFS/VM ensures that ext4_truncate() cannot run
  3029. * simultaneously on behalf of the same inode.
  3030. *
  3031. * As we work through the truncate and commit bits of it to the journal there
  3032. * is one core, guiding principle: the file's tree must always be consistent on
  3033. * disk. We must be able to restart the truncate after a crash.
  3034. *
  3035. * The file's tree may be transiently inconsistent in memory (although it
  3036. * probably isn't), but whenever we close off and commit a journal transaction,
  3037. * the contents of (the filesystem + the journal) must be consistent and
  3038. * restartable. It's pretty simple, really: bottom up, right to left (although
  3039. * left-to-right works OK too).
  3040. *
  3041. * Note that at recovery time, journal replay occurs *before* the restart of
  3042. * truncate against the orphan inode list.
  3043. *
  3044. * The committed inode has the new, desired i_size (which is the same as
  3045. * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
  3046. * that this inode's truncate did not complete and it will again call
  3047. * ext4_truncate() to have another go. So there will be instantiated blocks
  3048. * to the right of the truncation point in a crashed ext4 filesystem. But
  3049. * that's fine - as long as they are linked from the inode, the post-crash
  3050. * ext4_truncate() run will find them and release them.
  3051. */
  3052. void ext4_truncate(struct inode *inode)
  3053. {
  3054. trace_ext4_truncate_enter(inode);
  3055. if (!ext4_can_truncate(inode))
  3056. return;
  3057. ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
  3058. if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
  3059. ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
  3060. if (ext4_has_inline_data(inode)) {
  3061. int has_inline = 1;
  3062. ext4_inline_data_truncate(inode, &has_inline);
  3063. if (has_inline)
  3064. return;
  3065. }
  3066. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  3067. ext4_ext_truncate(inode);
  3068. else
  3069. ext4_ind_truncate(inode);
  3070. trace_ext4_truncate_exit(inode);
  3071. }
  3072. /*
  3073. * ext4_get_inode_loc returns with an extra refcount against the inode's
  3074. * underlying buffer_head on success. If 'in_mem' is true, we have all
  3075. * data in memory that is needed to recreate the on-disk version of this
  3076. * inode.
  3077. */
  3078. static int __ext4_get_inode_loc(struct inode *inode,
  3079. struct ext4_iloc *iloc, int in_mem)
  3080. {
  3081. struct ext4_group_desc *gdp;
  3082. struct buffer_head *bh;
  3083. struct super_block *sb = inode->i_sb;
  3084. ext4_fsblk_t block;
  3085. int inodes_per_block, inode_offset;
  3086. iloc->bh = NULL;
  3087. if (!ext4_valid_inum(sb, inode->i_ino))
  3088. return -EIO;
  3089. iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
  3090. gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
  3091. if (!gdp)
  3092. return -EIO;
  3093. /*
  3094. * Figure out the offset within the block group inode table
  3095. */
  3096. inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
  3097. inode_offset = ((inode->i_ino - 1) %
  3098. EXT4_INODES_PER_GROUP(sb));
  3099. block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
  3100. iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
  3101. bh = sb_getblk(sb, block);
  3102. if (unlikely(!bh))
  3103. return -ENOMEM;
  3104. if (!buffer_uptodate(bh)) {
  3105. lock_buffer(bh);
  3106. /*
  3107. * If the buffer has the write error flag, we have failed
  3108. * to write out another inode in the same block. In this
  3109. * case, we don't have to read the block because we may
  3110. * read the old inode data successfully.
  3111. */
  3112. if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
  3113. set_buffer_uptodate(bh);
  3114. if (buffer_uptodate(bh)) {
  3115. /* someone brought it uptodate while we waited */
  3116. unlock_buffer(bh);
  3117. goto has_buffer;
  3118. }
  3119. /*
  3120. * If we have all information of the inode in memory and this
  3121. * is the only valid inode in the block, we need not read the
  3122. * block.
  3123. */
  3124. if (in_mem) {
  3125. struct buffer_head *bitmap_bh;
  3126. int i, start;
  3127. start = inode_offset & ~(inodes_per_block - 1);
  3128. /* Is the inode bitmap in cache? */
  3129. bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
  3130. if (unlikely(!bitmap_bh))
  3131. goto make_io;
  3132. /*
  3133. * If the inode bitmap isn't in cache then the
  3134. * optimisation may end up performing two reads instead
  3135. * of one, so skip it.
  3136. */
  3137. if (!buffer_uptodate(bitmap_bh)) {
  3138. brelse(bitmap_bh);
  3139. goto make_io;
  3140. }
  3141. for (i = start; i < start + inodes_per_block; i++) {
  3142. if (i == inode_offset)
  3143. continue;
  3144. if (ext4_test_bit(i, bitmap_bh->b_data))
  3145. break;
  3146. }
  3147. brelse(bitmap_bh);
  3148. if (i == start + inodes_per_block) {
  3149. /* all other inodes are free, so skip I/O */
  3150. memset(bh->b_data, 0, bh->b_size);
  3151. set_buffer_uptodate(bh);
  3152. unlock_buffer(bh);
  3153. goto has_buffer;
  3154. }
  3155. }
  3156. make_io:
  3157. /*
  3158. * If we need to do any I/O, try to pre-readahead extra
  3159. * blocks from the inode table.
  3160. */
  3161. if (EXT4_SB(sb)->s_inode_readahead_blks) {
  3162. ext4_fsblk_t b, end, table;
  3163. unsigned num;
  3164. table = ext4_inode_table(sb, gdp);
  3165. /* s_inode_readahead_blks is always a power of 2 */
  3166. b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1);
  3167. if (table > b)
  3168. b = table;
  3169. end = b + EXT4_SB(sb)->s_inode_readahead_blks;
  3170. num = EXT4_INODES_PER_GROUP(sb);
  3171. if (ext4_has_group_desc_csum(sb))
  3172. num -= ext4_itable_unused_count(sb, gdp);
  3173. table += num / inodes_per_block;
  3174. if (end > table)
  3175. end = table;
  3176. while (b <= end)
  3177. sb_breadahead(sb, b++);
  3178. }
  3179. /*
  3180. * There are other valid inodes in the buffer, this inode
  3181. * has in-inode xattrs, or we don't have this inode in memory.
  3182. * Read the block from disk.
  3183. */
  3184. trace_ext4_load_inode(inode);
  3185. get_bh(bh);
  3186. bh->b_end_io = end_buffer_read_sync;
  3187. submit_bh(READ | REQ_META | REQ_PRIO, bh);
  3188. wait_on_buffer(bh);
  3189. if (!buffer_uptodate(bh)) {
  3190. EXT4_ERROR_INODE_BLOCK(inode, block,
  3191. "unable to read itable block");
  3192. brelse(bh);
  3193. return -EIO;
  3194. }
  3195. }
  3196. has_buffer:
  3197. iloc->bh = bh;
  3198. return 0;
  3199. }
  3200. int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
  3201. {
  3202. /* We have all inode data except xattrs in memory here. */
  3203. return __ext4_get_inode_loc(inode, iloc,
  3204. !ext4_test_inode_state(inode, EXT4_STATE_XATTR));
  3205. }
  3206. void ext4_set_inode_flags(struct inode *inode)
  3207. {
  3208. unsigned int flags = EXT4_I(inode)->i_flags;
  3209. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  3210. if (flags & EXT4_SYNC_FL)
  3211. inode->i_flags |= S_SYNC;
  3212. if (flags & EXT4_APPEND_FL)
  3213. inode->i_flags |= S_APPEND;
  3214. if (flags & EXT4_IMMUTABLE_FL)
  3215. inode->i_flags |= S_IMMUTABLE;
  3216. if (flags & EXT4_NOATIME_FL)
  3217. inode->i_flags |= S_NOATIME;
  3218. if (flags & EXT4_DIRSYNC_FL)
  3219. inode->i_flags |= S_DIRSYNC;
  3220. }
  3221. /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
  3222. void ext4_get_inode_flags(struct ext4_inode_info *ei)
  3223. {
  3224. unsigned int vfs_fl;
  3225. unsigned long old_fl, new_fl;
  3226. do {
  3227. vfs_fl = ei->vfs_inode.i_flags;
  3228. old_fl = ei->i_flags;
  3229. new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
  3230. EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|
  3231. EXT4_DIRSYNC_FL);
  3232. if (vfs_fl & S_SYNC)
  3233. new_fl |= EXT4_SYNC_FL;
  3234. if (vfs_fl & S_APPEND)
  3235. new_fl |= EXT4_APPEND_FL;
  3236. if (vfs_fl & S_IMMUTABLE)
  3237. new_fl |= EXT4_IMMUTABLE_FL;
  3238. if (vfs_fl & S_NOATIME)
  3239. new_fl |= EXT4_NOATIME_FL;
  3240. if (vfs_fl & S_DIRSYNC)
  3241. new_fl |= EXT4_DIRSYNC_FL;
  3242. } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl);
  3243. }
  3244. static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
  3245. struct ext4_inode_info *ei)
  3246. {
  3247. blkcnt_t i_blocks ;
  3248. struct inode *inode = &(ei->vfs_inode);
  3249. struct super_block *sb = inode->i_sb;
  3250. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  3251. EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
  3252. /* we are using combined 48 bit field */
  3253. i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
  3254. le32_to_cpu(raw_inode->i_blocks_lo);
  3255. if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
  3256. /* i_blocks represent file system block size */
  3257. return i_blocks << (inode->i_blkbits - 9);
  3258. } else {
  3259. return i_blocks;
  3260. }
  3261. } else {
  3262. return le32_to_cpu(raw_inode->i_blocks_lo);
  3263. }
  3264. }
  3265. static inline void ext4_iget_extra_inode(struct inode *inode,
  3266. struct ext4_inode *raw_inode,
  3267. struct ext4_inode_info *ei)
  3268. {
  3269. __le32 *magic = (void *)raw_inode +
  3270. EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize;
  3271. if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) {
  3272. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  3273. ext4_find_inline_data_nolock(inode);
  3274. } else
  3275. EXT4_I(inode)->i_inline_off = 0;
  3276. }
  3277. struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
  3278. {
  3279. struct ext4_iloc iloc;
  3280. struct ext4_inode *raw_inode;
  3281. struct ext4_inode_info *ei;
  3282. struct inode *inode;
  3283. journal_t *journal = EXT4_SB(sb)->s_journal;
  3284. long ret;
  3285. int block;
  3286. uid_t i_uid;
  3287. gid_t i_gid;
  3288. inode = iget_locked(sb, ino);
  3289. if (!inode)
  3290. return ERR_PTR(-ENOMEM);
  3291. if (!(inode->i_state & I_NEW))
  3292. return inode;
  3293. ei = EXT4_I(inode);
  3294. iloc.bh = NULL;
  3295. ret = __ext4_get_inode_loc(inode, &iloc, 0);
  3296. if (ret < 0)
  3297. goto bad_inode;
  3298. raw_inode = ext4_raw_inode(&iloc);
  3299. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  3300. ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
  3301. if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
  3302. EXT4_INODE_SIZE(inode->i_sb)) {
  3303. EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)",
  3304. EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize,
  3305. EXT4_INODE_SIZE(inode->i_sb));
  3306. ret = -EIO;
  3307. goto bad_inode;
  3308. }
  3309. } else
  3310. ei->i_extra_isize = 0;
  3311. /* Precompute checksum seed for inode metadata */
  3312. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  3313. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
  3314. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  3315. __u32 csum;
  3316. __le32 inum = cpu_to_le32(inode->i_ino);
  3317. __le32 gen = raw_inode->i_generation;
  3318. csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
  3319. sizeof(inum));
  3320. ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
  3321. sizeof(gen));
  3322. }
  3323. if (!ext4_inode_csum_verify(inode, raw_inode, ei)) {
  3324. EXT4_ERROR_INODE(inode, "checksum invalid");
  3325. ret = -EIO;
  3326. goto bad_inode;
  3327. }
  3328. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  3329. i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
  3330. i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
  3331. if (!(test_opt(inode->i_sb, NO_UID32))) {
  3332. i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
  3333. i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
  3334. }
  3335. i_uid_write(inode, i_uid);
  3336. i_gid_write(inode, i_gid);
  3337. set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
  3338. ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
  3339. ei->i_inline_off = 0;
  3340. ei->i_dir_start_lookup = 0;
  3341. ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
  3342. /* We now have enough fields to check if the inode was active or not.
  3343. * This is needed because nfsd might try to access dead inodes
  3344. * the test is that same one that e2fsck uses
  3345. * NeilBrown 1999oct15
  3346. */
  3347. if (inode->i_nlink == 0) {
  3348. if (inode->i_mode == 0 ||
  3349. !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
  3350. /* this inode is deleted */
  3351. ret = -ESTALE;
  3352. goto bad_inode;
  3353. }
  3354. /* The only unlinked inodes we let through here have
  3355. * valid i_mode and are being read by the orphan
  3356. * recovery code: that's fine, we're about to complete
  3357. * the process of deleting those. */
  3358. }
  3359. ei->i_flags = le32_to_cpu(raw_inode->i_flags);
  3360. inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
  3361. ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
  3362. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT))
  3363. ei->i_file_acl |=
  3364. ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
  3365. inode->i_size = ext4_isize(raw_inode);
  3366. ei->i_disksize = inode->i_size;
  3367. #ifdef CONFIG_QUOTA
  3368. ei->i_reserved_quota = 0;
  3369. #endif
  3370. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  3371. ei->i_block_group = iloc.block_group;
  3372. ei->i_last_alloc_group = ~0;
  3373. /*
  3374. * NOTE! The in-memory inode i_data array is in little-endian order
  3375. * even on big-endian machines: we do NOT byteswap the block numbers!
  3376. */
  3377. for (block = 0; block < EXT4_N_BLOCKS; block++)
  3378. ei->i_data[block] = raw_inode->i_block[block];
  3379. INIT_LIST_HEAD(&ei->i_orphan);
  3380. /*
  3381. * Set transaction id's of transactions that have to be committed
  3382. * to finish f[data]sync. We set them to currently running transaction
  3383. * as we cannot be sure that the inode or some of its metadata isn't
  3384. * part of the transaction - the inode could have been reclaimed and
  3385. * now it is reread from disk.
  3386. */
  3387. if (journal) {
  3388. transaction_t *transaction;
  3389. tid_t tid;
  3390. read_lock(&journal->j_state_lock);
  3391. if (journal->j_running_transaction)
  3392. transaction = journal->j_running_transaction;
  3393. else
  3394. transaction = journal->j_committing_transaction;
  3395. if (transaction)
  3396. tid = transaction->t_tid;
  3397. else
  3398. tid = journal->j_commit_sequence;
  3399. read_unlock(&journal->j_state_lock);
  3400. ei->i_sync_tid = tid;
  3401. ei->i_datasync_tid = tid;
  3402. }
  3403. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  3404. if (ei->i_extra_isize == 0) {
  3405. /* The extra space is currently unused. Use it. */
  3406. ei->i_extra_isize = sizeof(struct ext4_inode) -
  3407. EXT4_GOOD_OLD_INODE_SIZE;
  3408. } else {
  3409. ext4_iget_extra_inode(inode, raw_inode, ei);
  3410. }
  3411. }
  3412. EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
  3413. EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
  3414. EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
  3415. EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
  3416. inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
  3417. if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
  3418. if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
  3419. inode->i_version |=
  3420. (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
  3421. }
  3422. ret = 0;
  3423. if (ei->i_file_acl &&
  3424. !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
  3425. EXT4_ERROR_INODE(inode, "bad extended attribute block %llu",
  3426. ei->i_file_acl);
  3427. ret = -EIO;
  3428. goto bad_inode;
  3429. } else if (!ext4_has_inline_data(inode)) {
  3430. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
  3431. if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  3432. (S_ISLNK(inode->i_mode) &&
  3433. !ext4_inode_is_fast_symlink(inode))))
  3434. /* Validate extent which is part of inode */
  3435. ret = ext4_ext_check_inode(inode);
  3436. } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  3437. (S_ISLNK(inode->i_mode) &&
  3438. !ext4_inode_is_fast_symlink(inode))) {
  3439. /* Validate block references which are part of inode */
  3440. ret = ext4_ind_check_inode(inode);
  3441. }
  3442. }
  3443. if (ret)
  3444. goto bad_inode;
  3445. if (S_ISREG(inode->i_mode)) {
  3446. inode->i_op = &ext4_file_inode_operations;
  3447. inode->i_fop = &ext4_file_operations;
  3448. ext4_set_aops(inode);
  3449. } else if (S_ISDIR(inode->i_mode)) {
  3450. inode->i_op = &ext4_dir_inode_operations;
  3451. inode->i_fop = &ext4_dir_operations;
  3452. } else if (S_ISLNK(inode->i_mode)) {
  3453. if (ext4_inode_is_fast_symlink(inode)) {
  3454. inode->i_op = &ext4_fast_symlink_inode_operations;
  3455. nd_terminate_link(ei->i_data, inode->i_size,
  3456. sizeof(ei->i_data) - 1);
  3457. } else {
  3458. inode->i_op = &ext4_symlink_inode_operations;
  3459. ext4_set_aops(inode);
  3460. }
  3461. } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  3462. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  3463. inode->i_op = &ext4_special_inode_operations;
  3464. if (raw_inode->i_block[0])
  3465. init_special_inode(inode, inode->i_mode,
  3466. old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
  3467. else
  3468. init_special_inode(inode, inode->i_mode,
  3469. new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
  3470. } else {
  3471. ret = -EIO;
  3472. EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode);
  3473. goto bad_inode;
  3474. }
  3475. brelse(iloc.bh);
  3476. ext4_set_inode_flags(inode);
  3477. unlock_new_inode(inode);
  3478. return inode;
  3479. bad_inode:
  3480. brelse(iloc.bh);
  3481. iget_failed(inode);
  3482. return ERR_PTR(ret);
  3483. }
  3484. static int ext4_inode_blocks_set(handle_t *handle,
  3485. struct ext4_inode *raw_inode,
  3486. struct ext4_inode_info *ei)
  3487. {
  3488. struct inode *inode = &(ei->vfs_inode);
  3489. u64 i_blocks = inode->i_blocks;
  3490. struct super_block *sb = inode->i_sb;
  3491. if (i_blocks <= ~0U) {
  3492. /*
  3493. * i_blocks can be represented in a 32 bit variable
  3494. * as multiple of 512 bytes
  3495. */
  3496. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  3497. raw_inode->i_blocks_high = 0;
  3498. ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
  3499. return 0;
  3500. }
  3501. if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
  3502. return -EFBIG;
  3503. if (i_blocks <= 0xffffffffffffULL) {
  3504. /*
  3505. * i_blocks can be represented in a 48 bit variable
  3506. * as multiple of 512 bytes
  3507. */
  3508. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  3509. raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
  3510. ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
  3511. } else {
  3512. ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
  3513. /* i_block is stored in file system block size */
  3514. i_blocks = i_blocks >> (inode->i_blkbits - 9);
  3515. raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
  3516. raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
  3517. }
  3518. return 0;
  3519. }
  3520. /*
  3521. * Post the struct inode info into an on-disk inode location in the
  3522. * buffer-cache. This gobbles the caller's reference to the
  3523. * buffer_head in the inode location struct.
  3524. *
  3525. * The caller must have write access to iloc->bh.
  3526. */
  3527. static int ext4_do_update_inode(handle_t *handle,
  3528. struct inode *inode,
  3529. struct ext4_iloc *iloc)
  3530. {
  3531. struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
  3532. struct ext4_inode_info *ei = EXT4_I(inode);
  3533. struct buffer_head *bh = iloc->bh;
  3534. int err = 0, rc, block;
  3535. int need_datasync = 0;
  3536. uid_t i_uid;
  3537. gid_t i_gid;
  3538. /* For fields not not tracking in the in-memory inode,
  3539. * initialise them to zero for new inodes. */
  3540. if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
  3541. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  3542. ext4_get_inode_flags(ei);
  3543. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  3544. i_uid = i_uid_read(inode);
  3545. i_gid = i_gid_read(inode);
  3546. if (!(test_opt(inode->i_sb, NO_UID32))) {
  3547. raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid));
  3548. raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid));
  3549. /*
  3550. * Fix up interoperability with old kernels. Otherwise, old inodes get
  3551. * re-used with the upper 16 bits of the uid/gid intact
  3552. */
  3553. if (!ei->i_dtime) {
  3554. raw_inode->i_uid_high =
  3555. cpu_to_le16(high_16_bits(i_uid));
  3556. raw_inode->i_gid_high =
  3557. cpu_to_le16(high_16_bits(i_gid));
  3558. } else {
  3559. raw_inode->i_uid_high = 0;
  3560. raw_inode->i_gid_high = 0;
  3561. }
  3562. } else {
  3563. raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid));
  3564. raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid));
  3565. raw_inode->i_uid_high = 0;
  3566. raw_inode->i_gid_high = 0;
  3567. }
  3568. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  3569. EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
  3570. EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
  3571. EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
  3572. EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
  3573. if (ext4_inode_blocks_set(handle, raw_inode, ei))
  3574. goto out_brelse;
  3575. raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
  3576. raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
  3577. if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
  3578. cpu_to_le32(EXT4_OS_HURD))
  3579. raw_inode->i_file_acl_high =
  3580. cpu_to_le16(ei->i_file_acl >> 32);
  3581. raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
  3582. if (ei->i_disksize != ext4_isize(raw_inode)) {
  3583. ext4_isize_set(raw_inode, ei->i_disksize);
  3584. need_datasync = 1;
  3585. }
  3586. if (ei->i_disksize > 0x7fffffffULL) {
  3587. struct super_block *sb = inode->i_sb;
  3588. if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
  3589. EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
  3590. EXT4_SB(sb)->s_es->s_rev_level ==
  3591. cpu_to_le32(EXT4_GOOD_OLD_REV)) {
  3592. /* If this is the first large file
  3593. * created, add a flag to the superblock.
  3594. */
  3595. err = ext4_journal_get_write_access(handle,
  3596. EXT4_SB(sb)->s_sbh);
  3597. if (err)
  3598. goto out_brelse;
  3599. ext4_update_dynamic_rev(sb);
  3600. EXT4_SET_RO_COMPAT_FEATURE(sb,
  3601. EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
  3602. ext4_handle_sync(handle);
  3603. err = ext4_handle_dirty_super(handle, sb);
  3604. }
  3605. }
  3606. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  3607. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  3608. if (old_valid_dev(inode->i_rdev)) {
  3609. raw_inode->i_block[0] =
  3610. cpu_to_le32(old_encode_dev(inode->i_rdev));
  3611. raw_inode->i_block[1] = 0;
  3612. } else {
  3613. raw_inode->i_block[0] = 0;
  3614. raw_inode->i_block[1] =
  3615. cpu_to_le32(new_encode_dev(inode->i_rdev));
  3616. raw_inode->i_block[2] = 0;
  3617. }
  3618. } else if (!ext4_has_inline_data(inode)) {
  3619. for (block = 0; block < EXT4_N_BLOCKS; block++)
  3620. raw_inode->i_block[block] = ei->i_data[block];
  3621. }
  3622. raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
  3623. if (ei->i_extra_isize) {
  3624. if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
  3625. raw_inode->i_version_hi =
  3626. cpu_to_le32(inode->i_version >> 32);
  3627. raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
  3628. }
  3629. ext4_inode_csum_set(inode, raw_inode, ei);
  3630. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  3631. rc = ext4_handle_dirty_metadata(handle, NULL, bh);
  3632. if (!err)
  3633. err = rc;
  3634. ext4_clear_inode_state(inode, EXT4_STATE_NEW);
  3635. ext4_update_inode_fsync_trans(handle, inode, need_datasync);
  3636. out_brelse:
  3637. brelse(bh);
  3638. ext4_std_error(inode->i_sb, err);
  3639. return err;
  3640. }
  3641. /*
  3642. * ext4_write_inode()
  3643. *
  3644. * We are called from a few places:
  3645. *
  3646. * - Within generic_file_write() for O_SYNC files.
  3647. * Here, there will be no transaction running. We wait for any running
  3648. * transaction to commit.
  3649. *
  3650. * - Within sys_sync(), kupdate and such.
  3651. * We wait on commit, if tol to.
  3652. *
  3653. * - Within prune_icache() (PF_MEMALLOC == true)
  3654. * Here we simply return. We can't afford to block kswapd on the
  3655. * journal commit.
  3656. *
  3657. * In all cases it is actually safe for us to return without doing anything,
  3658. * because the inode has been copied into a raw inode buffer in
  3659. * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
  3660. * knfsd.
  3661. *
  3662. * Note that we are absolutely dependent upon all inode dirtiers doing the
  3663. * right thing: they *must* call mark_inode_dirty() after dirtying info in
  3664. * which we are interested.
  3665. *
  3666. * It would be a bug for them to not do this. The code:
  3667. *
  3668. * mark_inode_dirty(inode)
  3669. * stuff();
  3670. * inode->i_size = expr;
  3671. *
  3672. * is in error because a kswapd-driven write_inode() could occur while
  3673. * `stuff()' is running, and the new i_size will be lost. Plus the inode
  3674. * will no longer be on the superblock's dirty inode list.
  3675. */
  3676. int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
  3677. {
  3678. int err;
  3679. if (current->flags & PF_MEMALLOC)
  3680. return 0;
  3681. if (EXT4_SB(inode->i_sb)->s_journal) {
  3682. if (ext4_journal_current_handle()) {
  3683. jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
  3684. dump_stack();
  3685. return -EIO;
  3686. }
  3687. if (wbc->sync_mode != WB_SYNC_ALL)
  3688. return 0;
  3689. err = ext4_force_commit(inode->i_sb);
  3690. } else {
  3691. struct ext4_iloc iloc;
  3692. err = __ext4_get_inode_loc(inode, &iloc, 0);
  3693. if (err)
  3694. return err;
  3695. if (wbc->sync_mode == WB_SYNC_ALL)
  3696. sync_dirty_buffer(iloc.bh);
  3697. if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
  3698. EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr,
  3699. "IO error syncing inode");
  3700. err = -EIO;
  3701. }
  3702. brelse(iloc.bh);
  3703. }
  3704. return err;
  3705. }
  3706. /*
  3707. * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
  3708. * buffers that are attached to a page stradding i_size and are undergoing
  3709. * commit. In that case we have to wait for commit to finish and try again.
  3710. */
  3711. static void ext4_wait_for_tail_page_commit(struct inode *inode)
  3712. {
  3713. struct page *page;
  3714. unsigned offset;
  3715. journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
  3716. tid_t commit_tid = 0;
  3717. int ret;
  3718. offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
  3719. /*
  3720. * All buffers in the last page remain valid? Then there's nothing to
  3721. * do. We do the check mainly to optimize the common PAGE_CACHE_SIZE ==
  3722. * blocksize case
  3723. */
  3724. if (offset > PAGE_CACHE_SIZE - (1 << inode->i_blkbits))
  3725. return;
  3726. while (1) {
  3727. page = find_lock_page(inode->i_mapping,
  3728. inode->i_size >> PAGE_CACHE_SHIFT);
  3729. if (!page)
  3730. return;
  3731. ret = __ext4_journalled_invalidatepage(page, offset);
  3732. unlock_page(page);
  3733. page_cache_release(page);
  3734. if (ret != -EBUSY)
  3735. return;
  3736. commit_tid = 0;
  3737. read_lock(&journal->j_state_lock);
  3738. if (journal->j_committing_transaction)
  3739. commit_tid = journal->j_committing_transaction->t_tid;
  3740. read_unlock(&journal->j_state_lock);
  3741. if (commit_tid)
  3742. jbd2_log_wait_commit(journal, commit_tid);
  3743. }
  3744. }
  3745. /*
  3746. * ext4_setattr()
  3747. *
  3748. * Called from notify_change.
  3749. *
  3750. * We want to trap VFS attempts to truncate the file as soon as
  3751. * possible. In particular, we want to make sure that when the VFS
  3752. * shrinks i_size, we put the inode on the orphan list and modify
  3753. * i_disksize immediately, so that during the subsequent flushing of
  3754. * dirty pages and freeing of disk blocks, we can guarantee that any
  3755. * commit will leave the blocks being flushed in an unused state on
  3756. * disk. (On recovery, the inode will get truncated and the blocks will
  3757. * be freed, so we have a strong guarantee that no future commit will
  3758. * leave these blocks visible to the user.)
  3759. *
  3760. * Another thing we have to assure is that if we are in ordered mode
  3761. * and inode is still attached to the committing transaction, we must
  3762. * we start writeout of all the dirty pages which are being truncated.
  3763. * This way we are sure that all the data written in the previous
  3764. * transaction are already on disk (truncate waits for pages under
  3765. * writeback).
  3766. *
  3767. * Called with inode->i_mutex down.
  3768. */
  3769. int ext4_setattr(struct dentry *dentry, struct iattr *attr)
  3770. {
  3771. struct inode *inode = dentry->d_inode;
  3772. int error, rc = 0;
  3773. int orphan = 0;
  3774. const unsigned int ia_valid = attr->ia_valid;
  3775. error = inode_change_ok(inode, attr);
  3776. if (error)
  3777. return error;
  3778. if (is_quota_modification(inode, attr))
  3779. dquot_initialize(inode);
  3780. if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
  3781. (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
  3782. handle_t *handle;
  3783. /* (user+group)*(old+new) structure, inode write (sb,
  3784. * inode block, ? - but truncate inode update has it) */
  3785. handle = ext4_journal_start(inode, (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)+
  3786. EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb))+3);
  3787. if (IS_ERR(handle)) {
  3788. error = PTR_ERR(handle);
  3789. goto err_out;
  3790. }
  3791. error = dquot_transfer(inode, attr);
  3792. if (error) {
  3793. ext4_journal_stop(handle);
  3794. return error;
  3795. }
  3796. /* Update corresponding info in inode so that everything is in
  3797. * one transaction */
  3798. if (attr->ia_valid & ATTR_UID)
  3799. inode->i_uid = attr->ia_uid;
  3800. if (attr->ia_valid & ATTR_GID)
  3801. inode->i_gid = attr->ia_gid;
  3802. error = ext4_mark_inode_dirty(handle, inode);
  3803. ext4_journal_stop(handle);
  3804. }
  3805. if (attr->ia_valid & ATTR_SIZE) {
  3806. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
  3807. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  3808. if (attr->ia_size > sbi->s_bitmap_maxbytes)
  3809. return -EFBIG;
  3810. }
  3811. }
  3812. if (S_ISREG(inode->i_mode) &&
  3813. attr->ia_valid & ATTR_SIZE &&
  3814. (attr->ia_size < inode->i_size)) {
  3815. handle_t *handle;
  3816. handle = ext4_journal_start(inode, 3);
  3817. if (IS_ERR(handle)) {
  3818. error = PTR_ERR(handle);
  3819. goto err_out;
  3820. }
  3821. if (ext4_handle_valid(handle)) {
  3822. error = ext4_orphan_add(handle, inode);
  3823. orphan = 1;
  3824. }
  3825. EXT4_I(inode)->i_disksize = attr->ia_size;
  3826. rc = ext4_mark_inode_dirty(handle, inode);
  3827. if (!error)
  3828. error = rc;
  3829. ext4_journal_stop(handle);
  3830. if (ext4_should_order_data(inode)) {
  3831. error = ext4_begin_ordered_truncate(inode,
  3832. attr->ia_size);
  3833. if (error) {
  3834. /* Do as much error cleanup as possible */
  3835. handle = ext4_journal_start(inode, 3);
  3836. if (IS_ERR(handle)) {
  3837. ext4_orphan_del(NULL, inode);
  3838. goto err_out;
  3839. }
  3840. ext4_orphan_del(handle, inode);
  3841. orphan = 0;
  3842. ext4_journal_stop(handle);
  3843. goto err_out;
  3844. }
  3845. }
  3846. }
  3847. if (attr->ia_valid & ATTR_SIZE) {
  3848. if (attr->ia_size != inode->i_size) {
  3849. loff_t oldsize = inode->i_size;
  3850. i_size_write(inode, attr->ia_size);
  3851. /*
  3852. * Blocks are going to be removed from the inode. Wait
  3853. * for dio in flight. Temporarily disable
  3854. * dioread_nolock to prevent livelock.
  3855. */
  3856. if (orphan) {
  3857. if (!ext4_should_journal_data(inode)) {
  3858. ext4_inode_block_unlocked_dio(inode);
  3859. inode_dio_wait(inode);
  3860. ext4_inode_resume_unlocked_dio(inode);
  3861. } else
  3862. ext4_wait_for_tail_page_commit(inode);
  3863. }
  3864. /*
  3865. * Truncate pagecache after we've waited for commit
  3866. * in data=journal mode to make pages freeable.
  3867. */
  3868. truncate_pagecache(inode, oldsize, inode->i_size);
  3869. }
  3870. ext4_truncate(inode);
  3871. }
  3872. if (!rc) {
  3873. setattr_copy(inode, attr);
  3874. mark_inode_dirty(inode);
  3875. }
  3876. /*
  3877. * If the call to ext4_truncate failed to get a transaction handle at
  3878. * all, we need to clean up the in-core orphan list manually.
  3879. */
  3880. if (orphan && inode->i_nlink)
  3881. ext4_orphan_del(NULL, inode);
  3882. if (!rc && (ia_valid & ATTR_MODE))
  3883. rc = ext4_acl_chmod(inode);
  3884. err_out:
  3885. ext4_std_error(inode->i_sb, error);
  3886. if (!error)
  3887. error = rc;
  3888. return error;
  3889. }
  3890. int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
  3891. struct kstat *stat)
  3892. {
  3893. struct inode *inode;
  3894. unsigned long delalloc_blocks;
  3895. inode = dentry->d_inode;
  3896. generic_fillattr(inode, stat);
  3897. /*
  3898. * We can't update i_blocks if the block allocation is delayed
  3899. * otherwise in the case of system crash before the real block
  3900. * allocation is done, we will have i_blocks inconsistent with
  3901. * on-disk file blocks.
  3902. * We always keep i_blocks updated together with real
  3903. * allocation. But to not confuse with user, stat
  3904. * will return the blocks that include the delayed allocation
  3905. * blocks for this file.
  3906. */
  3907. delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
  3908. EXT4_I(inode)->i_reserved_data_blocks);
  3909. stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9;
  3910. return 0;
  3911. }
  3912. static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
  3913. {
  3914. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  3915. return ext4_ind_trans_blocks(inode, nrblocks, chunk);
  3916. return ext4_ext_index_trans_blocks(inode, nrblocks, chunk);
  3917. }
  3918. /*
  3919. * Account for index blocks, block groups bitmaps and block group
  3920. * descriptor blocks if modify datablocks and index blocks
  3921. * worse case, the indexs blocks spread over different block groups
  3922. *
  3923. * If datablocks are discontiguous, they are possible to spread over
  3924. * different block groups too. If they are contiguous, with flexbg,
  3925. * they could still across block group boundary.
  3926. *
  3927. * Also account for superblock, inode, quota and xattr blocks
  3928. */
  3929. static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
  3930. {
  3931. ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
  3932. int gdpblocks;
  3933. int idxblocks;
  3934. int ret = 0;
  3935. /*
  3936. * How many index blocks need to touch to modify nrblocks?
  3937. * The "Chunk" flag indicating whether the nrblocks is
  3938. * physically contiguous on disk
  3939. *
  3940. * For Direct IO and fallocate, they calls get_block to allocate
  3941. * one single extent at a time, so they could set the "Chunk" flag
  3942. */
  3943. idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
  3944. ret = idxblocks;
  3945. /*
  3946. * Now let's see how many group bitmaps and group descriptors need
  3947. * to account
  3948. */
  3949. groups = idxblocks;
  3950. if (chunk)
  3951. groups += 1;
  3952. else
  3953. groups += nrblocks;
  3954. gdpblocks = groups;
  3955. if (groups > ngroups)
  3956. groups = ngroups;
  3957. if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
  3958. gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
  3959. /* bitmaps and block group descriptor blocks */
  3960. ret += groups + gdpblocks;
  3961. /* Blocks for super block, inode, quota and xattr blocks */
  3962. ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
  3963. return ret;
  3964. }
  3965. /*
  3966. * Calculate the total number of credits to reserve to fit
  3967. * the modification of a single pages into a single transaction,
  3968. * which may include multiple chunks of block allocations.
  3969. *
  3970. * This could be called via ext4_write_begin()
  3971. *
  3972. * We need to consider the worse case, when
  3973. * one new block per extent.
  3974. */
  3975. int ext4_writepage_trans_blocks(struct inode *inode)
  3976. {
  3977. int bpp = ext4_journal_blocks_per_page(inode);
  3978. int ret;
  3979. ret = ext4_meta_trans_blocks(inode, bpp, 0);
  3980. /* Account for data blocks for journalled mode */
  3981. if (ext4_should_journal_data(inode))
  3982. ret += bpp;
  3983. return ret;
  3984. }
  3985. /*
  3986. * Calculate the journal credits for a chunk of data modification.
  3987. *
  3988. * This is called from DIO, fallocate or whoever calling
  3989. * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
  3990. *
  3991. * journal buffers for data blocks are not included here, as DIO
  3992. * and fallocate do no need to journal data buffers.
  3993. */
  3994. int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
  3995. {
  3996. return ext4_meta_trans_blocks(inode, nrblocks, 1);
  3997. }
  3998. /*
  3999. * The caller must have previously called ext4_reserve_inode_write().
  4000. * Give this, we know that the caller already has write access to iloc->bh.
  4001. */
  4002. int ext4_mark_iloc_dirty(handle_t *handle,
  4003. struct inode *inode, struct ext4_iloc *iloc)
  4004. {
  4005. int err = 0;
  4006. if (IS_I_VERSION(inode))
  4007. inode_inc_iversion(inode);
  4008. /* the do_update_inode consumes one bh->b_count */
  4009. get_bh(iloc->bh);
  4010. /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
  4011. err = ext4_do_update_inode(handle, inode, iloc);
  4012. put_bh(iloc->bh);
  4013. return err;
  4014. }
  4015. /*
  4016. * On success, We end up with an outstanding reference count against
  4017. * iloc->bh. This _must_ be cleaned up later.
  4018. */
  4019. int
  4020. ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
  4021. struct ext4_iloc *iloc)
  4022. {
  4023. int err;
  4024. err = ext4_get_inode_loc(inode, iloc);
  4025. if (!err) {
  4026. BUFFER_TRACE(iloc->bh, "get_write_access");
  4027. err = ext4_journal_get_write_access(handle, iloc->bh);
  4028. if (err) {
  4029. brelse(iloc->bh);
  4030. iloc->bh = NULL;
  4031. }
  4032. }
  4033. ext4_std_error(inode->i_sb, err);
  4034. return err;
  4035. }
  4036. /*
  4037. * Expand an inode by new_extra_isize bytes.
  4038. * Returns 0 on success or negative error number on failure.
  4039. */
  4040. static int ext4_expand_extra_isize(struct inode *inode,
  4041. unsigned int new_extra_isize,
  4042. struct ext4_iloc iloc,
  4043. handle_t *handle)
  4044. {
  4045. struct ext4_inode *raw_inode;
  4046. struct ext4_xattr_ibody_header *header;
  4047. if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
  4048. return 0;
  4049. raw_inode = ext4_raw_inode(&iloc);
  4050. header = IHDR(inode, raw_inode);
  4051. /* No extended attributes present */
  4052. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
  4053. header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
  4054. memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
  4055. new_extra_isize);
  4056. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  4057. return 0;
  4058. }
  4059. /* try to expand with EAs present */
  4060. return ext4_expand_extra_isize_ea(inode, new_extra_isize,
  4061. raw_inode, handle);
  4062. }
  4063. /*
  4064. * What we do here is to mark the in-core inode as clean with respect to inode
  4065. * dirtiness (it may still be data-dirty).
  4066. * This means that the in-core inode may be reaped by prune_icache
  4067. * without having to perform any I/O. This is a very good thing,
  4068. * because *any* task may call prune_icache - even ones which
  4069. * have a transaction open against a different journal.
  4070. *
  4071. * Is this cheating? Not really. Sure, we haven't written the
  4072. * inode out, but prune_icache isn't a user-visible syncing function.
  4073. * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
  4074. * we start and wait on commits.
  4075. */
  4076. int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
  4077. {
  4078. struct ext4_iloc iloc;
  4079. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  4080. static unsigned int mnt_count;
  4081. int err, ret;
  4082. might_sleep();
  4083. trace_ext4_mark_inode_dirty(inode, _RET_IP_);
  4084. err = ext4_reserve_inode_write(handle, inode, &iloc);
  4085. if (ext4_handle_valid(handle) &&
  4086. EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
  4087. !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
  4088. /*
  4089. * We need extra buffer credits since we may write into EA block
  4090. * with this same handle. If journal_extend fails, then it will
  4091. * only result in a minor loss of functionality for that inode.
  4092. * If this is felt to be critical, then e2fsck should be run to
  4093. * force a large enough s_min_extra_isize.
  4094. */
  4095. if ((jbd2_journal_extend(handle,
  4096. EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
  4097. ret = ext4_expand_extra_isize(inode,
  4098. sbi->s_want_extra_isize,
  4099. iloc, handle);
  4100. if (ret) {
  4101. ext4_set_inode_state(inode,
  4102. EXT4_STATE_NO_EXPAND);
  4103. if (mnt_count !=
  4104. le16_to_cpu(sbi->s_es->s_mnt_count)) {
  4105. ext4_warning(inode->i_sb,
  4106. "Unable to expand inode %lu. Delete"
  4107. " some EAs or run e2fsck.",
  4108. inode->i_ino);
  4109. mnt_count =
  4110. le16_to_cpu(sbi->s_es->s_mnt_count);
  4111. }
  4112. }
  4113. }
  4114. }
  4115. if (!err)
  4116. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  4117. return err;
  4118. }
  4119. /*
  4120. * ext4_dirty_inode() is called from __mark_inode_dirty()
  4121. *
  4122. * We're really interested in the case where a file is being extended.
  4123. * i_size has been changed by generic_commit_write() and we thus need
  4124. * to include the updated inode in the current transaction.
  4125. *
  4126. * Also, dquot_alloc_block() will always dirty the inode when blocks
  4127. * are allocated to the file.
  4128. *
  4129. * If the inode is marked synchronous, we don't honour that here - doing
  4130. * so would cause a commit on atime updates, which we don't bother doing.
  4131. * We handle synchronous inodes at the highest possible level.
  4132. */
  4133. void ext4_dirty_inode(struct inode *inode, int flags)
  4134. {
  4135. handle_t *handle;
  4136. handle = ext4_journal_start(inode, 2);
  4137. if (IS_ERR(handle))
  4138. goto out;
  4139. ext4_mark_inode_dirty(handle, inode);
  4140. ext4_journal_stop(handle);
  4141. out:
  4142. return;
  4143. }
  4144. #if 0
  4145. /*
  4146. * Bind an inode's backing buffer_head into this transaction, to prevent
  4147. * it from being flushed to disk early. Unlike
  4148. * ext4_reserve_inode_write, this leaves behind no bh reference and
  4149. * returns no iloc structure, so the caller needs to repeat the iloc
  4150. * lookup to mark the inode dirty later.
  4151. */
  4152. static int ext4_pin_inode(handle_t *handle, struct inode *inode)
  4153. {
  4154. struct ext4_iloc iloc;
  4155. int err = 0;
  4156. if (handle) {
  4157. err = ext4_get_inode_loc(inode, &iloc);
  4158. if (!err) {
  4159. BUFFER_TRACE(iloc.bh, "get_write_access");
  4160. err = jbd2_journal_get_write_access(handle, iloc.bh);
  4161. if (!err)
  4162. err = ext4_handle_dirty_metadata(handle,
  4163. NULL,
  4164. iloc.bh);
  4165. brelse(iloc.bh);
  4166. }
  4167. }
  4168. ext4_std_error(inode->i_sb, err);
  4169. return err;
  4170. }
  4171. #endif
  4172. int ext4_change_inode_journal_flag(struct inode *inode, int val)
  4173. {
  4174. journal_t *journal;
  4175. handle_t *handle;
  4176. int err;
  4177. /*
  4178. * We have to be very careful here: changing a data block's
  4179. * journaling status dynamically is dangerous. If we write a
  4180. * data block to the journal, change the status and then delete
  4181. * that block, we risk forgetting to revoke the old log record
  4182. * from the journal and so a subsequent replay can corrupt data.
  4183. * So, first we make sure that the journal is empty and that
  4184. * nobody is changing anything.
  4185. */
  4186. journal = EXT4_JOURNAL(inode);
  4187. if (!journal)
  4188. return 0;
  4189. if (is_journal_aborted(journal))
  4190. return -EROFS;
  4191. /* We have to allocate physical blocks for delalloc blocks
  4192. * before flushing journal. otherwise delalloc blocks can not
  4193. * be allocated any more. even more truncate on delalloc blocks
  4194. * could trigger BUG by flushing delalloc blocks in journal.
  4195. * There is no delalloc block in non-journal data mode.
  4196. */
  4197. if (val && test_opt(inode->i_sb, DELALLOC)) {
  4198. err = ext4_alloc_da_blocks(inode);
  4199. if (err < 0)
  4200. return err;
  4201. }
  4202. /* Wait for all existing dio workers */
  4203. ext4_inode_block_unlocked_dio(inode);
  4204. inode_dio_wait(inode);
  4205. jbd2_journal_lock_updates(journal);
  4206. /*
  4207. * OK, there are no updates running now, and all cached data is
  4208. * synced to disk. We are now in a completely consistent state
  4209. * which doesn't have anything in the journal, and we know that
  4210. * no filesystem updates are running, so it is safe to modify
  4211. * the inode's in-core data-journaling state flag now.
  4212. */
  4213. if (val)
  4214. ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
  4215. else {
  4216. jbd2_journal_flush(journal);
  4217. ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
  4218. }
  4219. ext4_set_aops(inode);
  4220. jbd2_journal_unlock_updates(journal);
  4221. ext4_inode_resume_unlocked_dio(inode);
  4222. /* Finally we can mark the inode as dirty. */
  4223. handle = ext4_journal_start(inode, 1);
  4224. if (IS_ERR(handle))
  4225. return PTR_ERR(handle);
  4226. err = ext4_mark_inode_dirty(handle, inode);
  4227. ext4_handle_sync(handle);
  4228. ext4_journal_stop(handle);
  4229. ext4_std_error(inode->i_sb, err);
  4230. return err;
  4231. }
  4232. static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
  4233. {
  4234. return !buffer_mapped(bh);
  4235. }
  4236. int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  4237. {
  4238. struct page *page = vmf->page;
  4239. loff_t size;
  4240. unsigned long len;
  4241. int ret;
  4242. struct file *file = vma->vm_file;
  4243. struct inode *inode = file->f_path.dentry->d_inode;
  4244. struct address_space *mapping = inode->i_mapping;
  4245. handle_t *handle;
  4246. get_block_t *get_block;
  4247. int retries = 0;
  4248. sb_start_pagefault(inode->i_sb);
  4249. file_update_time(vma->vm_file);
  4250. /* Delalloc case is easy... */
  4251. if (test_opt(inode->i_sb, DELALLOC) &&
  4252. !ext4_should_journal_data(inode) &&
  4253. !ext4_nonda_switch(inode->i_sb)) {
  4254. do {
  4255. ret = __block_page_mkwrite(vma, vmf,
  4256. ext4_da_get_block_prep);
  4257. } while (ret == -ENOSPC &&
  4258. ext4_should_retry_alloc(inode->i_sb, &retries));
  4259. goto out_ret;
  4260. }
  4261. lock_page(page);
  4262. size = i_size_read(inode);
  4263. /* Page got truncated from under us? */
  4264. if (page->mapping != mapping || page_offset(page) > size) {
  4265. unlock_page(page);
  4266. ret = VM_FAULT_NOPAGE;
  4267. goto out;
  4268. }
  4269. if (page->index == size >> PAGE_CACHE_SHIFT)
  4270. len = size & ~PAGE_CACHE_MASK;
  4271. else
  4272. len = PAGE_CACHE_SIZE;
  4273. /*
  4274. * Return if we have all the buffers mapped. This avoids the need to do
  4275. * journal_start/journal_stop which can block and take a long time
  4276. */
  4277. if (page_has_buffers(page)) {
  4278. if (!ext4_walk_page_buffers(NULL, page_buffers(page),
  4279. 0, len, NULL,
  4280. ext4_bh_unmapped)) {
  4281. /* Wait so that we don't change page under IO */
  4282. wait_on_page_writeback(page);
  4283. ret = VM_FAULT_LOCKED;
  4284. goto out;
  4285. }
  4286. }
  4287. unlock_page(page);
  4288. /* OK, we need to fill the hole... */
  4289. if (ext4_should_dioread_nolock(inode))
  4290. get_block = ext4_get_block_write;
  4291. else
  4292. get_block = ext4_get_block;
  4293. retry_alloc:
  4294. handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
  4295. if (IS_ERR(handle)) {
  4296. ret = VM_FAULT_SIGBUS;
  4297. goto out;
  4298. }
  4299. ret = __block_page_mkwrite(vma, vmf, get_block);
  4300. if (!ret && ext4_should_journal_data(inode)) {
  4301. if (ext4_walk_page_buffers(handle, page_buffers(page), 0,
  4302. PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) {
  4303. unlock_page(page);
  4304. ret = VM_FAULT_SIGBUS;
  4305. ext4_journal_stop(handle);
  4306. goto out;
  4307. }
  4308. ext4_set_inode_state(inode, EXT4_STATE_JDATA);
  4309. }
  4310. ext4_journal_stop(handle);
  4311. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  4312. goto retry_alloc;
  4313. out_ret:
  4314. ret = block_page_mkwrite_return(ret);
  4315. out:
  4316. sb_end_pagefault(inode->i_sb);
  4317. return ret;
  4318. }