extents.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947
  1. /*
  2. * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
  3. * Written by Alex Tomas <alex@clusterfs.com>
  4. *
  5. * Architecture independence:
  6. * Copyright (c) 2005, Bull S.A.
  7. * Written by Pierre Peiffer <pierre.peiffer@bull.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public Licens
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
  21. */
  22. /*
  23. * Extents support for EXT4
  24. *
  25. * TODO:
  26. * - ext4*_error() should be used in some situations
  27. * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
  28. * - smart tree reduction
  29. */
  30. #include <linux/module.h>
  31. #include <linux/fs.h>
  32. #include <linux/time.h>
  33. #include <linux/ext4_jbd2.h>
  34. #include <linux/jbd2.h>
  35. #include <linux/highuid.h>
  36. #include <linux/pagemap.h>
  37. #include <linux/quotaops.h>
  38. #include <linux/string.h>
  39. #include <linux/slab.h>
  40. #include <linux/falloc.h>
  41. #include <linux/ext4_fs_extents.h>
  42. #include <asm/uaccess.h>
  43. /*
  44. * ext_pblock:
  45. * combine low and high parts of physical block number into ext4_fsblk_t
  46. */
  47. static ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
  48. {
  49. ext4_fsblk_t block;
  50. block = le32_to_cpu(ex->ee_start_lo);
  51. block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1;
  52. return block;
  53. }
  54. /*
  55. * idx_pblock:
  56. * combine low and high parts of a leaf physical block number into ext4_fsblk_t
  57. */
  58. ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
  59. {
  60. ext4_fsblk_t block;
  61. block = le32_to_cpu(ix->ei_leaf_lo);
  62. block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1;
  63. return block;
  64. }
  65. /*
  66. * ext4_ext_store_pblock:
  67. * stores a large physical block number into an extent struct,
  68. * breaking it into parts
  69. */
  70. void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
  71. {
  72. ex->ee_start_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
  73. ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
  74. }
  75. /*
  76. * ext4_idx_store_pblock:
  77. * stores a large physical block number into an index struct,
  78. * breaking it into parts
  79. */
  80. static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb)
  81. {
  82. ix->ei_leaf_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
  83. ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
  84. }
  85. static handle_t *ext4_ext_journal_restart(handle_t *handle, int needed)
  86. {
  87. int err;
  88. if (handle->h_buffer_credits > needed)
  89. return handle;
  90. if (!ext4_journal_extend(handle, needed))
  91. return handle;
  92. err = ext4_journal_restart(handle, needed);
  93. return handle;
  94. }
  95. /*
  96. * could return:
  97. * - EROFS
  98. * - ENOMEM
  99. */
  100. static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
  101. struct ext4_ext_path *path)
  102. {
  103. if (path->p_bh) {
  104. /* path points to block */
  105. return ext4_journal_get_write_access(handle, path->p_bh);
  106. }
  107. /* path points to leaf/index in inode body */
  108. /* we use in-core data, no need to protect them */
  109. return 0;
  110. }
  111. /*
  112. * could return:
  113. * - EROFS
  114. * - ENOMEM
  115. * - EIO
  116. */
  117. static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
  118. struct ext4_ext_path *path)
  119. {
  120. int err;
  121. if (path->p_bh) {
  122. /* path points to block */
  123. err = ext4_journal_dirty_metadata(handle, path->p_bh);
  124. } else {
  125. /* path points to leaf/index in inode body */
  126. err = ext4_mark_inode_dirty(handle, inode);
  127. }
  128. return err;
  129. }
  130. static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
  131. struct ext4_ext_path *path,
  132. ext4_lblk_t block)
  133. {
  134. struct ext4_inode_info *ei = EXT4_I(inode);
  135. ext4_fsblk_t bg_start;
  136. ext4_fsblk_t last_block;
  137. ext4_grpblk_t colour;
  138. int depth;
  139. if (path) {
  140. struct ext4_extent *ex;
  141. depth = path->p_depth;
  142. /* try to predict block placement */
  143. ex = path[depth].p_ext;
  144. if (ex)
  145. return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
  146. /* it looks like index is empty;
  147. * try to find starting block from index itself */
  148. if (path[depth].p_bh)
  149. return path[depth].p_bh->b_blocknr;
  150. }
  151. /* OK. use inode's group */
  152. bg_start = (ei->i_block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) +
  153. le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block);
  154. last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
  155. if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
  156. colour = (current->pid % 16) *
  157. (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
  158. else
  159. colour = (current->pid % 16) * ((last_block - bg_start) / 16);
  160. return bg_start + colour + block;
  161. }
  162. static ext4_fsblk_t
  163. ext4_ext_new_block(handle_t *handle, struct inode *inode,
  164. struct ext4_ext_path *path,
  165. struct ext4_extent *ex, int *err)
  166. {
  167. ext4_fsblk_t goal, newblock;
  168. goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
  169. newblock = ext4_new_block(handle, inode, goal, err);
  170. return newblock;
  171. }
  172. static int ext4_ext_space_block(struct inode *inode)
  173. {
  174. int size;
  175. size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
  176. / sizeof(struct ext4_extent);
  177. #ifdef AGGRESSIVE_TEST
  178. if (size > 6)
  179. size = 6;
  180. #endif
  181. return size;
  182. }
  183. static int ext4_ext_space_block_idx(struct inode *inode)
  184. {
  185. int size;
  186. size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
  187. / sizeof(struct ext4_extent_idx);
  188. #ifdef AGGRESSIVE_TEST
  189. if (size > 5)
  190. size = 5;
  191. #endif
  192. return size;
  193. }
  194. static int ext4_ext_space_root(struct inode *inode)
  195. {
  196. int size;
  197. size = sizeof(EXT4_I(inode)->i_data);
  198. size -= sizeof(struct ext4_extent_header);
  199. size /= sizeof(struct ext4_extent);
  200. #ifdef AGGRESSIVE_TEST
  201. if (size > 3)
  202. size = 3;
  203. #endif
  204. return size;
  205. }
  206. static int ext4_ext_space_root_idx(struct inode *inode)
  207. {
  208. int size;
  209. size = sizeof(EXT4_I(inode)->i_data);
  210. size -= sizeof(struct ext4_extent_header);
  211. size /= sizeof(struct ext4_extent_idx);
  212. #ifdef AGGRESSIVE_TEST
  213. if (size > 4)
  214. size = 4;
  215. #endif
  216. return size;
  217. }
  218. static int
  219. ext4_ext_max_entries(struct inode *inode, int depth)
  220. {
  221. int max;
  222. if (depth == ext_depth(inode)) {
  223. if (depth == 0)
  224. max = ext4_ext_space_root(inode);
  225. else
  226. max = ext4_ext_space_root_idx(inode);
  227. } else {
  228. if (depth == 0)
  229. max = ext4_ext_space_block(inode);
  230. else
  231. max = ext4_ext_space_block_idx(inode);
  232. }
  233. return max;
  234. }
  235. static int __ext4_ext_check_header(const char *function, struct inode *inode,
  236. struct ext4_extent_header *eh,
  237. int depth)
  238. {
  239. const char *error_msg;
  240. int max = 0;
  241. if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
  242. error_msg = "invalid magic";
  243. goto corrupted;
  244. }
  245. if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
  246. error_msg = "unexpected eh_depth";
  247. goto corrupted;
  248. }
  249. if (unlikely(eh->eh_max == 0)) {
  250. error_msg = "invalid eh_max";
  251. goto corrupted;
  252. }
  253. max = ext4_ext_max_entries(inode, depth);
  254. if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
  255. error_msg = "too large eh_max";
  256. goto corrupted;
  257. }
  258. if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
  259. error_msg = "invalid eh_entries";
  260. goto corrupted;
  261. }
  262. return 0;
  263. corrupted:
  264. ext4_error(inode->i_sb, function,
  265. "bad header in inode #%lu: %s - magic %x, "
  266. "entries %u, max %u(%u), depth %u(%u)",
  267. inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
  268. le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
  269. max, le16_to_cpu(eh->eh_depth), depth);
  270. return -EIO;
  271. }
  272. #define ext4_ext_check_header(inode, eh, depth) \
  273. __ext4_ext_check_header(__FUNCTION__, inode, eh, depth)
  274. #ifdef EXT_DEBUG
  275. static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
  276. {
  277. int k, l = path->p_depth;
  278. ext_debug("path:");
  279. for (k = 0; k <= l; k++, path++) {
  280. if (path->p_idx) {
  281. ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
  282. idx_pblock(path->p_idx));
  283. } else if (path->p_ext) {
  284. ext_debug(" %d:%d:%llu ",
  285. le32_to_cpu(path->p_ext->ee_block),
  286. ext4_ext_get_actual_len(path->p_ext),
  287. ext_pblock(path->p_ext));
  288. } else
  289. ext_debug(" []");
  290. }
  291. ext_debug("\n");
  292. }
  293. static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
  294. {
  295. int depth = ext_depth(inode);
  296. struct ext4_extent_header *eh;
  297. struct ext4_extent *ex;
  298. int i;
  299. if (!path)
  300. return;
  301. eh = path[depth].p_hdr;
  302. ex = EXT_FIRST_EXTENT(eh);
  303. for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
  304. ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block),
  305. ext4_ext_get_actual_len(ex), ext_pblock(ex));
  306. }
  307. ext_debug("\n");
  308. }
  309. #else
  310. #define ext4_ext_show_path(inode,path)
  311. #define ext4_ext_show_leaf(inode,path)
  312. #endif
  313. void ext4_ext_drop_refs(struct ext4_ext_path *path)
  314. {
  315. int depth = path->p_depth;
  316. int i;
  317. for (i = 0; i <= depth; i++, path++)
  318. if (path->p_bh) {
  319. brelse(path->p_bh);
  320. path->p_bh = NULL;
  321. }
  322. }
  323. /*
  324. * ext4_ext_binsearch_idx:
  325. * binary search for the closest index of the given block
  326. * the header must be checked before calling this
  327. */
  328. static void
  329. ext4_ext_binsearch_idx(struct inode *inode,
  330. struct ext4_ext_path *path, ext4_lblk_t block)
  331. {
  332. struct ext4_extent_header *eh = path->p_hdr;
  333. struct ext4_extent_idx *r, *l, *m;
  334. ext_debug("binsearch for %u(idx): ", block);
  335. l = EXT_FIRST_INDEX(eh) + 1;
  336. r = EXT_LAST_INDEX(eh);
  337. while (l <= r) {
  338. m = l + (r - l) / 2;
  339. if (block < le32_to_cpu(m->ei_block))
  340. r = m - 1;
  341. else
  342. l = m + 1;
  343. ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
  344. m, le32_to_cpu(m->ei_block),
  345. r, le32_to_cpu(r->ei_block));
  346. }
  347. path->p_idx = l - 1;
  348. ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
  349. idx_pblock(path->p_idx));
  350. #ifdef CHECK_BINSEARCH
  351. {
  352. struct ext4_extent_idx *chix, *ix;
  353. int k;
  354. chix = ix = EXT_FIRST_INDEX(eh);
  355. for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
  356. if (k != 0 &&
  357. le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
  358. printk("k=%d, ix=0x%p, first=0x%p\n", k,
  359. ix, EXT_FIRST_INDEX(eh));
  360. printk("%u <= %u\n",
  361. le32_to_cpu(ix->ei_block),
  362. le32_to_cpu(ix[-1].ei_block));
  363. }
  364. BUG_ON(k && le32_to_cpu(ix->ei_block)
  365. <= le32_to_cpu(ix[-1].ei_block));
  366. if (block < le32_to_cpu(ix->ei_block))
  367. break;
  368. chix = ix;
  369. }
  370. BUG_ON(chix != path->p_idx);
  371. }
  372. #endif
  373. }
  374. /*
  375. * ext4_ext_binsearch:
  376. * binary search for closest extent of the given block
  377. * the header must be checked before calling this
  378. */
  379. static void
  380. ext4_ext_binsearch(struct inode *inode,
  381. struct ext4_ext_path *path, ext4_lblk_t block)
  382. {
  383. struct ext4_extent_header *eh = path->p_hdr;
  384. struct ext4_extent *r, *l, *m;
  385. if (eh->eh_entries == 0) {
  386. /*
  387. * this leaf is empty:
  388. * we get such a leaf in split/add case
  389. */
  390. return;
  391. }
  392. ext_debug("binsearch for %u: ", block);
  393. l = EXT_FIRST_EXTENT(eh) + 1;
  394. r = EXT_LAST_EXTENT(eh);
  395. while (l <= r) {
  396. m = l + (r - l) / 2;
  397. if (block < le32_to_cpu(m->ee_block))
  398. r = m - 1;
  399. else
  400. l = m + 1;
  401. ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
  402. m, le32_to_cpu(m->ee_block),
  403. r, le32_to_cpu(r->ee_block));
  404. }
  405. path->p_ext = l - 1;
  406. ext_debug(" -> %d:%llu:%d ",
  407. le32_to_cpu(path->p_ext->ee_block),
  408. ext_pblock(path->p_ext),
  409. ext4_ext_get_actual_len(path->p_ext));
  410. #ifdef CHECK_BINSEARCH
  411. {
  412. struct ext4_extent *chex, *ex;
  413. int k;
  414. chex = ex = EXT_FIRST_EXTENT(eh);
  415. for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
  416. BUG_ON(k && le32_to_cpu(ex->ee_block)
  417. <= le32_to_cpu(ex[-1].ee_block));
  418. if (block < le32_to_cpu(ex->ee_block))
  419. break;
  420. chex = ex;
  421. }
  422. BUG_ON(chex != path->p_ext);
  423. }
  424. #endif
  425. }
  426. int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
  427. {
  428. struct ext4_extent_header *eh;
  429. eh = ext_inode_hdr(inode);
  430. eh->eh_depth = 0;
  431. eh->eh_entries = 0;
  432. eh->eh_magic = EXT4_EXT_MAGIC;
  433. eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
  434. ext4_mark_inode_dirty(handle, inode);
  435. ext4_ext_invalidate_cache(inode);
  436. return 0;
  437. }
  438. struct ext4_ext_path *
  439. ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
  440. struct ext4_ext_path *path)
  441. {
  442. struct ext4_extent_header *eh;
  443. struct buffer_head *bh;
  444. short int depth, i, ppos = 0, alloc = 0;
  445. eh = ext_inode_hdr(inode);
  446. depth = ext_depth(inode);
  447. if (ext4_ext_check_header(inode, eh, depth))
  448. return ERR_PTR(-EIO);
  449. /* account possible depth increase */
  450. if (!path) {
  451. path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
  452. GFP_NOFS);
  453. if (!path)
  454. return ERR_PTR(-ENOMEM);
  455. alloc = 1;
  456. }
  457. path[0].p_hdr = eh;
  458. i = depth;
  459. /* walk through the tree */
  460. while (i) {
  461. ext_debug("depth %d: num %d, max %d\n",
  462. ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
  463. ext4_ext_binsearch_idx(inode, path + ppos, block);
  464. path[ppos].p_block = idx_pblock(path[ppos].p_idx);
  465. path[ppos].p_depth = i;
  466. path[ppos].p_ext = NULL;
  467. bh = sb_bread(inode->i_sb, path[ppos].p_block);
  468. if (!bh)
  469. goto err;
  470. eh = ext_block_hdr(bh);
  471. ppos++;
  472. BUG_ON(ppos > depth);
  473. path[ppos].p_bh = bh;
  474. path[ppos].p_hdr = eh;
  475. i--;
  476. if (ext4_ext_check_header(inode, eh, i))
  477. goto err;
  478. }
  479. path[ppos].p_depth = i;
  480. path[ppos].p_hdr = eh;
  481. path[ppos].p_ext = NULL;
  482. path[ppos].p_idx = NULL;
  483. /* find extent */
  484. ext4_ext_binsearch(inode, path + ppos, block);
  485. ext4_ext_show_path(inode, path);
  486. return path;
  487. err:
  488. ext4_ext_drop_refs(path);
  489. if (alloc)
  490. kfree(path);
  491. return ERR_PTR(-EIO);
  492. }
  493. /*
  494. * ext4_ext_insert_index:
  495. * insert new index [@logical;@ptr] into the block at @curp;
  496. * check where to insert: before @curp or after @curp
  497. */
  498. static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
  499. struct ext4_ext_path *curp,
  500. int logical, ext4_fsblk_t ptr)
  501. {
  502. struct ext4_extent_idx *ix;
  503. int len, err;
  504. err = ext4_ext_get_access(handle, inode, curp);
  505. if (err)
  506. return err;
  507. BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
  508. len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
  509. if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
  510. /* insert after */
  511. if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
  512. len = (len - 1) * sizeof(struct ext4_extent_idx);
  513. len = len < 0 ? 0 : len;
  514. ext_debug("insert new index %d after: %llu. "
  515. "move %d from 0x%p to 0x%p\n",
  516. logical, ptr, len,
  517. (curp->p_idx + 1), (curp->p_idx + 2));
  518. memmove(curp->p_idx + 2, curp->p_idx + 1, len);
  519. }
  520. ix = curp->p_idx + 1;
  521. } else {
  522. /* insert before */
  523. len = len * sizeof(struct ext4_extent_idx);
  524. len = len < 0 ? 0 : len;
  525. ext_debug("insert new index %d before: %llu. "
  526. "move %d from 0x%p to 0x%p\n",
  527. logical, ptr, len,
  528. curp->p_idx, (curp->p_idx + 1));
  529. memmove(curp->p_idx + 1, curp->p_idx, len);
  530. ix = curp->p_idx;
  531. }
  532. ix->ei_block = cpu_to_le32(logical);
  533. ext4_idx_store_pblock(ix, ptr);
  534. curp->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(curp->p_hdr->eh_entries)+1);
  535. BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
  536. > le16_to_cpu(curp->p_hdr->eh_max));
  537. BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
  538. err = ext4_ext_dirty(handle, inode, curp);
  539. ext4_std_error(inode->i_sb, err);
  540. return err;
  541. }
  542. /*
  543. * ext4_ext_split:
  544. * inserts new subtree into the path, using free index entry
  545. * at depth @at:
  546. * - allocates all needed blocks (new leaf and all intermediate index blocks)
  547. * - makes decision where to split
  548. * - moves remaining extents and index entries (right to the split point)
  549. * into the newly allocated blocks
  550. * - initializes subtree
  551. */
  552. static int ext4_ext_split(handle_t *handle, struct inode *inode,
  553. struct ext4_ext_path *path,
  554. struct ext4_extent *newext, int at)
  555. {
  556. struct buffer_head *bh = NULL;
  557. int depth = ext_depth(inode);
  558. struct ext4_extent_header *neh;
  559. struct ext4_extent_idx *fidx;
  560. struct ext4_extent *ex;
  561. int i = at, k, m, a;
  562. ext4_fsblk_t newblock, oldblock;
  563. __le32 border;
  564. ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
  565. int err = 0;
  566. /* make decision: where to split? */
  567. /* FIXME: now decision is simplest: at current extent */
  568. /* if current leaf will be split, then we should use
  569. * border from split point */
  570. BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
  571. if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
  572. border = path[depth].p_ext[1].ee_block;
  573. ext_debug("leaf will be split."
  574. " next leaf starts at %d\n",
  575. le32_to_cpu(border));
  576. } else {
  577. border = newext->ee_block;
  578. ext_debug("leaf will be added."
  579. " next leaf starts at %d\n",
  580. le32_to_cpu(border));
  581. }
  582. /*
  583. * If error occurs, then we break processing
  584. * and mark filesystem read-only. index won't
  585. * be inserted and tree will be in consistent
  586. * state. Next mount will repair buffers too.
  587. */
  588. /*
  589. * Get array to track all allocated blocks.
  590. * We need this to handle errors and free blocks
  591. * upon them.
  592. */
  593. ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
  594. if (!ablocks)
  595. return -ENOMEM;
  596. /* allocate all needed blocks */
  597. ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
  598. for (a = 0; a < depth - at; a++) {
  599. newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
  600. if (newblock == 0)
  601. goto cleanup;
  602. ablocks[a] = newblock;
  603. }
  604. /* initialize new leaf */
  605. newblock = ablocks[--a];
  606. BUG_ON(newblock == 0);
  607. bh = sb_getblk(inode->i_sb, newblock);
  608. if (!bh) {
  609. err = -EIO;
  610. goto cleanup;
  611. }
  612. lock_buffer(bh);
  613. err = ext4_journal_get_create_access(handle, bh);
  614. if (err)
  615. goto cleanup;
  616. neh = ext_block_hdr(bh);
  617. neh->eh_entries = 0;
  618. neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
  619. neh->eh_magic = EXT4_EXT_MAGIC;
  620. neh->eh_depth = 0;
  621. ex = EXT_FIRST_EXTENT(neh);
  622. /* move remainder of path[depth] to the new leaf */
  623. BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
  624. /* start copy from next extent */
  625. /* TODO: we could do it by single memmove */
  626. m = 0;
  627. path[depth].p_ext++;
  628. while (path[depth].p_ext <=
  629. EXT_MAX_EXTENT(path[depth].p_hdr)) {
  630. ext_debug("move %d:%llu:%d in new leaf %llu\n",
  631. le32_to_cpu(path[depth].p_ext->ee_block),
  632. ext_pblock(path[depth].p_ext),
  633. ext4_ext_get_actual_len(path[depth].p_ext),
  634. newblock);
  635. /*memmove(ex++, path[depth].p_ext++,
  636. sizeof(struct ext4_extent));
  637. neh->eh_entries++;*/
  638. path[depth].p_ext++;
  639. m++;
  640. }
  641. if (m) {
  642. memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
  643. neh->eh_entries = cpu_to_le16(le16_to_cpu(neh->eh_entries)+m);
  644. }
  645. set_buffer_uptodate(bh);
  646. unlock_buffer(bh);
  647. err = ext4_journal_dirty_metadata(handle, bh);
  648. if (err)
  649. goto cleanup;
  650. brelse(bh);
  651. bh = NULL;
  652. /* correct old leaf */
  653. if (m) {
  654. err = ext4_ext_get_access(handle, inode, path + depth);
  655. if (err)
  656. goto cleanup;
  657. path[depth].p_hdr->eh_entries =
  658. cpu_to_le16(le16_to_cpu(path[depth].p_hdr->eh_entries)-m);
  659. err = ext4_ext_dirty(handle, inode, path + depth);
  660. if (err)
  661. goto cleanup;
  662. }
  663. /* create intermediate indexes */
  664. k = depth - at - 1;
  665. BUG_ON(k < 0);
  666. if (k)
  667. ext_debug("create %d intermediate indices\n", k);
  668. /* insert new index into current index block */
  669. /* current depth stored in i var */
  670. i = depth - 1;
  671. while (k--) {
  672. oldblock = newblock;
  673. newblock = ablocks[--a];
  674. bh = sb_getblk(inode->i_sb, newblock);
  675. if (!bh) {
  676. err = -EIO;
  677. goto cleanup;
  678. }
  679. lock_buffer(bh);
  680. err = ext4_journal_get_create_access(handle, bh);
  681. if (err)
  682. goto cleanup;
  683. neh = ext_block_hdr(bh);
  684. neh->eh_entries = cpu_to_le16(1);
  685. neh->eh_magic = EXT4_EXT_MAGIC;
  686. neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
  687. neh->eh_depth = cpu_to_le16(depth - i);
  688. fidx = EXT_FIRST_INDEX(neh);
  689. fidx->ei_block = border;
  690. ext4_idx_store_pblock(fidx, oldblock);
  691. ext_debug("int.index at %d (block %llu): %u -> %llu\n",
  692. i, newblock, le32_to_cpu(border), oldblock);
  693. /* copy indexes */
  694. m = 0;
  695. path[i].p_idx++;
  696. ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
  697. EXT_MAX_INDEX(path[i].p_hdr));
  698. BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
  699. EXT_LAST_INDEX(path[i].p_hdr));
  700. while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
  701. ext_debug("%d: move %d:%llu in new index %llu\n", i,
  702. le32_to_cpu(path[i].p_idx->ei_block),
  703. idx_pblock(path[i].p_idx),
  704. newblock);
  705. /*memmove(++fidx, path[i].p_idx++,
  706. sizeof(struct ext4_extent_idx));
  707. neh->eh_entries++;
  708. BUG_ON(neh->eh_entries > neh->eh_max);*/
  709. path[i].p_idx++;
  710. m++;
  711. }
  712. if (m) {
  713. memmove(++fidx, path[i].p_idx - m,
  714. sizeof(struct ext4_extent_idx) * m);
  715. neh->eh_entries =
  716. cpu_to_le16(le16_to_cpu(neh->eh_entries) + m);
  717. }
  718. set_buffer_uptodate(bh);
  719. unlock_buffer(bh);
  720. err = ext4_journal_dirty_metadata(handle, bh);
  721. if (err)
  722. goto cleanup;
  723. brelse(bh);
  724. bh = NULL;
  725. /* correct old index */
  726. if (m) {
  727. err = ext4_ext_get_access(handle, inode, path + i);
  728. if (err)
  729. goto cleanup;
  730. path[i].p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path[i].p_hdr->eh_entries)-m);
  731. err = ext4_ext_dirty(handle, inode, path + i);
  732. if (err)
  733. goto cleanup;
  734. }
  735. i--;
  736. }
  737. /* insert new index */
  738. err = ext4_ext_insert_index(handle, inode, path + at,
  739. le32_to_cpu(border), newblock);
  740. cleanup:
  741. if (bh) {
  742. if (buffer_locked(bh))
  743. unlock_buffer(bh);
  744. brelse(bh);
  745. }
  746. if (err) {
  747. /* free all allocated blocks in error case */
  748. for (i = 0; i < depth; i++) {
  749. if (!ablocks[i])
  750. continue;
  751. ext4_free_blocks(handle, inode, ablocks[i], 1, 1);
  752. }
  753. }
  754. kfree(ablocks);
  755. return err;
  756. }
  757. /*
  758. * ext4_ext_grow_indepth:
  759. * implements tree growing procedure:
  760. * - allocates new block
  761. * - moves top-level data (index block or leaf) into the new block
  762. * - initializes new top-level, creating index that points to the
  763. * just created block
  764. */
  765. static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
  766. struct ext4_ext_path *path,
  767. struct ext4_extent *newext)
  768. {
  769. struct ext4_ext_path *curp = path;
  770. struct ext4_extent_header *neh;
  771. struct ext4_extent_idx *fidx;
  772. struct buffer_head *bh;
  773. ext4_fsblk_t newblock;
  774. int err = 0;
  775. newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
  776. if (newblock == 0)
  777. return err;
  778. bh = sb_getblk(inode->i_sb, newblock);
  779. if (!bh) {
  780. err = -EIO;
  781. ext4_std_error(inode->i_sb, err);
  782. return err;
  783. }
  784. lock_buffer(bh);
  785. err = ext4_journal_get_create_access(handle, bh);
  786. if (err) {
  787. unlock_buffer(bh);
  788. goto out;
  789. }
  790. /* move top-level index/leaf into new block */
  791. memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
  792. /* set size of new block */
  793. neh = ext_block_hdr(bh);
  794. /* old root could have indexes or leaves
  795. * so calculate e_max right way */
  796. if (ext_depth(inode))
  797. neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
  798. else
  799. neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
  800. neh->eh_magic = EXT4_EXT_MAGIC;
  801. set_buffer_uptodate(bh);
  802. unlock_buffer(bh);
  803. err = ext4_journal_dirty_metadata(handle, bh);
  804. if (err)
  805. goto out;
  806. /* create index in new top-level index: num,max,pointer */
  807. err = ext4_ext_get_access(handle, inode, curp);
  808. if (err)
  809. goto out;
  810. curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
  811. curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
  812. curp->p_hdr->eh_entries = cpu_to_le16(1);
  813. curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
  814. if (path[0].p_hdr->eh_depth)
  815. curp->p_idx->ei_block =
  816. EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
  817. else
  818. curp->p_idx->ei_block =
  819. EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
  820. ext4_idx_store_pblock(curp->p_idx, newblock);
  821. neh = ext_inode_hdr(inode);
  822. fidx = EXT_FIRST_INDEX(neh);
  823. ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
  824. le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
  825. le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
  826. neh->eh_depth = cpu_to_le16(path->p_depth + 1);
  827. err = ext4_ext_dirty(handle, inode, curp);
  828. out:
  829. brelse(bh);
  830. return err;
  831. }
  832. /*
  833. * ext4_ext_create_new_leaf:
  834. * finds empty index and adds new leaf.
  835. * if no free index is found, then it requests in-depth growing.
  836. */
  837. static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
  838. struct ext4_ext_path *path,
  839. struct ext4_extent *newext)
  840. {
  841. struct ext4_ext_path *curp;
  842. int depth, i, err = 0;
  843. repeat:
  844. i = depth = ext_depth(inode);
  845. /* walk up to the tree and look for free index entry */
  846. curp = path + depth;
  847. while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
  848. i--;
  849. curp--;
  850. }
  851. /* we use already allocated block for index block,
  852. * so subsequent data blocks should be contiguous */
  853. if (EXT_HAS_FREE_INDEX(curp)) {
  854. /* if we found index with free entry, then use that
  855. * entry: create all needed subtree and add new leaf */
  856. err = ext4_ext_split(handle, inode, path, newext, i);
  857. /* refill path */
  858. ext4_ext_drop_refs(path);
  859. path = ext4_ext_find_extent(inode,
  860. (ext4_lblk_t)le32_to_cpu(newext->ee_block),
  861. path);
  862. if (IS_ERR(path))
  863. err = PTR_ERR(path);
  864. } else {
  865. /* tree is full, time to grow in depth */
  866. err = ext4_ext_grow_indepth(handle, inode, path, newext);
  867. if (err)
  868. goto out;
  869. /* refill path */
  870. ext4_ext_drop_refs(path);
  871. path = ext4_ext_find_extent(inode,
  872. (ext4_lblk_t)le32_to_cpu(newext->ee_block),
  873. path);
  874. if (IS_ERR(path)) {
  875. err = PTR_ERR(path);
  876. goto out;
  877. }
  878. /*
  879. * only first (depth 0 -> 1) produces free space;
  880. * in all other cases we have to split the grown tree
  881. */
  882. depth = ext_depth(inode);
  883. if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
  884. /* now we need to split */
  885. goto repeat;
  886. }
  887. }
  888. out:
  889. return err;
  890. }
  891. /*
  892. * search the closest allocated block to the left for *logical
  893. * and returns it at @logical + it's physical address at @phys
  894. * if *logical is the smallest allocated block, the function
  895. * returns 0 at @phys
  896. * return value contains 0 (success) or error code
  897. */
  898. int
  899. ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
  900. ext4_lblk_t *logical, ext4_fsblk_t *phys)
  901. {
  902. struct ext4_extent_idx *ix;
  903. struct ext4_extent *ex;
  904. int depth, ee_len;
  905. BUG_ON(path == NULL);
  906. depth = path->p_depth;
  907. *phys = 0;
  908. if (depth == 0 && path->p_ext == NULL)
  909. return 0;
  910. /* usually extent in the path covers blocks smaller
  911. * then *logical, but it can be that extent is the
  912. * first one in the file */
  913. ex = path[depth].p_ext;
  914. ee_len = ext4_ext_get_actual_len(ex);
  915. if (*logical < le32_to_cpu(ex->ee_block)) {
  916. BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
  917. while (--depth >= 0) {
  918. ix = path[depth].p_idx;
  919. BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
  920. }
  921. return 0;
  922. }
  923. BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
  924. *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
  925. *phys = ext_pblock(ex) + ee_len - 1;
  926. return 0;
  927. }
  928. /*
  929. * search the closest allocated block to the right for *logical
  930. * and returns it at @logical + it's physical address at @phys
  931. * if *logical is the smallest allocated block, the function
  932. * returns 0 at @phys
  933. * return value contains 0 (success) or error code
  934. */
  935. int
  936. ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
  937. ext4_lblk_t *logical, ext4_fsblk_t *phys)
  938. {
  939. struct buffer_head *bh = NULL;
  940. struct ext4_extent_header *eh;
  941. struct ext4_extent_idx *ix;
  942. struct ext4_extent *ex;
  943. ext4_fsblk_t block;
  944. int depth, ee_len;
  945. BUG_ON(path == NULL);
  946. depth = path->p_depth;
  947. *phys = 0;
  948. if (depth == 0 && path->p_ext == NULL)
  949. return 0;
  950. /* usually extent in the path covers blocks smaller
  951. * then *logical, but it can be that extent is the
  952. * first one in the file */
  953. ex = path[depth].p_ext;
  954. ee_len = ext4_ext_get_actual_len(ex);
  955. if (*logical < le32_to_cpu(ex->ee_block)) {
  956. BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
  957. while (--depth >= 0) {
  958. ix = path[depth].p_idx;
  959. BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
  960. }
  961. *logical = le32_to_cpu(ex->ee_block);
  962. *phys = ext_pblock(ex);
  963. return 0;
  964. }
  965. BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
  966. if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
  967. /* next allocated block in this leaf */
  968. ex++;
  969. *logical = le32_to_cpu(ex->ee_block);
  970. *phys = ext_pblock(ex);
  971. return 0;
  972. }
  973. /* go up and search for index to the right */
  974. while (--depth >= 0) {
  975. ix = path[depth].p_idx;
  976. if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
  977. break;
  978. }
  979. if (depth < 0) {
  980. /* we've gone up to the root and
  981. * found no index to the right */
  982. return 0;
  983. }
  984. /* we've found index to the right, let's
  985. * follow it and find the closest allocated
  986. * block to the right */
  987. ix++;
  988. block = idx_pblock(ix);
  989. while (++depth < path->p_depth) {
  990. bh = sb_bread(inode->i_sb, block);
  991. if (bh == NULL)
  992. return -EIO;
  993. eh = ext_block_hdr(bh);
  994. if (ext4_ext_check_header(inode, eh, depth)) {
  995. put_bh(bh);
  996. return -EIO;
  997. }
  998. ix = EXT_FIRST_INDEX(eh);
  999. block = idx_pblock(ix);
  1000. put_bh(bh);
  1001. }
  1002. bh = sb_bread(inode->i_sb, block);
  1003. if (bh == NULL)
  1004. return -EIO;
  1005. eh = ext_block_hdr(bh);
  1006. if (ext4_ext_check_header(inode, eh, path->p_depth - depth)) {
  1007. put_bh(bh);
  1008. return -EIO;
  1009. }
  1010. ex = EXT_FIRST_EXTENT(eh);
  1011. *logical = le32_to_cpu(ex->ee_block);
  1012. *phys = ext_pblock(ex);
  1013. put_bh(bh);
  1014. return 0;
  1015. }
  1016. /*
  1017. * ext4_ext_next_allocated_block:
  1018. * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
  1019. * NOTE: it considers block number from index entry as
  1020. * allocated block. Thus, index entries have to be consistent
  1021. * with leaves.
  1022. */
  1023. static ext4_lblk_t
  1024. ext4_ext_next_allocated_block(struct ext4_ext_path *path)
  1025. {
  1026. int depth;
  1027. BUG_ON(path == NULL);
  1028. depth = path->p_depth;
  1029. if (depth == 0 && path->p_ext == NULL)
  1030. return EXT_MAX_BLOCK;
  1031. while (depth >= 0) {
  1032. if (depth == path->p_depth) {
  1033. /* leaf */
  1034. if (path[depth].p_ext !=
  1035. EXT_LAST_EXTENT(path[depth].p_hdr))
  1036. return le32_to_cpu(path[depth].p_ext[1].ee_block);
  1037. } else {
  1038. /* index */
  1039. if (path[depth].p_idx !=
  1040. EXT_LAST_INDEX(path[depth].p_hdr))
  1041. return le32_to_cpu(path[depth].p_idx[1].ei_block);
  1042. }
  1043. depth--;
  1044. }
  1045. return EXT_MAX_BLOCK;
  1046. }
  1047. /*
  1048. * ext4_ext_next_leaf_block:
  1049. * returns first allocated block from next leaf or EXT_MAX_BLOCK
  1050. */
  1051. static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
  1052. struct ext4_ext_path *path)
  1053. {
  1054. int depth;
  1055. BUG_ON(path == NULL);
  1056. depth = path->p_depth;
  1057. /* zero-tree has no leaf blocks at all */
  1058. if (depth == 0)
  1059. return EXT_MAX_BLOCK;
  1060. /* go to index block */
  1061. depth--;
  1062. while (depth >= 0) {
  1063. if (path[depth].p_idx !=
  1064. EXT_LAST_INDEX(path[depth].p_hdr))
  1065. return (ext4_lblk_t)
  1066. le32_to_cpu(path[depth].p_idx[1].ei_block);
  1067. depth--;
  1068. }
  1069. return EXT_MAX_BLOCK;
  1070. }
  1071. /*
  1072. * ext4_ext_correct_indexes:
  1073. * if leaf gets modified and modified extent is first in the leaf,
  1074. * then we have to correct all indexes above.
  1075. * TODO: do we need to correct tree in all cases?
  1076. */
  1077. static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
  1078. struct ext4_ext_path *path)
  1079. {
  1080. struct ext4_extent_header *eh;
  1081. int depth = ext_depth(inode);
  1082. struct ext4_extent *ex;
  1083. __le32 border;
  1084. int k, err = 0;
  1085. eh = path[depth].p_hdr;
  1086. ex = path[depth].p_ext;
  1087. BUG_ON(ex == NULL);
  1088. BUG_ON(eh == NULL);
  1089. if (depth == 0) {
  1090. /* there is no tree at all */
  1091. return 0;
  1092. }
  1093. if (ex != EXT_FIRST_EXTENT(eh)) {
  1094. /* we correct tree if first leaf got modified only */
  1095. return 0;
  1096. }
  1097. /*
  1098. * TODO: we need correction if border is smaller than current one
  1099. */
  1100. k = depth - 1;
  1101. border = path[depth].p_ext->ee_block;
  1102. err = ext4_ext_get_access(handle, inode, path + k);
  1103. if (err)
  1104. return err;
  1105. path[k].p_idx->ei_block = border;
  1106. err = ext4_ext_dirty(handle, inode, path + k);
  1107. if (err)
  1108. return err;
  1109. while (k--) {
  1110. /* change all left-side indexes */
  1111. if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
  1112. break;
  1113. err = ext4_ext_get_access(handle, inode, path + k);
  1114. if (err)
  1115. break;
  1116. path[k].p_idx->ei_block = border;
  1117. err = ext4_ext_dirty(handle, inode, path + k);
  1118. if (err)
  1119. break;
  1120. }
  1121. return err;
  1122. }
  1123. static int
  1124. ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
  1125. struct ext4_extent *ex2)
  1126. {
  1127. unsigned short ext1_ee_len, ext2_ee_len, max_len;
  1128. /*
  1129. * Make sure that either both extents are uninitialized, or
  1130. * both are _not_.
  1131. */
  1132. if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
  1133. return 0;
  1134. if (ext4_ext_is_uninitialized(ex1))
  1135. max_len = EXT_UNINIT_MAX_LEN;
  1136. else
  1137. max_len = EXT_INIT_MAX_LEN;
  1138. ext1_ee_len = ext4_ext_get_actual_len(ex1);
  1139. ext2_ee_len = ext4_ext_get_actual_len(ex2);
  1140. if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
  1141. le32_to_cpu(ex2->ee_block))
  1142. return 0;
  1143. /*
  1144. * To allow future support for preallocated extents to be added
  1145. * as an RO_COMPAT feature, refuse to merge to extents if
  1146. * this can result in the top bit of ee_len being set.
  1147. */
  1148. if (ext1_ee_len + ext2_ee_len > max_len)
  1149. return 0;
  1150. #ifdef AGGRESSIVE_TEST
  1151. if (ext1_ee_len >= 4)
  1152. return 0;
  1153. #endif
  1154. if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
  1155. return 1;
  1156. return 0;
  1157. }
  1158. /*
  1159. * This function tries to merge the "ex" extent to the next extent in the tree.
  1160. * It always tries to merge towards right. If you want to merge towards
  1161. * left, pass "ex - 1" as argument instead of "ex".
  1162. * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
  1163. * 1 if they got merged.
  1164. */
  1165. int ext4_ext_try_to_merge(struct inode *inode,
  1166. struct ext4_ext_path *path,
  1167. struct ext4_extent *ex)
  1168. {
  1169. struct ext4_extent_header *eh;
  1170. unsigned int depth, len;
  1171. int merge_done = 0;
  1172. int uninitialized = 0;
  1173. depth = ext_depth(inode);
  1174. BUG_ON(path[depth].p_hdr == NULL);
  1175. eh = path[depth].p_hdr;
  1176. while (ex < EXT_LAST_EXTENT(eh)) {
  1177. if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
  1178. break;
  1179. /* merge with next extent! */
  1180. if (ext4_ext_is_uninitialized(ex))
  1181. uninitialized = 1;
  1182. ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
  1183. + ext4_ext_get_actual_len(ex + 1));
  1184. if (uninitialized)
  1185. ext4_ext_mark_uninitialized(ex);
  1186. if (ex + 1 < EXT_LAST_EXTENT(eh)) {
  1187. len = (EXT_LAST_EXTENT(eh) - ex - 1)
  1188. * sizeof(struct ext4_extent);
  1189. memmove(ex + 1, ex + 2, len);
  1190. }
  1191. eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries) - 1);
  1192. merge_done = 1;
  1193. WARN_ON(eh->eh_entries == 0);
  1194. if (!eh->eh_entries)
  1195. ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
  1196. "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
  1197. }
  1198. return merge_done;
  1199. }
  1200. /*
  1201. * check if a portion of the "newext" extent overlaps with an
  1202. * existing extent.
  1203. *
  1204. * If there is an overlap discovered, it updates the length of the newext
  1205. * such that there will be no overlap, and then returns 1.
  1206. * If there is no overlap found, it returns 0.
  1207. */
  1208. unsigned int ext4_ext_check_overlap(struct inode *inode,
  1209. struct ext4_extent *newext,
  1210. struct ext4_ext_path *path)
  1211. {
  1212. ext4_lblk_t b1, b2;
  1213. unsigned int depth, len1;
  1214. unsigned int ret = 0;
  1215. b1 = le32_to_cpu(newext->ee_block);
  1216. len1 = ext4_ext_get_actual_len(newext);
  1217. depth = ext_depth(inode);
  1218. if (!path[depth].p_ext)
  1219. goto out;
  1220. b2 = le32_to_cpu(path[depth].p_ext->ee_block);
  1221. /*
  1222. * get the next allocated block if the extent in the path
  1223. * is before the requested block(s)
  1224. */
  1225. if (b2 < b1) {
  1226. b2 = ext4_ext_next_allocated_block(path);
  1227. if (b2 == EXT_MAX_BLOCK)
  1228. goto out;
  1229. }
  1230. /* check for wrap through zero on extent logical start block*/
  1231. if (b1 + len1 < b1) {
  1232. len1 = EXT_MAX_BLOCK - b1;
  1233. newext->ee_len = cpu_to_le16(len1);
  1234. ret = 1;
  1235. }
  1236. /* check for overlap */
  1237. if (b1 + len1 > b2) {
  1238. newext->ee_len = cpu_to_le16(b2 - b1);
  1239. ret = 1;
  1240. }
  1241. out:
  1242. return ret;
  1243. }
  1244. /*
  1245. * ext4_ext_insert_extent:
  1246. * tries to merge requsted extent into the existing extent or
  1247. * inserts requested extent as new one into the tree,
  1248. * creating new leaf in the no-space case.
  1249. */
  1250. int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
  1251. struct ext4_ext_path *path,
  1252. struct ext4_extent *newext)
  1253. {
  1254. struct ext4_extent_header * eh;
  1255. struct ext4_extent *ex, *fex;
  1256. struct ext4_extent *nearex; /* nearest extent */
  1257. struct ext4_ext_path *npath = NULL;
  1258. int depth, len, err;
  1259. ext4_lblk_t next;
  1260. unsigned uninitialized = 0;
  1261. BUG_ON(ext4_ext_get_actual_len(newext) == 0);
  1262. depth = ext_depth(inode);
  1263. ex = path[depth].p_ext;
  1264. BUG_ON(path[depth].p_hdr == NULL);
  1265. /* try to insert block into found extent and return */
  1266. if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
  1267. ext_debug("append %d block to %d:%d (from %llu)\n",
  1268. ext4_ext_get_actual_len(newext),
  1269. le32_to_cpu(ex->ee_block),
  1270. ext4_ext_get_actual_len(ex), ext_pblock(ex));
  1271. err = ext4_ext_get_access(handle, inode, path + depth);
  1272. if (err)
  1273. return err;
  1274. /*
  1275. * ext4_can_extents_be_merged should have checked that either
  1276. * both extents are uninitialized, or both aren't. Thus we
  1277. * need to check only one of them here.
  1278. */
  1279. if (ext4_ext_is_uninitialized(ex))
  1280. uninitialized = 1;
  1281. ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
  1282. + ext4_ext_get_actual_len(newext));
  1283. if (uninitialized)
  1284. ext4_ext_mark_uninitialized(ex);
  1285. eh = path[depth].p_hdr;
  1286. nearex = ex;
  1287. goto merge;
  1288. }
  1289. repeat:
  1290. depth = ext_depth(inode);
  1291. eh = path[depth].p_hdr;
  1292. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
  1293. goto has_space;
  1294. /* probably next leaf has space for us? */
  1295. fex = EXT_LAST_EXTENT(eh);
  1296. next = ext4_ext_next_leaf_block(inode, path);
  1297. if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
  1298. && next != EXT_MAX_BLOCK) {
  1299. ext_debug("next leaf block - %d\n", next);
  1300. BUG_ON(npath != NULL);
  1301. npath = ext4_ext_find_extent(inode, next, NULL);
  1302. if (IS_ERR(npath))
  1303. return PTR_ERR(npath);
  1304. BUG_ON(npath->p_depth != path->p_depth);
  1305. eh = npath[depth].p_hdr;
  1306. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
  1307. ext_debug("next leaf isnt full(%d)\n",
  1308. le16_to_cpu(eh->eh_entries));
  1309. path = npath;
  1310. goto repeat;
  1311. }
  1312. ext_debug("next leaf has no free space(%d,%d)\n",
  1313. le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
  1314. }
  1315. /*
  1316. * There is no free space in the found leaf.
  1317. * We're gonna add a new leaf in the tree.
  1318. */
  1319. err = ext4_ext_create_new_leaf(handle, inode, path, newext);
  1320. if (err)
  1321. goto cleanup;
  1322. depth = ext_depth(inode);
  1323. eh = path[depth].p_hdr;
  1324. has_space:
  1325. nearex = path[depth].p_ext;
  1326. err = ext4_ext_get_access(handle, inode, path + depth);
  1327. if (err)
  1328. goto cleanup;
  1329. if (!nearex) {
  1330. /* there is no extent in this leaf, create first one */
  1331. ext_debug("first extent in the leaf: %d:%llu:%d\n",
  1332. le32_to_cpu(newext->ee_block),
  1333. ext_pblock(newext),
  1334. ext4_ext_get_actual_len(newext));
  1335. path[depth].p_ext = EXT_FIRST_EXTENT(eh);
  1336. } else if (le32_to_cpu(newext->ee_block)
  1337. > le32_to_cpu(nearex->ee_block)) {
  1338. /* BUG_ON(newext->ee_block == nearex->ee_block); */
  1339. if (nearex != EXT_LAST_EXTENT(eh)) {
  1340. len = EXT_MAX_EXTENT(eh) - nearex;
  1341. len = (len - 1) * sizeof(struct ext4_extent);
  1342. len = len < 0 ? 0 : len;
  1343. ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
  1344. "move %d from 0x%p to 0x%p\n",
  1345. le32_to_cpu(newext->ee_block),
  1346. ext_pblock(newext),
  1347. ext4_ext_get_actual_len(newext),
  1348. nearex, len, nearex + 1, nearex + 2);
  1349. memmove(nearex + 2, nearex + 1, len);
  1350. }
  1351. path[depth].p_ext = nearex + 1;
  1352. } else {
  1353. BUG_ON(newext->ee_block == nearex->ee_block);
  1354. len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
  1355. len = len < 0 ? 0 : len;
  1356. ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
  1357. "move %d from 0x%p to 0x%p\n",
  1358. le32_to_cpu(newext->ee_block),
  1359. ext_pblock(newext),
  1360. ext4_ext_get_actual_len(newext),
  1361. nearex, len, nearex + 1, nearex + 2);
  1362. memmove(nearex + 1, nearex, len);
  1363. path[depth].p_ext = nearex;
  1364. }
  1365. eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)+1);
  1366. nearex = path[depth].p_ext;
  1367. nearex->ee_block = newext->ee_block;
  1368. ext4_ext_store_pblock(nearex, ext_pblock(newext));
  1369. nearex->ee_len = newext->ee_len;
  1370. merge:
  1371. /* try to merge extents to the right */
  1372. ext4_ext_try_to_merge(inode, path, nearex);
  1373. /* try to merge extents to the left */
  1374. /* time to correct all indexes above */
  1375. err = ext4_ext_correct_indexes(handle, inode, path);
  1376. if (err)
  1377. goto cleanup;
  1378. err = ext4_ext_dirty(handle, inode, path + depth);
  1379. cleanup:
  1380. if (npath) {
  1381. ext4_ext_drop_refs(npath);
  1382. kfree(npath);
  1383. }
  1384. ext4_ext_tree_changed(inode);
  1385. ext4_ext_invalidate_cache(inode);
  1386. return err;
  1387. }
  1388. static void
  1389. ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
  1390. __u32 len, ext4_fsblk_t start, int type)
  1391. {
  1392. struct ext4_ext_cache *cex;
  1393. BUG_ON(len == 0);
  1394. cex = &EXT4_I(inode)->i_cached_extent;
  1395. cex->ec_type = type;
  1396. cex->ec_block = block;
  1397. cex->ec_len = len;
  1398. cex->ec_start = start;
  1399. }
  1400. /*
  1401. * ext4_ext_put_gap_in_cache:
  1402. * calculate boundaries of the gap that the requested block fits into
  1403. * and cache this gap
  1404. */
  1405. static void
  1406. ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
  1407. ext4_lblk_t block)
  1408. {
  1409. int depth = ext_depth(inode);
  1410. unsigned long len;
  1411. ext4_lblk_t lblock;
  1412. struct ext4_extent *ex;
  1413. ex = path[depth].p_ext;
  1414. if (ex == NULL) {
  1415. /* there is no extent yet, so gap is [0;-] */
  1416. lblock = 0;
  1417. len = EXT_MAX_BLOCK;
  1418. ext_debug("cache gap(whole file):");
  1419. } else if (block < le32_to_cpu(ex->ee_block)) {
  1420. lblock = block;
  1421. len = le32_to_cpu(ex->ee_block) - block;
  1422. ext_debug("cache gap(before): %u [%u:%u]",
  1423. block,
  1424. le32_to_cpu(ex->ee_block),
  1425. ext4_ext_get_actual_len(ex));
  1426. } else if (block >= le32_to_cpu(ex->ee_block)
  1427. + ext4_ext_get_actual_len(ex)) {
  1428. ext4_lblk_t next;
  1429. lblock = le32_to_cpu(ex->ee_block)
  1430. + ext4_ext_get_actual_len(ex);
  1431. next = ext4_ext_next_allocated_block(path);
  1432. ext_debug("cache gap(after): [%u:%u] %u",
  1433. le32_to_cpu(ex->ee_block),
  1434. ext4_ext_get_actual_len(ex),
  1435. block);
  1436. BUG_ON(next == lblock);
  1437. len = next - lblock;
  1438. } else {
  1439. lblock = len = 0;
  1440. BUG();
  1441. }
  1442. ext_debug(" -> %u:%lu\n", lblock, len);
  1443. ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
  1444. }
  1445. static int
  1446. ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
  1447. struct ext4_extent *ex)
  1448. {
  1449. struct ext4_ext_cache *cex;
  1450. cex = &EXT4_I(inode)->i_cached_extent;
  1451. /* has cache valid data? */
  1452. if (cex->ec_type == EXT4_EXT_CACHE_NO)
  1453. return EXT4_EXT_CACHE_NO;
  1454. BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
  1455. cex->ec_type != EXT4_EXT_CACHE_EXTENT);
  1456. if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
  1457. ex->ee_block = cpu_to_le32(cex->ec_block);
  1458. ext4_ext_store_pblock(ex, cex->ec_start);
  1459. ex->ee_len = cpu_to_le16(cex->ec_len);
  1460. ext_debug("%u cached by %u:%u:%llu\n",
  1461. block,
  1462. cex->ec_block, cex->ec_len, cex->ec_start);
  1463. return cex->ec_type;
  1464. }
  1465. /* not in cache */
  1466. return EXT4_EXT_CACHE_NO;
  1467. }
  1468. /*
  1469. * ext4_ext_rm_idx:
  1470. * removes index from the index block.
  1471. * It's used in truncate case only, thus all requests are for
  1472. * last index in the block only.
  1473. */
  1474. static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
  1475. struct ext4_ext_path *path)
  1476. {
  1477. struct buffer_head *bh;
  1478. int err;
  1479. ext4_fsblk_t leaf;
  1480. /* free index block */
  1481. path--;
  1482. leaf = idx_pblock(path->p_idx);
  1483. BUG_ON(path->p_hdr->eh_entries == 0);
  1484. err = ext4_ext_get_access(handle, inode, path);
  1485. if (err)
  1486. return err;
  1487. path->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path->p_hdr->eh_entries)-1);
  1488. err = ext4_ext_dirty(handle, inode, path);
  1489. if (err)
  1490. return err;
  1491. ext_debug("index is empty, remove it, free block %llu\n", leaf);
  1492. bh = sb_find_get_block(inode->i_sb, leaf);
  1493. ext4_forget(handle, 1, inode, bh, leaf);
  1494. ext4_free_blocks(handle, inode, leaf, 1, 1);
  1495. return err;
  1496. }
  1497. /*
  1498. * ext4_ext_calc_credits_for_insert:
  1499. * This routine returns max. credits that the extent tree can consume.
  1500. * It should be OK for low-performance paths like ->writepage()
  1501. * To allow many writing processes to fit into a single transaction,
  1502. * the caller should calculate credits under i_data_sem and
  1503. * pass the actual path.
  1504. */
  1505. int ext4_ext_calc_credits_for_insert(struct inode *inode,
  1506. struct ext4_ext_path *path)
  1507. {
  1508. int depth, needed;
  1509. if (path) {
  1510. /* probably there is space in leaf? */
  1511. depth = ext_depth(inode);
  1512. if (le16_to_cpu(path[depth].p_hdr->eh_entries)
  1513. < le16_to_cpu(path[depth].p_hdr->eh_max))
  1514. return 1;
  1515. }
  1516. /*
  1517. * given 32-bit logical block (4294967296 blocks), max. tree
  1518. * can be 4 levels in depth -- 4 * 340^4 == 53453440000.
  1519. * Let's also add one more level for imbalance.
  1520. */
  1521. depth = 5;
  1522. /* allocation of new data block(s) */
  1523. needed = 2;
  1524. /*
  1525. * tree can be full, so it would need to grow in depth:
  1526. * we need one credit to modify old root, credits for
  1527. * new root will be added in split accounting
  1528. */
  1529. needed += 1;
  1530. /*
  1531. * Index split can happen, we would need:
  1532. * allocate intermediate indexes (bitmap + group)
  1533. * + change two blocks at each level, but root (already included)
  1534. */
  1535. needed += (depth * 2) + (depth * 2);
  1536. /* any allocation modifies superblock */
  1537. needed += 1;
  1538. return needed;
  1539. }
  1540. static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
  1541. struct ext4_extent *ex,
  1542. ext4_lblk_t from, ext4_lblk_t to)
  1543. {
  1544. struct buffer_head *bh;
  1545. unsigned short ee_len = ext4_ext_get_actual_len(ex);
  1546. int i, metadata = 0;
  1547. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  1548. metadata = 1;
  1549. #ifdef EXTENTS_STATS
  1550. {
  1551. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1552. spin_lock(&sbi->s_ext_stats_lock);
  1553. sbi->s_ext_blocks += ee_len;
  1554. sbi->s_ext_extents++;
  1555. if (ee_len < sbi->s_ext_min)
  1556. sbi->s_ext_min = ee_len;
  1557. if (ee_len > sbi->s_ext_max)
  1558. sbi->s_ext_max = ee_len;
  1559. if (ext_depth(inode) > sbi->s_depth_max)
  1560. sbi->s_depth_max = ext_depth(inode);
  1561. spin_unlock(&sbi->s_ext_stats_lock);
  1562. }
  1563. #endif
  1564. if (from >= le32_to_cpu(ex->ee_block)
  1565. && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
  1566. /* tail removal */
  1567. ext4_lblk_t num;
  1568. ext4_fsblk_t start;
  1569. num = le32_to_cpu(ex->ee_block) + ee_len - from;
  1570. start = ext_pblock(ex) + ee_len - num;
  1571. ext_debug("free last %u blocks starting %llu\n", num, start);
  1572. for (i = 0; i < num; i++) {
  1573. bh = sb_find_get_block(inode->i_sb, start + i);
  1574. ext4_forget(handle, 0, inode, bh, start + i);
  1575. }
  1576. ext4_free_blocks(handle, inode, start, num, metadata);
  1577. } else if (from == le32_to_cpu(ex->ee_block)
  1578. && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
  1579. printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
  1580. from, to, le32_to_cpu(ex->ee_block), ee_len);
  1581. } else {
  1582. printk(KERN_INFO "strange request: removal(2) "
  1583. "%u-%u from %u:%u\n",
  1584. from, to, le32_to_cpu(ex->ee_block), ee_len);
  1585. }
  1586. return 0;
  1587. }
  1588. static int
  1589. ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
  1590. struct ext4_ext_path *path, ext4_lblk_t start)
  1591. {
  1592. int err = 0, correct_index = 0;
  1593. int depth = ext_depth(inode), credits;
  1594. struct ext4_extent_header *eh;
  1595. ext4_lblk_t a, b, block;
  1596. unsigned num;
  1597. ext4_lblk_t ex_ee_block;
  1598. unsigned short ex_ee_len;
  1599. unsigned uninitialized = 0;
  1600. struct ext4_extent *ex;
  1601. /* the header must be checked already in ext4_ext_remove_space() */
  1602. ext_debug("truncate since %u in leaf\n", start);
  1603. if (!path[depth].p_hdr)
  1604. path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
  1605. eh = path[depth].p_hdr;
  1606. BUG_ON(eh == NULL);
  1607. /* find where to start removing */
  1608. ex = EXT_LAST_EXTENT(eh);
  1609. ex_ee_block = le32_to_cpu(ex->ee_block);
  1610. if (ext4_ext_is_uninitialized(ex))
  1611. uninitialized = 1;
  1612. ex_ee_len = ext4_ext_get_actual_len(ex);
  1613. while (ex >= EXT_FIRST_EXTENT(eh) &&
  1614. ex_ee_block + ex_ee_len > start) {
  1615. ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
  1616. path[depth].p_ext = ex;
  1617. a = ex_ee_block > start ? ex_ee_block : start;
  1618. b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
  1619. ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
  1620. ext_debug(" border %u:%u\n", a, b);
  1621. if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
  1622. block = 0;
  1623. num = 0;
  1624. BUG();
  1625. } else if (a != ex_ee_block) {
  1626. /* remove tail of the extent */
  1627. block = ex_ee_block;
  1628. num = a - block;
  1629. } else if (b != ex_ee_block + ex_ee_len - 1) {
  1630. /* remove head of the extent */
  1631. block = a;
  1632. num = b - a;
  1633. /* there is no "make a hole" API yet */
  1634. BUG();
  1635. } else {
  1636. /* remove whole extent: excellent! */
  1637. block = ex_ee_block;
  1638. num = 0;
  1639. BUG_ON(a != ex_ee_block);
  1640. BUG_ON(b != ex_ee_block + ex_ee_len - 1);
  1641. }
  1642. /* at present, extent can't cross block group: */
  1643. /* leaf + bitmap + group desc + sb + inode */
  1644. credits = 5;
  1645. if (ex == EXT_FIRST_EXTENT(eh)) {
  1646. correct_index = 1;
  1647. credits += (ext_depth(inode)) + 1;
  1648. }
  1649. #ifdef CONFIG_QUOTA
  1650. credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
  1651. #endif
  1652. handle = ext4_ext_journal_restart(handle, credits);
  1653. if (IS_ERR(handle)) {
  1654. err = PTR_ERR(handle);
  1655. goto out;
  1656. }
  1657. err = ext4_ext_get_access(handle, inode, path + depth);
  1658. if (err)
  1659. goto out;
  1660. err = ext4_remove_blocks(handle, inode, ex, a, b);
  1661. if (err)
  1662. goto out;
  1663. if (num == 0) {
  1664. /* this extent is removed; mark slot entirely unused */
  1665. ext4_ext_store_pblock(ex, 0);
  1666. eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
  1667. }
  1668. ex->ee_block = cpu_to_le32(block);
  1669. ex->ee_len = cpu_to_le16(num);
  1670. /*
  1671. * Do not mark uninitialized if all the blocks in the
  1672. * extent have been removed.
  1673. */
  1674. if (uninitialized && num)
  1675. ext4_ext_mark_uninitialized(ex);
  1676. err = ext4_ext_dirty(handle, inode, path + depth);
  1677. if (err)
  1678. goto out;
  1679. ext_debug("new extent: %u:%u:%llu\n", block, num,
  1680. ext_pblock(ex));
  1681. ex--;
  1682. ex_ee_block = le32_to_cpu(ex->ee_block);
  1683. ex_ee_len = ext4_ext_get_actual_len(ex);
  1684. }
  1685. if (correct_index && eh->eh_entries)
  1686. err = ext4_ext_correct_indexes(handle, inode, path);
  1687. /* if this leaf is free, then we should
  1688. * remove it from index block above */
  1689. if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
  1690. err = ext4_ext_rm_idx(handle, inode, path + depth);
  1691. out:
  1692. return err;
  1693. }
  1694. /*
  1695. * ext4_ext_more_to_rm:
  1696. * returns 1 if current index has to be freed (even partial)
  1697. */
  1698. static int
  1699. ext4_ext_more_to_rm(struct ext4_ext_path *path)
  1700. {
  1701. BUG_ON(path->p_idx == NULL);
  1702. if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
  1703. return 0;
  1704. /*
  1705. * if truncate on deeper level happened, it wasn't partial,
  1706. * so we have to consider current index for truncation
  1707. */
  1708. if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
  1709. return 0;
  1710. return 1;
  1711. }
  1712. static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
  1713. {
  1714. struct super_block *sb = inode->i_sb;
  1715. int depth = ext_depth(inode);
  1716. struct ext4_ext_path *path;
  1717. handle_t *handle;
  1718. int i = 0, err = 0;
  1719. ext_debug("truncate since %u\n", start);
  1720. /* probably first extent we're gonna free will be last in block */
  1721. handle = ext4_journal_start(inode, depth + 1);
  1722. if (IS_ERR(handle))
  1723. return PTR_ERR(handle);
  1724. ext4_ext_invalidate_cache(inode);
  1725. /*
  1726. * We start scanning from right side, freeing all the blocks
  1727. * after i_size and walking into the tree depth-wise.
  1728. */
  1729. path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_KERNEL);
  1730. if (path == NULL) {
  1731. ext4_journal_stop(handle);
  1732. return -ENOMEM;
  1733. }
  1734. path[0].p_hdr = ext_inode_hdr(inode);
  1735. if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) {
  1736. err = -EIO;
  1737. goto out;
  1738. }
  1739. path[0].p_depth = depth;
  1740. while (i >= 0 && err == 0) {
  1741. if (i == depth) {
  1742. /* this is leaf block */
  1743. err = ext4_ext_rm_leaf(handle, inode, path, start);
  1744. /* root level has p_bh == NULL, brelse() eats this */
  1745. brelse(path[i].p_bh);
  1746. path[i].p_bh = NULL;
  1747. i--;
  1748. continue;
  1749. }
  1750. /* this is index block */
  1751. if (!path[i].p_hdr) {
  1752. ext_debug("initialize header\n");
  1753. path[i].p_hdr = ext_block_hdr(path[i].p_bh);
  1754. }
  1755. if (!path[i].p_idx) {
  1756. /* this level hasn't been touched yet */
  1757. path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
  1758. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
  1759. ext_debug("init index ptr: hdr 0x%p, num %d\n",
  1760. path[i].p_hdr,
  1761. le16_to_cpu(path[i].p_hdr->eh_entries));
  1762. } else {
  1763. /* we were already here, see at next index */
  1764. path[i].p_idx--;
  1765. }
  1766. ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
  1767. i, EXT_FIRST_INDEX(path[i].p_hdr),
  1768. path[i].p_idx);
  1769. if (ext4_ext_more_to_rm(path + i)) {
  1770. struct buffer_head *bh;
  1771. /* go to the next level */
  1772. ext_debug("move to level %d (block %llu)\n",
  1773. i + 1, idx_pblock(path[i].p_idx));
  1774. memset(path + i + 1, 0, sizeof(*path));
  1775. bh = sb_bread(sb, idx_pblock(path[i].p_idx));
  1776. if (!bh) {
  1777. /* should we reset i_size? */
  1778. err = -EIO;
  1779. break;
  1780. }
  1781. if (WARN_ON(i + 1 > depth)) {
  1782. err = -EIO;
  1783. break;
  1784. }
  1785. if (ext4_ext_check_header(inode, ext_block_hdr(bh),
  1786. depth - i - 1)) {
  1787. err = -EIO;
  1788. break;
  1789. }
  1790. path[i + 1].p_bh = bh;
  1791. /* save actual number of indexes since this
  1792. * number is changed at the next iteration */
  1793. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
  1794. i++;
  1795. } else {
  1796. /* we finished processing this index, go up */
  1797. if (path[i].p_hdr->eh_entries == 0 && i > 0) {
  1798. /* index is empty, remove it;
  1799. * handle must be already prepared by the
  1800. * truncatei_leaf() */
  1801. err = ext4_ext_rm_idx(handle, inode, path + i);
  1802. }
  1803. /* root level has p_bh == NULL, brelse() eats this */
  1804. brelse(path[i].p_bh);
  1805. path[i].p_bh = NULL;
  1806. i--;
  1807. ext_debug("return to level %d\n", i);
  1808. }
  1809. }
  1810. /* TODO: flexible tree reduction should be here */
  1811. if (path->p_hdr->eh_entries == 0) {
  1812. /*
  1813. * truncate to zero freed all the tree,
  1814. * so we need to correct eh_depth
  1815. */
  1816. err = ext4_ext_get_access(handle, inode, path);
  1817. if (err == 0) {
  1818. ext_inode_hdr(inode)->eh_depth = 0;
  1819. ext_inode_hdr(inode)->eh_max =
  1820. cpu_to_le16(ext4_ext_space_root(inode));
  1821. err = ext4_ext_dirty(handle, inode, path);
  1822. }
  1823. }
  1824. out:
  1825. ext4_ext_tree_changed(inode);
  1826. ext4_ext_drop_refs(path);
  1827. kfree(path);
  1828. ext4_journal_stop(handle);
  1829. return err;
  1830. }
  1831. /*
  1832. * called at mount time
  1833. */
  1834. void ext4_ext_init(struct super_block *sb)
  1835. {
  1836. /*
  1837. * possible initialization would be here
  1838. */
  1839. if (test_opt(sb, EXTENTS)) {
  1840. printk("EXT4-fs: file extents enabled");
  1841. #ifdef AGGRESSIVE_TEST
  1842. printk(", aggressive tests");
  1843. #endif
  1844. #ifdef CHECK_BINSEARCH
  1845. printk(", check binsearch");
  1846. #endif
  1847. #ifdef EXTENTS_STATS
  1848. printk(", stats");
  1849. #endif
  1850. printk("\n");
  1851. #ifdef EXTENTS_STATS
  1852. spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
  1853. EXT4_SB(sb)->s_ext_min = 1 << 30;
  1854. EXT4_SB(sb)->s_ext_max = 0;
  1855. #endif
  1856. }
  1857. }
  1858. /*
  1859. * called at umount time
  1860. */
  1861. void ext4_ext_release(struct super_block *sb)
  1862. {
  1863. if (!test_opt(sb, EXTENTS))
  1864. return;
  1865. #ifdef EXTENTS_STATS
  1866. if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
  1867. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1868. printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
  1869. sbi->s_ext_blocks, sbi->s_ext_extents,
  1870. sbi->s_ext_blocks / sbi->s_ext_extents);
  1871. printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
  1872. sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
  1873. }
  1874. #endif
  1875. }
  1876. static void bi_complete(struct bio *bio, int error)
  1877. {
  1878. complete((struct completion *)bio->bi_private);
  1879. }
  1880. /* FIXME!! we need to try to merge to left or right after zero-out */
  1881. static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
  1882. {
  1883. int ret = -EIO;
  1884. struct bio *bio;
  1885. int blkbits, blocksize;
  1886. sector_t ee_pblock;
  1887. struct completion event;
  1888. unsigned int ee_len, len, done, offset;
  1889. blkbits = inode->i_blkbits;
  1890. blocksize = inode->i_sb->s_blocksize;
  1891. ee_len = ext4_ext_get_actual_len(ex);
  1892. ee_pblock = ext_pblock(ex);
  1893. /* convert ee_pblock to 512 byte sectors */
  1894. ee_pblock = ee_pblock << (blkbits - 9);
  1895. while (ee_len > 0) {
  1896. if (ee_len > BIO_MAX_PAGES)
  1897. len = BIO_MAX_PAGES;
  1898. else
  1899. len = ee_len;
  1900. bio = bio_alloc(GFP_NOIO, len);
  1901. if (!bio)
  1902. return -ENOMEM;
  1903. bio->bi_sector = ee_pblock;
  1904. bio->bi_bdev = inode->i_sb->s_bdev;
  1905. done = 0;
  1906. offset = 0;
  1907. while (done < len) {
  1908. ret = bio_add_page(bio, ZERO_PAGE(0),
  1909. blocksize, offset);
  1910. if (ret != blocksize) {
  1911. /*
  1912. * We can't add any more pages because of
  1913. * hardware limitations. Start a new bio.
  1914. */
  1915. break;
  1916. }
  1917. done++;
  1918. offset += blocksize;
  1919. if (offset >= PAGE_CACHE_SIZE)
  1920. offset = 0;
  1921. }
  1922. init_completion(&event);
  1923. bio->bi_private = &event;
  1924. bio->bi_end_io = bi_complete;
  1925. submit_bio(WRITE, bio);
  1926. wait_for_completion(&event);
  1927. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  1928. ret = 0;
  1929. else {
  1930. ret = -EIO;
  1931. break;
  1932. }
  1933. bio_put(bio);
  1934. ee_len -= done;
  1935. ee_pblock += done << (blkbits - 9);
  1936. }
  1937. return ret;
  1938. }
  1939. #define EXT4_EXT_ZERO_LEN 7
  1940. /*
  1941. * This function is called by ext4_ext_get_blocks() if someone tries to write
  1942. * to an uninitialized extent. It may result in splitting the uninitialized
  1943. * extent into multiple extents (upto three - one initialized and two
  1944. * uninitialized).
  1945. * There are three possibilities:
  1946. * a> There is no split required: Entire extent should be initialized
  1947. * b> Splits in two extents: Write is happening at either end of the extent
  1948. * c> Splits in three extents: Somone is writing in middle of the extent
  1949. */
  1950. static int ext4_ext_convert_to_initialized(handle_t *handle,
  1951. struct inode *inode,
  1952. struct ext4_ext_path *path,
  1953. ext4_lblk_t iblock,
  1954. unsigned long max_blocks)
  1955. {
  1956. struct ext4_extent *ex, newex, orig_ex;
  1957. struct ext4_extent *ex1 = NULL;
  1958. struct ext4_extent *ex2 = NULL;
  1959. struct ext4_extent *ex3 = NULL;
  1960. struct ext4_extent_header *eh;
  1961. ext4_lblk_t ee_block;
  1962. unsigned int allocated, ee_len, depth;
  1963. ext4_fsblk_t newblock;
  1964. int err = 0;
  1965. int ret = 0;
  1966. depth = ext_depth(inode);
  1967. eh = path[depth].p_hdr;
  1968. ex = path[depth].p_ext;
  1969. ee_block = le32_to_cpu(ex->ee_block);
  1970. ee_len = ext4_ext_get_actual_len(ex);
  1971. allocated = ee_len - (iblock - ee_block);
  1972. newblock = iblock - ee_block + ext_pblock(ex);
  1973. ex2 = ex;
  1974. orig_ex.ee_block = ex->ee_block;
  1975. orig_ex.ee_len = cpu_to_le16(ee_len);
  1976. ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
  1977. err = ext4_ext_get_access(handle, inode, path + depth);
  1978. if (err)
  1979. goto out;
  1980. /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
  1981. if (ee_len <= 2*EXT4_EXT_ZERO_LEN) {
  1982. err = ext4_ext_zeroout(inode, &orig_ex);
  1983. if (err)
  1984. goto fix_extent_len;
  1985. /* update the extent length and mark as initialized */
  1986. ex->ee_block = orig_ex.ee_block;
  1987. ex->ee_len = orig_ex.ee_len;
  1988. ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
  1989. ext4_ext_dirty(handle, inode, path + depth);
  1990. /* zeroed the full extent */
  1991. return allocated;
  1992. }
  1993. /* ex1: ee_block to iblock - 1 : uninitialized */
  1994. if (iblock > ee_block) {
  1995. ex1 = ex;
  1996. ex1->ee_len = cpu_to_le16(iblock - ee_block);
  1997. ext4_ext_mark_uninitialized(ex1);
  1998. ex2 = &newex;
  1999. }
  2000. /*
  2001. * for sanity, update the length of the ex2 extent before
  2002. * we insert ex3, if ex1 is NULL. This is to avoid temporary
  2003. * overlap of blocks.
  2004. */
  2005. if (!ex1 && allocated > max_blocks)
  2006. ex2->ee_len = cpu_to_le16(max_blocks);
  2007. /* ex3: to ee_block + ee_len : uninitialised */
  2008. if (allocated > max_blocks) {
  2009. unsigned int newdepth;
  2010. /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
  2011. if (allocated <= EXT4_EXT_ZERO_LEN) {
  2012. /* Mark first half uninitialized.
  2013. * Mark second half initialized and zero out the
  2014. * initialized extent
  2015. */
  2016. ex->ee_block = orig_ex.ee_block;
  2017. ex->ee_len = cpu_to_le16(ee_len - allocated);
  2018. ext4_ext_mark_uninitialized(ex);
  2019. ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
  2020. ext4_ext_dirty(handle, inode, path + depth);
  2021. ex3 = &newex;
  2022. ex3->ee_block = cpu_to_le32(iblock);
  2023. ext4_ext_store_pblock(ex3, newblock);
  2024. ex3->ee_len = cpu_to_le16(allocated);
  2025. err = ext4_ext_insert_extent(handle, inode, path, ex3);
  2026. if (err == -ENOSPC) {
  2027. err = ext4_ext_zeroout(inode, &orig_ex);
  2028. if (err)
  2029. goto fix_extent_len;
  2030. ex->ee_block = orig_ex.ee_block;
  2031. ex->ee_len = orig_ex.ee_len;
  2032. ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
  2033. ext4_ext_dirty(handle, inode, path + depth);
  2034. /* zeroed the full extent */
  2035. return allocated;
  2036. } else if (err)
  2037. goto fix_extent_len;
  2038. /*
  2039. * We need to zero out the second half because
  2040. * an fallocate request can update file size and
  2041. * converting the second half to initialized extent
  2042. * implies that we can leak some junk data to user
  2043. * space.
  2044. */
  2045. err = ext4_ext_zeroout(inode, ex3);
  2046. if (err) {
  2047. /*
  2048. * We should actually mark the
  2049. * second half as uninit and return error
  2050. * Insert would have changed the extent
  2051. */
  2052. depth = ext_depth(inode);
  2053. ext4_ext_drop_refs(path);
  2054. path = ext4_ext_find_extent(inode,
  2055. iblock, path);
  2056. if (IS_ERR(path)) {
  2057. err = PTR_ERR(path);
  2058. return err;
  2059. }
  2060. ex = path[depth].p_ext;
  2061. err = ext4_ext_get_access(handle, inode,
  2062. path + depth);
  2063. if (err)
  2064. return err;
  2065. ext4_ext_mark_uninitialized(ex);
  2066. ext4_ext_dirty(handle, inode, path + depth);
  2067. return err;
  2068. }
  2069. /* zeroed the second half */
  2070. return allocated;
  2071. }
  2072. ex3 = &newex;
  2073. ex3->ee_block = cpu_to_le32(iblock + max_blocks);
  2074. ext4_ext_store_pblock(ex3, newblock + max_blocks);
  2075. ex3->ee_len = cpu_to_le16(allocated - max_blocks);
  2076. ext4_ext_mark_uninitialized(ex3);
  2077. err = ext4_ext_insert_extent(handle, inode, path, ex3);
  2078. if (err == -ENOSPC) {
  2079. err = ext4_ext_zeroout(inode, &orig_ex);
  2080. if (err)
  2081. goto fix_extent_len;
  2082. /* update the extent length and mark as initialized */
  2083. ex->ee_block = orig_ex.ee_block;
  2084. ex->ee_len = orig_ex.ee_len;
  2085. ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
  2086. ext4_ext_dirty(handle, inode, path + depth);
  2087. /* zeroed the full extent */
  2088. return allocated;
  2089. } else if (err)
  2090. goto fix_extent_len;
  2091. /*
  2092. * The depth, and hence eh & ex might change
  2093. * as part of the insert above.
  2094. */
  2095. newdepth = ext_depth(inode);
  2096. /*
  2097. * update the extent length after successfull insert of the
  2098. * split extent
  2099. */
  2100. orig_ex.ee_len = cpu_to_le16(ee_len -
  2101. ext4_ext_get_actual_len(ex3));
  2102. if (newdepth != depth) {
  2103. depth = newdepth;
  2104. ext4_ext_drop_refs(path);
  2105. path = ext4_ext_find_extent(inode, iblock, path);
  2106. if (IS_ERR(path)) {
  2107. err = PTR_ERR(path);
  2108. goto out;
  2109. }
  2110. eh = path[depth].p_hdr;
  2111. ex = path[depth].p_ext;
  2112. if (ex2 != &newex)
  2113. ex2 = ex;
  2114. err = ext4_ext_get_access(handle, inode, path + depth);
  2115. if (err)
  2116. goto out;
  2117. }
  2118. allocated = max_blocks;
  2119. /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
  2120. * to insert a extent in the middle zerout directly
  2121. * otherwise give the extent a chance to merge to left
  2122. */
  2123. if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
  2124. iblock != ee_block) {
  2125. err = ext4_ext_zeroout(inode, &orig_ex);
  2126. if (err)
  2127. goto fix_extent_len;
  2128. /* update the extent length and mark as initialized */
  2129. ex->ee_block = orig_ex.ee_block;
  2130. ex->ee_len = orig_ex.ee_len;
  2131. ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
  2132. ext4_ext_dirty(handle, inode, path + depth);
  2133. /* zero out the first half */
  2134. return allocated;
  2135. }
  2136. }
  2137. /*
  2138. * If there was a change of depth as part of the
  2139. * insertion of ex3 above, we need to update the length
  2140. * of the ex1 extent again here
  2141. */
  2142. if (ex1 && ex1 != ex) {
  2143. ex1 = ex;
  2144. ex1->ee_len = cpu_to_le16(iblock - ee_block);
  2145. ext4_ext_mark_uninitialized(ex1);
  2146. ex2 = &newex;
  2147. }
  2148. /* ex2: iblock to iblock + maxblocks-1 : initialised */
  2149. ex2->ee_block = cpu_to_le32(iblock);
  2150. ext4_ext_store_pblock(ex2, newblock);
  2151. ex2->ee_len = cpu_to_le16(allocated);
  2152. if (ex2 != ex)
  2153. goto insert;
  2154. /*
  2155. * New (initialized) extent starts from the first block
  2156. * in the current extent. i.e., ex2 == ex
  2157. * We have to see if it can be merged with the extent
  2158. * on the left.
  2159. */
  2160. if (ex2 > EXT_FIRST_EXTENT(eh)) {
  2161. /*
  2162. * To merge left, pass "ex2 - 1" to try_to_merge(),
  2163. * since it merges towards right _only_.
  2164. */
  2165. ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
  2166. if (ret) {
  2167. err = ext4_ext_correct_indexes(handle, inode, path);
  2168. if (err)
  2169. goto out;
  2170. depth = ext_depth(inode);
  2171. ex2--;
  2172. }
  2173. }
  2174. /*
  2175. * Try to Merge towards right. This might be required
  2176. * only when the whole extent is being written to.
  2177. * i.e. ex2 == ex and ex3 == NULL.
  2178. */
  2179. if (!ex3) {
  2180. ret = ext4_ext_try_to_merge(inode, path, ex2);
  2181. if (ret) {
  2182. err = ext4_ext_correct_indexes(handle, inode, path);
  2183. if (err)
  2184. goto out;
  2185. }
  2186. }
  2187. /* Mark modified extent as dirty */
  2188. err = ext4_ext_dirty(handle, inode, path + depth);
  2189. goto out;
  2190. insert:
  2191. err = ext4_ext_insert_extent(handle, inode, path, &newex);
  2192. if (err == -ENOSPC) {
  2193. err = ext4_ext_zeroout(inode, &orig_ex);
  2194. if (err)
  2195. goto fix_extent_len;
  2196. /* update the extent length and mark as initialized */
  2197. ex->ee_block = orig_ex.ee_block;
  2198. ex->ee_len = orig_ex.ee_len;
  2199. ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
  2200. ext4_ext_dirty(handle, inode, path + depth);
  2201. /* zero out the first half */
  2202. return allocated;
  2203. } else if (err)
  2204. goto fix_extent_len;
  2205. out:
  2206. return err ? err : allocated;
  2207. fix_extent_len:
  2208. ex->ee_block = orig_ex.ee_block;
  2209. ex->ee_len = orig_ex.ee_len;
  2210. ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
  2211. ext4_ext_mark_uninitialized(ex);
  2212. ext4_ext_dirty(handle, inode, path + depth);
  2213. return err;
  2214. }
  2215. /*
  2216. * Block allocation/map/preallocation routine for extents based files
  2217. *
  2218. *
  2219. * Need to be called with
  2220. * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
  2221. * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
  2222. *
  2223. * return > 0, number of of blocks already mapped/allocated
  2224. * if create == 0 and these are pre-allocated blocks
  2225. * buffer head is unmapped
  2226. * otherwise blocks are mapped
  2227. *
  2228. * return = 0, if plain look up failed (blocks have not been allocated)
  2229. * buffer head is unmapped
  2230. *
  2231. * return < 0, error case.
  2232. */
  2233. int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
  2234. ext4_lblk_t iblock,
  2235. unsigned long max_blocks, struct buffer_head *bh_result,
  2236. int create, int extend_disksize)
  2237. {
  2238. struct ext4_ext_path *path = NULL;
  2239. struct ext4_extent_header *eh;
  2240. struct ext4_extent newex, *ex;
  2241. ext4_fsblk_t goal, newblock;
  2242. int err = 0, depth, ret;
  2243. unsigned long allocated = 0;
  2244. struct ext4_allocation_request ar;
  2245. __clear_bit(BH_New, &bh_result->b_state);
  2246. ext_debug("blocks %u/%lu requested for inode %u\n",
  2247. iblock, max_blocks, inode->i_ino);
  2248. /* check in cache */
  2249. goal = ext4_ext_in_cache(inode, iblock, &newex);
  2250. if (goal) {
  2251. if (goal == EXT4_EXT_CACHE_GAP) {
  2252. if (!create) {
  2253. /*
  2254. * block isn't allocated yet and
  2255. * user doesn't want to allocate it
  2256. */
  2257. goto out2;
  2258. }
  2259. /* we should allocate requested block */
  2260. } else if (goal == EXT4_EXT_CACHE_EXTENT) {
  2261. /* block is already allocated */
  2262. newblock = iblock
  2263. - le32_to_cpu(newex.ee_block)
  2264. + ext_pblock(&newex);
  2265. /* number of remaining blocks in the extent */
  2266. allocated = ext4_ext_get_actual_len(&newex) -
  2267. (iblock - le32_to_cpu(newex.ee_block));
  2268. goto out;
  2269. } else {
  2270. BUG();
  2271. }
  2272. }
  2273. /* find extent for this block */
  2274. path = ext4_ext_find_extent(inode, iblock, NULL);
  2275. if (IS_ERR(path)) {
  2276. err = PTR_ERR(path);
  2277. path = NULL;
  2278. goto out2;
  2279. }
  2280. depth = ext_depth(inode);
  2281. /*
  2282. * consistent leaf must not be empty;
  2283. * this situation is possible, though, _during_ tree modification;
  2284. * this is why assert can't be put in ext4_ext_find_extent()
  2285. */
  2286. BUG_ON(path[depth].p_ext == NULL && depth != 0);
  2287. eh = path[depth].p_hdr;
  2288. ex = path[depth].p_ext;
  2289. if (ex) {
  2290. ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
  2291. ext4_fsblk_t ee_start = ext_pblock(ex);
  2292. unsigned short ee_len;
  2293. /*
  2294. * Uninitialized extents are treated as holes, except that
  2295. * we split out initialized portions during a write.
  2296. */
  2297. ee_len = ext4_ext_get_actual_len(ex);
  2298. /* if found extent covers block, simply return it */
  2299. if (iblock >= ee_block && iblock < ee_block + ee_len) {
  2300. newblock = iblock - ee_block + ee_start;
  2301. /* number of remaining blocks in the extent */
  2302. allocated = ee_len - (iblock - ee_block);
  2303. ext_debug("%u fit into %lu:%d -> %llu\n", iblock,
  2304. ee_block, ee_len, newblock);
  2305. /* Do not put uninitialized extent in the cache */
  2306. if (!ext4_ext_is_uninitialized(ex)) {
  2307. ext4_ext_put_in_cache(inode, ee_block,
  2308. ee_len, ee_start,
  2309. EXT4_EXT_CACHE_EXTENT);
  2310. goto out;
  2311. }
  2312. if (create == EXT4_CREATE_UNINITIALIZED_EXT)
  2313. goto out;
  2314. if (!create) {
  2315. /*
  2316. * We have blocks reserved already. We
  2317. * return allocated blocks so that delalloc
  2318. * won't do block reservation for us. But
  2319. * the buffer head will be unmapped so that
  2320. * a read from the block returns 0s.
  2321. */
  2322. if (allocated > max_blocks)
  2323. allocated = max_blocks;
  2324. /* mark the buffer unwritten */
  2325. __set_bit(BH_Unwritten, &bh_result->b_state);
  2326. goto out2;
  2327. }
  2328. ret = ext4_ext_convert_to_initialized(handle, inode,
  2329. path, iblock,
  2330. max_blocks);
  2331. if (ret <= 0) {
  2332. err = ret;
  2333. goto out2;
  2334. } else
  2335. allocated = ret;
  2336. goto outnew;
  2337. }
  2338. }
  2339. /*
  2340. * requested block isn't allocated yet;
  2341. * we couldn't try to create block if create flag is zero
  2342. */
  2343. if (!create) {
  2344. /*
  2345. * put just found gap into cache to speed up
  2346. * subsequent requests
  2347. */
  2348. ext4_ext_put_gap_in_cache(inode, path, iblock);
  2349. goto out2;
  2350. }
  2351. /*
  2352. * Okay, we need to do block allocation. Lazily initialize the block
  2353. * allocation info here if necessary.
  2354. */
  2355. if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info))
  2356. ext4_init_block_alloc_info(inode);
  2357. /* find neighbour allocated blocks */
  2358. ar.lleft = iblock;
  2359. err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
  2360. if (err)
  2361. goto out2;
  2362. ar.lright = iblock;
  2363. err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
  2364. if (err)
  2365. goto out2;
  2366. /*
  2367. * See if request is beyond maximum number of blocks we can have in
  2368. * a single extent. For an initialized extent this limit is
  2369. * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
  2370. * EXT_UNINIT_MAX_LEN.
  2371. */
  2372. if (max_blocks > EXT_INIT_MAX_LEN &&
  2373. create != EXT4_CREATE_UNINITIALIZED_EXT)
  2374. max_blocks = EXT_INIT_MAX_LEN;
  2375. else if (max_blocks > EXT_UNINIT_MAX_LEN &&
  2376. create == EXT4_CREATE_UNINITIALIZED_EXT)
  2377. max_blocks = EXT_UNINIT_MAX_LEN;
  2378. /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
  2379. newex.ee_block = cpu_to_le32(iblock);
  2380. newex.ee_len = cpu_to_le16(max_blocks);
  2381. err = ext4_ext_check_overlap(inode, &newex, path);
  2382. if (err)
  2383. allocated = ext4_ext_get_actual_len(&newex);
  2384. else
  2385. allocated = max_blocks;
  2386. /* allocate new block */
  2387. ar.inode = inode;
  2388. ar.goal = ext4_ext_find_goal(inode, path, iblock);
  2389. ar.logical = iblock;
  2390. ar.len = allocated;
  2391. if (S_ISREG(inode->i_mode))
  2392. ar.flags = EXT4_MB_HINT_DATA;
  2393. else
  2394. /* disable in-core preallocation for non-regular files */
  2395. ar.flags = 0;
  2396. newblock = ext4_mb_new_blocks(handle, &ar, &err);
  2397. if (!newblock)
  2398. goto out2;
  2399. ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
  2400. goal, newblock, allocated);
  2401. /* try to insert new extent into found leaf and return */
  2402. ext4_ext_store_pblock(&newex, newblock);
  2403. newex.ee_len = cpu_to_le16(ar.len);
  2404. if (create == EXT4_CREATE_UNINITIALIZED_EXT) /* Mark uninitialized */
  2405. ext4_ext_mark_uninitialized(&newex);
  2406. err = ext4_ext_insert_extent(handle, inode, path, &newex);
  2407. if (err) {
  2408. /* free data blocks we just allocated */
  2409. /* not a good idea to call discard here directly,
  2410. * but otherwise we'd need to call it every free() */
  2411. ext4_mb_discard_inode_preallocations(inode);
  2412. ext4_free_blocks(handle, inode, ext_pblock(&newex),
  2413. ext4_ext_get_actual_len(&newex), 0);
  2414. goto out2;
  2415. }
  2416. if (extend_disksize && inode->i_size > EXT4_I(inode)->i_disksize)
  2417. EXT4_I(inode)->i_disksize = inode->i_size;
  2418. /* previous routine could use block we allocated */
  2419. newblock = ext_pblock(&newex);
  2420. allocated = ext4_ext_get_actual_len(&newex);
  2421. outnew:
  2422. __set_bit(BH_New, &bh_result->b_state);
  2423. /* Cache only when it is _not_ an uninitialized extent */
  2424. if (create != EXT4_CREATE_UNINITIALIZED_EXT)
  2425. ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
  2426. EXT4_EXT_CACHE_EXTENT);
  2427. out:
  2428. if (allocated > max_blocks)
  2429. allocated = max_blocks;
  2430. ext4_ext_show_leaf(inode, path);
  2431. __set_bit(BH_Mapped, &bh_result->b_state);
  2432. bh_result->b_bdev = inode->i_sb->s_bdev;
  2433. bh_result->b_blocknr = newblock;
  2434. out2:
  2435. if (path) {
  2436. ext4_ext_drop_refs(path);
  2437. kfree(path);
  2438. }
  2439. return err ? err : allocated;
  2440. }
  2441. void ext4_ext_truncate(struct inode * inode, struct page *page)
  2442. {
  2443. struct address_space *mapping = inode->i_mapping;
  2444. struct super_block *sb = inode->i_sb;
  2445. ext4_lblk_t last_block;
  2446. handle_t *handle;
  2447. int err = 0;
  2448. /*
  2449. * probably first extent we're gonna free will be last in block
  2450. */
  2451. err = ext4_writepage_trans_blocks(inode) + 3;
  2452. handle = ext4_journal_start(inode, err);
  2453. if (IS_ERR(handle)) {
  2454. if (page) {
  2455. clear_highpage(page);
  2456. flush_dcache_page(page);
  2457. unlock_page(page);
  2458. page_cache_release(page);
  2459. }
  2460. return;
  2461. }
  2462. if (page)
  2463. ext4_block_truncate_page(handle, page, mapping, inode->i_size);
  2464. down_write(&EXT4_I(inode)->i_data_sem);
  2465. ext4_ext_invalidate_cache(inode);
  2466. ext4_mb_discard_inode_preallocations(inode);
  2467. /*
  2468. * TODO: optimization is possible here.
  2469. * Probably we need not scan at all,
  2470. * because page truncation is enough.
  2471. */
  2472. if (ext4_orphan_add(handle, inode))
  2473. goto out_stop;
  2474. /* we have to know where to truncate from in crash case */
  2475. EXT4_I(inode)->i_disksize = inode->i_size;
  2476. ext4_mark_inode_dirty(handle, inode);
  2477. last_block = (inode->i_size + sb->s_blocksize - 1)
  2478. >> EXT4_BLOCK_SIZE_BITS(sb);
  2479. err = ext4_ext_remove_space(inode, last_block);
  2480. /* In a multi-transaction truncate, we only make the final
  2481. * transaction synchronous.
  2482. */
  2483. if (IS_SYNC(inode))
  2484. handle->h_sync = 1;
  2485. out_stop:
  2486. /*
  2487. * If this was a simple ftruncate() and the file will remain alive,
  2488. * then we need to clear up the orphan record which we created above.
  2489. * However, if this was a real unlink then we were called by
  2490. * ext4_delete_inode(), and we allow that function to clean up the
  2491. * orphan info for us.
  2492. */
  2493. if (inode->i_nlink)
  2494. ext4_orphan_del(handle, inode);
  2495. up_write(&EXT4_I(inode)->i_data_sem);
  2496. ext4_journal_stop(handle);
  2497. }
  2498. /*
  2499. * ext4_ext_writepage_trans_blocks:
  2500. * calculate max number of blocks we could modify
  2501. * in order to allocate new block for an inode
  2502. */
  2503. int ext4_ext_writepage_trans_blocks(struct inode *inode, int num)
  2504. {
  2505. int needed;
  2506. needed = ext4_ext_calc_credits_for_insert(inode, NULL);
  2507. /* caller wants to allocate num blocks, but note it includes sb */
  2508. needed = needed * num - (num - 1);
  2509. #ifdef CONFIG_QUOTA
  2510. needed += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
  2511. #endif
  2512. return needed;
  2513. }
  2514. static void ext4_falloc_update_inode(struct inode *inode,
  2515. int mode, loff_t new_size, int update_ctime)
  2516. {
  2517. struct timespec now;
  2518. if (update_ctime) {
  2519. now = current_fs_time(inode->i_sb);
  2520. if (!timespec_equal(&inode->i_ctime, &now))
  2521. inode->i_ctime = now;
  2522. }
  2523. /*
  2524. * Update only when preallocation was requested beyond
  2525. * the file size.
  2526. */
  2527. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  2528. new_size > i_size_read(inode)) {
  2529. i_size_write(inode, new_size);
  2530. EXT4_I(inode)->i_disksize = new_size;
  2531. }
  2532. }
  2533. /*
  2534. * preallocate space for a file. This implements ext4's fallocate inode
  2535. * operation, which gets called from sys_fallocate system call.
  2536. * For block-mapped files, posix_fallocate should fall back to the method
  2537. * of writing zeroes to the required new blocks (the same behavior which is
  2538. * expected for file systems which do not support fallocate() system call).
  2539. */
  2540. long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
  2541. {
  2542. handle_t *handle;
  2543. ext4_lblk_t block;
  2544. loff_t new_size;
  2545. unsigned long max_blocks;
  2546. int ret = 0;
  2547. int ret2 = 0;
  2548. int retries = 0;
  2549. struct buffer_head map_bh;
  2550. unsigned int credits, blkbits = inode->i_blkbits;
  2551. /*
  2552. * currently supporting (pre)allocate mode for extent-based
  2553. * files _only_
  2554. */
  2555. if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
  2556. return -EOPNOTSUPP;
  2557. /* preallocation to directories is currently not supported */
  2558. if (S_ISDIR(inode->i_mode))
  2559. return -ENODEV;
  2560. block = offset >> blkbits;
  2561. /*
  2562. * We can't just convert len to max_blocks because
  2563. * If blocksize = 4096 offset = 3072 and len = 2048
  2564. */
  2565. max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
  2566. - block;
  2567. /*
  2568. * credits to insert 1 extent into extent tree + buffers to be able to
  2569. * modify 1 super block, 1 block bitmap and 1 group descriptor.
  2570. */
  2571. credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + 3;
  2572. mutex_lock(&inode->i_mutex);
  2573. retry:
  2574. while (ret >= 0 && ret < max_blocks) {
  2575. block = block + ret;
  2576. max_blocks = max_blocks - ret;
  2577. handle = ext4_journal_start(inode, credits);
  2578. if (IS_ERR(handle)) {
  2579. ret = PTR_ERR(handle);
  2580. break;
  2581. }
  2582. ret = ext4_get_blocks_wrap(handle, inode, block,
  2583. max_blocks, &map_bh,
  2584. EXT4_CREATE_UNINITIALIZED_EXT, 0);
  2585. if (ret <= 0) {
  2586. #ifdef EXT4FS_DEBUG
  2587. WARN_ON(ret <= 0);
  2588. printk(KERN_ERR "%s: ext4_ext_get_blocks "
  2589. "returned error inode#%lu, block=%u, "
  2590. "max_blocks=%lu", __func__,
  2591. inode->i_ino, block, max_blocks);
  2592. #endif
  2593. ext4_mark_inode_dirty(handle, inode);
  2594. ret2 = ext4_journal_stop(handle);
  2595. break;
  2596. }
  2597. if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
  2598. blkbits) >> blkbits))
  2599. new_size = offset + len;
  2600. else
  2601. new_size = (block + ret) << blkbits;
  2602. ext4_falloc_update_inode(inode, mode, new_size,
  2603. buffer_new(&map_bh));
  2604. ext4_mark_inode_dirty(handle, inode);
  2605. ret2 = ext4_journal_stop(handle);
  2606. if (ret2)
  2607. break;
  2608. }
  2609. if (ret == -ENOSPC &&
  2610. ext4_should_retry_alloc(inode->i_sb, &retries)) {
  2611. ret = 0;
  2612. goto retry;
  2613. }
  2614. mutex_unlock(&inode->i_mutex);
  2615. return ret > 0 ? ret2 : ret;
  2616. }