inode.c 139 KB

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