inode.c 146 KB

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