inode.c 148 KB

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