inode.c 145 KB

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