inode.c 151 KB

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