inode.c 89 KB

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