inode.c 150 KB

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