inode.c 104 KB

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