inode.c 154 KB

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