inode.c 140 KB

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