inode.c 149 KB

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