inode.c 101 KB

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