inode.c 139 KB

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