inode.c 146 KB

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