inode.c 126 KB

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