inode.c 152 KB

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