inode.c 150 KB

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