inode.c 135 KB

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