inode.c 142 KB

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