inode.c 151 KB

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