inode.c 150 KB

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