inode.c 142 KB

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