inode.c 140 KB

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