inode.c 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522
  1. /*
  2. * linux/fs/ext3/inode.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/inode.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * Goal-directed block allocation by Stephen Tweedie
  16. * (sct@redhat.com), 1993, 1998
  17. * Big-endian to little-endian byte-swapping/bitmaps by
  18. * David S. Miller (davem@caip.rutgers.edu), 1995
  19. * 64-bit file support on 64-bit platforms by Jakub Jelinek
  20. * (jj@sunsite.ms.mff.cuni.cz)
  21. *
  22. * Assorted race fixes, rewrite of ext3_get_block() by Al Viro, 2000
  23. */
  24. #include <linux/module.h>
  25. #include <linux/fs.h>
  26. #include <linux/time.h>
  27. #include <linux/ext3_jbd.h>
  28. #include <linux/jbd.h>
  29. #include <linux/highuid.h>
  30. #include <linux/pagemap.h>
  31. #include <linux/quotaops.h>
  32. #include <linux/string.h>
  33. #include <linux/buffer_head.h>
  34. #include <linux/writeback.h>
  35. #include <linux/mpage.h>
  36. #include <linux/uio.h>
  37. #include <linux/bio.h>
  38. #include <linux/fiemap.h>
  39. #include <linux/namei.h>
  40. #include <trace/events/ext3.h>
  41. #include "xattr.h"
  42. #include "acl.h"
  43. static int ext3_writepage_trans_blocks(struct inode *inode);
  44. /*
  45. * Test whether an inode is a fast symlink.
  46. */
  47. static int ext3_inode_is_fast_symlink(struct inode *inode)
  48. {
  49. int ea_blocks = EXT3_I(inode)->i_file_acl ?
  50. (inode->i_sb->s_blocksize >> 9) : 0;
  51. return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
  52. }
  53. /*
  54. * The ext3 forget function must perform a revoke if we are freeing data
  55. * which has been journaled. Metadata (eg. indirect blocks) must be
  56. * revoked in all cases.
  57. *
  58. * "bh" may be NULL: a metadata block may have been freed from memory
  59. * but there may still be a record of it in the journal, and that record
  60. * still needs to be revoked.
  61. */
  62. int ext3_forget(handle_t *handle, int is_metadata, struct inode *inode,
  63. struct buffer_head *bh, ext3_fsblk_t blocknr)
  64. {
  65. int err;
  66. might_sleep();
  67. trace_ext3_forget(inode, is_metadata, blocknr);
  68. BUFFER_TRACE(bh, "enter");
  69. jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
  70. "data mode %lx\n",
  71. bh, is_metadata, inode->i_mode,
  72. test_opt(inode->i_sb, DATA_FLAGS));
  73. /* Never use the revoke function if we are doing full data
  74. * journaling: there is no need to, and a V1 superblock won't
  75. * support it. Otherwise, only skip the revoke on un-journaled
  76. * data blocks. */
  77. if (test_opt(inode->i_sb, DATA_FLAGS) == EXT3_MOUNT_JOURNAL_DATA ||
  78. (!is_metadata && !ext3_should_journal_data(inode))) {
  79. if (bh) {
  80. BUFFER_TRACE(bh, "call journal_forget");
  81. return ext3_journal_forget(handle, bh);
  82. }
  83. return 0;
  84. }
  85. /*
  86. * data!=journal && (is_metadata || should_journal_data(inode))
  87. */
  88. BUFFER_TRACE(bh, "call ext3_journal_revoke");
  89. err = ext3_journal_revoke(handle, blocknr, bh);
  90. if (err)
  91. ext3_abort(inode->i_sb, __func__,
  92. "error %d when attempting revoke", err);
  93. BUFFER_TRACE(bh, "exit");
  94. return err;
  95. }
  96. /*
  97. * Work out how many blocks we need to proceed with the next chunk of a
  98. * truncate transaction.
  99. */
  100. static unsigned long blocks_for_truncate(struct inode *inode)
  101. {
  102. unsigned long needed;
  103. needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
  104. /* Give ourselves just enough room to cope with inodes in which
  105. * i_blocks is corrupt: we've seen disk corruptions in the past
  106. * which resulted in random data in an inode which looked enough
  107. * like a regular file for ext3 to try to delete it. Things
  108. * will go a bit crazy if that happens, but at least we should
  109. * try not to panic the whole kernel. */
  110. if (needed < 2)
  111. needed = 2;
  112. /* But we need to bound the transaction so we don't overflow the
  113. * journal. */
  114. if (needed > EXT3_MAX_TRANS_DATA)
  115. needed = EXT3_MAX_TRANS_DATA;
  116. return EXT3_DATA_TRANS_BLOCKS(inode->i_sb) + needed;
  117. }
  118. /*
  119. * Truncate transactions can be complex and absolutely huge. So we need to
  120. * be able to restart the transaction at a conventient checkpoint to make
  121. * sure we don't overflow the journal.
  122. *
  123. * start_transaction gets us a new handle for a truncate transaction,
  124. * and extend_transaction tries to extend the existing one a bit. If
  125. * extend fails, we need to propagate the failure up and restart the
  126. * transaction in the top-level truncate loop. --sct
  127. */
  128. static handle_t *start_transaction(struct inode *inode)
  129. {
  130. handle_t *result;
  131. result = ext3_journal_start(inode, blocks_for_truncate(inode));
  132. if (!IS_ERR(result))
  133. return result;
  134. ext3_std_error(inode->i_sb, PTR_ERR(result));
  135. return result;
  136. }
  137. /*
  138. * Try to extend this transaction for the purposes of truncation.
  139. *
  140. * Returns 0 if we managed to create more room. If we can't create more
  141. * room, and the transaction must be restarted we return 1.
  142. */
  143. static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
  144. {
  145. if (handle->h_buffer_credits > EXT3_RESERVE_TRANS_BLOCKS)
  146. return 0;
  147. if (!ext3_journal_extend(handle, blocks_for_truncate(inode)))
  148. return 0;
  149. return 1;
  150. }
  151. /*
  152. * Restart the transaction associated with *handle. This does a commit,
  153. * so before we call here everything must be consistently dirtied against
  154. * this transaction.
  155. */
  156. static int truncate_restart_transaction(handle_t *handle, struct inode *inode)
  157. {
  158. int ret;
  159. jbd_debug(2, "restarting handle %p\n", handle);
  160. /*
  161. * Drop truncate_mutex to avoid deadlock with ext3_get_blocks_handle
  162. * At this moment, get_block can be called only for blocks inside
  163. * i_size since page cache has been already dropped and writes are
  164. * blocked by i_mutex. So we can safely drop the truncate_mutex.
  165. */
  166. mutex_unlock(&EXT3_I(inode)->truncate_mutex);
  167. ret = ext3_journal_restart(handle, blocks_for_truncate(inode));
  168. mutex_lock(&EXT3_I(inode)->truncate_mutex);
  169. return ret;
  170. }
  171. /*
  172. * Called at inode eviction from icache
  173. */
  174. void ext3_evict_inode (struct inode *inode)
  175. {
  176. struct ext3_block_alloc_info *rsv;
  177. handle_t *handle;
  178. int want_delete = 0;
  179. trace_ext3_evict_inode(inode);
  180. if (!inode->i_nlink && !is_bad_inode(inode)) {
  181. dquot_initialize(inode);
  182. want_delete = 1;
  183. }
  184. truncate_inode_pages(&inode->i_data, 0);
  185. ext3_discard_reservation(inode);
  186. rsv = EXT3_I(inode)->i_block_alloc_info;
  187. EXT3_I(inode)->i_block_alloc_info = NULL;
  188. if (unlikely(rsv))
  189. kfree(rsv);
  190. if (!want_delete)
  191. goto no_delete;
  192. handle = start_transaction(inode);
  193. if (IS_ERR(handle)) {
  194. /*
  195. * If we're going to skip the normal cleanup, we still need to
  196. * make sure that the in-core orphan linked list is properly
  197. * cleaned up.
  198. */
  199. ext3_orphan_del(NULL, inode);
  200. goto no_delete;
  201. }
  202. if (IS_SYNC(inode))
  203. handle->h_sync = 1;
  204. inode->i_size = 0;
  205. if (inode->i_blocks)
  206. ext3_truncate(inode);
  207. /*
  208. * Kill off the orphan record created when the inode lost the last
  209. * link. Note that ext3_orphan_del() has to be able to cope with the
  210. * deletion of a non-existent orphan - ext3_truncate() could
  211. * have removed the record.
  212. */
  213. ext3_orphan_del(handle, inode);
  214. EXT3_I(inode)->i_dtime = get_seconds();
  215. /*
  216. * One subtle ordering requirement: if anything has gone wrong
  217. * (transaction abort, IO errors, whatever), then we can still
  218. * do these next steps (the fs will already have been marked as
  219. * having errors), but we can't free the inode if the mark_dirty
  220. * fails.
  221. */
  222. if (ext3_mark_inode_dirty(handle, inode)) {
  223. /* If that failed, just dquot_drop() and be done with that */
  224. dquot_drop(inode);
  225. end_writeback(inode);
  226. } else {
  227. ext3_xattr_delete_inode(handle, inode);
  228. dquot_free_inode(inode);
  229. dquot_drop(inode);
  230. end_writeback(inode);
  231. ext3_free_inode(handle, inode);
  232. }
  233. ext3_journal_stop(handle);
  234. return;
  235. no_delete:
  236. end_writeback(inode);
  237. dquot_drop(inode);
  238. }
  239. typedef struct {
  240. __le32 *p;
  241. __le32 key;
  242. struct buffer_head *bh;
  243. } Indirect;
  244. static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
  245. {
  246. p->key = *(p->p = v);
  247. p->bh = bh;
  248. }
  249. static int verify_chain(Indirect *from, Indirect *to)
  250. {
  251. while (from <= to && from->key == *from->p)
  252. from++;
  253. return (from > to);
  254. }
  255. /**
  256. * ext3_block_to_path - parse the block number into array of offsets
  257. * @inode: inode in question (we are only interested in its superblock)
  258. * @i_block: block number to be parsed
  259. * @offsets: array to store the offsets in
  260. * @boundary: set this non-zero if the referred-to block is likely to be
  261. * followed (on disk) by an indirect block.
  262. *
  263. * To store the locations of file's data ext3 uses a data structure common
  264. * for UNIX filesystems - tree of pointers anchored in the inode, with
  265. * data blocks at leaves and indirect blocks in intermediate nodes.
  266. * This function translates the block number into path in that tree -
  267. * return value is the path length and @offsets[n] is the offset of
  268. * pointer to (n+1)th node in the nth one. If @block is out of range
  269. * (negative or too large) warning is printed and zero returned.
  270. *
  271. * Note: function doesn't find node addresses, so no IO is needed. All
  272. * we need to know is the capacity of indirect blocks (taken from the
  273. * inode->i_sb).
  274. */
  275. /*
  276. * Portability note: the last comparison (check that we fit into triple
  277. * indirect block) is spelled differently, because otherwise on an
  278. * architecture with 32-bit longs and 8Kb pages we might get into trouble
  279. * if our filesystem had 8Kb blocks. We might use long long, but that would
  280. * kill us on x86. Oh, well, at least the sign propagation does not matter -
  281. * i_block would have to be negative in the very beginning, so we would not
  282. * get there at all.
  283. */
  284. static int ext3_block_to_path(struct inode *inode,
  285. long i_block, int offsets[4], int *boundary)
  286. {
  287. int ptrs = EXT3_ADDR_PER_BLOCK(inode->i_sb);
  288. int ptrs_bits = EXT3_ADDR_PER_BLOCK_BITS(inode->i_sb);
  289. const long direct_blocks = EXT3_NDIR_BLOCKS,
  290. indirect_blocks = ptrs,
  291. double_blocks = (1 << (ptrs_bits * 2));
  292. int n = 0;
  293. int final = 0;
  294. if (i_block < 0) {
  295. ext3_warning (inode->i_sb, "ext3_block_to_path", "block < 0");
  296. } else if (i_block < direct_blocks) {
  297. offsets[n++] = i_block;
  298. final = direct_blocks;
  299. } else if ( (i_block -= direct_blocks) < indirect_blocks) {
  300. offsets[n++] = EXT3_IND_BLOCK;
  301. offsets[n++] = i_block;
  302. final = ptrs;
  303. } else if ((i_block -= indirect_blocks) < double_blocks) {
  304. offsets[n++] = EXT3_DIND_BLOCK;
  305. offsets[n++] = i_block >> ptrs_bits;
  306. offsets[n++] = i_block & (ptrs - 1);
  307. final = ptrs;
  308. } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
  309. offsets[n++] = EXT3_TIND_BLOCK;
  310. offsets[n++] = i_block >> (ptrs_bits * 2);
  311. offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
  312. offsets[n++] = i_block & (ptrs - 1);
  313. final = ptrs;
  314. } else {
  315. ext3_warning(inode->i_sb, "ext3_block_to_path", "block > big");
  316. }
  317. if (boundary)
  318. *boundary = final - 1 - (i_block & (ptrs - 1));
  319. return n;
  320. }
  321. /**
  322. * ext3_get_branch - read the chain of indirect blocks leading to data
  323. * @inode: inode in question
  324. * @depth: depth of the chain (1 - direct pointer, etc.)
  325. * @offsets: offsets of pointers in inode/indirect blocks
  326. * @chain: place to store the result
  327. * @err: here we store the error value
  328. *
  329. * Function fills the array of triples <key, p, bh> and returns %NULL
  330. * if everything went OK or the pointer to the last filled triple
  331. * (incomplete one) otherwise. Upon the return chain[i].key contains
  332. * the number of (i+1)-th block in the chain (as it is stored in memory,
  333. * i.e. little-endian 32-bit), chain[i].p contains the address of that
  334. * number (it points into struct inode for i==0 and into the bh->b_data
  335. * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
  336. * block for i>0 and NULL for i==0. In other words, it holds the block
  337. * numbers of the chain, addresses they were taken from (and where we can
  338. * verify that chain did not change) and buffer_heads hosting these
  339. * numbers.
  340. *
  341. * Function stops when it stumbles upon zero pointer (absent block)
  342. * (pointer to last triple returned, *@err == 0)
  343. * or when it gets an IO error reading an indirect block
  344. * (ditto, *@err == -EIO)
  345. * or when it notices that chain had been changed while it was reading
  346. * (ditto, *@err == -EAGAIN)
  347. * or when it reads all @depth-1 indirect blocks successfully and finds
  348. * the whole chain, all way to the data (returns %NULL, *err == 0).
  349. */
  350. static Indirect *ext3_get_branch(struct inode *inode, int depth, int *offsets,
  351. Indirect chain[4], int *err)
  352. {
  353. struct super_block *sb = inode->i_sb;
  354. Indirect *p = chain;
  355. struct buffer_head *bh;
  356. *err = 0;
  357. /* i_data is not going away, no lock needed */
  358. add_chain (chain, NULL, EXT3_I(inode)->i_data + *offsets);
  359. if (!p->key)
  360. goto no_block;
  361. while (--depth) {
  362. bh = sb_bread(sb, le32_to_cpu(p->key));
  363. if (!bh)
  364. goto failure;
  365. /* Reader: pointers */
  366. if (!verify_chain(chain, p))
  367. goto changed;
  368. add_chain(++p, bh, (__le32*)bh->b_data + *++offsets);
  369. /* Reader: end */
  370. if (!p->key)
  371. goto no_block;
  372. }
  373. return NULL;
  374. changed:
  375. brelse(bh);
  376. *err = -EAGAIN;
  377. goto no_block;
  378. failure:
  379. *err = -EIO;
  380. no_block:
  381. return p;
  382. }
  383. /**
  384. * ext3_find_near - find a place for allocation with sufficient locality
  385. * @inode: owner
  386. * @ind: descriptor of indirect block.
  387. *
  388. * This function returns the preferred place for block allocation.
  389. * It is used when heuristic for sequential allocation fails.
  390. * Rules are:
  391. * + if there is a block to the left of our position - allocate near it.
  392. * + if pointer will live in indirect block - allocate near that block.
  393. * + if pointer will live in inode - allocate in the same
  394. * cylinder group.
  395. *
  396. * In the latter case we colour the starting block by the callers PID to
  397. * prevent it from clashing with concurrent allocations for a different inode
  398. * in the same block group. The PID is used here so that functionally related
  399. * files will be close-by on-disk.
  400. *
  401. * Caller must make sure that @ind is valid and will stay that way.
  402. */
  403. static ext3_fsblk_t ext3_find_near(struct inode *inode, Indirect *ind)
  404. {
  405. struct ext3_inode_info *ei = EXT3_I(inode);
  406. __le32 *start = ind->bh ? (__le32*) ind->bh->b_data : ei->i_data;
  407. __le32 *p;
  408. ext3_fsblk_t bg_start;
  409. ext3_grpblk_t colour;
  410. /* Try to find previous block */
  411. for (p = ind->p - 1; p >= start; p--) {
  412. if (*p)
  413. return le32_to_cpu(*p);
  414. }
  415. /* No such thing, so let's try location of indirect block */
  416. if (ind->bh)
  417. return ind->bh->b_blocknr;
  418. /*
  419. * It is going to be referred to from the inode itself? OK, just put it
  420. * into the same cylinder group then.
  421. */
  422. bg_start = ext3_group_first_block_no(inode->i_sb, ei->i_block_group);
  423. colour = (current->pid % 16) *
  424. (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
  425. return bg_start + colour;
  426. }
  427. /**
  428. * ext3_find_goal - find a preferred place for allocation.
  429. * @inode: owner
  430. * @block: block we want
  431. * @partial: pointer to the last triple within a chain
  432. *
  433. * Normally this function find the preferred place for block allocation,
  434. * returns it.
  435. */
  436. static ext3_fsblk_t ext3_find_goal(struct inode *inode, long block,
  437. Indirect *partial)
  438. {
  439. struct ext3_block_alloc_info *block_i;
  440. block_i = EXT3_I(inode)->i_block_alloc_info;
  441. /*
  442. * try the heuristic for sequential allocation,
  443. * failing that at least try to get decent locality.
  444. */
  445. if (block_i && (block == block_i->last_alloc_logical_block + 1)
  446. && (block_i->last_alloc_physical_block != 0)) {
  447. return block_i->last_alloc_physical_block + 1;
  448. }
  449. return ext3_find_near(inode, partial);
  450. }
  451. /**
  452. * ext3_blks_to_allocate - Look up the block map and count the number
  453. * of direct blocks need to be allocated for the given branch.
  454. *
  455. * @branch: chain of indirect blocks
  456. * @k: number of blocks need for indirect blocks
  457. * @blks: number of data blocks to be mapped.
  458. * @blocks_to_boundary: the offset in the indirect block
  459. *
  460. * return the total number of blocks to be allocate, including the
  461. * direct and indirect blocks.
  462. */
  463. static int ext3_blks_to_allocate(Indirect *branch, int k, unsigned long blks,
  464. int blocks_to_boundary)
  465. {
  466. unsigned long count = 0;
  467. /*
  468. * Simple case, [t,d]Indirect block(s) has not allocated yet
  469. * then it's clear blocks on that path have not allocated
  470. */
  471. if (k > 0) {
  472. /* right now we don't handle cross boundary allocation */
  473. if (blks < blocks_to_boundary + 1)
  474. count += blks;
  475. else
  476. count += blocks_to_boundary + 1;
  477. return count;
  478. }
  479. count++;
  480. while (count < blks && count <= blocks_to_boundary &&
  481. le32_to_cpu(*(branch[0].p + count)) == 0) {
  482. count++;
  483. }
  484. return count;
  485. }
  486. /**
  487. * ext3_alloc_blocks - multiple allocate blocks needed for a branch
  488. * @handle: handle for this transaction
  489. * @inode: owner
  490. * @goal: preferred place for allocation
  491. * @indirect_blks: the number of blocks need to allocate for indirect
  492. * blocks
  493. * @blks: number of blocks need to allocated for direct blocks
  494. * @new_blocks: on return it will store the new block numbers for
  495. * the indirect blocks(if needed) and the first direct block,
  496. * @err: here we store the error value
  497. *
  498. * return the number of direct blocks allocated
  499. */
  500. static int ext3_alloc_blocks(handle_t *handle, struct inode *inode,
  501. ext3_fsblk_t goal, int indirect_blks, int blks,
  502. ext3_fsblk_t new_blocks[4], int *err)
  503. {
  504. int target, i;
  505. unsigned long count = 0;
  506. int index = 0;
  507. ext3_fsblk_t current_block = 0;
  508. int ret = 0;
  509. /*
  510. * Here we try to allocate the requested multiple blocks at once,
  511. * on a best-effort basis.
  512. * To build a branch, we should allocate blocks for
  513. * the indirect blocks(if not allocated yet), and at least
  514. * the first direct block of this branch. That's the
  515. * minimum number of blocks need to allocate(required)
  516. */
  517. target = blks + indirect_blks;
  518. while (1) {
  519. count = target;
  520. /* allocating blocks for indirect blocks and direct blocks */
  521. current_block = ext3_new_blocks(handle,inode,goal,&count,err);
  522. if (*err)
  523. goto failed_out;
  524. target -= count;
  525. /* allocate blocks for indirect blocks */
  526. while (index < indirect_blks && count) {
  527. new_blocks[index++] = current_block++;
  528. count--;
  529. }
  530. if (count > 0)
  531. break;
  532. }
  533. /* save the new block number for the first direct block */
  534. new_blocks[index] = current_block;
  535. /* total number of blocks allocated for direct blocks */
  536. ret = count;
  537. *err = 0;
  538. return ret;
  539. failed_out:
  540. for (i = 0; i <index; i++)
  541. ext3_free_blocks(handle, inode, new_blocks[i], 1);
  542. return ret;
  543. }
  544. /**
  545. * ext3_alloc_branch - allocate and set up a chain of blocks.
  546. * @handle: handle for this transaction
  547. * @inode: owner
  548. * @indirect_blks: number of allocated indirect blocks
  549. * @blks: number of allocated direct blocks
  550. * @goal: preferred place for allocation
  551. * @offsets: offsets (in the blocks) to store the pointers to next.
  552. * @branch: place to store the chain in.
  553. *
  554. * This function allocates blocks, zeroes out all but the last one,
  555. * links them into chain and (if we are synchronous) writes them to disk.
  556. * In other words, it prepares a branch that can be spliced onto the
  557. * inode. It stores the information about that chain in the branch[], in
  558. * the same format as ext3_get_branch() would do. We are calling it after
  559. * we had read the existing part of chain and partial points to the last
  560. * triple of that (one with zero ->key). Upon the exit we have the same
  561. * picture as after the successful ext3_get_block(), except that in one
  562. * place chain is disconnected - *branch->p is still zero (we did not
  563. * set the last link), but branch->key contains the number that should
  564. * be placed into *branch->p to fill that gap.
  565. *
  566. * If allocation fails we free all blocks we've allocated (and forget
  567. * their buffer_heads) and return the error value the from failed
  568. * ext3_alloc_block() (normally -ENOSPC). Otherwise we set the chain
  569. * as described above and return 0.
  570. */
  571. static int ext3_alloc_branch(handle_t *handle, struct inode *inode,
  572. int indirect_blks, int *blks, ext3_fsblk_t goal,
  573. int *offsets, Indirect *branch)
  574. {
  575. int blocksize = inode->i_sb->s_blocksize;
  576. int i, n = 0;
  577. int err = 0;
  578. struct buffer_head *bh;
  579. int num;
  580. ext3_fsblk_t new_blocks[4];
  581. ext3_fsblk_t current_block;
  582. num = ext3_alloc_blocks(handle, inode, goal, indirect_blks,
  583. *blks, new_blocks, &err);
  584. if (err)
  585. return err;
  586. branch[0].key = cpu_to_le32(new_blocks[0]);
  587. /*
  588. * metadata blocks and data blocks are allocated.
  589. */
  590. for (n = 1; n <= indirect_blks; n++) {
  591. /*
  592. * Get buffer_head for parent block, zero it out
  593. * and set the pointer to new one, then send
  594. * parent to disk.
  595. */
  596. bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
  597. branch[n].bh = bh;
  598. lock_buffer(bh);
  599. BUFFER_TRACE(bh, "call get_create_access");
  600. err = ext3_journal_get_create_access(handle, bh);
  601. if (err) {
  602. unlock_buffer(bh);
  603. brelse(bh);
  604. goto failed;
  605. }
  606. memset(bh->b_data, 0, blocksize);
  607. branch[n].p = (__le32 *) bh->b_data + offsets[n];
  608. branch[n].key = cpu_to_le32(new_blocks[n]);
  609. *branch[n].p = branch[n].key;
  610. if ( n == indirect_blks) {
  611. current_block = new_blocks[n];
  612. /*
  613. * End of chain, update the last new metablock of
  614. * the chain to point to the new allocated
  615. * data blocks numbers
  616. */
  617. for (i=1; i < num; i++)
  618. *(branch[n].p + i) = cpu_to_le32(++current_block);
  619. }
  620. BUFFER_TRACE(bh, "marking uptodate");
  621. set_buffer_uptodate(bh);
  622. unlock_buffer(bh);
  623. BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
  624. err = ext3_journal_dirty_metadata(handle, bh);
  625. if (err)
  626. goto failed;
  627. }
  628. *blks = num;
  629. return err;
  630. failed:
  631. /* Allocation failed, free what we already allocated */
  632. for (i = 1; i <= n ; i++) {
  633. BUFFER_TRACE(branch[i].bh, "call journal_forget");
  634. ext3_journal_forget(handle, branch[i].bh);
  635. }
  636. for (i = 0; i <indirect_blks; i++)
  637. ext3_free_blocks(handle, inode, new_blocks[i], 1);
  638. ext3_free_blocks(handle, inode, new_blocks[i], num);
  639. return err;
  640. }
  641. /**
  642. * ext3_splice_branch - splice the allocated branch onto inode.
  643. * @handle: handle for this transaction
  644. * @inode: owner
  645. * @block: (logical) number of block we are adding
  646. * @where: location of missing link
  647. * @num: number of indirect blocks we are adding
  648. * @blks: number of direct blocks we are adding
  649. *
  650. * This function fills the missing link and does all housekeeping needed in
  651. * inode (->i_blocks, etc.). In case of success we end up with the full
  652. * chain to new block and return 0.
  653. */
  654. static int ext3_splice_branch(handle_t *handle, struct inode *inode,
  655. long block, Indirect *where, int num, int blks)
  656. {
  657. int i;
  658. int err = 0;
  659. struct ext3_block_alloc_info *block_i;
  660. ext3_fsblk_t current_block;
  661. struct ext3_inode_info *ei = EXT3_I(inode);
  662. block_i = ei->i_block_alloc_info;
  663. /*
  664. * If we're splicing into a [td]indirect block (as opposed to the
  665. * inode) then we need to get write access to the [td]indirect block
  666. * before the splice.
  667. */
  668. if (where->bh) {
  669. BUFFER_TRACE(where->bh, "get_write_access");
  670. err = ext3_journal_get_write_access(handle, where->bh);
  671. if (err)
  672. goto err_out;
  673. }
  674. /* That's it */
  675. *where->p = where->key;
  676. /*
  677. * Update the host buffer_head or inode to point to more just allocated
  678. * direct blocks blocks
  679. */
  680. if (num == 0 && blks > 1) {
  681. current_block = le32_to_cpu(where->key) + 1;
  682. for (i = 1; i < blks; i++)
  683. *(where->p + i ) = cpu_to_le32(current_block++);
  684. }
  685. /*
  686. * update the most recently allocated logical & physical block
  687. * in i_block_alloc_info, to assist find the proper goal block for next
  688. * allocation
  689. */
  690. if (block_i) {
  691. block_i->last_alloc_logical_block = block + blks - 1;
  692. block_i->last_alloc_physical_block =
  693. le32_to_cpu(where[num].key) + blks - 1;
  694. }
  695. /* We are done with atomic stuff, now do the rest of housekeeping */
  696. inode->i_ctime = CURRENT_TIME_SEC;
  697. ext3_mark_inode_dirty(handle, inode);
  698. /* ext3_mark_inode_dirty already updated i_sync_tid */
  699. atomic_set(&ei->i_datasync_tid, handle->h_transaction->t_tid);
  700. /* had we spliced it onto indirect block? */
  701. if (where->bh) {
  702. /*
  703. * If we spliced it onto an indirect block, we haven't
  704. * altered the inode. Note however that if it is being spliced
  705. * onto an indirect block at the very end of the file (the
  706. * file is growing) then we *will* alter the inode to reflect
  707. * the new i_size. But that is not done here - it is done in
  708. * generic_commit_write->__mark_inode_dirty->ext3_dirty_inode.
  709. */
  710. jbd_debug(5, "splicing indirect only\n");
  711. BUFFER_TRACE(where->bh, "call ext3_journal_dirty_metadata");
  712. err = ext3_journal_dirty_metadata(handle, where->bh);
  713. if (err)
  714. goto err_out;
  715. } else {
  716. /*
  717. * OK, we spliced it into the inode itself on a direct block.
  718. * Inode was dirtied above.
  719. */
  720. jbd_debug(5, "splicing direct\n");
  721. }
  722. return err;
  723. err_out:
  724. for (i = 1; i <= num; i++) {
  725. BUFFER_TRACE(where[i].bh, "call journal_forget");
  726. ext3_journal_forget(handle, where[i].bh);
  727. ext3_free_blocks(handle,inode,le32_to_cpu(where[i-1].key),1);
  728. }
  729. ext3_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks);
  730. return err;
  731. }
  732. /*
  733. * Allocation strategy is simple: if we have to allocate something, we will
  734. * have to go the whole way to leaf. So let's do it before attaching anything
  735. * to tree, set linkage between the newborn blocks, write them if sync is
  736. * required, recheck the path, free and repeat if check fails, otherwise
  737. * set the last missing link (that will protect us from any truncate-generated
  738. * removals - all blocks on the path are immune now) and possibly force the
  739. * write on the parent block.
  740. * That has a nice additional property: no special recovery from the failed
  741. * allocations is needed - we simply release blocks and do not touch anything
  742. * reachable from inode.
  743. *
  744. * `handle' can be NULL if create == 0.
  745. *
  746. * The BKL may not be held on entry here. Be sure to take it early.
  747. * return > 0, # of blocks mapped or allocated.
  748. * return = 0, if plain lookup failed.
  749. * return < 0, error case.
  750. */
  751. int ext3_get_blocks_handle(handle_t *handle, struct inode *inode,
  752. sector_t iblock, unsigned long maxblocks,
  753. struct buffer_head *bh_result,
  754. int create)
  755. {
  756. int err = -EIO;
  757. int offsets[4];
  758. Indirect chain[4];
  759. Indirect *partial;
  760. ext3_fsblk_t goal;
  761. int indirect_blks;
  762. int blocks_to_boundary = 0;
  763. int depth;
  764. struct ext3_inode_info *ei = EXT3_I(inode);
  765. int count = 0;
  766. ext3_fsblk_t first_block = 0;
  767. trace_ext3_get_blocks_enter(inode, iblock, maxblocks, create);
  768. J_ASSERT(handle != NULL || create == 0);
  769. depth = ext3_block_to_path(inode,iblock,offsets,&blocks_to_boundary);
  770. if (depth == 0)
  771. goto out;
  772. partial = ext3_get_branch(inode, depth, offsets, chain, &err);
  773. /* Simplest case - block found, no allocation needed */
  774. if (!partial) {
  775. first_block = le32_to_cpu(chain[depth - 1].key);
  776. clear_buffer_new(bh_result);
  777. count++;
  778. /*map more blocks*/
  779. while (count < maxblocks && count <= blocks_to_boundary) {
  780. ext3_fsblk_t blk;
  781. if (!verify_chain(chain, chain + depth - 1)) {
  782. /*
  783. * Indirect block might be removed by
  784. * truncate while we were reading it.
  785. * Handling of that case: forget what we've
  786. * got now. Flag the err as EAGAIN, so it
  787. * will reread.
  788. */
  789. err = -EAGAIN;
  790. count = 0;
  791. break;
  792. }
  793. blk = le32_to_cpu(*(chain[depth-1].p + count));
  794. if (blk == first_block + count)
  795. count++;
  796. else
  797. break;
  798. }
  799. if (err != -EAGAIN)
  800. goto got_it;
  801. }
  802. /* Next simple case - plain lookup or failed read of indirect block */
  803. if (!create || err == -EIO)
  804. goto cleanup;
  805. /*
  806. * Block out ext3_truncate while we alter the tree
  807. */
  808. mutex_lock(&ei->truncate_mutex);
  809. /*
  810. * If the indirect block is missing while we are reading
  811. * the chain(ext3_get_branch() returns -EAGAIN err), or
  812. * if the chain has been changed after we grab the semaphore,
  813. * (either because another process truncated this branch, or
  814. * another get_block allocated this branch) re-grab the chain to see if
  815. * the request block has been allocated or not.
  816. *
  817. * Since we already block the truncate/other get_block
  818. * at this point, we will have the current copy of the chain when we
  819. * splice the branch into the tree.
  820. */
  821. if (err == -EAGAIN || !verify_chain(chain, partial)) {
  822. while (partial > chain) {
  823. brelse(partial->bh);
  824. partial--;
  825. }
  826. partial = ext3_get_branch(inode, depth, offsets, chain, &err);
  827. if (!partial) {
  828. count++;
  829. mutex_unlock(&ei->truncate_mutex);
  830. if (err)
  831. goto cleanup;
  832. clear_buffer_new(bh_result);
  833. goto got_it;
  834. }
  835. }
  836. /*
  837. * Okay, we need to do block allocation. Lazily initialize the block
  838. * allocation info here if necessary
  839. */
  840. if (S_ISREG(inode->i_mode) && (!ei->i_block_alloc_info))
  841. ext3_init_block_alloc_info(inode);
  842. goal = ext3_find_goal(inode, iblock, partial);
  843. /* the number of blocks need to allocate for [d,t]indirect blocks */
  844. indirect_blks = (chain + depth) - partial - 1;
  845. /*
  846. * Next look up the indirect map to count the totoal number of
  847. * direct blocks to allocate for this branch.
  848. */
  849. count = ext3_blks_to_allocate(partial, indirect_blks,
  850. maxblocks, blocks_to_boundary);
  851. err = ext3_alloc_branch(handle, inode, indirect_blks, &count, goal,
  852. offsets + (partial - chain), partial);
  853. /*
  854. * The ext3_splice_branch call will free and forget any buffers
  855. * on the new chain if there is a failure, but that risks using
  856. * up transaction credits, especially for bitmaps where the
  857. * credits cannot be returned. Can we handle this somehow? We
  858. * may need to return -EAGAIN upwards in the worst case. --sct
  859. */
  860. if (!err)
  861. err = ext3_splice_branch(handle, inode, iblock,
  862. partial, indirect_blks, count);
  863. mutex_unlock(&ei->truncate_mutex);
  864. if (err)
  865. goto cleanup;
  866. set_buffer_new(bh_result);
  867. got_it:
  868. map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
  869. if (count > blocks_to_boundary)
  870. set_buffer_boundary(bh_result);
  871. err = count;
  872. /* Clean up and exit */
  873. partial = chain + depth - 1; /* the whole chain */
  874. cleanup:
  875. while (partial > chain) {
  876. BUFFER_TRACE(partial->bh, "call brelse");
  877. brelse(partial->bh);
  878. partial--;
  879. }
  880. BUFFER_TRACE(bh_result, "returned");
  881. out:
  882. trace_ext3_get_blocks_exit(inode, iblock,
  883. depth ? le32_to_cpu(chain[depth-1].key) : 0,
  884. count, err);
  885. return err;
  886. }
  887. /* Maximum number of blocks we map for direct IO at once. */
  888. #define DIO_MAX_BLOCKS 4096
  889. /*
  890. * Number of credits we need for writing DIO_MAX_BLOCKS:
  891. * We need sb + group descriptor + bitmap + inode -> 4
  892. * For B blocks with A block pointers per block we need:
  893. * 1 (triple ind.) + (B/A/A + 2) (doubly ind.) + (B/A + 2) (indirect).
  894. * If we plug in 4096 for B and 256 for A (for 1KB block size), we get 25.
  895. */
  896. #define DIO_CREDITS 25
  897. static int ext3_get_block(struct inode *inode, sector_t iblock,
  898. struct buffer_head *bh_result, int create)
  899. {
  900. handle_t *handle = ext3_journal_current_handle();
  901. int ret = 0, started = 0;
  902. unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
  903. if (create && !handle) { /* Direct IO write... */
  904. if (max_blocks > DIO_MAX_BLOCKS)
  905. max_blocks = DIO_MAX_BLOCKS;
  906. handle = ext3_journal_start(inode, DIO_CREDITS +
  907. EXT3_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb));
  908. if (IS_ERR(handle)) {
  909. ret = PTR_ERR(handle);
  910. goto out;
  911. }
  912. started = 1;
  913. }
  914. ret = ext3_get_blocks_handle(handle, inode, iblock,
  915. max_blocks, bh_result, create);
  916. if (ret > 0) {
  917. bh_result->b_size = (ret << inode->i_blkbits);
  918. ret = 0;
  919. }
  920. if (started)
  921. ext3_journal_stop(handle);
  922. out:
  923. return ret;
  924. }
  925. int ext3_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  926. u64 start, u64 len)
  927. {
  928. return generic_block_fiemap(inode, fieinfo, start, len,
  929. ext3_get_block);
  930. }
  931. /*
  932. * `handle' can be NULL if create is zero
  933. */
  934. struct buffer_head *ext3_getblk(handle_t *handle, struct inode *inode,
  935. long block, int create, int *errp)
  936. {
  937. struct buffer_head dummy;
  938. int fatal = 0, err;
  939. J_ASSERT(handle != NULL || create == 0);
  940. dummy.b_state = 0;
  941. dummy.b_blocknr = -1000;
  942. buffer_trace_init(&dummy.b_history);
  943. err = ext3_get_blocks_handle(handle, inode, block, 1,
  944. &dummy, create);
  945. /*
  946. * ext3_get_blocks_handle() returns number of blocks
  947. * mapped. 0 in case of a HOLE.
  948. */
  949. if (err > 0) {
  950. if (err > 1)
  951. WARN_ON(1);
  952. err = 0;
  953. }
  954. *errp = err;
  955. if (!err && buffer_mapped(&dummy)) {
  956. struct buffer_head *bh;
  957. bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
  958. if (!bh) {
  959. *errp = -EIO;
  960. goto err;
  961. }
  962. if (buffer_new(&dummy)) {
  963. J_ASSERT(create != 0);
  964. J_ASSERT(handle != NULL);
  965. /*
  966. * Now that we do not always journal data, we should
  967. * keep in mind whether this should always journal the
  968. * new buffer as metadata. For now, regular file
  969. * writes use ext3_get_block instead, so it's not a
  970. * problem.
  971. */
  972. lock_buffer(bh);
  973. BUFFER_TRACE(bh, "call get_create_access");
  974. fatal = ext3_journal_get_create_access(handle, bh);
  975. if (!fatal && !buffer_uptodate(bh)) {
  976. memset(bh->b_data,0,inode->i_sb->s_blocksize);
  977. set_buffer_uptodate(bh);
  978. }
  979. unlock_buffer(bh);
  980. BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
  981. err = ext3_journal_dirty_metadata(handle, bh);
  982. if (!fatal)
  983. fatal = err;
  984. } else {
  985. BUFFER_TRACE(bh, "not a new buffer");
  986. }
  987. if (fatal) {
  988. *errp = fatal;
  989. brelse(bh);
  990. bh = NULL;
  991. }
  992. return bh;
  993. }
  994. err:
  995. return NULL;
  996. }
  997. struct buffer_head *ext3_bread(handle_t *handle, struct inode *inode,
  998. int block, int create, int *err)
  999. {
  1000. struct buffer_head * bh;
  1001. bh = ext3_getblk(handle, inode, block, create, err);
  1002. if (!bh)
  1003. return bh;
  1004. if (buffer_uptodate(bh))
  1005. return bh;
  1006. ll_rw_block(READ_META, 1, &bh);
  1007. wait_on_buffer(bh);
  1008. if (buffer_uptodate(bh))
  1009. return bh;
  1010. put_bh(bh);
  1011. *err = -EIO;
  1012. return NULL;
  1013. }
  1014. static int walk_page_buffers( handle_t *handle,
  1015. struct buffer_head *head,
  1016. unsigned from,
  1017. unsigned to,
  1018. int *partial,
  1019. int (*fn)( handle_t *handle,
  1020. struct buffer_head *bh))
  1021. {
  1022. struct buffer_head *bh;
  1023. unsigned block_start, block_end;
  1024. unsigned blocksize = head->b_size;
  1025. int err, ret = 0;
  1026. struct buffer_head *next;
  1027. for ( bh = head, block_start = 0;
  1028. ret == 0 && (bh != head || !block_start);
  1029. block_start = block_end, bh = next)
  1030. {
  1031. next = bh->b_this_page;
  1032. block_end = block_start + blocksize;
  1033. if (block_end <= from || block_start >= to) {
  1034. if (partial && !buffer_uptodate(bh))
  1035. *partial = 1;
  1036. continue;
  1037. }
  1038. err = (*fn)(handle, bh);
  1039. if (!ret)
  1040. ret = err;
  1041. }
  1042. return ret;
  1043. }
  1044. /*
  1045. * To preserve ordering, it is essential that the hole instantiation and
  1046. * the data write be encapsulated in a single transaction. We cannot
  1047. * close off a transaction and start a new one between the ext3_get_block()
  1048. * and the commit_write(). So doing the journal_start at the start of
  1049. * prepare_write() is the right place.
  1050. *
  1051. * Also, this function can nest inside ext3_writepage() ->
  1052. * block_write_full_page(). In that case, we *know* that ext3_writepage()
  1053. * has generated enough buffer credits to do the whole page. So we won't
  1054. * block on the journal in that case, which is good, because the caller may
  1055. * be PF_MEMALLOC.
  1056. *
  1057. * By accident, ext3 can be reentered when a transaction is open via
  1058. * quota file writes. If we were to commit the transaction while thus
  1059. * reentered, there can be a deadlock - we would be holding a quota
  1060. * lock, and the commit would never complete if another thread had a
  1061. * transaction open and was blocking on the quota lock - a ranking
  1062. * violation.
  1063. *
  1064. * So what we do is to rely on the fact that journal_stop/journal_start
  1065. * will _not_ run commit under these circumstances because handle->h_ref
  1066. * is elevated. We'll still have enough credits for the tiny quotafile
  1067. * write.
  1068. */
  1069. static int do_journal_get_write_access(handle_t *handle,
  1070. struct buffer_head *bh)
  1071. {
  1072. int dirty = buffer_dirty(bh);
  1073. int ret;
  1074. if (!buffer_mapped(bh) || buffer_freed(bh))
  1075. return 0;
  1076. /*
  1077. * __block_prepare_write() could have dirtied some buffers. Clean
  1078. * the dirty bit as jbd2_journal_get_write_access() could complain
  1079. * otherwise about fs integrity issues. Setting of the dirty bit
  1080. * by __block_prepare_write() isn't a real problem here as we clear
  1081. * the bit before releasing a page lock and thus writeback cannot
  1082. * ever write the buffer.
  1083. */
  1084. if (dirty)
  1085. clear_buffer_dirty(bh);
  1086. ret = ext3_journal_get_write_access(handle, bh);
  1087. if (!ret && dirty)
  1088. ret = ext3_journal_dirty_metadata(handle, bh);
  1089. return ret;
  1090. }
  1091. /*
  1092. * Truncate blocks that were not used by write. We have to truncate the
  1093. * pagecache as well so that corresponding buffers get properly unmapped.
  1094. */
  1095. static void ext3_truncate_failed_write(struct inode *inode)
  1096. {
  1097. truncate_inode_pages(inode->i_mapping, inode->i_size);
  1098. ext3_truncate(inode);
  1099. }
  1100. static int ext3_write_begin(struct file *file, struct address_space *mapping,
  1101. loff_t pos, unsigned len, unsigned flags,
  1102. struct page **pagep, void **fsdata)
  1103. {
  1104. struct inode *inode = mapping->host;
  1105. int ret;
  1106. handle_t *handle;
  1107. int retries = 0;
  1108. struct page *page;
  1109. pgoff_t index;
  1110. unsigned from, to;
  1111. /* Reserve one block more for addition to orphan list in case
  1112. * we allocate blocks but write fails for some reason */
  1113. int needed_blocks = ext3_writepage_trans_blocks(inode) + 1;
  1114. trace_ext3_write_begin(inode, pos, len, flags);
  1115. index = pos >> PAGE_CACHE_SHIFT;
  1116. from = pos & (PAGE_CACHE_SIZE - 1);
  1117. to = from + len;
  1118. retry:
  1119. page = grab_cache_page_write_begin(mapping, index, flags);
  1120. if (!page)
  1121. return -ENOMEM;
  1122. *pagep = page;
  1123. handle = ext3_journal_start(inode, needed_blocks);
  1124. if (IS_ERR(handle)) {
  1125. unlock_page(page);
  1126. page_cache_release(page);
  1127. ret = PTR_ERR(handle);
  1128. goto out;
  1129. }
  1130. ret = __block_write_begin(page, pos, len, ext3_get_block);
  1131. if (ret)
  1132. goto write_begin_failed;
  1133. if (ext3_should_journal_data(inode)) {
  1134. ret = walk_page_buffers(handle, page_buffers(page),
  1135. from, to, NULL, do_journal_get_write_access);
  1136. }
  1137. write_begin_failed:
  1138. if (ret) {
  1139. /*
  1140. * block_write_begin may have instantiated a few blocks
  1141. * outside i_size. Trim these off again. Don't need
  1142. * i_size_read because we hold i_mutex.
  1143. *
  1144. * Add inode to orphan list in case we crash before truncate
  1145. * finishes. Do this only if ext3_can_truncate() agrees so
  1146. * that orphan processing code is happy.
  1147. */
  1148. if (pos + len > inode->i_size && ext3_can_truncate(inode))
  1149. ext3_orphan_add(handle, inode);
  1150. ext3_journal_stop(handle);
  1151. unlock_page(page);
  1152. page_cache_release(page);
  1153. if (pos + len > inode->i_size)
  1154. ext3_truncate_failed_write(inode);
  1155. }
  1156. if (ret == -ENOSPC && ext3_should_retry_alloc(inode->i_sb, &retries))
  1157. goto retry;
  1158. out:
  1159. return ret;
  1160. }
  1161. int ext3_journal_dirty_data(handle_t *handle, struct buffer_head *bh)
  1162. {
  1163. int err = journal_dirty_data(handle, bh);
  1164. if (err)
  1165. ext3_journal_abort_handle(__func__, __func__,
  1166. bh, handle, err);
  1167. return err;
  1168. }
  1169. /* For ordered writepage and write_end functions */
  1170. static int journal_dirty_data_fn(handle_t *handle, struct buffer_head *bh)
  1171. {
  1172. /*
  1173. * Write could have mapped the buffer but it didn't copy the data in
  1174. * yet. So avoid filing such buffer into a transaction.
  1175. */
  1176. if (buffer_mapped(bh) && buffer_uptodate(bh))
  1177. return ext3_journal_dirty_data(handle, bh);
  1178. return 0;
  1179. }
  1180. /* For write_end() in data=journal mode */
  1181. static int write_end_fn(handle_t *handle, struct buffer_head *bh)
  1182. {
  1183. if (!buffer_mapped(bh) || buffer_freed(bh))
  1184. return 0;
  1185. set_buffer_uptodate(bh);
  1186. return ext3_journal_dirty_metadata(handle, bh);
  1187. }
  1188. /*
  1189. * This is nasty and subtle: ext3_write_begin() could have allocated blocks
  1190. * for the whole page but later we failed to copy the data in. Update inode
  1191. * size according to what we managed to copy. The rest is going to be
  1192. * truncated in write_end function.
  1193. */
  1194. static void update_file_sizes(struct inode *inode, loff_t pos, unsigned copied)
  1195. {
  1196. /* What matters to us is i_disksize. We don't write i_size anywhere */
  1197. if (pos + copied > inode->i_size)
  1198. i_size_write(inode, pos + copied);
  1199. if (pos + copied > EXT3_I(inode)->i_disksize) {
  1200. EXT3_I(inode)->i_disksize = pos + copied;
  1201. mark_inode_dirty(inode);
  1202. }
  1203. }
  1204. /*
  1205. * We need to pick up the new inode size which generic_commit_write gave us
  1206. * `file' can be NULL - eg, when called from page_symlink().
  1207. *
  1208. * ext3 never places buffers on inode->i_mapping->private_list. metadata
  1209. * buffers are managed internally.
  1210. */
  1211. static int ext3_ordered_write_end(struct file *file,
  1212. struct address_space *mapping,
  1213. loff_t pos, unsigned len, unsigned copied,
  1214. struct page *page, void *fsdata)
  1215. {
  1216. handle_t *handle = ext3_journal_current_handle();
  1217. struct inode *inode = file->f_mapping->host;
  1218. unsigned from, to;
  1219. int ret = 0, ret2;
  1220. trace_ext3_ordered_write_end(inode, pos, len, copied);
  1221. copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
  1222. from = pos & (PAGE_CACHE_SIZE - 1);
  1223. to = from + copied;
  1224. ret = walk_page_buffers(handle, page_buffers(page),
  1225. from, to, NULL, journal_dirty_data_fn);
  1226. if (ret == 0)
  1227. update_file_sizes(inode, pos, copied);
  1228. /*
  1229. * There may be allocated blocks outside of i_size because
  1230. * we failed to copy some data. Prepare for truncate.
  1231. */
  1232. if (pos + len > inode->i_size && ext3_can_truncate(inode))
  1233. ext3_orphan_add(handle, inode);
  1234. ret2 = ext3_journal_stop(handle);
  1235. if (!ret)
  1236. ret = ret2;
  1237. unlock_page(page);
  1238. page_cache_release(page);
  1239. if (pos + len > inode->i_size)
  1240. ext3_truncate_failed_write(inode);
  1241. return ret ? ret : copied;
  1242. }
  1243. static int ext3_writeback_write_end(struct file *file,
  1244. struct address_space *mapping,
  1245. loff_t pos, unsigned len, unsigned copied,
  1246. struct page *page, void *fsdata)
  1247. {
  1248. handle_t *handle = ext3_journal_current_handle();
  1249. struct inode *inode = file->f_mapping->host;
  1250. int ret;
  1251. trace_ext3_writeback_write_end(inode, pos, len, copied);
  1252. copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
  1253. update_file_sizes(inode, pos, copied);
  1254. /*
  1255. * There may be allocated blocks outside of i_size because
  1256. * we failed to copy some data. Prepare for truncate.
  1257. */
  1258. if (pos + len > inode->i_size && ext3_can_truncate(inode))
  1259. ext3_orphan_add(handle, inode);
  1260. ret = ext3_journal_stop(handle);
  1261. unlock_page(page);
  1262. page_cache_release(page);
  1263. if (pos + len > inode->i_size)
  1264. ext3_truncate_failed_write(inode);
  1265. return ret ? ret : copied;
  1266. }
  1267. static int ext3_journalled_write_end(struct file *file,
  1268. struct address_space *mapping,
  1269. loff_t pos, unsigned len, unsigned copied,
  1270. struct page *page, void *fsdata)
  1271. {
  1272. handle_t *handle = ext3_journal_current_handle();
  1273. struct inode *inode = mapping->host;
  1274. int ret = 0, ret2;
  1275. int partial = 0;
  1276. unsigned from, to;
  1277. trace_ext3_journalled_write_end(inode, pos, len, copied);
  1278. from = pos & (PAGE_CACHE_SIZE - 1);
  1279. to = from + len;
  1280. if (copied < len) {
  1281. if (!PageUptodate(page))
  1282. copied = 0;
  1283. page_zero_new_buffers(page, from + copied, to);
  1284. to = from + copied;
  1285. }
  1286. ret = walk_page_buffers(handle, page_buffers(page), from,
  1287. to, &partial, write_end_fn);
  1288. if (!partial)
  1289. SetPageUptodate(page);
  1290. if (pos + copied > inode->i_size)
  1291. i_size_write(inode, pos + copied);
  1292. /*
  1293. * There may be allocated blocks outside of i_size because
  1294. * we failed to copy some data. Prepare for truncate.
  1295. */
  1296. if (pos + len > inode->i_size && ext3_can_truncate(inode))
  1297. ext3_orphan_add(handle, inode);
  1298. ext3_set_inode_state(inode, EXT3_STATE_JDATA);
  1299. if (inode->i_size > EXT3_I(inode)->i_disksize) {
  1300. EXT3_I(inode)->i_disksize = inode->i_size;
  1301. ret2 = ext3_mark_inode_dirty(handle, inode);
  1302. if (!ret)
  1303. ret = ret2;
  1304. }
  1305. ret2 = ext3_journal_stop(handle);
  1306. if (!ret)
  1307. ret = ret2;
  1308. unlock_page(page);
  1309. page_cache_release(page);
  1310. if (pos + len > inode->i_size)
  1311. ext3_truncate_failed_write(inode);
  1312. return ret ? ret : copied;
  1313. }
  1314. /*
  1315. * bmap() is special. It gets used by applications such as lilo and by
  1316. * the swapper to find the on-disk block of a specific piece of data.
  1317. *
  1318. * Naturally, this is dangerous if the block concerned is still in the
  1319. * journal. If somebody makes a swapfile on an ext3 data-journaling
  1320. * filesystem and enables swap, then they may get a nasty shock when the
  1321. * data getting swapped to that swapfile suddenly gets overwritten by
  1322. * the original zero's written out previously to the journal and
  1323. * awaiting writeback in the kernel's buffer cache.
  1324. *
  1325. * So, if we see any bmap calls here on a modified, data-journaled file,
  1326. * take extra steps to flush any blocks which might be in the cache.
  1327. */
  1328. static sector_t ext3_bmap(struct address_space *mapping, sector_t block)
  1329. {
  1330. struct inode *inode = mapping->host;
  1331. journal_t *journal;
  1332. int err;
  1333. if (ext3_test_inode_state(inode, EXT3_STATE_JDATA)) {
  1334. /*
  1335. * This is a REALLY heavyweight approach, but the use of
  1336. * bmap on dirty files is expected to be extremely rare:
  1337. * only if we run lilo or swapon on a freshly made file
  1338. * do we expect this to happen.
  1339. *
  1340. * (bmap requires CAP_SYS_RAWIO so this does not
  1341. * represent an unprivileged user DOS attack --- we'd be
  1342. * in trouble if mortal users could trigger this path at
  1343. * will.)
  1344. *
  1345. * NB. EXT3_STATE_JDATA is not set on files other than
  1346. * regular files. If somebody wants to bmap a directory
  1347. * or symlink and gets confused because the buffer
  1348. * hasn't yet been flushed to disk, they deserve
  1349. * everything they get.
  1350. */
  1351. ext3_clear_inode_state(inode, EXT3_STATE_JDATA);
  1352. journal = EXT3_JOURNAL(inode);
  1353. journal_lock_updates(journal);
  1354. err = journal_flush(journal);
  1355. journal_unlock_updates(journal);
  1356. if (err)
  1357. return 0;
  1358. }
  1359. return generic_block_bmap(mapping,block,ext3_get_block);
  1360. }
  1361. static int bget_one(handle_t *handle, struct buffer_head *bh)
  1362. {
  1363. get_bh(bh);
  1364. return 0;
  1365. }
  1366. static int bput_one(handle_t *handle, struct buffer_head *bh)
  1367. {
  1368. put_bh(bh);
  1369. return 0;
  1370. }
  1371. static int buffer_unmapped(handle_t *handle, struct buffer_head *bh)
  1372. {
  1373. return !buffer_mapped(bh);
  1374. }
  1375. /*
  1376. * Note that we always start a transaction even if we're not journalling
  1377. * data. This is to preserve ordering: any hole instantiation within
  1378. * __block_write_full_page -> ext3_get_block() should be journalled
  1379. * along with the data so we don't crash and then get metadata which
  1380. * refers to old data.
  1381. *
  1382. * In all journalling modes block_write_full_page() will start the I/O.
  1383. *
  1384. * Problem:
  1385. *
  1386. * ext3_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
  1387. * ext3_writepage()
  1388. *
  1389. * Similar for:
  1390. *
  1391. * ext3_file_write() -> generic_file_write() -> __alloc_pages() -> ...
  1392. *
  1393. * Same applies to ext3_get_block(). We will deadlock on various things like
  1394. * lock_journal and i_truncate_mutex.
  1395. *
  1396. * Setting PF_MEMALLOC here doesn't work - too many internal memory
  1397. * allocations fail.
  1398. *
  1399. * 16May01: If we're reentered then journal_current_handle() will be
  1400. * non-zero. We simply *return*.
  1401. *
  1402. * 1 July 2001: @@@ FIXME:
  1403. * In journalled data mode, a data buffer may be metadata against the
  1404. * current transaction. But the same file is part of a shared mapping
  1405. * and someone does a writepage() on it.
  1406. *
  1407. * We will move the buffer onto the async_data list, but *after* it has
  1408. * been dirtied. So there's a small window where we have dirty data on
  1409. * BJ_Metadata.
  1410. *
  1411. * Note that this only applies to the last partial page in the file. The
  1412. * bit which block_write_full_page() uses prepare/commit for. (That's
  1413. * broken code anyway: it's wrong for msync()).
  1414. *
  1415. * It's a rare case: affects the final partial page, for journalled data
  1416. * where the file is subject to bith write() and writepage() in the same
  1417. * transction. To fix it we'll need a custom block_write_full_page().
  1418. * We'll probably need that anyway for journalling writepage() output.
  1419. *
  1420. * We don't honour synchronous mounts for writepage(). That would be
  1421. * disastrous. Any write() or metadata operation will sync the fs for
  1422. * us.
  1423. *
  1424. * AKPM2: if all the page's buffers are mapped to disk and !data=journal,
  1425. * we don't need to open a transaction here.
  1426. */
  1427. static int ext3_ordered_writepage(struct page *page,
  1428. struct writeback_control *wbc)
  1429. {
  1430. struct inode *inode = page->mapping->host;
  1431. struct buffer_head *page_bufs;
  1432. handle_t *handle = NULL;
  1433. int ret = 0;
  1434. int err;
  1435. J_ASSERT(PageLocked(page));
  1436. WARN_ON_ONCE(IS_RDONLY(inode));
  1437. /*
  1438. * We give up here if we're reentered, because it might be for a
  1439. * different filesystem.
  1440. */
  1441. if (ext3_journal_current_handle())
  1442. goto out_fail;
  1443. trace_ext3_ordered_writepage(page);
  1444. if (!page_has_buffers(page)) {
  1445. create_empty_buffers(page, inode->i_sb->s_blocksize,
  1446. (1 << BH_Dirty)|(1 << BH_Uptodate));
  1447. page_bufs = page_buffers(page);
  1448. } else {
  1449. page_bufs = page_buffers(page);
  1450. if (!walk_page_buffers(NULL, page_bufs, 0, PAGE_CACHE_SIZE,
  1451. NULL, buffer_unmapped)) {
  1452. /* Provide NULL get_block() to catch bugs if buffers
  1453. * weren't really mapped */
  1454. return block_write_full_page(page, NULL, wbc);
  1455. }
  1456. }
  1457. handle = ext3_journal_start(inode, ext3_writepage_trans_blocks(inode));
  1458. if (IS_ERR(handle)) {
  1459. ret = PTR_ERR(handle);
  1460. goto out_fail;
  1461. }
  1462. walk_page_buffers(handle, page_bufs, 0,
  1463. PAGE_CACHE_SIZE, NULL, bget_one);
  1464. ret = block_write_full_page(page, ext3_get_block, wbc);
  1465. /*
  1466. * The page can become unlocked at any point now, and
  1467. * truncate can then come in and change things. So we
  1468. * can't touch *page from now on. But *page_bufs is
  1469. * safe due to elevated refcount.
  1470. */
  1471. /*
  1472. * And attach them to the current transaction. But only if
  1473. * block_write_full_page() succeeded. Otherwise they are unmapped,
  1474. * and generally junk.
  1475. */
  1476. if (ret == 0) {
  1477. err = walk_page_buffers(handle, page_bufs, 0, PAGE_CACHE_SIZE,
  1478. NULL, journal_dirty_data_fn);
  1479. if (!ret)
  1480. ret = err;
  1481. }
  1482. walk_page_buffers(handle, page_bufs, 0,
  1483. PAGE_CACHE_SIZE, NULL, bput_one);
  1484. err = ext3_journal_stop(handle);
  1485. if (!ret)
  1486. ret = err;
  1487. return ret;
  1488. out_fail:
  1489. redirty_page_for_writepage(wbc, page);
  1490. unlock_page(page);
  1491. return ret;
  1492. }
  1493. static int ext3_writeback_writepage(struct page *page,
  1494. struct writeback_control *wbc)
  1495. {
  1496. struct inode *inode = page->mapping->host;
  1497. handle_t *handle = NULL;
  1498. int ret = 0;
  1499. int err;
  1500. J_ASSERT(PageLocked(page));
  1501. WARN_ON_ONCE(IS_RDONLY(inode));
  1502. if (ext3_journal_current_handle())
  1503. goto out_fail;
  1504. trace_ext3_writeback_writepage(page);
  1505. if (page_has_buffers(page)) {
  1506. if (!walk_page_buffers(NULL, page_buffers(page), 0,
  1507. PAGE_CACHE_SIZE, NULL, buffer_unmapped)) {
  1508. /* Provide NULL get_block() to catch bugs if buffers
  1509. * weren't really mapped */
  1510. return block_write_full_page(page, NULL, wbc);
  1511. }
  1512. }
  1513. handle = ext3_journal_start(inode, ext3_writepage_trans_blocks(inode));
  1514. if (IS_ERR(handle)) {
  1515. ret = PTR_ERR(handle);
  1516. goto out_fail;
  1517. }
  1518. ret = block_write_full_page(page, ext3_get_block, wbc);
  1519. err = ext3_journal_stop(handle);
  1520. if (!ret)
  1521. ret = err;
  1522. return ret;
  1523. out_fail:
  1524. redirty_page_for_writepage(wbc, page);
  1525. unlock_page(page);
  1526. return ret;
  1527. }
  1528. static int ext3_journalled_writepage(struct page *page,
  1529. struct writeback_control *wbc)
  1530. {
  1531. struct inode *inode = page->mapping->host;
  1532. handle_t *handle = NULL;
  1533. int ret = 0;
  1534. int err;
  1535. J_ASSERT(PageLocked(page));
  1536. WARN_ON_ONCE(IS_RDONLY(inode));
  1537. if (ext3_journal_current_handle())
  1538. goto no_write;
  1539. trace_ext3_journalled_writepage(page);
  1540. handle = ext3_journal_start(inode, ext3_writepage_trans_blocks(inode));
  1541. if (IS_ERR(handle)) {
  1542. ret = PTR_ERR(handle);
  1543. goto no_write;
  1544. }
  1545. if (!page_has_buffers(page) || PageChecked(page)) {
  1546. /*
  1547. * It's mmapped pagecache. Add buffers and journal it. There
  1548. * doesn't seem much point in redirtying the page here.
  1549. */
  1550. ClearPageChecked(page);
  1551. ret = __block_write_begin(page, 0, PAGE_CACHE_SIZE,
  1552. ext3_get_block);
  1553. if (ret != 0) {
  1554. ext3_journal_stop(handle);
  1555. goto out_unlock;
  1556. }
  1557. ret = walk_page_buffers(handle, page_buffers(page), 0,
  1558. PAGE_CACHE_SIZE, NULL, do_journal_get_write_access);
  1559. err = walk_page_buffers(handle, page_buffers(page), 0,
  1560. PAGE_CACHE_SIZE, NULL, write_end_fn);
  1561. if (ret == 0)
  1562. ret = err;
  1563. ext3_set_inode_state(inode, EXT3_STATE_JDATA);
  1564. unlock_page(page);
  1565. } else {
  1566. /*
  1567. * It may be a page full of checkpoint-mode buffers. We don't
  1568. * really know unless we go poke around in the buffer_heads.
  1569. * But block_write_full_page will do the right thing.
  1570. */
  1571. ret = block_write_full_page(page, ext3_get_block, wbc);
  1572. }
  1573. err = ext3_journal_stop(handle);
  1574. if (!ret)
  1575. ret = err;
  1576. out:
  1577. return ret;
  1578. no_write:
  1579. redirty_page_for_writepage(wbc, page);
  1580. out_unlock:
  1581. unlock_page(page);
  1582. goto out;
  1583. }
  1584. static int ext3_readpage(struct file *file, struct page *page)
  1585. {
  1586. trace_ext3_readpage(page);
  1587. return mpage_readpage(page, ext3_get_block);
  1588. }
  1589. static int
  1590. ext3_readpages(struct file *file, struct address_space *mapping,
  1591. struct list_head *pages, unsigned nr_pages)
  1592. {
  1593. return mpage_readpages(mapping, pages, nr_pages, ext3_get_block);
  1594. }
  1595. static void ext3_invalidatepage(struct page *page, unsigned long offset)
  1596. {
  1597. journal_t *journal = EXT3_JOURNAL(page->mapping->host);
  1598. trace_ext3_invalidatepage(page, offset);
  1599. /*
  1600. * If it's a full truncate we just forget about the pending dirtying
  1601. */
  1602. if (offset == 0)
  1603. ClearPageChecked(page);
  1604. journal_invalidatepage(journal, page, offset);
  1605. }
  1606. static int ext3_releasepage(struct page *page, gfp_t wait)
  1607. {
  1608. journal_t *journal = EXT3_JOURNAL(page->mapping->host);
  1609. trace_ext3_releasepage(page);
  1610. WARN_ON(PageChecked(page));
  1611. if (!page_has_buffers(page))
  1612. return 0;
  1613. return journal_try_to_free_buffers(journal, page, wait);
  1614. }
  1615. /*
  1616. * If the O_DIRECT write will extend the file then add this inode to the
  1617. * orphan list. So recovery will truncate it back to the original size
  1618. * if the machine crashes during the write.
  1619. *
  1620. * If the O_DIRECT write is intantiating holes inside i_size and the machine
  1621. * crashes then stale disk data _may_ be exposed inside the file. But current
  1622. * VFS code falls back into buffered path in that case so we are safe.
  1623. */
  1624. static ssize_t ext3_direct_IO(int rw, struct kiocb *iocb,
  1625. const struct iovec *iov, loff_t offset,
  1626. unsigned long nr_segs)
  1627. {
  1628. struct file *file = iocb->ki_filp;
  1629. struct inode *inode = file->f_mapping->host;
  1630. struct ext3_inode_info *ei = EXT3_I(inode);
  1631. handle_t *handle;
  1632. ssize_t ret;
  1633. int orphan = 0;
  1634. size_t count = iov_length(iov, nr_segs);
  1635. int retries = 0;
  1636. trace_ext3_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw);
  1637. if (rw == WRITE) {
  1638. loff_t final_size = offset + count;
  1639. if (final_size > inode->i_size) {
  1640. /* Credits for sb + inode write */
  1641. handle = ext3_journal_start(inode, 2);
  1642. if (IS_ERR(handle)) {
  1643. ret = PTR_ERR(handle);
  1644. goto out;
  1645. }
  1646. ret = ext3_orphan_add(handle, inode);
  1647. if (ret) {
  1648. ext3_journal_stop(handle);
  1649. goto out;
  1650. }
  1651. orphan = 1;
  1652. ei->i_disksize = inode->i_size;
  1653. ext3_journal_stop(handle);
  1654. }
  1655. }
  1656. retry:
  1657. ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
  1658. offset, nr_segs,
  1659. ext3_get_block, NULL);
  1660. /*
  1661. * In case of error extending write may have instantiated a few
  1662. * blocks outside i_size. Trim these off again.
  1663. */
  1664. if (unlikely((rw & WRITE) && ret < 0)) {
  1665. loff_t isize = i_size_read(inode);
  1666. loff_t end = offset + iov_length(iov, nr_segs);
  1667. if (end > isize)
  1668. ext3_truncate_failed_write(inode);
  1669. }
  1670. if (ret == -ENOSPC && ext3_should_retry_alloc(inode->i_sb, &retries))
  1671. goto retry;
  1672. if (orphan) {
  1673. int err;
  1674. /* Credits for sb + inode write */
  1675. handle = ext3_journal_start(inode, 2);
  1676. if (IS_ERR(handle)) {
  1677. /* This is really bad luck. We've written the data
  1678. * but cannot extend i_size. Truncate allocated blocks
  1679. * and pretend the write failed... */
  1680. ext3_truncate_failed_write(inode);
  1681. ret = PTR_ERR(handle);
  1682. goto out;
  1683. }
  1684. if (inode->i_nlink)
  1685. ext3_orphan_del(handle, inode);
  1686. if (ret > 0) {
  1687. loff_t end = offset + ret;
  1688. if (end > inode->i_size) {
  1689. ei->i_disksize = end;
  1690. i_size_write(inode, end);
  1691. /*
  1692. * We're going to return a positive `ret'
  1693. * here due to non-zero-length I/O, so there's
  1694. * no way of reporting error returns from
  1695. * ext3_mark_inode_dirty() to userspace. So
  1696. * ignore it.
  1697. */
  1698. ext3_mark_inode_dirty(handle, inode);
  1699. }
  1700. }
  1701. err = ext3_journal_stop(handle);
  1702. if (ret == 0)
  1703. ret = err;
  1704. }
  1705. out:
  1706. trace_ext3_direct_IO_exit(inode, offset,
  1707. iov_length(iov, nr_segs), rw, ret);
  1708. return ret;
  1709. }
  1710. /*
  1711. * Pages can be marked dirty completely asynchronously from ext3's journalling
  1712. * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
  1713. * much here because ->set_page_dirty is called under VFS locks. The page is
  1714. * not necessarily locked.
  1715. *
  1716. * We cannot just dirty the page and leave attached buffers clean, because the
  1717. * buffers' dirty state is "definitive". We cannot just set the buffers dirty
  1718. * or jbddirty because all the journalling code will explode.
  1719. *
  1720. * So what we do is to mark the page "pending dirty" and next time writepage
  1721. * is called, propagate that into the buffers appropriately.
  1722. */
  1723. static int ext3_journalled_set_page_dirty(struct page *page)
  1724. {
  1725. SetPageChecked(page);
  1726. return __set_page_dirty_nobuffers(page);
  1727. }
  1728. static const struct address_space_operations ext3_ordered_aops = {
  1729. .readpage = ext3_readpage,
  1730. .readpages = ext3_readpages,
  1731. .writepage = ext3_ordered_writepage,
  1732. .write_begin = ext3_write_begin,
  1733. .write_end = ext3_ordered_write_end,
  1734. .bmap = ext3_bmap,
  1735. .invalidatepage = ext3_invalidatepage,
  1736. .releasepage = ext3_releasepage,
  1737. .direct_IO = ext3_direct_IO,
  1738. .migratepage = buffer_migrate_page,
  1739. .is_partially_uptodate = block_is_partially_uptodate,
  1740. .error_remove_page = generic_error_remove_page,
  1741. };
  1742. static const struct address_space_operations ext3_writeback_aops = {
  1743. .readpage = ext3_readpage,
  1744. .readpages = ext3_readpages,
  1745. .writepage = ext3_writeback_writepage,
  1746. .write_begin = ext3_write_begin,
  1747. .write_end = ext3_writeback_write_end,
  1748. .bmap = ext3_bmap,
  1749. .invalidatepage = ext3_invalidatepage,
  1750. .releasepage = ext3_releasepage,
  1751. .direct_IO = ext3_direct_IO,
  1752. .migratepage = buffer_migrate_page,
  1753. .is_partially_uptodate = block_is_partially_uptodate,
  1754. .error_remove_page = generic_error_remove_page,
  1755. };
  1756. static const struct address_space_operations ext3_journalled_aops = {
  1757. .readpage = ext3_readpage,
  1758. .readpages = ext3_readpages,
  1759. .writepage = ext3_journalled_writepage,
  1760. .write_begin = ext3_write_begin,
  1761. .write_end = ext3_journalled_write_end,
  1762. .set_page_dirty = ext3_journalled_set_page_dirty,
  1763. .bmap = ext3_bmap,
  1764. .invalidatepage = ext3_invalidatepage,
  1765. .releasepage = ext3_releasepage,
  1766. .is_partially_uptodate = block_is_partially_uptodate,
  1767. .error_remove_page = generic_error_remove_page,
  1768. };
  1769. void ext3_set_aops(struct inode *inode)
  1770. {
  1771. if (ext3_should_order_data(inode))
  1772. inode->i_mapping->a_ops = &ext3_ordered_aops;
  1773. else if (ext3_should_writeback_data(inode))
  1774. inode->i_mapping->a_ops = &ext3_writeback_aops;
  1775. else
  1776. inode->i_mapping->a_ops = &ext3_journalled_aops;
  1777. }
  1778. /*
  1779. * ext3_block_truncate_page() zeroes out a mapping from file offset `from'
  1780. * up to the end of the block which corresponds to `from'.
  1781. * This required during truncate. We need to physically zero the tail end
  1782. * of that block so it doesn't yield old data if the file is later grown.
  1783. */
  1784. static int ext3_block_truncate_page(handle_t *handle, struct page *page,
  1785. struct address_space *mapping, loff_t from)
  1786. {
  1787. ext3_fsblk_t index = from >> PAGE_CACHE_SHIFT;
  1788. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  1789. unsigned blocksize, iblock, length, pos;
  1790. struct inode *inode = mapping->host;
  1791. struct buffer_head *bh;
  1792. int err = 0;
  1793. blocksize = inode->i_sb->s_blocksize;
  1794. length = blocksize - (offset & (blocksize - 1));
  1795. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  1796. if (!page_has_buffers(page))
  1797. create_empty_buffers(page, blocksize, 0);
  1798. /* Find the buffer that contains "offset" */
  1799. bh = page_buffers(page);
  1800. pos = blocksize;
  1801. while (offset >= pos) {
  1802. bh = bh->b_this_page;
  1803. iblock++;
  1804. pos += blocksize;
  1805. }
  1806. err = 0;
  1807. if (buffer_freed(bh)) {
  1808. BUFFER_TRACE(bh, "freed: skip");
  1809. goto unlock;
  1810. }
  1811. if (!buffer_mapped(bh)) {
  1812. BUFFER_TRACE(bh, "unmapped");
  1813. ext3_get_block(inode, iblock, bh, 0);
  1814. /* unmapped? It's a hole - nothing to do */
  1815. if (!buffer_mapped(bh)) {
  1816. BUFFER_TRACE(bh, "still unmapped");
  1817. goto unlock;
  1818. }
  1819. }
  1820. /* Ok, it's mapped. Make sure it's up-to-date */
  1821. if (PageUptodate(page))
  1822. set_buffer_uptodate(bh);
  1823. if (!buffer_uptodate(bh)) {
  1824. err = -EIO;
  1825. ll_rw_block(READ, 1, &bh);
  1826. wait_on_buffer(bh);
  1827. /* Uhhuh. Read error. Complain and punt. */
  1828. if (!buffer_uptodate(bh))
  1829. goto unlock;
  1830. }
  1831. if (ext3_should_journal_data(inode)) {
  1832. BUFFER_TRACE(bh, "get write access");
  1833. err = ext3_journal_get_write_access(handle, bh);
  1834. if (err)
  1835. goto unlock;
  1836. }
  1837. zero_user(page, offset, length);
  1838. BUFFER_TRACE(bh, "zeroed end of block");
  1839. err = 0;
  1840. if (ext3_should_journal_data(inode)) {
  1841. err = ext3_journal_dirty_metadata(handle, bh);
  1842. } else {
  1843. if (ext3_should_order_data(inode))
  1844. err = ext3_journal_dirty_data(handle, bh);
  1845. mark_buffer_dirty(bh);
  1846. }
  1847. unlock:
  1848. unlock_page(page);
  1849. page_cache_release(page);
  1850. return err;
  1851. }
  1852. /*
  1853. * Probably it should be a library function... search for first non-zero word
  1854. * or memcmp with zero_page, whatever is better for particular architecture.
  1855. * Linus?
  1856. */
  1857. static inline int all_zeroes(__le32 *p, __le32 *q)
  1858. {
  1859. while (p < q)
  1860. if (*p++)
  1861. return 0;
  1862. return 1;
  1863. }
  1864. /**
  1865. * ext3_find_shared - find the indirect blocks for partial truncation.
  1866. * @inode: inode in question
  1867. * @depth: depth of the affected branch
  1868. * @offsets: offsets of pointers in that branch (see ext3_block_to_path)
  1869. * @chain: place to store the pointers to partial indirect blocks
  1870. * @top: place to the (detached) top of branch
  1871. *
  1872. * This is a helper function used by ext3_truncate().
  1873. *
  1874. * When we do truncate() we may have to clean the ends of several
  1875. * indirect blocks but leave the blocks themselves alive. Block is
  1876. * partially truncated if some data below the new i_size is referred
  1877. * from it (and it is on the path to the first completely truncated
  1878. * data block, indeed). We have to free the top of that path along
  1879. * with everything to the right of the path. Since no allocation
  1880. * past the truncation point is possible until ext3_truncate()
  1881. * finishes, we may safely do the latter, but top of branch may
  1882. * require special attention - pageout below the truncation point
  1883. * might try to populate it.
  1884. *
  1885. * We atomically detach the top of branch from the tree, store the
  1886. * block number of its root in *@top, pointers to buffer_heads of
  1887. * partially truncated blocks - in @chain[].bh and pointers to
  1888. * their last elements that should not be removed - in
  1889. * @chain[].p. Return value is the pointer to last filled element
  1890. * of @chain.
  1891. *
  1892. * The work left to caller to do the actual freeing of subtrees:
  1893. * a) free the subtree starting from *@top
  1894. * b) free the subtrees whose roots are stored in
  1895. * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
  1896. * c) free the subtrees growing from the inode past the @chain[0].
  1897. * (no partially truncated stuff there). */
  1898. static Indirect *ext3_find_shared(struct inode *inode, int depth,
  1899. int offsets[4], Indirect chain[4], __le32 *top)
  1900. {
  1901. Indirect *partial, *p;
  1902. int k, err;
  1903. *top = 0;
  1904. /* Make k index the deepest non-null offset + 1 */
  1905. for (k = depth; k > 1 && !offsets[k-1]; k--)
  1906. ;
  1907. partial = ext3_get_branch(inode, k, offsets, chain, &err);
  1908. /* Writer: pointers */
  1909. if (!partial)
  1910. partial = chain + k-1;
  1911. /*
  1912. * If the branch acquired continuation since we've looked at it -
  1913. * fine, it should all survive and (new) top doesn't belong to us.
  1914. */
  1915. if (!partial->key && *partial->p)
  1916. /* Writer: end */
  1917. goto no_top;
  1918. for (p=partial; p>chain && all_zeroes((__le32*)p->bh->b_data,p->p); p--)
  1919. ;
  1920. /*
  1921. * OK, we've found the last block that must survive. The rest of our
  1922. * branch should be detached before unlocking. However, if that rest
  1923. * of branch is all ours and does not grow immediately from the inode
  1924. * it's easier to cheat and just decrement partial->p.
  1925. */
  1926. if (p == chain + k - 1 && p > chain) {
  1927. p->p--;
  1928. } else {
  1929. *top = *p->p;
  1930. /* Nope, don't do this in ext3. Must leave the tree intact */
  1931. #if 0
  1932. *p->p = 0;
  1933. #endif
  1934. }
  1935. /* Writer: end */
  1936. while(partial > p) {
  1937. brelse(partial->bh);
  1938. partial--;
  1939. }
  1940. no_top:
  1941. return partial;
  1942. }
  1943. /*
  1944. * Zero a number of block pointers in either an inode or an indirect block.
  1945. * If we restart the transaction we must again get write access to the
  1946. * indirect block for further modification.
  1947. *
  1948. * We release `count' blocks on disk, but (last - first) may be greater
  1949. * than `count' because there can be holes in there.
  1950. */
  1951. static void ext3_clear_blocks(handle_t *handle, struct inode *inode,
  1952. struct buffer_head *bh, ext3_fsblk_t block_to_free,
  1953. unsigned long count, __le32 *first, __le32 *last)
  1954. {
  1955. __le32 *p;
  1956. if (try_to_extend_transaction(handle, inode)) {
  1957. if (bh) {
  1958. BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
  1959. if (ext3_journal_dirty_metadata(handle, bh))
  1960. return;
  1961. }
  1962. ext3_mark_inode_dirty(handle, inode);
  1963. truncate_restart_transaction(handle, inode);
  1964. if (bh) {
  1965. BUFFER_TRACE(bh, "retaking write access");
  1966. if (ext3_journal_get_write_access(handle, bh))
  1967. return;
  1968. }
  1969. }
  1970. /*
  1971. * Any buffers which are on the journal will be in memory. We find
  1972. * them on the hash table so journal_revoke() will run journal_forget()
  1973. * on them. We've already detached each block from the file, so
  1974. * bforget() in journal_forget() should be safe.
  1975. *
  1976. * AKPM: turn on bforget in journal_forget()!!!
  1977. */
  1978. for (p = first; p < last; p++) {
  1979. u32 nr = le32_to_cpu(*p);
  1980. if (nr) {
  1981. struct buffer_head *bh;
  1982. *p = 0;
  1983. bh = sb_find_get_block(inode->i_sb, nr);
  1984. ext3_forget(handle, 0, inode, bh, nr);
  1985. }
  1986. }
  1987. ext3_free_blocks(handle, inode, block_to_free, count);
  1988. }
  1989. /**
  1990. * ext3_free_data - free a list of data blocks
  1991. * @handle: handle for this transaction
  1992. * @inode: inode we are dealing with
  1993. * @this_bh: indirect buffer_head which contains *@first and *@last
  1994. * @first: array of block numbers
  1995. * @last: points immediately past the end of array
  1996. *
  1997. * We are freeing all blocks referred from that array (numbers are stored as
  1998. * little-endian 32-bit) and updating @inode->i_blocks appropriately.
  1999. *
  2000. * We accumulate contiguous runs of blocks to free. Conveniently, if these
  2001. * blocks are contiguous then releasing them at one time will only affect one
  2002. * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
  2003. * actually use a lot of journal space.
  2004. *
  2005. * @this_bh will be %NULL if @first and @last point into the inode's direct
  2006. * block pointers.
  2007. */
  2008. static void ext3_free_data(handle_t *handle, struct inode *inode,
  2009. struct buffer_head *this_bh,
  2010. __le32 *first, __le32 *last)
  2011. {
  2012. ext3_fsblk_t block_to_free = 0; /* Starting block # of a run */
  2013. unsigned long count = 0; /* Number of blocks in the run */
  2014. __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
  2015. corresponding to
  2016. block_to_free */
  2017. ext3_fsblk_t nr; /* Current block # */
  2018. __le32 *p; /* Pointer into inode/ind
  2019. for current block */
  2020. int err;
  2021. if (this_bh) { /* For indirect block */
  2022. BUFFER_TRACE(this_bh, "get_write_access");
  2023. err = ext3_journal_get_write_access(handle, this_bh);
  2024. /* Important: if we can't update the indirect pointers
  2025. * to the blocks, we can't free them. */
  2026. if (err)
  2027. return;
  2028. }
  2029. for (p = first; p < last; p++) {
  2030. nr = le32_to_cpu(*p);
  2031. if (nr) {
  2032. /* accumulate blocks to free if they're contiguous */
  2033. if (count == 0) {
  2034. block_to_free = nr;
  2035. block_to_free_p = p;
  2036. count = 1;
  2037. } else if (nr == block_to_free + count) {
  2038. count++;
  2039. } else {
  2040. ext3_clear_blocks(handle, inode, this_bh,
  2041. block_to_free,
  2042. count, block_to_free_p, p);
  2043. block_to_free = nr;
  2044. block_to_free_p = p;
  2045. count = 1;
  2046. }
  2047. }
  2048. }
  2049. if (count > 0)
  2050. ext3_clear_blocks(handle, inode, this_bh, block_to_free,
  2051. count, block_to_free_p, p);
  2052. if (this_bh) {
  2053. BUFFER_TRACE(this_bh, "call ext3_journal_dirty_metadata");
  2054. /*
  2055. * The buffer head should have an attached journal head at this
  2056. * point. However, if the data is corrupted and an indirect
  2057. * block pointed to itself, it would have been detached when
  2058. * the block was cleared. Check for this instead of OOPSing.
  2059. */
  2060. if (bh2jh(this_bh))
  2061. ext3_journal_dirty_metadata(handle, this_bh);
  2062. else
  2063. ext3_error(inode->i_sb, "ext3_free_data",
  2064. "circular indirect block detected, "
  2065. "inode=%lu, block=%llu",
  2066. inode->i_ino,
  2067. (unsigned long long)this_bh->b_blocknr);
  2068. }
  2069. }
  2070. /**
  2071. * ext3_free_branches - free an array of branches
  2072. * @handle: JBD handle for this transaction
  2073. * @inode: inode we are dealing with
  2074. * @parent_bh: the buffer_head which contains *@first and *@last
  2075. * @first: array of block numbers
  2076. * @last: pointer immediately past the end of array
  2077. * @depth: depth of the branches to free
  2078. *
  2079. * We are freeing all blocks referred from these branches (numbers are
  2080. * stored as little-endian 32-bit) and updating @inode->i_blocks
  2081. * appropriately.
  2082. */
  2083. static void ext3_free_branches(handle_t *handle, struct inode *inode,
  2084. struct buffer_head *parent_bh,
  2085. __le32 *first, __le32 *last, int depth)
  2086. {
  2087. ext3_fsblk_t nr;
  2088. __le32 *p;
  2089. if (is_handle_aborted(handle))
  2090. return;
  2091. if (depth--) {
  2092. struct buffer_head *bh;
  2093. int addr_per_block = EXT3_ADDR_PER_BLOCK(inode->i_sb);
  2094. p = last;
  2095. while (--p >= first) {
  2096. nr = le32_to_cpu(*p);
  2097. if (!nr)
  2098. continue; /* A hole */
  2099. /* Go read the buffer for the next level down */
  2100. bh = sb_bread(inode->i_sb, nr);
  2101. /*
  2102. * A read failure? Report error and clear slot
  2103. * (should be rare).
  2104. */
  2105. if (!bh) {
  2106. ext3_error(inode->i_sb, "ext3_free_branches",
  2107. "Read failure, inode=%lu, block="E3FSBLK,
  2108. inode->i_ino, nr);
  2109. continue;
  2110. }
  2111. /* This zaps the entire block. Bottom up. */
  2112. BUFFER_TRACE(bh, "free child branches");
  2113. ext3_free_branches(handle, inode, bh,
  2114. (__le32*)bh->b_data,
  2115. (__le32*)bh->b_data + addr_per_block,
  2116. depth);
  2117. /*
  2118. * Everything below this this pointer has been
  2119. * released. Now let this top-of-subtree go.
  2120. *
  2121. * We want the freeing of this indirect block to be
  2122. * atomic in the journal with the updating of the
  2123. * bitmap block which owns it. So make some room in
  2124. * the journal.
  2125. *
  2126. * We zero the parent pointer *after* freeing its
  2127. * pointee in the bitmaps, so if extend_transaction()
  2128. * for some reason fails to put the bitmap changes and
  2129. * the release into the same transaction, recovery
  2130. * will merely complain about releasing a free block,
  2131. * rather than leaking blocks.
  2132. */
  2133. if (is_handle_aborted(handle))
  2134. return;
  2135. if (try_to_extend_transaction(handle, inode)) {
  2136. ext3_mark_inode_dirty(handle, inode);
  2137. truncate_restart_transaction(handle, inode);
  2138. }
  2139. /*
  2140. * We've probably journalled the indirect block several
  2141. * times during the truncate. But it's no longer
  2142. * needed and we now drop it from the transaction via
  2143. * journal_revoke().
  2144. *
  2145. * That's easy if it's exclusively part of this
  2146. * transaction. But if it's part of the committing
  2147. * transaction then journal_forget() will simply
  2148. * brelse() it. That means that if the underlying
  2149. * block is reallocated in ext3_get_block(),
  2150. * unmap_underlying_metadata() will find this block
  2151. * and will try to get rid of it. damn, damn. Thus
  2152. * we don't allow a block to be reallocated until
  2153. * a transaction freeing it has fully committed.
  2154. *
  2155. * We also have to make sure journal replay after a
  2156. * crash does not overwrite non-journaled data blocks
  2157. * with old metadata when the block got reallocated for
  2158. * data. Thus we have to store a revoke record for a
  2159. * block in the same transaction in which we free the
  2160. * block.
  2161. */
  2162. ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
  2163. ext3_free_blocks(handle, inode, nr, 1);
  2164. if (parent_bh) {
  2165. /*
  2166. * The block which we have just freed is
  2167. * pointed to by an indirect block: journal it
  2168. */
  2169. BUFFER_TRACE(parent_bh, "get_write_access");
  2170. if (!ext3_journal_get_write_access(handle,
  2171. parent_bh)){
  2172. *p = 0;
  2173. BUFFER_TRACE(parent_bh,
  2174. "call ext3_journal_dirty_metadata");
  2175. ext3_journal_dirty_metadata(handle,
  2176. parent_bh);
  2177. }
  2178. }
  2179. }
  2180. } else {
  2181. /* We have reached the bottom of the tree. */
  2182. BUFFER_TRACE(parent_bh, "free data blocks");
  2183. ext3_free_data(handle, inode, parent_bh, first, last);
  2184. }
  2185. }
  2186. int ext3_can_truncate(struct inode *inode)
  2187. {
  2188. if (S_ISREG(inode->i_mode))
  2189. return 1;
  2190. if (S_ISDIR(inode->i_mode))
  2191. return 1;
  2192. if (S_ISLNK(inode->i_mode))
  2193. return !ext3_inode_is_fast_symlink(inode);
  2194. return 0;
  2195. }
  2196. /*
  2197. * ext3_truncate()
  2198. *
  2199. * We block out ext3_get_block() block instantiations across the entire
  2200. * transaction, and VFS/VM ensures that ext3_truncate() cannot run
  2201. * simultaneously on behalf of the same inode.
  2202. *
  2203. * As we work through the truncate and commmit bits of it to the journal there
  2204. * is one core, guiding principle: the file's tree must always be consistent on
  2205. * disk. We must be able to restart the truncate after a crash.
  2206. *
  2207. * The file's tree may be transiently inconsistent in memory (although it
  2208. * probably isn't), but whenever we close off and commit a journal transaction,
  2209. * the contents of (the filesystem + the journal) must be consistent and
  2210. * restartable. It's pretty simple, really: bottom up, right to left (although
  2211. * left-to-right works OK too).
  2212. *
  2213. * Note that at recovery time, journal replay occurs *before* the restart of
  2214. * truncate against the orphan inode list.
  2215. *
  2216. * The committed inode has the new, desired i_size (which is the same as
  2217. * i_disksize in this case). After a crash, ext3_orphan_cleanup() will see
  2218. * that this inode's truncate did not complete and it will again call
  2219. * ext3_truncate() to have another go. So there will be instantiated blocks
  2220. * to the right of the truncation point in a crashed ext3 filesystem. But
  2221. * that's fine - as long as they are linked from the inode, the post-crash
  2222. * ext3_truncate() run will find them and release them.
  2223. */
  2224. void ext3_truncate(struct inode *inode)
  2225. {
  2226. handle_t *handle;
  2227. struct ext3_inode_info *ei = EXT3_I(inode);
  2228. __le32 *i_data = ei->i_data;
  2229. int addr_per_block = EXT3_ADDR_PER_BLOCK(inode->i_sb);
  2230. struct address_space *mapping = inode->i_mapping;
  2231. int offsets[4];
  2232. Indirect chain[4];
  2233. Indirect *partial;
  2234. __le32 nr = 0;
  2235. int n;
  2236. long last_block;
  2237. unsigned blocksize = inode->i_sb->s_blocksize;
  2238. struct page *page;
  2239. trace_ext3_truncate_enter(inode);
  2240. if (!ext3_can_truncate(inode))
  2241. goto out_notrans;
  2242. if (inode->i_size == 0 && ext3_should_writeback_data(inode))
  2243. ext3_set_inode_state(inode, EXT3_STATE_FLUSH_ON_CLOSE);
  2244. /*
  2245. * We have to lock the EOF page here, because lock_page() nests
  2246. * outside journal_start().
  2247. */
  2248. if ((inode->i_size & (blocksize - 1)) == 0) {
  2249. /* Block boundary? Nothing to do */
  2250. page = NULL;
  2251. } else {
  2252. page = grab_cache_page(mapping,
  2253. inode->i_size >> PAGE_CACHE_SHIFT);
  2254. if (!page)
  2255. goto out_notrans;
  2256. }
  2257. handle = start_transaction(inode);
  2258. if (IS_ERR(handle)) {
  2259. if (page) {
  2260. clear_highpage(page);
  2261. flush_dcache_page(page);
  2262. unlock_page(page);
  2263. page_cache_release(page);
  2264. }
  2265. goto out_notrans;
  2266. }
  2267. last_block = (inode->i_size + blocksize-1)
  2268. >> EXT3_BLOCK_SIZE_BITS(inode->i_sb);
  2269. if (page)
  2270. ext3_block_truncate_page(handle, page, mapping, inode->i_size);
  2271. n = ext3_block_to_path(inode, last_block, offsets, NULL);
  2272. if (n == 0)
  2273. goto out_stop; /* error */
  2274. /*
  2275. * OK. This truncate is going to happen. We add the inode to the
  2276. * orphan list, so that if this truncate spans multiple transactions,
  2277. * and we crash, we will resume the truncate when the filesystem
  2278. * recovers. It also marks the inode dirty, to catch the new size.
  2279. *
  2280. * Implication: the file must always be in a sane, consistent
  2281. * truncatable state while each transaction commits.
  2282. */
  2283. if (ext3_orphan_add(handle, inode))
  2284. goto out_stop;
  2285. /*
  2286. * The orphan list entry will now protect us from any crash which
  2287. * occurs before the truncate completes, so it is now safe to propagate
  2288. * the new, shorter inode size (held for now in i_size) into the
  2289. * on-disk inode. We do this via i_disksize, which is the value which
  2290. * ext3 *really* writes onto the disk inode.
  2291. */
  2292. ei->i_disksize = inode->i_size;
  2293. /*
  2294. * From here we block out all ext3_get_block() callers who want to
  2295. * modify the block allocation tree.
  2296. */
  2297. mutex_lock(&ei->truncate_mutex);
  2298. if (n == 1) { /* direct blocks */
  2299. ext3_free_data(handle, inode, NULL, i_data+offsets[0],
  2300. i_data + EXT3_NDIR_BLOCKS);
  2301. goto do_indirects;
  2302. }
  2303. partial = ext3_find_shared(inode, n, offsets, chain, &nr);
  2304. /* Kill the top of shared branch (not detached) */
  2305. if (nr) {
  2306. if (partial == chain) {
  2307. /* Shared branch grows from the inode */
  2308. ext3_free_branches(handle, inode, NULL,
  2309. &nr, &nr+1, (chain+n-1) - partial);
  2310. *partial->p = 0;
  2311. /*
  2312. * We mark the inode dirty prior to restart,
  2313. * and prior to stop. No need for it here.
  2314. */
  2315. } else {
  2316. /* Shared branch grows from an indirect block */
  2317. ext3_free_branches(handle, inode, partial->bh,
  2318. partial->p,
  2319. partial->p+1, (chain+n-1) - partial);
  2320. }
  2321. }
  2322. /* Clear the ends of indirect blocks on the shared branch */
  2323. while (partial > chain) {
  2324. ext3_free_branches(handle, inode, partial->bh, partial->p + 1,
  2325. (__le32*)partial->bh->b_data+addr_per_block,
  2326. (chain+n-1) - partial);
  2327. BUFFER_TRACE(partial->bh, "call brelse");
  2328. brelse (partial->bh);
  2329. partial--;
  2330. }
  2331. do_indirects:
  2332. /* Kill the remaining (whole) subtrees */
  2333. switch (offsets[0]) {
  2334. default:
  2335. nr = i_data[EXT3_IND_BLOCK];
  2336. if (nr) {
  2337. ext3_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
  2338. i_data[EXT3_IND_BLOCK] = 0;
  2339. }
  2340. case EXT3_IND_BLOCK:
  2341. nr = i_data[EXT3_DIND_BLOCK];
  2342. if (nr) {
  2343. ext3_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
  2344. i_data[EXT3_DIND_BLOCK] = 0;
  2345. }
  2346. case EXT3_DIND_BLOCK:
  2347. nr = i_data[EXT3_TIND_BLOCK];
  2348. if (nr) {
  2349. ext3_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
  2350. i_data[EXT3_TIND_BLOCK] = 0;
  2351. }
  2352. case EXT3_TIND_BLOCK:
  2353. ;
  2354. }
  2355. ext3_discard_reservation(inode);
  2356. mutex_unlock(&ei->truncate_mutex);
  2357. inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
  2358. ext3_mark_inode_dirty(handle, inode);
  2359. /*
  2360. * In a multi-transaction truncate, we only make the final transaction
  2361. * synchronous
  2362. */
  2363. if (IS_SYNC(inode))
  2364. handle->h_sync = 1;
  2365. out_stop:
  2366. /*
  2367. * If this was a simple ftruncate(), and the file will remain alive
  2368. * then we need to clear up the orphan record which we created above.
  2369. * However, if this was a real unlink then we were called by
  2370. * ext3_evict_inode(), and we allow that function to clean up the
  2371. * orphan info for us.
  2372. */
  2373. if (inode->i_nlink)
  2374. ext3_orphan_del(handle, inode);
  2375. ext3_journal_stop(handle);
  2376. trace_ext3_truncate_exit(inode);
  2377. return;
  2378. out_notrans:
  2379. /*
  2380. * Delete the inode from orphan list so that it doesn't stay there
  2381. * forever and trigger assertion on umount.
  2382. */
  2383. if (inode->i_nlink)
  2384. ext3_orphan_del(NULL, inode);
  2385. trace_ext3_truncate_exit(inode);
  2386. }
  2387. static ext3_fsblk_t ext3_get_inode_block(struct super_block *sb,
  2388. unsigned long ino, struct ext3_iloc *iloc)
  2389. {
  2390. unsigned long block_group;
  2391. unsigned long offset;
  2392. ext3_fsblk_t block;
  2393. struct ext3_group_desc *gdp;
  2394. if (!ext3_valid_inum(sb, ino)) {
  2395. /*
  2396. * This error is already checked for in namei.c unless we are
  2397. * looking at an NFS filehandle, in which case no error
  2398. * report is needed
  2399. */
  2400. return 0;
  2401. }
  2402. block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
  2403. gdp = ext3_get_group_desc(sb, block_group, NULL);
  2404. if (!gdp)
  2405. return 0;
  2406. /*
  2407. * Figure out the offset within the block group inode table
  2408. */
  2409. offset = ((ino - 1) % EXT3_INODES_PER_GROUP(sb)) *
  2410. EXT3_INODE_SIZE(sb);
  2411. block = le32_to_cpu(gdp->bg_inode_table) +
  2412. (offset >> EXT3_BLOCK_SIZE_BITS(sb));
  2413. iloc->block_group = block_group;
  2414. iloc->offset = offset & (EXT3_BLOCK_SIZE(sb) - 1);
  2415. return block;
  2416. }
  2417. /*
  2418. * ext3_get_inode_loc returns with an extra refcount against the inode's
  2419. * underlying buffer_head on success. If 'in_mem' is true, we have all
  2420. * data in memory that is needed to recreate the on-disk version of this
  2421. * inode.
  2422. */
  2423. static int __ext3_get_inode_loc(struct inode *inode,
  2424. struct ext3_iloc *iloc, int in_mem)
  2425. {
  2426. ext3_fsblk_t block;
  2427. struct buffer_head *bh;
  2428. block = ext3_get_inode_block(inode->i_sb, inode->i_ino, iloc);
  2429. if (!block)
  2430. return -EIO;
  2431. bh = sb_getblk(inode->i_sb, block);
  2432. if (!bh) {
  2433. ext3_error (inode->i_sb, "ext3_get_inode_loc",
  2434. "unable to read inode block - "
  2435. "inode=%lu, block="E3FSBLK,
  2436. inode->i_ino, block);
  2437. return -EIO;
  2438. }
  2439. if (!buffer_uptodate(bh)) {
  2440. lock_buffer(bh);
  2441. /*
  2442. * If the buffer has the write error flag, we have failed
  2443. * to write out another inode in the same block. In this
  2444. * case, we don't have to read the block because we may
  2445. * read the old inode data successfully.
  2446. */
  2447. if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
  2448. set_buffer_uptodate(bh);
  2449. if (buffer_uptodate(bh)) {
  2450. /* someone brought it uptodate while we waited */
  2451. unlock_buffer(bh);
  2452. goto has_buffer;
  2453. }
  2454. /*
  2455. * If we have all information of the inode in memory and this
  2456. * is the only valid inode in the block, we need not read the
  2457. * block.
  2458. */
  2459. if (in_mem) {
  2460. struct buffer_head *bitmap_bh;
  2461. struct ext3_group_desc *desc;
  2462. int inodes_per_buffer;
  2463. int inode_offset, i;
  2464. int block_group;
  2465. int start;
  2466. block_group = (inode->i_ino - 1) /
  2467. EXT3_INODES_PER_GROUP(inode->i_sb);
  2468. inodes_per_buffer = bh->b_size /
  2469. EXT3_INODE_SIZE(inode->i_sb);
  2470. inode_offset = ((inode->i_ino - 1) %
  2471. EXT3_INODES_PER_GROUP(inode->i_sb));
  2472. start = inode_offset & ~(inodes_per_buffer - 1);
  2473. /* Is the inode bitmap in cache? */
  2474. desc = ext3_get_group_desc(inode->i_sb,
  2475. block_group, NULL);
  2476. if (!desc)
  2477. goto make_io;
  2478. bitmap_bh = sb_getblk(inode->i_sb,
  2479. le32_to_cpu(desc->bg_inode_bitmap));
  2480. if (!bitmap_bh)
  2481. goto make_io;
  2482. /*
  2483. * If the inode bitmap isn't in cache then the
  2484. * optimisation may end up performing two reads instead
  2485. * of one, so skip it.
  2486. */
  2487. if (!buffer_uptodate(bitmap_bh)) {
  2488. brelse(bitmap_bh);
  2489. goto make_io;
  2490. }
  2491. for (i = start; i < start + inodes_per_buffer; i++) {
  2492. if (i == inode_offset)
  2493. continue;
  2494. if (ext3_test_bit(i, bitmap_bh->b_data))
  2495. break;
  2496. }
  2497. brelse(bitmap_bh);
  2498. if (i == start + inodes_per_buffer) {
  2499. /* all other inodes are free, so skip I/O */
  2500. memset(bh->b_data, 0, bh->b_size);
  2501. set_buffer_uptodate(bh);
  2502. unlock_buffer(bh);
  2503. goto has_buffer;
  2504. }
  2505. }
  2506. make_io:
  2507. /*
  2508. * There are other valid inodes in the buffer, this inode
  2509. * has in-inode xattrs, or we don't have this inode in memory.
  2510. * Read the block from disk.
  2511. */
  2512. trace_ext3_load_inode(inode);
  2513. get_bh(bh);
  2514. bh->b_end_io = end_buffer_read_sync;
  2515. submit_bh(READ_META, bh);
  2516. wait_on_buffer(bh);
  2517. if (!buffer_uptodate(bh)) {
  2518. ext3_error(inode->i_sb, "ext3_get_inode_loc",
  2519. "unable to read inode block - "
  2520. "inode=%lu, block="E3FSBLK,
  2521. inode->i_ino, block);
  2522. brelse(bh);
  2523. return -EIO;
  2524. }
  2525. }
  2526. has_buffer:
  2527. iloc->bh = bh;
  2528. return 0;
  2529. }
  2530. int ext3_get_inode_loc(struct inode *inode, struct ext3_iloc *iloc)
  2531. {
  2532. /* We have all inode data except xattrs in memory here. */
  2533. return __ext3_get_inode_loc(inode, iloc,
  2534. !ext3_test_inode_state(inode, EXT3_STATE_XATTR));
  2535. }
  2536. void ext3_set_inode_flags(struct inode *inode)
  2537. {
  2538. unsigned int flags = EXT3_I(inode)->i_flags;
  2539. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  2540. if (flags & EXT3_SYNC_FL)
  2541. inode->i_flags |= S_SYNC;
  2542. if (flags & EXT3_APPEND_FL)
  2543. inode->i_flags |= S_APPEND;
  2544. if (flags & EXT3_IMMUTABLE_FL)
  2545. inode->i_flags |= S_IMMUTABLE;
  2546. if (flags & EXT3_NOATIME_FL)
  2547. inode->i_flags |= S_NOATIME;
  2548. if (flags & EXT3_DIRSYNC_FL)
  2549. inode->i_flags |= S_DIRSYNC;
  2550. }
  2551. /* Propagate flags from i_flags to EXT3_I(inode)->i_flags */
  2552. void ext3_get_inode_flags(struct ext3_inode_info *ei)
  2553. {
  2554. unsigned int flags = ei->vfs_inode.i_flags;
  2555. ei->i_flags &= ~(EXT3_SYNC_FL|EXT3_APPEND_FL|
  2556. EXT3_IMMUTABLE_FL|EXT3_NOATIME_FL|EXT3_DIRSYNC_FL);
  2557. if (flags & S_SYNC)
  2558. ei->i_flags |= EXT3_SYNC_FL;
  2559. if (flags & S_APPEND)
  2560. ei->i_flags |= EXT3_APPEND_FL;
  2561. if (flags & S_IMMUTABLE)
  2562. ei->i_flags |= EXT3_IMMUTABLE_FL;
  2563. if (flags & S_NOATIME)
  2564. ei->i_flags |= EXT3_NOATIME_FL;
  2565. if (flags & S_DIRSYNC)
  2566. ei->i_flags |= EXT3_DIRSYNC_FL;
  2567. }
  2568. struct inode *ext3_iget(struct super_block *sb, unsigned long ino)
  2569. {
  2570. struct ext3_iloc iloc;
  2571. struct ext3_inode *raw_inode;
  2572. struct ext3_inode_info *ei;
  2573. struct buffer_head *bh;
  2574. struct inode *inode;
  2575. journal_t *journal = EXT3_SB(sb)->s_journal;
  2576. transaction_t *transaction;
  2577. long ret;
  2578. int block;
  2579. inode = iget_locked(sb, ino);
  2580. if (!inode)
  2581. return ERR_PTR(-ENOMEM);
  2582. if (!(inode->i_state & I_NEW))
  2583. return inode;
  2584. ei = EXT3_I(inode);
  2585. ei->i_block_alloc_info = NULL;
  2586. ret = __ext3_get_inode_loc(inode, &iloc, 0);
  2587. if (ret < 0)
  2588. goto bad_inode;
  2589. bh = iloc.bh;
  2590. raw_inode = ext3_raw_inode(&iloc);
  2591. inode->i_mode = le16_to_cpu(raw_inode->i_mode);
  2592. inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
  2593. inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
  2594. if(!(test_opt (inode->i_sb, NO_UID32))) {
  2595. inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
  2596. inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
  2597. }
  2598. inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
  2599. inode->i_size = le32_to_cpu(raw_inode->i_size);
  2600. inode->i_atime.tv_sec = (signed)le32_to_cpu(raw_inode->i_atime);
  2601. inode->i_ctime.tv_sec = (signed)le32_to_cpu(raw_inode->i_ctime);
  2602. inode->i_mtime.tv_sec = (signed)le32_to_cpu(raw_inode->i_mtime);
  2603. inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = inode->i_mtime.tv_nsec = 0;
  2604. ei->i_state_flags = 0;
  2605. ei->i_dir_start_lookup = 0;
  2606. ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
  2607. /* We now have enough fields to check if the inode was active or not.
  2608. * This is needed because nfsd might try to access dead inodes
  2609. * the test is that same one that e2fsck uses
  2610. * NeilBrown 1999oct15
  2611. */
  2612. if (inode->i_nlink == 0) {
  2613. if (inode->i_mode == 0 ||
  2614. !(EXT3_SB(inode->i_sb)->s_mount_state & EXT3_ORPHAN_FS)) {
  2615. /* this inode is deleted */
  2616. brelse (bh);
  2617. ret = -ESTALE;
  2618. goto bad_inode;
  2619. }
  2620. /* The only unlinked inodes we let through here have
  2621. * valid i_mode and are being read by the orphan
  2622. * recovery code: that's fine, we're about to complete
  2623. * the process of deleting those. */
  2624. }
  2625. inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
  2626. ei->i_flags = le32_to_cpu(raw_inode->i_flags);
  2627. #ifdef EXT3_FRAGMENTS
  2628. ei->i_faddr = le32_to_cpu(raw_inode->i_faddr);
  2629. ei->i_frag_no = raw_inode->i_frag;
  2630. ei->i_frag_size = raw_inode->i_fsize;
  2631. #endif
  2632. ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
  2633. if (!S_ISREG(inode->i_mode)) {
  2634. ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
  2635. } else {
  2636. inode->i_size |=
  2637. ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
  2638. }
  2639. ei->i_disksize = inode->i_size;
  2640. inode->i_generation = le32_to_cpu(raw_inode->i_generation);
  2641. ei->i_block_group = iloc.block_group;
  2642. /*
  2643. * NOTE! The in-memory inode i_data array is in little-endian order
  2644. * even on big-endian machines: we do NOT byteswap the block numbers!
  2645. */
  2646. for (block = 0; block < EXT3_N_BLOCKS; block++)
  2647. ei->i_data[block] = raw_inode->i_block[block];
  2648. INIT_LIST_HEAD(&ei->i_orphan);
  2649. /*
  2650. * Set transaction id's of transactions that have to be committed
  2651. * to finish f[data]sync. We set them to currently running transaction
  2652. * as we cannot be sure that the inode or some of its metadata isn't
  2653. * part of the transaction - the inode could have been reclaimed and
  2654. * now it is reread from disk.
  2655. */
  2656. if (journal) {
  2657. tid_t tid;
  2658. spin_lock(&journal->j_state_lock);
  2659. if (journal->j_running_transaction)
  2660. transaction = journal->j_running_transaction;
  2661. else
  2662. transaction = journal->j_committing_transaction;
  2663. if (transaction)
  2664. tid = transaction->t_tid;
  2665. else
  2666. tid = journal->j_commit_sequence;
  2667. spin_unlock(&journal->j_state_lock);
  2668. atomic_set(&ei->i_sync_tid, tid);
  2669. atomic_set(&ei->i_datasync_tid, tid);
  2670. }
  2671. if (inode->i_ino >= EXT3_FIRST_INO(inode->i_sb) + 1 &&
  2672. EXT3_INODE_SIZE(inode->i_sb) > EXT3_GOOD_OLD_INODE_SIZE) {
  2673. /*
  2674. * When mke2fs creates big inodes it does not zero out
  2675. * the unused bytes above EXT3_GOOD_OLD_INODE_SIZE,
  2676. * so ignore those first few inodes.
  2677. */
  2678. ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
  2679. if (EXT3_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
  2680. EXT3_INODE_SIZE(inode->i_sb)) {
  2681. brelse (bh);
  2682. ret = -EIO;
  2683. goto bad_inode;
  2684. }
  2685. if (ei->i_extra_isize == 0) {
  2686. /* The extra space is currently unused. Use it. */
  2687. ei->i_extra_isize = sizeof(struct ext3_inode) -
  2688. EXT3_GOOD_OLD_INODE_SIZE;
  2689. } else {
  2690. __le32 *magic = (void *)raw_inode +
  2691. EXT3_GOOD_OLD_INODE_SIZE +
  2692. ei->i_extra_isize;
  2693. if (*magic == cpu_to_le32(EXT3_XATTR_MAGIC))
  2694. ext3_set_inode_state(inode, EXT3_STATE_XATTR);
  2695. }
  2696. } else
  2697. ei->i_extra_isize = 0;
  2698. if (S_ISREG(inode->i_mode)) {
  2699. inode->i_op = &ext3_file_inode_operations;
  2700. inode->i_fop = &ext3_file_operations;
  2701. ext3_set_aops(inode);
  2702. } else if (S_ISDIR(inode->i_mode)) {
  2703. inode->i_op = &ext3_dir_inode_operations;
  2704. inode->i_fop = &ext3_dir_operations;
  2705. } else if (S_ISLNK(inode->i_mode)) {
  2706. if (ext3_inode_is_fast_symlink(inode)) {
  2707. inode->i_op = &ext3_fast_symlink_inode_operations;
  2708. nd_terminate_link(ei->i_data, inode->i_size,
  2709. sizeof(ei->i_data) - 1);
  2710. } else {
  2711. inode->i_op = &ext3_symlink_inode_operations;
  2712. ext3_set_aops(inode);
  2713. }
  2714. } else {
  2715. inode->i_op = &ext3_special_inode_operations;
  2716. if (raw_inode->i_block[0])
  2717. init_special_inode(inode, inode->i_mode,
  2718. old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
  2719. else
  2720. init_special_inode(inode, inode->i_mode,
  2721. new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
  2722. }
  2723. brelse (iloc.bh);
  2724. ext3_set_inode_flags(inode);
  2725. unlock_new_inode(inode);
  2726. return inode;
  2727. bad_inode:
  2728. iget_failed(inode);
  2729. return ERR_PTR(ret);
  2730. }
  2731. /*
  2732. * Post the struct inode info into an on-disk inode location in the
  2733. * buffer-cache. This gobbles the caller's reference to the
  2734. * buffer_head in the inode location struct.
  2735. *
  2736. * The caller must have write access to iloc->bh.
  2737. */
  2738. static int ext3_do_update_inode(handle_t *handle,
  2739. struct inode *inode,
  2740. struct ext3_iloc *iloc)
  2741. {
  2742. struct ext3_inode *raw_inode = ext3_raw_inode(iloc);
  2743. struct ext3_inode_info *ei = EXT3_I(inode);
  2744. struct buffer_head *bh = iloc->bh;
  2745. int err = 0, rc, block;
  2746. again:
  2747. /* we can't allow multiple procs in here at once, its a bit racey */
  2748. lock_buffer(bh);
  2749. /* For fields not not tracking in the in-memory inode,
  2750. * initialise them to zero for new inodes. */
  2751. if (ext3_test_inode_state(inode, EXT3_STATE_NEW))
  2752. memset(raw_inode, 0, EXT3_SB(inode->i_sb)->s_inode_size);
  2753. ext3_get_inode_flags(ei);
  2754. raw_inode->i_mode = cpu_to_le16(inode->i_mode);
  2755. if(!(test_opt(inode->i_sb, NO_UID32))) {
  2756. raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
  2757. raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
  2758. /*
  2759. * Fix up interoperability with old kernels. Otherwise, old inodes get
  2760. * re-used with the upper 16 bits of the uid/gid intact
  2761. */
  2762. if(!ei->i_dtime) {
  2763. raw_inode->i_uid_high =
  2764. cpu_to_le16(high_16_bits(inode->i_uid));
  2765. raw_inode->i_gid_high =
  2766. cpu_to_le16(high_16_bits(inode->i_gid));
  2767. } else {
  2768. raw_inode->i_uid_high = 0;
  2769. raw_inode->i_gid_high = 0;
  2770. }
  2771. } else {
  2772. raw_inode->i_uid_low =
  2773. cpu_to_le16(fs_high2lowuid(inode->i_uid));
  2774. raw_inode->i_gid_low =
  2775. cpu_to_le16(fs_high2lowgid(inode->i_gid));
  2776. raw_inode->i_uid_high = 0;
  2777. raw_inode->i_gid_high = 0;
  2778. }
  2779. raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
  2780. raw_inode->i_size = cpu_to_le32(ei->i_disksize);
  2781. raw_inode->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
  2782. raw_inode->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
  2783. raw_inode->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
  2784. raw_inode->i_blocks = cpu_to_le32(inode->i_blocks);
  2785. raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
  2786. raw_inode->i_flags = cpu_to_le32(ei->i_flags);
  2787. #ifdef EXT3_FRAGMENTS
  2788. raw_inode->i_faddr = cpu_to_le32(ei->i_faddr);
  2789. raw_inode->i_frag = ei->i_frag_no;
  2790. raw_inode->i_fsize = ei->i_frag_size;
  2791. #endif
  2792. raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl);
  2793. if (!S_ISREG(inode->i_mode)) {
  2794. raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl);
  2795. } else {
  2796. raw_inode->i_size_high =
  2797. cpu_to_le32(ei->i_disksize >> 32);
  2798. if (ei->i_disksize > 0x7fffffffULL) {
  2799. struct super_block *sb = inode->i_sb;
  2800. if (!EXT3_HAS_RO_COMPAT_FEATURE(sb,
  2801. EXT3_FEATURE_RO_COMPAT_LARGE_FILE) ||
  2802. EXT3_SB(sb)->s_es->s_rev_level ==
  2803. cpu_to_le32(EXT3_GOOD_OLD_REV)) {
  2804. /* If this is the first large file
  2805. * created, add a flag to the superblock.
  2806. */
  2807. unlock_buffer(bh);
  2808. err = ext3_journal_get_write_access(handle,
  2809. EXT3_SB(sb)->s_sbh);
  2810. if (err)
  2811. goto out_brelse;
  2812. ext3_update_dynamic_rev(sb);
  2813. EXT3_SET_RO_COMPAT_FEATURE(sb,
  2814. EXT3_FEATURE_RO_COMPAT_LARGE_FILE);
  2815. handle->h_sync = 1;
  2816. err = ext3_journal_dirty_metadata(handle,
  2817. EXT3_SB(sb)->s_sbh);
  2818. /* get our lock and start over */
  2819. goto again;
  2820. }
  2821. }
  2822. }
  2823. raw_inode->i_generation = cpu_to_le32(inode->i_generation);
  2824. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  2825. if (old_valid_dev(inode->i_rdev)) {
  2826. raw_inode->i_block[0] =
  2827. cpu_to_le32(old_encode_dev(inode->i_rdev));
  2828. raw_inode->i_block[1] = 0;
  2829. } else {
  2830. raw_inode->i_block[0] = 0;
  2831. raw_inode->i_block[1] =
  2832. cpu_to_le32(new_encode_dev(inode->i_rdev));
  2833. raw_inode->i_block[2] = 0;
  2834. }
  2835. } else for (block = 0; block < EXT3_N_BLOCKS; block++)
  2836. raw_inode->i_block[block] = ei->i_data[block];
  2837. if (ei->i_extra_isize)
  2838. raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
  2839. BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
  2840. unlock_buffer(bh);
  2841. rc = ext3_journal_dirty_metadata(handle, bh);
  2842. if (!err)
  2843. err = rc;
  2844. ext3_clear_inode_state(inode, EXT3_STATE_NEW);
  2845. atomic_set(&ei->i_sync_tid, handle->h_transaction->t_tid);
  2846. out_brelse:
  2847. brelse (bh);
  2848. ext3_std_error(inode->i_sb, err);
  2849. return err;
  2850. }
  2851. /*
  2852. * ext3_write_inode()
  2853. *
  2854. * We are called from a few places:
  2855. *
  2856. * - Within generic_file_write() for O_SYNC files.
  2857. * Here, there will be no transaction running. We wait for any running
  2858. * trasnaction to commit.
  2859. *
  2860. * - Within sys_sync(), kupdate and such.
  2861. * We wait on commit, if tol to.
  2862. *
  2863. * - Within prune_icache() (PF_MEMALLOC == true)
  2864. * Here we simply return. We can't afford to block kswapd on the
  2865. * journal commit.
  2866. *
  2867. * In all cases it is actually safe for us to return without doing anything,
  2868. * because the inode has been copied into a raw inode buffer in
  2869. * ext3_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
  2870. * knfsd.
  2871. *
  2872. * Note that we are absolutely dependent upon all inode dirtiers doing the
  2873. * right thing: they *must* call mark_inode_dirty() after dirtying info in
  2874. * which we are interested.
  2875. *
  2876. * It would be a bug for them to not do this. The code:
  2877. *
  2878. * mark_inode_dirty(inode)
  2879. * stuff();
  2880. * inode->i_size = expr;
  2881. *
  2882. * is in error because a kswapd-driven write_inode() could occur while
  2883. * `stuff()' is running, and the new i_size will be lost. Plus the inode
  2884. * will no longer be on the superblock's dirty inode list.
  2885. */
  2886. int ext3_write_inode(struct inode *inode, struct writeback_control *wbc)
  2887. {
  2888. if (current->flags & PF_MEMALLOC)
  2889. return 0;
  2890. if (ext3_journal_current_handle()) {
  2891. jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
  2892. dump_stack();
  2893. return -EIO;
  2894. }
  2895. if (wbc->sync_mode != WB_SYNC_ALL)
  2896. return 0;
  2897. return ext3_force_commit(inode->i_sb);
  2898. }
  2899. /*
  2900. * ext3_setattr()
  2901. *
  2902. * Called from notify_change.
  2903. *
  2904. * We want to trap VFS attempts to truncate the file as soon as
  2905. * possible. In particular, we want to make sure that when the VFS
  2906. * shrinks i_size, we put the inode on the orphan list and modify
  2907. * i_disksize immediately, so that during the subsequent flushing of
  2908. * dirty pages and freeing of disk blocks, we can guarantee that any
  2909. * commit will leave the blocks being flushed in an unused state on
  2910. * disk. (On recovery, the inode will get truncated and the blocks will
  2911. * be freed, so we have a strong guarantee that no future commit will
  2912. * leave these blocks visible to the user.)
  2913. *
  2914. * Called with inode->sem down.
  2915. */
  2916. int ext3_setattr(struct dentry *dentry, struct iattr *attr)
  2917. {
  2918. struct inode *inode = dentry->d_inode;
  2919. int error, rc = 0;
  2920. const unsigned int ia_valid = attr->ia_valid;
  2921. error = inode_change_ok(inode, attr);
  2922. if (error)
  2923. return error;
  2924. if (is_quota_modification(inode, attr))
  2925. dquot_initialize(inode);
  2926. if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
  2927. (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
  2928. handle_t *handle;
  2929. /* (user+group)*(old+new) structure, inode write (sb,
  2930. * inode block, ? - but truncate inode update has it) */
  2931. handle = ext3_journal_start(inode, EXT3_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)+
  2932. EXT3_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)+3);
  2933. if (IS_ERR(handle)) {
  2934. error = PTR_ERR(handle);
  2935. goto err_out;
  2936. }
  2937. error = dquot_transfer(inode, attr);
  2938. if (error) {
  2939. ext3_journal_stop(handle);
  2940. return error;
  2941. }
  2942. /* Update corresponding info in inode so that everything is in
  2943. * one transaction */
  2944. if (attr->ia_valid & ATTR_UID)
  2945. inode->i_uid = attr->ia_uid;
  2946. if (attr->ia_valid & ATTR_GID)
  2947. inode->i_gid = attr->ia_gid;
  2948. error = ext3_mark_inode_dirty(handle, inode);
  2949. ext3_journal_stop(handle);
  2950. }
  2951. if (S_ISREG(inode->i_mode) &&
  2952. attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) {
  2953. handle_t *handle;
  2954. handle = ext3_journal_start(inode, 3);
  2955. if (IS_ERR(handle)) {
  2956. error = PTR_ERR(handle);
  2957. goto err_out;
  2958. }
  2959. error = ext3_orphan_add(handle, inode);
  2960. EXT3_I(inode)->i_disksize = attr->ia_size;
  2961. rc = ext3_mark_inode_dirty(handle, inode);
  2962. if (!error)
  2963. error = rc;
  2964. ext3_journal_stop(handle);
  2965. }
  2966. if ((attr->ia_valid & ATTR_SIZE) &&
  2967. attr->ia_size != i_size_read(inode)) {
  2968. truncate_setsize(inode, attr->ia_size);
  2969. ext3_truncate(inode);
  2970. }
  2971. setattr_copy(inode, attr);
  2972. mark_inode_dirty(inode);
  2973. if (ia_valid & ATTR_MODE)
  2974. rc = ext3_acl_chmod(inode);
  2975. err_out:
  2976. ext3_std_error(inode->i_sb, error);
  2977. if (!error)
  2978. error = rc;
  2979. return error;
  2980. }
  2981. /*
  2982. * How many blocks doth make a writepage()?
  2983. *
  2984. * With N blocks per page, it may be:
  2985. * N data blocks
  2986. * 2 indirect block
  2987. * 2 dindirect
  2988. * 1 tindirect
  2989. * N+5 bitmap blocks (from the above)
  2990. * N+5 group descriptor summary blocks
  2991. * 1 inode block
  2992. * 1 superblock.
  2993. * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quote files
  2994. *
  2995. * 3 * (N + 5) + 2 + 2 * EXT3_SINGLEDATA_TRANS_BLOCKS
  2996. *
  2997. * With ordered or writeback data it's the same, less the N data blocks.
  2998. *
  2999. * If the inode's direct blocks can hold an integral number of pages then a
  3000. * page cannot straddle two indirect blocks, and we can only touch one indirect
  3001. * and dindirect block, and the "5" above becomes "3".
  3002. *
  3003. * This still overestimates under most circumstances. If we were to pass the
  3004. * start and end offsets in here as well we could do block_to_path() on each
  3005. * block and work out the exact number of indirects which are touched. Pah.
  3006. */
  3007. static int ext3_writepage_trans_blocks(struct inode *inode)
  3008. {
  3009. int bpp = ext3_journal_blocks_per_page(inode);
  3010. int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
  3011. int ret;
  3012. if (ext3_should_journal_data(inode))
  3013. ret = 3 * (bpp + indirects) + 2;
  3014. else
  3015. ret = 2 * (bpp + indirects) + indirects + 2;
  3016. #ifdef CONFIG_QUOTA
  3017. /* We know that structure was already allocated during dquot_initialize so
  3018. * we will be updating only the data blocks + inodes */
  3019. ret += EXT3_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
  3020. #endif
  3021. return ret;
  3022. }
  3023. /*
  3024. * The caller must have previously called ext3_reserve_inode_write().
  3025. * Give this, we know that the caller already has write access to iloc->bh.
  3026. */
  3027. int ext3_mark_iloc_dirty(handle_t *handle,
  3028. struct inode *inode, struct ext3_iloc *iloc)
  3029. {
  3030. int err = 0;
  3031. /* the do_update_inode consumes one bh->b_count */
  3032. get_bh(iloc->bh);
  3033. /* ext3_do_update_inode() does journal_dirty_metadata */
  3034. err = ext3_do_update_inode(handle, inode, iloc);
  3035. put_bh(iloc->bh);
  3036. return err;
  3037. }
  3038. /*
  3039. * On success, We end up with an outstanding reference count against
  3040. * iloc->bh. This _must_ be cleaned up later.
  3041. */
  3042. int
  3043. ext3_reserve_inode_write(handle_t *handle, struct inode *inode,
  3044. struct ext3_iloc *iloc)
  3045. {
  3046. int err = 0;
  3047. if (handle) {
  3048. err = ext3_get_inode_loc(inode, iloc);
  3049. if (!err) {
  3050. BUFFER_TRACE(iloc->bh, "get_write_access");
  3051. err = ext3_journal_get_write_access(handle, iloc->bh);
  3052. if (err) {
  3053. brelse(iloc->bh);
  3054. iloc->bh = NULL;
  3055. }
  3056. }
  3057. }
  3058. ext3_std_error(inode->i_sb, err);
  3059. return err;
  3060. }
  3061. /*
  3062. * What we do here is to mark the in-core inode as clean with respect to inode
  3063. * dirtiness (it may still be data-dirty).
  3064. * This means that the in-core inode may be reaped by prune_icache
  3065. * without having to perform any I/O. This is a very good thing,
  3066. * because *any* task may call prune_icache - even ones which
  3067. * have a transaction open against a different journal.
  3068. *
  3069. * Is this cheating? Not really. Sure, we haven't written the
  3070. * inode out, but prune_icache isn't a user-visible syncing function.
  3071. * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
  3072. * we start and wait on commits.
  3073. *
  3074. * Is this efficient/effective? Well, we're being nice to the system
  3075. * by cleaning up our inodes proactively so they can be reaped
  3076. * without I/O. But we are potentially leaving up to five seconds'
  3077. * worth of inodes floating about which prune_icache wants us to
  3078. * write out. One way to fix that would be to get prune_icache()
  3079. * to do a write_super() to free up some memory. It has the desired
  3080. * effect.
  3081. */
  3082. int ext3_mark_inode_dirty(handle_t *handle, struct inode *inode)
  3083. {
  3084. struct ext3_iloc iloc;
  3085. int err;
  3086. might_sleep();
  3087. trace_ext3_mark_inode_dirty(inode, _RET_IP_);
  3088. err = ext3_reserve_inode_write(handle, inode, &iloc);
  3089. if (!err)
  3090. err = ext3_mark_iloc_dirty(handle, inode, &iloc);
  3091. return err;
  3092. }
  3093. /*
  3094. * ext3_dirty_inode() is called from __mark_inode_dirty()
  3095. *
  3096. * We're really interested in the case where a file is being extended.
  3097. * i_size has been changed by generic_commit_write() and we thus need
  3098. * to include the updated inode in the current transaction.
  3099. *
  3100. * Also, dquot_alloc_space() will always dirty the inode when blocks
  3101. * are allocated to the file.
  3102. *
  3103. * If the inode is marked synchronous, we don't honour that here - doing
  3104. * so would cause a commit on atime updates, which we don't bother doing.
  3105. * We handle synchronous inodes at the highest possible level.
  3106. */
  3107. void ext3_dirty_inode(struct inode *inode, int flags)
  3108. {
  3109. handle_t *current_handle = ext3_journal_current_handle();
  3110. handle_t *handle;
  3111. handle = ext3_journal_start(inode, 2);
  3112. if (IS_ERR(handle))
  3113. goto out;
  3114. if (current_handle &&
  3115. current_handle->h_transaction != handle->h_transaction) {
  3116. /* This task has a transaction open against a different fs */
  3117. printk(KERN_EMERG "%s: transactions do not match!\n",
  3118. __func__);
  3119. } else {
  3120. jbd_debug(5, "marking dirty. outer handle=%p\n",
  3121. current_handle);
  3122. ext3_mark_inode_dirty(handle, inode);
  3123. }
  3124. ext3_journal_stop(handle);
  3125. out:
  3126. return;
  3127. }
  3128. #if 0
  3129. /*
  3130. * Bind an inode's backing buffer_head into this transaction, to prevent
  3131. * it from being flushed to disk early. Unlike
  3132. * ext3_reserve_inode_write, this leaves behind no bh reference and
  3133. * returns no iloc structure, so the caller needs to repeat the iloc
  3134. * lookup to mark the inode dirty later.
  3135. */
  3136. static int ext3_pin_inode(handle_t *handle, struct inode *inode)
  3137. {
  3138. struct ext3_iloc iloc;
  3139. int err = 0;
  3140. if (handle) {
  3141. err = ext3_get_inode_loc(inode, &iloc);
  3142. if (!err) {
  3143. BUFFER_TRACE(iloc.bh, "get_write_access");
  3144. err = journal_get_write_access(handle, iloc.bh);
  3145. if (!err)
  3146. err = ext3_journal_dirty_metadata(handle,
  3147. iloc.bh);
  3148. brelse(iloc.bh);
  3149. }
  3150. }
  3151. ext3_std_error(inode->i_sb, err);
  3152. return err;
  3153. }
  3154. #endif
  3155. int ext3_change_inode_journal_flag(struct inode *inode, int val)
  3156. {
  3157. journal_t *journal;
  3158. handle_t *handle;
  3159. int err;
  3160. /*
  3161. * We have to be very careful here: changing a data block's
  3162. * journaling status dynamically is dangerous. If we write a
  3163. * data block to the journal, change the status and then delete
  3164. * that block, we risk forgetting to revoke the old log record
  3165. * from the journal and so a subsequent replay can corrupt data.
  3166. * So, first we make sure that the journal is empty and that
  3167. * nobody is changing anything.
  3168. */
  3169. journal = EXT3_JOURNAL(inode);
  3170. if (is_journal_aborted(journal))
  3171. return -EROFS;
  3172. journal_lock_updates(journal);
  3173. journal_flush(journal);
  3174. /*
  3175. * OK, there are no updates running now, and all cached data is
  3176. * synced to disk. We are now in a completely consistent state
  3177. * which doesn't have anything in the journal, and we know that
  3178. * no filesystem updates are running, so it is safe to modify
  3179. * the inode's in-core data-journaling state flag now.
  3180. */
  3181. if (val)
  3182. EXT3_I(inode)->i_flags |= EXT3_JOURNAL_DATA_FL;
  3183. else
  3184. EXT3_I(inode)->i_flags &= ~EXT3_JOURNAL_DATA_FL;
  3185. ext3_set_aops(inode);
  3186. journal_unlock_updates(journal);
  3187. /* Finally we can mark the inode as dirty. */
  3188. handle = ext3_journal_start(inode, 1);
  3189. if (IS_ERR(handle))
  3190. return PTR_ERR(handle);
  3191. err = ext3_mark_inode_dirty(handle, inode);
  3192. handle->h_sync = 1;
  3193. ext3_journal_stop(handle);
  3194. ext3_std_error(inode->i_sb, err);
  3195. return err;
  3196. }