inode.c 150 KB

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