inode.c 150 KB

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