extents.c 69 KB

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