inode.c 150 KB

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