inode.c 138 KB

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