inode.c 146 KB

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