alloc.c 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * alloc.c
  5. *
  6. * Extent allocs and frees
  7. *
  8. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. */
  25. #include <linux/fs.h>
  26. #include <linux/types.h>
  27. #include <linux/slab.h>
  28. #include <linux/highmem.h>
  29. #include <linux/swap.h>
  30. #define MLOG_MASK_PREFIX ML_DISK_ALLOC
  31. #include <cluster/masklog.h>
  32. #include "ocfs2.h"
  33. #include "alloc.h"
  34. #include "aops.h"
  35. #include "dlmglue.h"
  36. #include "extent_map.h"
  37. #include "inode.h"
  38. #include "journal.h"
  39. #include "localalloc.h"
  40. #include "suballoc.h"
  41. #include "sysfile.h"
  42. #include "file.h"
  43. #include "super.h"
  44. #include "uptodate.h"
  45. #include "buffer_head_io.h"
  46. static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
  47. /*
  48. * Structures which describe a path through a btree, and functions to
  49. * manipulate them.
  50. *
  51. * The idea here is to be as generic as possible with the tree
  52. * manipulation code.
  53. */
  54. struct ocfs2_path_item {
  55. struct buffer_head *bh;
  56. struct ocfs2_extent_list *el;
  57. };
  58. #define OCFS2_MAX_PATH_DEPTH 5
  59. struct ocfs2_path {
  60. int p_tree_depth;
  61. struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
  62. };
  63. #define path_root_bh(_path) ((_path)->p_node[0].bh)
  64. #define path_root_el(_path) ((_path)->p_node[0].el)
  65. #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
  66. #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
  67. #define path_num_items(_path) ((_path)->p_tree_depth + 1)
  68. /*
  69. * Reset the actual path elements so that we can re-use the structure
  70. * to build another path. Generally, this involves freeing the buffer
  71. * heads.
  72. */
  73. static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
  74. {
  75. int i, start = 0, depth = 0;
  76. struct ocfs2_path_item *node;
  77. if (keep_root)
  78. start = 1;
  79. for(i = start; i < path_num_items(path); i++) {
  80. node = &path->p_node[i];
  81. brelse(node->bh);
  82. node->bh = NULL;
  83. node->el = NULL;
  84. }
  85. /*
  86. * Tree depth may change during truncate, or insert. If we're
  87. * keeping the root extent list, then make sure that our path
  88. * structure reflects the proper depth.
  89. */
  90. if (keep_root)
  91. depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
  92. path->p_tree_depth = depth;
  93. }
  94. static void ocfs2_free_path(struct ocfs2_path *path)
  95. {
  96. if (path) {
  97. ocfs2_reinit_path(path, 0);
  98. kfree(path);
  99. }
  100. }
  101. /*
  102. * Make the *dest path the same as src and re-initialize src path to
  103. * have a root only.
  104. */
  105. static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
  106. {
  107. int i;
  108. BUG_ON(path_root_bh(dest) != path_root_bh(src));
  109. for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
  110. brelse(dest->p_node[i].bh);
  111. dest->p_node[i].bh = src->p_node[i].bh;
  112. dest->p_node[i].el = src->p_node[i].el;
  113. src->p_node[i].bh = NULL;
  114. src->p_node[i].el = NULL;
  115. }
  116. }
  117. /*
  118. * Insert an extent block at given index.
  119. *
  120. * This will not take an additional reference on eb_bh.
  121. */
  122. static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
  123. struct buffer_head *eb_bh)
  124. {
  125. struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
  126. /*
  127. * Right now, no root bh is an extent block, so this helps
  128. * catch code errors with dinode trees. The assertion can be
  129. * safely removed if we ever need to insert extent block
  130. * structures at the root.
  131. */
  132. BUG_ON(index == 0);
  133. path->p_node[index].bh = eb_bh;
  134. path->p_node[index].el = &eb->h_list;
  135. }
  136. static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
  137. struct ocfs2_extent_list *root_el)
  138. {
  139. struct ocfs2_path *path;
  140. BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
  141. path = kzalloc(sizeof(*path), GFP_NOFS);
  142. if (path) {
  143. path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
  144. get_bh(root_bh);
  145. path_root_bh(path) = root_bh;
  146. path_root_el(path) = root_el;
  147. }
  148. return path;
  149. }
  150. /*
  151. * Allocate and initialize a new path based on a disk inode tree.
  152. */
  153. static struct ocfs2_path *ocfs2_new_inode_path(struct buffer_head *di_bh)
  154. {
  155. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  156. struct ocfs2_extent_list *el = &di->id2.i_list;
  157. return ocfs2_new_path(di_bh, el);
  158. }
  159. /*
  160. * Convenience function to journal all components in a path.
  161. */
  162. static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
  163. struct ocfs2_path *path)
  164. {
  165. int i, ret = 0;
  166. if (!path)
  167. goto out;
  168. for(i = 0; i < path_num_items(path); i++) {
  169. ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
  170. OCFS2_JOURNAL_ACCESS_WRITE);
  171. if (ret < 0) {
  172. mlog_errno(ret);
  173. goto out;
  174. }
  175. }
  176. out:
  177. return ret;
  178. }
  179. enum ocfs2_contig_type {
  180. CONTIG_NONE = 0,
  181. CONTIG_LEFT,
  182. CONTIG_RIGHT
  183. };
  184. static int ocfs2_block_extent_contig(struct super_block *sb,
  185. struct ocfs2_extent_rec *ext,
  186. u64 blkno)
  187. {
  188. return blkno == (le64_to_cpu(ext->e_blkno) +
  189. ocfs2_clusters_to_blocks(sb,
  190. le32_to_cpu(ext->e_clusters)));
  191. }
  192. static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
  193. struct ocfs2_extent_rec *right)
  194. {
  195. return (le32_to_cpu(left->e_cpos) + le32_to_cpu(left->e_clusters) ==
  196. le32_to_cpu(right->e_cpos));
  197. }
  198. static enum ocfs2_contig_type
  199. ocfs2_extent_contig(struct inode *inode,
  200. struct ocfs2_extent_rec *ext,
  201. struct ocfs2_extent_rec *insert_rec)
  202. {
  203. u64 blkno = le64_to_cpu(insert_rec->e_blkno);
  204. if (ocfs2_extents_adjacent(ext, insert_rec) &&
  205. ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
  206. return CONTIG_RIGHT;
  207. blkno = le64_to_cpu(ext->e_blkno);
  208. if (ocfs2_extents_adjacent(insert_rec, ext) &&
  209. ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
  210. return CONTIG_LEFT;
  211. return CONTIG_NONE;
  212. }
  213. /*
  214. * NOTE: We can have pretty much any combination of contiguousness and
  215. * appending.
  216. *
  217. * The usefulness of APPEND_TAIL is more in that it lets us know that
  218. * we'll have to update the path to that leaf.
  219. */
  220. enum ocfs2_append_type {
  221. APPEND_NONE = 0,
  222. APPEND_TAIL,
  223. };
  224. struct ocfs2_insert_type {
  225. enum ocfs2_append_type ins_appending;
  226. enum ocfs2_contig_type ins_contig;
  227. int ins_contig_index;
  228. int ins_free_records;
  229. int ins_tree_depth;
  230. };
  231. /*
  232. * How many free extents have we got before we need more meta data?
  233. */
  234. int ocfs2_num_free_extents(struct ocfs2_super *osb,
  235. struct inode *inode,
  236. struct ocfs2_dinode *fe)
  237. {
  238. int retval;
  239. struct ocfs2_extent_list *el;
  240. struct ocfs2_extent_block *eb;
  241. struct buffer_head *eb_bh = NULL;
  242. mlog_entry_void();
  243. if (!OCFS2_IS_VALID_DINODE(fe)) {
  244. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  245. retval = -EIO;
  246. goto bail;
  247. }
  248. if (fe->i_last_eb_blk) {
  249. retval = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
  250. &eb_bh, OCFS2_BH_CACHED, inode);
  251. if (retval < 0) {
  252. mlog_errno(retval);
  253. goto bail;
  254. }
  255. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  256. el = &eb->h_list;
  257. } else
  258. el = &fe->id2.i_list;
  259. BUG_ON(el->l_tree_depth != 0);
  260. retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
  261. bail:
  262. if (eb_bh)
  263. brelse(eb_bh);
  264. mlog_exit(retval);
  265. return retval;
  266. }
  267. /* expects array to already be allocated
  268. *
  269. * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
  270. * l_count for you
  271. */
  272. static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
  273. handle_t *handle,
  274. struct inode *inode,
  275. int wanted,
  276. struct ocfs2_alloc_context *meta_ac,
  277. struct buffer_head *bhs[])
  278. {
  279. int count, status, i;
  280. u16 suballoc_bit_start;
  281. u32 num_got;
  282. u64 first_blkno;
  283. struct ocfs2_extent_block *eb;
  284. mlog_entry_void();
  285. count = 0;
  286. while (count < wanted) {
  287. status = ocfs2_claim_metadata(osb,
  288. handle,
  289. meta_ac,
  290. wanted - count,
  291. &suballoc_bit_start,
  292. &num_got,
  293. &first_blkno);
  294. if (status < 0) {
  295. mlog_errno(status);
  296. goto bail;
  297. }
  298. for(i = count; i < (num_got + count); i++) {
  299. bhs[i] = sb_getblk(osb->sb, first_blkno);
  300. if (bhs[i] == NULL) {
  301. status = -EIO;
  302. mlog_errno(status);
  303. goto bail;
  304. }
  305. ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
  306. status = ocfs2_journal_access(handle, inode, bhs[i],
  307. OCFS2_JOURNAL_ACCESS_CREATE);
  308. if (status < 0) {
  309. mlog_errno(status);
  310. goto bail;
  311. }
  312. memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
  313. eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
  314. /* Ok, setup the minimal stuff here. */
  315. strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
  316. eb->h_blkno = cpu_to_le64(first_blkno);
  317. eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
  318. #ifndef OCFS2_USE_ALL_METADATA_SUBALLOCATORS
  319. /* we always use slot zero's suballocator */
  320. eb->h_suballoc_slot = 0;
  321. #else
  322. eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
  323. #endif
  324. eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
  325. eb->h_list.l_count =
  326. cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
  327. suballoc_bit_start++;
  328. first_blkno++;
  329. /* We'll also be dirtied by the caller, so
  330. * this isn't absolutely necessary. */
  331. status = ocfs2_journal_dirty(handle, bhs[i]);
  332. if (status < 0) {
  333. mlog_errno(status);
  334. goto bail;
  335. }
  336. }
  337. count += num_got;
  338. }
  339. status = 0;
  340. bail:
  341. if (status < 0) {
  342. for(i = 0; i < wanted; i++) {
  343. if (bhs[i])
  344. brelse(bhs[i]);
  345. bhs[i] = NULL;
  346. }
  347. }
  348. mlog_exit(status);
  349. return status;
  350. }
  351. /*
  352. * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
  353. *
  354. * Returns the sum of the rightmost extent rec logical offset and
  355. * cluster count.
  356. *
  357. * ocfs2_add_branch() uses this to determine what logical cluster
  358. * value should be populated into the leftmost new branch records.
  359. *
  360. * ocfs2_shift_tree_depth() uses this to determine the # clusters
  361. * value for the new topmost tree record.
  362. */
  363. static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
  364. {
  365. int i;
  366. i = le16_to_cpu(el->l_next_free_rec) - 1;
  367. return le32_to_cpu(el->l_recs[i].e_cpos) +
  368. le32_to_cpu(el->l_recs[i].e_clusters);
  369. }
  370. /*
  371. * Add an entire tree branch to our inode. eb_bh is the extent block
  372. * to start at, if we don't want to start the branch at the dinode
  373. * structure.
  374. *
  375. * last_eb_bh is required as we have to update it's next_leaf pointer
  376. * for the new last extent block.
  377. *
  378. * the new branch will be 'empty' in the sense that every block will
  379. * contain a single record with e_clusters == 0.
  380. */
  381. static int ocfs2_add_branch(struct ocfs2_super *osb,
  382. handle_t *handle,
  383. struct inode *inode,
  384. struct buffer_head *fe_bh,
  385. struct buffer_head *eb_bh,
  386. struct buffer_head *last_eb_bh,
  387. struct ocfs2_alloc_context *meta_ac)
  388. {
  389. int status, new_blocks, i;
  390. u64 next_blkno, new_last_eb_blk;
  391. struct buffer_head *bh;
  392. struct buffer_head **new_eb_bhs = NULL;
  393. struct ocfs2_dinode *fe;
  394. struct ocfs2_extent_block *eb;
  395. struct ocfs2_extent_list *eb_el;
  396. struct ocfs2_extent_list *el;
  397. u32 new_cpos;
  398. mlog_entry_void();
  399. BUG_ON(!last_eb_bh);
  400. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  401. if (eb_bh) {
  402. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  403. el = &eb->h_list;
  404. } else
  405. el = &fe->id2.i_list;
  406. /* we never add a branch to a leaf. */
  407. BUG_ON(!el->l_tree_depth);
  408. new_blocks = le16_to_cpu(el->l_tree_depth);
  409. /* allocate the number of new eb blocks we need */
  410. new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
  411. GFP_KERNEL);
  412. if (!new_eb_bhs) {
  413. status = -ENOMEM;
  414. mlog_errno(status);
  415. goto bail;
  416. }
  417. status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
  418. meta_ac, new_eb_bhs);
  419. if (status < 0) {
  420. mlog_errno(status);
  421. goto bail;
  422. }
  423. eb = (struct ocfs2_extent_block *)last_eb_bh->b_data;
  424. new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
  425. /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
  426. * linked with the rest of the tree.
  427. * conversly, new_eb_bhs[0] is the new bottommost leaf.
  428. *
  429. * when we leave the loop, new_last_eb_blk will point to the
  430. * newest leaf, and next_blkno will point to the topmost extent
  431. * block. */
  432. next_blkno = new_last_eb_blk = 0;
  433. for(i = 0; i < new_blocks; i++) {
  434. bh = new_eb_bhs[i];
  435. eb = (struct ocfs2_extent_block *) bh->b_data;
  436. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  437. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  438. status = -EIO;
  439. goto bail;
  440. }
  441. eb_el = &eb->h_list;
  442. status = ocfs2_journal_access(handle, inode, bh,
  443. OCFS2_JOURNAL_ACCESS_CREATE);
  444. if (status < 0) {
  445. mlog_errno(status);
  446. goto bail;
  447. }
  448. eb->h_next_leaf_blk = 0;
  449. eb_el->l_tree_depth = cpu_to_le16(i);
  450. eb_el->l_next_free_rec = cpu_to_le16(1);
  451. /*
  452. * This actually counts as an empty extent as
  453. * c_clusters == 0
  454. */
  455. eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
  456. eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
  457. eb_el->l_recs[0].e_clusters = cpu_to_le32(0);
  458. if (!eb_el->l_tree_depth)
  459. new_last_eb_blk = le64_to_cpu(eb->h_blkno);
  460. status = ocfs2_journal_dirty(handle, bh);
  461. if (status < 0) {
  462. mlog_errno(status);
  463. goto bail;
  464. }
  465. next_blkno = le64_to_cpu(eb->h_blkno);
  466. }
  467. /* This is a bit hairy. We want to update up to three blocks
  468. * here without leaving any of them in an inconsistent state
  469. * in case of error. We don't have to worry about
  470. * journal_dirty erroring as it won't unless we've aborted the
  471. * handle (in which case we would never be here) so reserving
  472. * the write with journal_access is all we need to do. */
  473. status = ocfs2_journal_access(handle, inode, last_eb_bh,
  474. OCFS2_JOURNAL_ACCESS_WRITE);
  475. if (status < 0) {
  476. mlog_errno(status);
  477. goto bail;
  478. }
  479. status = ocfs2_journal_access(handle, inode, fe_bh,
  480. OCFS2_JOURNAL_ACCESS_WRITE);
  481. if (status < 0) {
  482. mlog_errno(status);
  483. goto bail;
  484. }
  485. if (eb_bh) {
  486. status = ocfs2_journal_access(handle, inode, eb_bh,
  487. OCFS2_JOURNAL_ACCESS_WRITE);
  488. if (status < 0) {
  489. mlog_errno(status);
  490. goto bail;
  491. }
  492. }
  493. /* Link the new branch into the rest of the tree (el will
  494. * either be on the fe, or the extent block passed in. */
  495. i = le16_to_cpu(el->l_next_free_rec);
  496. el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
  497. el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
  498. el->l_recs[i].e_clusters = 0;
  499. le16_add_cpu(&el->l_next_free_rec, 1);
  500. /* fe needs a new last extent block pointer, as does the
  501. * next_leaf on the previously last-extent-block. */
  502. fe->i_last_eb_blk = cpu_to_le64(new_last_eb_blk);
  503. eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
  504. eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
  505. status = ocfs2_journal_dirty(handle, last_eb_bh);
  506. if (status < 0)
  507. mlog_errno(status);
  508. status = ocfs2_journal_dirty(handle, fe_bh);
  509. if (status < 0)
  510. mlog_errno(status);
  511. if (eb_bh) {
  512. status = ocfs2_journal_dirty(handle, eb_bh);
  513. if (status < 0)
  514. mlog_errno(status);
  515. }
  516. status = 0;
  517. bail:
  518. if (new_eb_bhs) {
  519. for (i = 0; i < new_blocks; i++)
  520. if (new_eb_bhs[i])
  521. brelse(new_eb_bhs[i]);
  522. kfree(new_eb_bhs);
  523. }
  524. mlog_exit(status);
  525. return status;
  526. }
  527. /*
  528. * adds another level to the allocation tree.
  529. * returns back the new extent block so you can add a branch to it
  530. * after this call.
  531. */
  532. static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
  533. handle_t *handle,
  534. struct inode *inode,
  535. struct buffer_head *fe_bh,
  536. struct ocfs2_alloc_context *meta_ac,
  537. struct buffer_head **ret_new_eb_bh)
  538. {
  539. int status, i;
  540. u32 new_clusters;
  541. struct buffer_head *new_eb_bh = NULL;
  542. struct ocfs2_dinode *fe;
  543. struct ocfs2_extent_block *eb;
  544. struct ocfs2_extent_list *fe_el;
  545. struct ocfs2_extent_list *eb_el;
  546. mlog_entry_void();
  547. status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
  548. &new_eb_bh);
  549. if (status < 0) {
  550. mlog_errno(status);
  551. goto bail;
  552. }
  553. eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
  554. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  555. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  556. status = -EIO;
  557. goto bail;
  558. }
  559. eb_el = &eb->h_list;
  560. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  561. fe_el = &fe->id2.i_list;
  562. status = ocfs2_journal_access(handle, inode, new_eb_bh,
  563. OCFS2_JOURNAL_ACCESS_CREATE);
  564. if (status < 0) {
  565. mlog_errno(status);
  566. goto bail;
  567. }
  568. /* copy the fe data into the new extent block */
  569. eb_el->l_tree_depth = fe_el->l_tree_depth;
  570. eb_el->l_next_free_rec = fe_el->l_next_free_rec;
  571. for(i = 0; i < le16_to_cpu(fe_el->l_next_free_rec); i++) {
  572. eb_el->l_recs[i].e_cpos = fe_el->l_recs[i].e_cpos;
  573. eb_el->l_recs[i].e_clusters = fe_el->l_recs[i].e_clusters;
  574. eb_el->l_recs[i].e_blkno = fe_el->l_recs[i].e_blkno;
  575. }
  576. status = ocfs2_journal_dirty(handle, new_eb_bh);
  577. if (status < 0) {
  578. mlog_errno(status);
  579. goto bail;
  580. }
  581. status = ocfs2_journal_access(handle, inode, fe_bh,
  582. OCFS2_JOURNAL_ACCESS_WRITE);
  583. if (status < 0) {
  584. mlog_errno(status);
  585. goto bail;
  586. }
  587. new_clusters = ocfs2_sum_rightmost_rec(eb_el);
  588. /* update fe now */
  589. le16_add_cpu(&fe_el->l_tree_depth, 1);
  590. fe_el->l_recs[0].e_cpos = 0;
  591. fe_el->l_recs[0].e_blkno = eb->h_blkno;
  592. fe_el->l_recs[0].e_clusters = cpu_to_le32(new_clusters);
  593. for(i = 1; i < le16_to_cpu(fe_el->l_next_free_rec); i++) {
  594. fe_el->l_recs[i].e_cpos = 0;
  595. fe_el->l_recs[i].e_clusters = 0;
  596. fe_el->l_recs[i].e_blkno = 0;
  597. }
  598. fe_el->l_next_free_rec = cpu_to_le16(1);
  599. /* If this is our 1st tree depth shift, then last_eb_blk
  600. * becomes the allocated extent block */
  601. if (fe_el->l_tree_depth == cpu_to_le16(1))
  602. fe->i_last_eb_blk = eb->h_blkno;
  603. status = ocfs2_journal_dirty(handle, fe_bh);
  604. if (status < 0) {
  605. mlog_errno(status);
  606. goto bail;
  607. }
  608. *ret_new_eb_bh = new_eb_bh;
  609. new_eb_bh = NULL;
  610. status = 0;
  611. bail:
  612. if (new_eb_bh)
  613. brelse(new_eb_bh);
  614. mlog_exit(status);
  615. return status;
  616. }
  617. /*
  618. * Should only be called when there is no space left in any of the
  619. * leaf nodes. What we want to do is find the lowest tree depth
  620. * non-leaf extent block with room for new records. There are three
  621. * valid results of this search:
  622. *
  623. * 1) a lowest extent block is found, then we pass it back in
  624. * *lowest_eb_bh and return '0'
  625. *
  626. * 2) the search fails to find anything, but the dinode has room. We
  627. * pass NULL back in *lowest_eb_bh, but still return '0'
  628. *
  629. * 3) the search fails to find anything AND the dinode is full, in
  630. * which case we return > 0
  631. *
  632. * return status < 0 indicates an error.
  633. */
  634. static int ocfs2_find_branch_target(struct ocfs2_super *osb,
  635. struct inode *inode,
  636. struct buffer_head *fe_bh,
  637. struct buffer_head **target_bh)
  638. {
  639. int status = 0, i;
  640. u64 blkno;
  641. struct ocfs2_dinode *fe;
  642. struct ocfs2_extent_block *eb;
  643. struct ocfs2_extent_list *el;
  644. struct buffer_head *bh = NULL;
  645. struct buffer_head *lowest_bh = NULL;
  646. mlog_entry_void();
  647. *target_bh = NULL;
  648. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  649. el = &fe->id2.i_list;
  650. while(le16_to_cpu(el->l_tree_depth) > 1) {
  651. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  652. ocfs2_error(inode->i_sb, "Dinode %llu has empty "
  653. "extent list (next_free_rec == 0)",
  654. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  655. status = -EIO;
  656. goto bail;
  657. }
  658. i = le16_to_cpu(el->l_next_free_rec) - 1;
  659. blkno = le64_to_cpu(el->l_recs[i].e_blkno);
  660. if (!blkno) {
  661. ocfs2_error(inode->i_sb, "Dinode %llu has extent "
  662. "list where extent # %d has no physical "
  663. "block start",
  664. (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
  665. status = -EIO;
  666. goto bail;
  667. }
  668. if (bh) {
  669. brelse(bh);
  670. bh = NULL;
  671. }
  672. status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED,
  673. inode);
  674. if (status < 0) {
  675. mlog_errno(status);
  676. goto bail;
  677. }
  678. eb = (struct ocfs2_extent_block *) bh->b_data;
  679. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  680. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  681. status = -EIO;
  682. goto bail;
  683. }
  684. el = &eb->h_list;
  685. if (le16_to_cpu(el->l_next_free_rec) <
  686. le16_to_cpu(el->l_count)) {
  687. if (lowest_bh)
  688. brelse(lowest_bh);
  689. lowest_bh = bh;
  690. get_bh(lowest_bh);
  691. }
  692. }
  693. /* If we didn't find one and the fe doesn't have any room,
  694. * then return '1' */
  695. if (!lowest_bh
  696. && (fe->id2.i_list.l_next_free_rec == fe->id2.i_list.l_count))
  697. status = 1;
  698. *target_bh = lowest_bh;
  699. bail:
  700. if (bh)
  701. brelse(bh);
  702. mlog_exit(status);
  703. return status;
  704. }
  705. static inline int ocfs2_is_empty_extent(struct ocfs2_extent_rec *rec)
  706. {
  707. return !rec->e_clusters;
  708. }
  709. /*
  710. * This function will discard the rightmost extent record.
  711. */
  712. static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
  713. {
  714. int next_free = le16_to_cpu(el->l_next_free_rec);
  715. int count = le16_to_cpu(el->l_count);
  716. unsigned int num_bytes;
  717. BUG_ON(!next_free);
  718. /* This will cause us to go off the end of our extent list. */
  719. BUG_ON(next_free >= count);
  720. num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
  721. memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
  722. }
  723. static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
  724. struct ocfs2_extent_rec *insert_rec)
  725. {
  726. int i, insert_index, next_free, has_empty, num_bytes;
  727. u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
  728. struct ocfs2_extent_rec *rec;
  729. next_free = le16_to_cpu(el->l_next_free_rec);
  730. has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
  731. BUG_ON(!next_free);
  732. /* The tree code before us didn't allow enough room in the leaf. */
  733. if (el->l_next_free_rec == el->l_count && !has_empty)
  734. BUG();
  735. /*
  736. * The easiest way to approach this is to just remove the
  737. * empty extent and temporarily decrement next_free.
  738. */
  739. if (has_empty) {
  740. /*
  741. * If next_free was 1 (only an empty extent), this
  742. * loop won't execute, which is fine. We still want
  743. * the decrement above to happen.
  744. */
  745. for(i = 0; i < (next_free - 1); i++)
  746. el->l_recs[i] = el->l_recs[i+1];
  747. next_free--;
  748. }
  749. /*
  750. * Figure out what the new record index should be.
  751. */
  752. for(i = 0; i < next_free; i++) {
  753. rec = &el->l_recs[i];
  754. if (insert_cpos < le32_to_cpu(rec->e_cpos))
  755. break;
  756. }
  757. insert_index = i;
  758. mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
  759. insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
  760. BUG_ON(insert_index < 0);
  761. BUG_ON(insert_index >= le16_to_cpu(el->l_count));
  762. BUG_ON(insert_index > next_free);
  763. /*
  764. * No need to memmove if we're just adding to the tail.
  765. */
  766. if (insert_index != next_free) {
  767. BUG_ON(next_free >= le16_to_cpu(el->l_count));
  768. num_bytes = next_free - insert_index;
  769. num_bytes *= sizeof(struct ocfs2_extent_rec);
  770. memmove(&el->l_recs[insert_index + 1],
  771. &el->l_recs[insert_index],
  772. num_bytes);
  773. }
  774. /*
  775. * Either we had an empty extent, and need to re-increment or
  776. * there was no empty extent on a non full rightmost leaf node,
  777. * in which case we still need to increment.
  778. */
  779. next_free++;
  780. el->l_next_free_rec = cpu_to_le16(next_free);
  781. /*
  782. * Make sure none of the math above just messed up our tree.
  783. */
  784. BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
  785. el->l_recs[insert_index] = *insert_rec;
  786. }
  787. /*
  788. * Create an empty extent record .
  789. *
  790. * l_next_free_rec may be updated.
  791. *
  792. * If an empty extent already exists do nothing.
  793. */
  794. static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
  795. {
  796. int next_free = le16_to_cpu(el->l_next_free_rec);
  797. if (next_free == 0)
  798. goto set_and_inc;
  799. if (ocfs2_is_empty_extent(&el->l_recs[0]))
  800. return;
  801. mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
  802. "Asked to create an empty extent in a full list:\n"
  803. "count = %u, tree depth = %u",
  804. le16_to_cpu(el->l_count),
  805. le16_to_cpu(el->l_tree_depth));
  806. ocfs2_shift_records_right(el);
  807. set_and_inc:
  808. le16_add_cpu(&el->l_next_free_rec, 1);
  809. memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  810. }
  811. /*
  812. * For a rotation which involves two leaf nodes, the "root node" is
  813. * the lowest level tree node which contains a path to both leafs. This
  814. * resulting set of information can be used to form a complete "subtree"
  815. *
  816. * This function is passed two full paths from the dinode down to a
  817. * pair of adjacent leaves. It's task is to figure out which path
  818. * index contains the subtree root - this can be the root index itself
  819. * in a worst-case rotation.
  820. *
  821. * The array index of the subtree root is passed back.
  822. */
  823. static int ocfs2_find_subtree_root(struct inode *inode,
  824. struct ocfs2_path *left,
  825. struct ocfs2_path *right)
  826. {
  827. int i = 0;
  828. /*
  829. * Check that the caller passed in two paths from the same tree.
  830. */
  831. BUG_ON(path_root_bh(left) != path_root_bh(right));
  832. do {
  833. i++;
  834. /*
  835. * The caller didn't pass two adjacent paths.
  836. */
  837. mlog_bug_on_msg(i > left->p_tree_depth,
  838. "Inode %lu, left depth %u, right depth %u\n"
  839. "left leaf blk %llu, right leaf blk %llu\n",
  840. inode->i_ino, left->p_tree_depth,
  841. right->p_tree_depth,
  842. (unsigned long long)path_leaf_bh(left)->b_blocknr,
  843. (unsigned long long)path_leaf_bh(right)->b_blocknr);
  844. } while (left->p_node[i].bh->b_blocknr ==
  845. right->p_node[i].bh->b_blocknr);
  846. return i - 1;
  847. }
  848. typedef void (path_insert_t)(void *, struct buffer_head *);
  849. /*
  850. * Traverse a btree path in search of cpos, starting at root_el.
  851. *
  852. * This code can be called with a cpos larger than the tree, in which
  853. * case it will return the rightmost path.
  854. */
  855. static int __ocfs2_find_path(struct inode *inode,
  856. struct ocfs2_extent_list *root_el, u32 cpos,
  857. path_insert_t *func, void *data)
  858. {
  859. int i, ret = 0;
  860. u32 range;
  861. u64 blkno;
  862. struct buffer_head *bh = NULL;
  863. struct ocfs2_extent_block *eb;
  864. struct ocfs2_extent_list *el;
  865. struct ocfs2_extent_rec *rec;
  866. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  867. el = root_el;
  868. while (el->l_tree_depth) {
  869. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  870. ocfs2_error(inode->i_sb,
  871. "Inode %llu has empty extent list at "
  872. "depth %u\n",
  873. (unsigned long long)oi->ip_blkno,
  874. le16_to_cpu(el->l_tree_depth));
  875. ret = -EROFS;
  876. goto out;
  877. }
  878. for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
  879. rec = &el->l_recs[i];
  880. /*
  881. * In the case that cpos is off the allocation
  882. * tree, this should just wind up returning the
  883. * rightmost record.
  884. */
  885. range = le32_to_cpu(rec->e_cpos) +
  886. le32_to_cpu(rec->e_clusters);
  887. if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
  888. break;
  889. }
  890. blkno = le64_to_cpu(el->l_recs[i].e_blkno);
  891. if (blkno == 0) {
  892. ocfs2_error(inode->i_sb,
  893. "Inode %llu has bad blkno in extent list "
  894. "at depth %u (index %d)\n",
  895. (unsigned long long)oi->ip_blkno,
  896. le16_to_cpu(el->l_tree_depth), i);
  897. ret = -EROFS;
  898. goto out;
  899. }
  900. brelse(bh);
  901. bh = NULL;
  902. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
  903. &bh, OCFS2_BH_CACHED, inode);
  904. if (ret) {
  905. mlog_errno(ret);
  906. goto out;
  907. }
  908. eb = (struct ocfs2_extent_block *) bh->b_data;
  909. el = &eb->h_list;
  910. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  911. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  912. ret = -EIO;
  913. goto out;
  914. }
  915. if (le16_to_cpu(el->l_next_free_rec) >
  916. le16_to_cpu(el->l_count)) {
  917. ocfs2_error(inode->i_sb,
  918. "Inode %llu has bad count in extent list "
  919. "at block %llu (next free=%u, count=%u)\n",
  920. (unsigned long long)oi->ip_blkno,
  921. (unsigned long long)bh->b_blocknr,
  922. le16_to_cpu(el->l_next_free_rec),
  923. le16_to_cpu(el->l_count));
  924. ret = -EROFS;
  925. goto out;
  926. }
  927. if (func)
  928. func(data, bh);
  929. }
  930. out:
  931. /*
  932. * Catch any trailing bh that the loop didn't handle.
  933. */
  934. brelse(bh);
  935. return ret;
  936. }
  937. /*
  938. * Given an initialized path (that is, it has a valid root extent
  939. * list), this function will traverse the btree in search of the path
  940. * which would contain cpos.
  941. *
  942. * The path traveled is recorded in the path structure.
  943. *
  944. * Note that this will not do any comparisons on leaf node extent
  945. * records, so it will work fine in the case that we just added a tree
  946. * branch.
  947. */
  948. struct find_path_data {
  949. int index;
  950. struct ocfs2_path *path;
  951. };
  952. static void find_path_ins(void *data, struct buffer_head *bh)
  953. {
  954. struct find_path_data *fp = data;
  955. get_bh(bh);
  956. ocfs2_path_insert_eb(fp->path, fp->index, bh);
  957. fp->index++;
  958. }
  959. static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
  960. u32 cpos)
  961. {
  962. struct find_path_data data;
  963. data.index = 1;
  964. data.path = path;
  965. return __ocfs2_find_path(inode, path_root_el(path), cpos,
  966. find_path_ins, &data);
  967. }
  968. static void find_leaf_ins(void *data, struct buffer_head *bh)
  969. {
  970. struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
  971. struct ocfs2_extent_list *el = &eb->h_list;
  972. struct buffer_head **ret = data;
  973. /* We want to retain only the leaf block. */
  974. if (le16_to_cpu(el->l_tree_depth) == 0) {
  975. get_bh(bh);
  976. *ret = bh;
  977. }
  978. }
  979. /*
  980. * Find the leaf block in the tree which would contain cpos. No
  981. * checking of the actual leaf is done.
  982. *
  983. * Some paths want to call this instead of allocating a path structure
  984. * and calling ocfs2_find_path().
  985. *
  986. * This function doesn't handle non btree extent lists.
  987. */
  988. int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
  989. u32 cpos, struct buffer_head **leaf_bh)
  990. {
  991. int ret;
  992. struct buffer_head *bh = NULL;
  993. ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
  994. if (ret) {
  995. mlog_errno(ret);
  996. goto out;
  997. }
  998. *leaf_bh = bh;
  999. out:
  1000. return ret;
  1001. }
  1002. /*
  1003. * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
  1004. *
  1005. * Basically, we've moved stuff around at the bottom of the tree and
  1006. * we need to fix up the extent records above the changes to reflect
  1007. * the new changes.
  1008. *
  1009. * left_rec: the record on the left.
  1010. * left_child_el: is the child list pointed to by left_rec
  1011. * right_rec: the record to the right of left_rec
  1012. * right_child_el: is the child list pointed to by right_rec
  1013. *
  1014. * By definition, this only works on interior nodes.
  1015. */
  1016. static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
  1017. struct ocfs2_extent_list *left_child_el,
  1018. struct ocfs2_extent_rec *right_rec,
  1019. struct ocfs2_extent_list *right_child_el)
  1020. {
  1021. u32 left_clusters, right_end;
  1022. /*
  1023. * Interior nodes never have holes. Their cpos is the cpos of
  1024. * the leftmost record in their child list. Their cluster
  1025. * count covers the full theoretical range of their child list
  1026. * - the range between their cpos and the cpos of the record
  1027. * immediately to their right.
  1028. */
  1029. left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
  1030. left_clusters -= le32_to_cpu(left_rec->e_cpos);
  1031. left_rec->e_clusters = cpu_to_le32(left_clusters);
  1032. /*
  1033. * Calculate the rightmost cluster count boundary before
  1034. * moving cpos - we will need to adjust e_clusters after
  1035. * updating e_cpos to keep the same highest cluster count.
  1036. */
  1037. right_end = le32_to_cpu(right_rec->e_cpos);
  1038. right_end += le32_to_cpu(right_rec->e_clusters);
  1039. right_rec->e_cpos = left_rec->e_cpos;
  1040. le32_add_cpu(&right_rec->e_cpos, left_clusters);
  1041. right_end -= le32_to_cpu(right_rec->e_cpos);
  1042. right_rec->e_clusters = cpu_to_le32(right_end);
  1043. }
  1044. /*
  1045. * Adjust the adjacent root node records involved in a
  1046. * rotation. left_el_blkno is passed in as a key so that we can easily
  1047. * find it's index in the root list.
  1048. */
  1049. static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
  1050. struct ocfs2_extent_list *left_el,
  1051. struct ocfs2_extent_list *right_el,
  1052. u64 left_el_blkno)
  1053. {
  1054. int i;
  1055. BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
  1056. le16_to_cpu(left_el->l_tree_depth));
  1057. for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
  1058. if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
  1059. break;
  1060. }
  1061. /*
  1062. * The path walking code should have never returned a root and
  1063. * two paths which are not adjacent.
  1064. */
  1065. BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
  1066. ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
  1067. &root_el->l_recs[i + 1], right_el);
  1068. }
  1069. /*
  1070. * We've changed a leaf block (in right_path) and need to reflect that
  1071. * change back up the subtree.
  1072. *
  1073. * This happens in multiple places:
  1074. * - When we've moved an extent record from the left path leaf to the right
  1075. * path leaf to make room for an empty extent in the left path leaf.
  1076. * - When our insert into the right path leaf is at the leftmost edge
  1077. * and requires an update of the path immediately to it's left. This
  1078. * can occur at the end of some types of rotation and appending inserts.
  1079. */
  1080. static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
  1081. struct ocfs2_path *left_path,
  1082. struct ocfs2_path *right_path,
  1083. int subtree_index)
  1084. {
  1085. int ret, i, idx;
  1086. struct ocfs2_extent_list *el, *left_el, *right_el;
  1087. struct ocfs2_extent_rec *left_rec, *right_rec;
  1088. struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
  1089. /*
  1090. * Update the counts and position values within all the
  1091. * interior nodes to reflect the leaf rotation we just did.
  1092. *
  1093. * The root node is handled below the loop.
  1094. *
  1095. * We begin the loop with right_el and left_el pointing to the
  1096. * leaf lists and work our way up.
  1097. *
  1098. * NOTE: within this loop, left_el and right_el always refer
  1099. * to the *child* lists.
  1100. */
  1101. left_el = path_leaf_el(left_path);
  1102. right_el = path_leaf_el(right_path);
  1103. for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
  1104. mlog(0, "Adjust records at index %u\n", i);
  1105. /*
  1106. * One nice property of knowing that all of these
  1107. * nodes are below the root is that we only deal with
  1108. * the leftmost right node record and the rightmost
  1109. * left node record.
  1110. */
  1111. el = left_path->p_node[i].el;
  1112. idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
  1113. left_rec = &el->l_recs[idx];
  1114. el = right_path->p_node[i].el;
  1115. right_rec = &el->l_recs[0];
  1116. ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
  1117. right_el);
  1118. ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
  1119. if (ret)
  1120. mlog_errno(ret);
  1121. ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
  1122. if (ret)
  1123. mlog_errno(ret);
  1124. /*
  1125. * Setup our list pointers now so that the current
  1126. * parents become children in the next iteration.
  1127. */
  1128. left_el = left_path->p_node[i].el;
  1129. right_el = right_path->p_node[i].el;
  1130. }
  1131. /*
  1132. * At the root node, adjust the two adjacent records which
  1133. * begin our path to the leaves.
  1134. */
  1135. el = left_path->p_node[subtree_index].el;
  1136. left_el = left_path->p_node[subtree_index + 1].el;
  1137. right_el = right_path->p_node[subtree_index + 1].el;
  1138. ocfs2_adjust_root_records(el, left_el, right_el,
  1139. left_path->p_node[subtree_index + 1].bh->b_blocknr);
  1140. root_bh = left_path->p_node[subtree_index].bh;
  1141. ret = ocfs2_journal_dirty(handle, root_bh);
  1142. if (ret)
  1143. mlog_errno(ret);
  1144. }
  1145. static int ocfs2_rotate_subtree_right(struct inode *inode,
  1146. handle_t *handle,
  1147. struct ocfs2_path *left_path,
  1148. struct ocfs2_path *right_path,
  1149. int subtree_index)
  1150. {
  1151. int ret, i;
  1152. struct buffer_head *right_leaf_bh;
  1153. struct buffer_head *left_leaf_bh = NULL;
  1154. struct buffer_head *root_bh;
  1155. struct ocfs2_extent_list *right_el, *left_el;
  1156. struct ocfs2_extent_rec move_rec;
  1157. left_leaf_bh = path_leaf_bh(left_path);
  1158. left_el = path_leaf_el(left_path);
  1159. if (left_el->l_next_free_rec != left_el->l_count) {
  1160. ocfs2_error(inode->i_sb,
  1161. "Inode %llu has non-full interior leaf node %llu"
  1162. "(next free = %u)",
  1163. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1164. (unsigned long long)left_leaf_bh->b_blocknr,
  1165. le16_to_cpu(left_el->l_next_free_rec));
  1166. return -EROFS;
  1167. }
  1168. /*
  1169. * This extent block may already have an empty record, so we
  1170. * return early if so.
  1171. */
  1172. if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
  1173. return 0;
  1174. root_bh = left_path->p_node[subtree_index].bh;
  1175. BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
  1176. ret = ocfs2_journal_access(handle, inode, root_bh,
  1177. OCFS2_JOURNAL_ACCESS_WRITE);
  1178. if (ret) {
  1179. mlog_errno(ret);
  1180. goto out;
  1181. }
  1182. for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
  1183. ret = ocfs2_journal_access(handle, inode,
  1184. right_path->p_node[i].bh,
  1185. OCFS2_JOURNAL_ACCESS_WRITE);
  1186. if (ret) {
  1187. mlog_errno(ret);
  1188. goto out;
  1189. }
  1190. ret = ocfs2_journal_access(handle, inode,
  1191. left_path->p_node[i].bh,
  1192. OCFS2_JOURNAL_ACCESS_WRITE);
  1193. if (ret) {
  1194. mlog_errno(ret);
  1195. goto out;
  1196. }
  1197. }
  1198. right_leaf_bh = path_leaf_bh(right_path);
  1199. right_el = path_leaf_el(right_path);
  1200. /* This is a code error, not a disk corruption. */
  1201. mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
  1202. "because rightmost leaf block %llu is empty\n",
  1203. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1204. (unsigned long long)right_leaf_bh->b_blocknr);
  1205. ocfs2_create_empty_extent(right_el);
  1206. ret = ocfs2_journal_dirty(handle, right_leaf_bh);
  1207. if (ret) {
  1208. mlog_errno(ret);
  1209. goto out;
  1210. }
  1211. /* Do the copy now. */
  1212. i = le16_to_cpu(left_el->l_next_free_rec) - 1;
  1213. move_rec = left_el->l_recs[i];
  1214. right_el->l_recs[0] = move_rec;
  1215. /*
  1216. * Clear out the record we just copied and shift everything
  1217. * over, leaving an empty extent in the left leaf.
  1218. *
  1219. * We temporarily subtract from next_free_rec so that the
  1220. * shift will lose the tail record (which is now defunct).
  1221. */
  1222. le16_add_cpu(&left_el->l_next_free_rec, -1);
  1223. ocfs2_shift_records_right(left_el);
  1224. memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
  1225. le16_add_cpu(&left_el->l_next_free_rec, 1);
  1226. ret = ocfs2_journal_dirty(handle, left_leaf_bh);
  1227. if (ret) {
  1228. mlog_errno(ret);
  1229. goto out;
  1230. }
  1231. ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
  1232. subtree_index);
  1233. out:
  1234. return ret;
  1235. }
  1236. /*
  1237. * Given a full path, determine what cpos value would return us a path
  1238. * containing the leaf immediately to the left of the current one.
  1239. *
  1240. * Will return zero if the path passed in is already the leftmost path.
  1241. */
  1242. static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
  1243. struct ocfs2_path *path, u32 *cpos)
  1244. {
  1245. int i, j, ret = 0;
  1246. u64 blkno;
  1247. struct ocfs2_extent_list *el;
  1248. *cpos = 0;
  1249. blkno = path_leaf_bh(path)->b_blocknr;
  1250. /* Start at the tree node just above the leaf and work our way up. */
  1251. i = path->p_tree_depth - 1;
  1252. while (i >= 0) {
  1253. el = path->p_node[i].el;
  1254. /*
  1255. * Find the extent record just before the one in our
  1256. * path.
  1257. */
  1258. for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
  1259. if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
  1260. if (j == 0) {
  1261. if (i == 0) {
  1262. /*
  1263. * We've determined that the
  1264. * path specified is already
  1265. * the leftmost one - return a
  1266. * cpos of zero.
  1267. */
  1268. goto out;
  1269. }
  1270. /*
  1271. * The leftmost record points to our
  1272. * leaf - we need to travel up the
  1273. * tree one level.
  1274. */
  1275. goto next_node;
  1276. }
  1277. *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
  1278. *cpos = *cpos + le32_to_cpu(el->l_recs[j - 1].e_clusters) - 1;
  1279. goto out;
  1280. }
  1281. }
  1282. /*
  1283. * If we got here, we never found a valid node where
  1284. * the tree indicated one should be.
  1285. */
  1286. ocfs2_error(sb,
  1287. "Invalid extent tree at extent block %llu\n",
  1288. (unsigned long long)blkno);
  1289. ret = -EROFS;
  1290. goto out;
  1291. next_node:
  1292. blkno = path->p_node[i].bh->b_blocknr;
  1293. i--;
  1294. }
  1295. out:
  1296. return ret;
  1297. }
  1298. static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
  1299. struct ocfs2_path *path)
  1300. {
  1301. int credits = (path->p_tree_depth - subtree_depth) * 2 + 1;
  1302. if (handle->h_buffer_credits < credits)
  1303. return ocfs2_extend_trans(handle, credits);
  1304. return 0;
  1305. }
  1306. /*
  1307. * Trap the case where we're inserting into the theoretical range past
  1308. * the _actual_ left leaf range. Otherwise, we'll rotate a record
  1309. * whose cpos is less than ours into the right leaf.
  1310. *
  1311. * It's only necessary to look at the rightmost record of the left
  1312. * leaf because the logic that calls us should ensure that the
  1313. * theoretical ranges in the path components above the leaves are
  1314. * correct.
  1315. */
  1316. static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
  1317. u32 insert_cpos)
  1318. {
  1319. struct ocfs2_extent_list *left_el;
  1320. struct ocfs2_extent_rec *rec;
  1321. int next_free;
  1322. left_el = path_leaf_el(left_path);
  1323. next_free = le16_to_cpu(left_el->l_next_free_rec);
  1324. rec = &left_el->l_recs[next_free - 1];
  1325. if (insert_cpos > le32_to_cpu(rec->e_cpos))
  1326. return 1;
  1327. return 0;
  1328. }
  1329. /*
  1330. * Rotate all the records in a btree right one record, starting at insert_cpos.
  1331. *
  1332. * The path to the rightmost leaf should be passed in.
  1333. *
  1334. * The array is assumed to be large enough to hold an entire path (tree depth).
  1335. *
  1336. * Upon succesful return from this function:
  1337. *
  1338. * - The 'right_path' array will contain a path to the leaf block
  1339. * whose range contains e_cpos.
  1340. * - That leaf block will have a single empty extent in list index 0.
  1341. * - In the case that the rotation requires a post-insert update,
  1342. * *ret_left_path will contain a valid path which can be passed to
  1343. * ocfs2_insert_path().
  1344. */
  1345. static int ocfs2_rotate_tree_right(struct inode *inode,
  1346. handle_t *handle,
  1347. u32 insert_cpos,
  1348. struct ocfs2_path *right_path,
  1349. struct ocfs2_path **ret_left_path)
  1350. {
  1351. int ret, start;
  1352. u32 cpos;
  1353. struct ocfs2_path *left_path = NULL;
  1354. *ret_left_path = NULL;
  1355. left_path = ocfs2_new_path(path_root_bh(right_path),
  1356. path_root_el(right_path));
  1357. if (!left_path) {
  1358. ret = -ENOMEM;
  1359. mlog_errno(ret);
  1360. goto out;
  1361. }
  1362. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
  1363. if (ret) {
  1364. mlog_errno(ret);
  1365. goto out;
  1366. }
  1367. mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
  1368. /*
  1369. * What we want to do here is:
  1370. *
  1371. * 1) Start with the rightmost path.
  1372. *
  1373. * 2) Determine a path to the leaf block directly to the left
  1374. * of that leaf.
  1375. *
  1376. * 3) Determine the 'subtree root' - the lowest level tree node
  1377. * which contains a path to both leaves.
  1378. *
  1379. * 4) Rotate the subtree.
  1380. *
  1381. * 5) Find the next subtree by considering the left path to be
  1382. * the new right path.
  1383. *
  1384. * The check at the top of this while loop also accepts
  1385. * insert_cpos == cpos because cpos is only a _theoretical_
  1386. * value to get us the left path - insert_cpos might very well
  1387. * be filling that hole.
  1388. *
  1389. * Stop at a cpos of '0' because we either started at the
  1390. * leftmost branch (i.e., a tree with one branch and a
  1391. * rotation inside of it), or we've gone as far as we can in
  1392. * rotating subtrees.
  1393. */
  1394. while (cpos && insert_cpos <= cpos) {
  1395. mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
  1396. insert_cpos, cpos);
  1397. ret = ocfs2_find_path(inode, left_path, cpos);
  1398. if (ret) {
  1399. mlog_errno(ret);
  1400. goto out;
  1401. }
  1402. mlog_bug_on_msg(path_leaf_bh(left_path) ==
  1403. path_leaf_bh(right_path),
  1404. "Inode %lu: error during insert of %u "
  1405. "(left path cpos %u) results in two identical "
  1406. "paths ending at %llu\n",
  1407. inode->i_ino, insert_cpos, cpos,
  1408. (unsigned long long)
  1409. path_leaf_bh(left_path)->b_blocknr);
  1410. if (ocfs2_rotate_requires_path_adjustment(left_path,
  1411. insert_cpos)) {
  1412. mlog(0, "Path adjustment required\n");
  1413. /*
  1414. * We've rotated the tree as much as we
  1415. * should. The rest is up to
  1416. * ocfs2_insert_path() to complete, after the
  1417. * record insertion. We indicate this
  1418. * situation by returning the left path.
  1419. *
  1420. * The reason we don't adjust the records here
  1421. * before the record insert is that an error
  1422. * later might break the rule where a parent
  1423. * record e_cpos will reflect the actual
  1424. * e_cpos of the 1st nonempty record of the
  1425. * child list.
  1426. */
  1427. *ret_left_path = left_path;
  1428. goto out_ret_path;
  1429. }
  1430. start = ocfs2_find_subtree_root(inode, left_path, right_path);
  1431. mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
  1432. start,
  1433. (unsigned long long) right_path->p_node[start].bh->b_blocknr,
  1434. right_path->p_tree_depth);
  1435. ret = ocfs2_extend_rotate_transaction(handle, start,
  1436. right_path);
  1437. if (ret) {
  1438. mlog_errno(ret);
  1439. goto out;
  1440. }
  1441. ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
  1442. right_path, start);
  1443. if (ret) {
  1444. mlog_errno(ret);
  1445. goto out;
  1446. }
  1447. /*
  1448. * There is no need to re-read the next right path
  1449. * as we know that it'll be our current left
  1450. * path. Optimize by copying values instead.
  1451. */
  1452. ocfs2_mv_path(right_path, left_path);
  1453. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
  1454. &cpos);
  1455. if (ret) {
  1456. mlog_errno(ret);
  1457. goto out;
  1458. }
  1459. }
  1460. out:
  1461. ocfs2_free_path(left_path);
  1462. out_ret_path:
  1463. return ret;
  1464. }
  1465. /*
  1466. * Do the final bits of extent record insertion at the target leaf
  1467. * list. If this leaf is part of an allocation tree, it is assumed
  1468. * that the tree above has been prepared.
  1469. */
  1470. static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
  1471. struct ocfs2_extent_list *el,
  1472. struct ocfs2_insert_type *insert,
  1473. struct inode *inode)
  1474. {
  1475. int i = insert->ins_contig_index;
  1476. unsigned int range;
  1477. struct ocfs2_extent_rec *rec;
  1478. BUG_ON(el->l_tree_depth);
  1479. /*
  1480. * Contiguous insert - either left or right.
  1481. */
  1482. if (insert->ins_contig != CONTIG_NONE) {
  1483. rec = &el->l_recs[i];
  1484. if (insert->ins_contig == CONTIG_LEFT) {
  1485. rec->e_blkno = insert_rec->e_blkno;
  1486. rec->e_cpos = insert_rec->e_cpos;
  1487. }
  1488. le32_add_cpu(&rec->e_clusters,
  1489. le32_to_cpu(insert_rec->e_clusters));
  1490. return;
  1491. }
  1492. /*
  1493. * Handle insert into an empty leaf.
  1494. */
  1495. if (le16_to_cpu(el->l_next_free_rec) == 0 ||
  1496. ((le16_to_cpu(el->l_next_free_rec) == 1) &&
  1497. ocfs2_is_empty_extent(&el->l_recs[0]))) {
  1498. el->l_recs[0] = *insert_rec;
  1499. el->l_next_free_rec = cpu_to_le16(1);
  1500. return;
  1501. }
  1502. /*
  1503. * Appending insert.
  1504. */
  1505. if (insert->ins_appending == APPEND_TAIL) {
  1506. i = le16_to_cpu(el->l_next_free_rec) - 1;
  1507. rec = &el->l_recs[i];
  1508. range = le32_to_cpu(rec->e_cpos) + le32_to_cpu(rec->e_clusters);
  1509. BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
  1510. mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
  1511. le16_to_cpu(el->l_count),
  1512. "inode %lu, depth %u, count %u, next free %u, "
  1513. "rec.cpos %u, rec.clusters %u, "
  1514. "insert.cpos %u, insert.clusters %u\n",
  1515. inode->i_ino,
  1516. le16_to_cpu(el->l_tree_depth),
  1517. le16_to_cpu(el->l_count),
  1518. le16_to_cpu(el->l_next_free_rec),
  1519. le32_to_cpu(el->l_recs[i].e_cpos),
  1520. le32_to_cpu(el->l_recs[i].e_clusters),
  1521. le32_to_cpu(insert_rec->e_cpos),
  1522. le32_to_cpu(insert_rec->e_clusters));
  1523. i++;
  1524. el->l_recs[i] = *insert_rec;
  1525. le16_add_cpu(&el->l_next_free_rec, 1);
  1526. return;
  1527. }
  1528. /*
  1529. * Ok, we have to rotate.
  1530. *
  1531. * At this point, it is safe to assume that inserting into an
  1532. * empty leaf and appending to a leaf have both been handled
  1533. * above.
  1534. *
  1535. * This leaf needs to have space, either by the empty 1st
  1536. * extent record, or by virtue of an l_next_rec < l_count.
  1537. */
  1538. ocfs2_rotate_leaf(el, insert_rec);
  1539. }
  1540. static inline void ocfs2_update_dinode_clusters(struct inode *inode,
  1541. struct ocfs2_dinode *di,
  1542. u32 clusters)
  1543. {
  1544. le32_add_cpu(&di->i_clusters, clusters);
  1545. spin_lock(&OCFS2_I(inode)->ip_lock);
  1546. OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
  1547. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1548. }
  1549. static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
  1550. struct ocfs2_extent_rec *insert_rec,
  1551. struct ocfs2_path *right_path,
  1552. struct ocfs2_path **ret_left_path)
  1553. {
  1554. int ret, i, next_free;
  1555. struct buffer_head *bh;
  1556. struct ocfs2_extent_list *el;
  1557. struct ocfs2_path *left_path = NULL;
  1558. *ret_left_path = NULL;
  1559. /*
  1560. * If our appending insert is at the leftmost edge of a leaf,
  1561. * then we might need to update the rightmost records of the
  1562. * neighboring path.
  1563. */
  1564. el = path_leaf_el(right_path);
  1565. next_free = le16_to_cpu(el->l_next_free_rec);
  1566. if (next_free == 0 ||
  1567. (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
  1568. u32 left_cpos;
  1569. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
  1570. &left_cpos);
  1571. if (ret) {
  1572. mlog_errno(ret);
  1573. goto out;
  1574. }
  1575. mlog(0, "Append may need a left path update. cpos: %u, "
  1576. "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
  1577. left_cpos);
  1578. /*
  1579. * No need to worry if the append is already in the
  1580. * leftmost leaf.
  1581. */
  1582. if (left_cpos) {
  1583. left_path = ocfs2_new_path(path_root_bh(right_path),
  1584. path_root_el(right_path));
  1585. if (!left_path) {
  1586. ret = -ENOMEM;
  1587. mlog_errno(ret);
  1588. goto out;
  1589. }
  1590. ret = ocfs2_find_path(inode, left_path, left_cpos);
  1591. if (ret) {
  1592. mlog_errno(ret);
  1593. goto out;
  1594. }
  1595. /*
  1596. * ocfs2_insert_path() will pass the left_path to the
  1597. * journal for us.
  1598. */
  1599. }
  1600. }
  1601. ret = ocfs2_journal_access_path(inode, handle, right_path);
  1602. if (ret) {
  1603. mlog_errno(ret);
  1604. goto out;
  1605. }
  1606. el = path_root_el(right_path);
  1607. bh = path_root_bh(right_path);
  1608. i = 0;
  1609. while (1) {
  1610. next_free = le16_to_cpu(el->l_next_free_rec);
  1611. if (next_free == 0) {
  1612. ocfs2_error(inode->i_sb,
  1613. "Dinode %llu has a bad extent list",
  1614. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1615. ret = -EIO;
  1616. goto out;
  1617. }
  1618. el->l_recs[next_free - 1].e_clusters = insert_rec->e_cpos;
  1619. le32_add_cpu(&el->l_recs[next_free - 1].e_clusters,
  1620. le32_to_cpu(insert_rec->e_clusters));
  1621. le32_add_cpu(&el->l_recs[next_free - 1].e_clusters,
  1622. -le32_to_cpu(el->l_recs[next_free - 1].e_cpos));
  1623. ret = ocfs2_journal_dirty(handle, bh);
  1624. if (ret)
  1625. mlog_errno(ret);
  1626. if (++i >= right_path->p_tree_depth)
  1627. break;
  1628. bh = right_path->p_node[i].bh;
  1629. el = right_path->p_node[i].el;
  1630. }
  1631. *ret_left_path = left_path;
  1632. ret = 0;
  1633. out:
  1634. if (ret != 0)
  1635. ocfs2_free_path(left_path);
  1636. return ret;
  1637. }
  1638. /*
  1639. * This function only does inserts on an allocation b-tree. For dinode
  1640. * lists, ocfs2_insert_at_leaf() is called directly.
  1641. *
  1642. * right_path is the path we want to do the actual insert
  1643. * in. left_path should only be passed in if we need to update that
  1644. * portion of the tree after an edge insert.
  1645. */
  1646. static int ocfs2_insert_path(struct inode *inode,
  1647. handle_t *handle,
  1648. struct ocfs2_path *left_path,
  1649. struct ocfs2_path *right_path,
  1650. struct ocfs2_extent_rec *insert_rec,
  1651. struct ocfs2_insert_type *insert)
  1652. {
  1653. int ret, subtree_index;
  1654. struct buffer_head *leaf_bh = path_leaf_bh(right_path);
  1655. struct ocfs2_extent_list *el;
  1656. /*
  1657. * Pass both paths to the journal. The majority of inserts
  1658. * will be touching all components anyway.
  1659. */
  1660. ret = ocfs2_journal_access_path(inode, handle, right_path);
  1661. if (ret < 0) {
  1662. mlog_errno(ret);
  1663. goto out;
  1664. }
  1665. if (left_path) {
  1666. int credits = handle->h_buffer_credits;
  1667. /*
  1668. * There's a chance that left_path got passed back to
  1669. * us without being accounted for in the
  1670. * journal. Extend our transaction here to be sure we
  1671. * can change those blocks.
  1672. */
  1673. credits += left_path->p_tree_depth;
  1674. ret = ocfs2_extend_trans(handle, credits);
  1675. if (ret < 0) {
  1676. mlog_errno(ret);
  1677. goto out;
  1678. }
  1679. ret = ocfs2_journal_access_path(inode, handle, left_path);
  1680. if (ret < 0) {
  1681. mlog_errno(ret);
  1682. goto out;
  1683. }
  1684. }
  1685. el = path_leaf_el(right_path);
  1686. ocfs2_insert_at_leaf(insert_rec, el, insert, inode);
  1687. ret = ocfs2_journal_dirty(handle, leaf_bh);
  1688. if (ret)
  1689. mlog_errno(ret);
  1690. if (left_path) {
  1691. /*
  1692. * The rotate code has indicated that we need to fix
  1693. * up portions of the tree after the insert.
  1694. *
  1695. * XXX: Should we extend the transaction here?
  1696. */
  1697. subtree_index = ocfs2_find_subtree_root(inode, left_path,
  1698. right_path);
  1699. ocfs2_complete_edge_insert(inode, handle, left_path,
  1700. right_path, subtree_index);
  1701. }
  1702. ret = 0;
  1703. out:
  1704. return ret;
  1705. }
  1706. static int ocfs2_do_insert_extent(struct inode *inode,
  1707. handle_t *handle,
  1708. struct buffer_head *di_bh,
  1709. struct ocfs2_extent_rec *insert_rec,
  1710. struct ocfs2_insert_type *type)
  1711. {
  1712. int ret, rotate = 0;
  1713. u32 cpos;
  1714. struct ocfs2_path *right_path = NULL;
  1715. struct ocfs2_path *left_path = NULL;
  1716. struct ocfs2_dinode *di;
  1717. struct ocfs2_extent_list *el;
  1718. di = (struct ocfs2_dinode *) di_bh->b_data;
  1719. el = &di->id2.i_list;
  1720. ret = ocfs2_journal_access(handle, inode, di_bh,
  1721. OCFS2_JOURNAL_ACCESS_WRITE);
  1722. if (ret) {
  1723. mlog_errno(ret);
  1724. goto out;
  1725. }
  1726. if (le16_to_cpu(el->l_tree_depth) == 0) {
  1727. ocfs2_insert_at_leaf(insert_rec, el, type, inode);
  1728. goto out_update_clusters;
  1729. }
  1730. right_path = ocfs2_new_inode_path(di_bh);
  1731. if (!right_path) {
  1732. ret = -ENOMEM;
  1733. mlog_errno(ret);
  1734. goto out;
  1735. }
  1736. /*
  1737. * Determine the path to start with. Rotations need the
  1738. * rightmost path, everything else can go directly to the
  1739. * target leaf.
  1740. */
  1741. cpos = le32_to_cpu(insert_rec->e_cpos);
  1742. if (type->ins_appending == APPEND_NONE &&
  1743. type->ins_contig == CONTIG_NONE) {
  1744. rotate = 1;
  1745. cpos = UINT_MAX;
  1746. }
  1747. ret = ocfs2_find_path(inode, right_path, cpos);
  1748. if (ret) {
  1749. mlog_errno(ret);
  1750. goto out;
  1751. }
  1752. /*
  1753. * Rotations and appends need special treatment - they modify
  1754. * parts of the tree's above them.
  1755. *
  1756. * Both might pass back a path immediate to the left of the
  1757. * one being inserted to. This will be cause
  1758. * ocfs2_insert_path() to modify the rightmost records of
  1759. * left_path to account for an edge insert.
  1760. *
  1761. * XXX: When modifying this code, keep in mind that an insert
  1762. * can wind up skipping both of these two special cases...
  1763. */
  1764. if (rotate) {
  1765. ret = ocfs2_rotate_tree_right(inode, handle,
  1766. le32_to_cpu(insert_rec->e_cpos),
  1767. right_path, &left_path);
  1768. if (ret) {
  1769. mlog_errno(ret);
  1770. goto out;
  1771. }
  1772. } else if (type->ins_appending == APPEND_TAIL
  1773. && type->ins_contig != CONTIG_LEFT) {
  1774. ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
  1775. right_path, &left_path);
  1776. if (ret) {
  1777. mlog_errno(ret);
  1778. goto out;
  1779. }
  1780. }
  1781. ret = ocfs2_insert_path(inode, handle, left_path, right_path,
  1782. insert_rec, type);
  1783. if (ret) {
  1784. mlog_errno(ret);
  1785. goto out;
  1786. }
  1787. out_update_clusters:
  1788. ocfs2_update_dinode_clusters(inode, di,
  1789. le32_to_cpu(insert_rec->e_clusters));
  1790. ret = ocfs2_journal_dirty(handle, di_bh);
  1791. if (ret)
  1792. mlog_errno(ret);
  1793. out:
  1794. ocfs2_free_path(left_path);
  1795. ocfs2_free_path(right_path);
  1796. return ret;
  1797. }
  1798. static void ocfs2_figure_contig_type(struct inode *inode,
  1799. struct ocfs2_insert_type *insert,
  1800. struct ocfs2_extent_list *el,
  1801. struct ocfs2_extent_rec *insert_rec)
  1802. {
  1803. int i;
  1804. enum ocfs2_contig_type contig_type = CONTIG_NONE;
  1805. for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
  1806. contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
  1807. insert_rec);
  1808. if (contig_type != CONTIG_NONE) {
  1809. insert->ins_contig_index = i;
  1810. break;
  1811. }
  1812. }
  1813. insert->ins_contig = contig_type;
  1814. }
  1815. /*
  1816. * This should only be called against the righmost leaf extent list.
  1817. *
  1818. * ocfs2_figure_appending_type() will figure out whether we'll have to
  1819. * insert at the tail of the rightmost leaf.
  1820. *
  1821. * This should also work against the dinode list for tree's with 0
  1822. * depth. If we consider the dinode list to be the rightmost leaf node
  1823. * then the logic here makes sense.
  1824. */
  1825. static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
  1826. struct ocfs2_extent_list *el,
  1827. struct ocfs2_extent_rec *insert_rec)
  1828. {
  1829. int i;
  1830. u32 cpos = le32_to_cpu(insert_rec->e_cpos);
  1831. struct ocfs2_extent_rec *rec;
  1832. insert->ins_appending = APPEND_NONE;
  1833. BUG_ON(el->l_tree_depth);
  1834. if (!el->l_next_free_rec)
  1835. goto set_tail_append;
  1836. if (ocfs2_is_empty_extent(&el->l_recs[0])) {
  1837. /* Were all records empty? */
  1838. if (le16_to_cpu(el->l_next_free_rec) == 1)
  1839. goto set_tail_append;
  1840. }
  1841. i = le16_to_cpu(el->l_next_free_rec) - 1;
  1842. rec = &el->l_recs[i];
  1843. if (cpos >= (le32_to_cpu(rec->e_cpos) + le32_to_cpu(rec->e_clusters)))
  1844. goto set_tail_append;
  1845. return;
  1846. set_tail_append:
  1847. insert->ins_appending = APPEND_TAIL;
  1848. }
  1849. /*
  1850. * Helper function called at the begining of an insert.
  1851. *
  1852. * This computes a few things that are commonly used in the process of
  1853. * inserting into the btree:
  1854. * - Whether the new extent is contiguous with an existing one.
  1855. * - The current tree depth.
  1856. * - Whether the insert is an appending one.
  1857. * - The total # of free records in the tree.
  1858. *
  1859. * All of the information is stored on the ocfs2_insert_type
  1860. * structure.
  1861. */
  1862. static int ocfs2_figure_insert_type(struct inode *inode,
  1863. struct buffer_head *di_bh,
  1864. struct buffer_head **last_eb_bh,
  1865. struct ocfs2_extent_rec *insert_rec,
  1866. struct ocfs2_insert_type *insert)
  1867. {
  1868. int ret;
  1869. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1870. struct ocfs2_extent_block *eb;
  1871. struct ocfs2_extent_list *el;
  1872. struct ocfs2_path *path = NULL;
  1873. struct buffer_head *bh = NULL;
  1874. el = &di->id2.i_list;
  1875. insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
  1876. if (el->l_tree_depth) {
  1877. /*
  1878. * If we have tree depth, we read in the
  1879. * rightmost extent block ahead of time as
  1880. * ocfs2_figure_insert_type() and ocfs2_add_branch()
  1881. * may want it later.
  1882. */
  1883. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  1884. le64_to_cpu(di->i_last_eb_blk), &bh,
  1885. OCFS2_BH_CACHED, inode);
  1886. if (ret) {
  1887. mlog_exit(ret);
  1888. goto out;
  1889. }
  1890. eb = (struct ocfs2_extent_block *) bh->b_data;
  1891. el = &eb->h_list;
  1892. }
  1893. /*
  1894. * Unless we have a contiguous insert, we'll need to know if
  1895. * there is room left in our allocation tree for another
  1896. * extent record.
  1897. *
  1898. * XXX: This test is simplistic, we can search for empty
  1899. * extent records too.
  1900. */
  1901. insert->ins_free_records = le16_to_cpu(el->l_count) -
  1902. le16_to_cpu(el->l_next_free_rec);
  1903. if (!insert->ins_tree_depth) {
  1904. ocfs2_figure_contig_type(inode, insert, el, insert_rec);
  1905. ocfs2_figure_appending_type(insert, el, insert_rec);
  1906. return 0;
  1907. }
  1908. path = ocfs2_new_inode_path(di_bh);
  1909. if (!path) {
  1910. ret = -ENOMEM;
  1911. mlog_errno(ret);
  1912. goto out;
  1913. }
  1914. /*
  1915. * In the case that we're inserting past what the tree
  1916. * currently accounts for, ocfs2_find_path() will return for
  1917. * us the rightmost tree path. This is accounted for below in
  1918. * the appending code.
  1919. */
  1920. ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
  1921. if (ret) {
  1922. mlog_errno(ret);
  1923. goto out;
  1924. }
  1925. el = path_leaf_el(path);
  1926. /*
  1927. * Now that we have the path, there's two things we want to determine:
  1928. * 1) Contiguousness (also set contig_index if this is so)
  1929. *
  1930. * 2) Are we doing an append? We can trivially break this up
  1931. * into two types of appends: simple record append, or a
  1932. * rotate inside the tail leaf.
  1933. */
  1934. ocfs2_figure_contig_type(inode, insert, el, insert_rec);
  1935. /*
  1936. * The insert code isn't quite ready to deal with all cases of
  1937. * left contiguousness. Specifically, if it's an insert into
  1938. * the 1st record in a leaf, it will require the adjustment of
  1939. * e_clusters on the last record of the path directly to it's
  1940. * left. For now, just catch that case and fool the layers
  1941. * above us. This works just fine for tree_depth == 0, which
  1942. * is why we allow that above.
  1943. */
  1944. if (insert->ins_contig == CONTIG_LEFT &&
  1945. insert->ins_contig_index == 0)
  1946. insert->ins_contig = CONTIG_NONE;
  1947. /*
  1948. * Ok, so we can simply compare against last_eb to figure out
  1949. * whether the path doesn't exist. This will only happen in
  1950. * the case that we're doing a tail append, so maybe we can
  1951. * take advantage of that information somehow.
  1952. */
  1953. if (le64_to_cpu(di->i_last_eb_blk) == path_leaf_bh(path)->b_blocknr) {
  1954. /*
  1955. * Ok, ocfs2_find_path() returned us the rightmost
  1956. * tree path. This might be an appending insert. There are
  1957. * two cases:
  1958. * 1) We're doing a true append at the tail:
  1959. * -This might even be off the end of the leaf
  1960. * 2) We're "appending" by rotating in the tail
  1961. */
  1962. ocfs2_figure_appending_type(insert, el, insert_rec);
  1963. }
  1964. out:
  1965. ocfs2_free_path(path);
  1966. if (ret == 0)
  1967. *last_eb_bh = bh;
  1968. else
  1969. brelse(bh);
  1970. return ret;
  1971. }
  1972. /*
  1973. * Insert an extent into an inode btree.
  1974. *
  1975. * The caller needs to update fe->i_clusters
  1976. */
  1977. int ocfs2_insert_extent(struct ocfs2_super *osb,
  1978. handle_t *handle,
  1979. struct inode *inode,
  1980. struct buffer_head *fe_bh,
  1981. u32 cpos,
  1982. u64 start_blk,
  1983. u32 new_clusters,
  1984. struct ocfs2_alloc_context *meta_ac)
  1985. {
  1986. int status, shift;
  1987. struct buffer_head *last_eb_bh = NULL;
  1988. struct buffer_head *bh = NULL;
  1989. struct ocfs2_insert_type insert = {0, };
  1990. struct ocfs2_extent_rec rec;
  1991. mlog(0, "add %u clusters at position %u to inode %llu\n",
  1992. new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1993. mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
  1994. (OCFS2_I(inode)->ip_clusters != cpos),
  1995. "Device %s, asking for sparse allocation: inode %llu, "
  1996. "cpos %u, clusters %u\n",
  1997. osb->dev_str,
  1998. (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos,
  1999. OCFS2_I(inode)->ip_clusters);
  2000. rec.e_cpos = cpu_to_le32(cpos);
  2001. rec.e_blkno = cpu_to_le64(start_blk);
  2002. rec.e_clusters = cpu_to_le32(new_clusters);
  2003. status = ocfs2_figure_insert_type(inode, fe_bh, &last_eb_bh, &rec,
  2004. &insert);
  2005. if (status < 0) {
  2006. mlog_errno(status);
  2007. goto bail;
  2008. }
  2009. mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
  2010. "Insert.contig_index: %d, Insert.free_records: %d, "
  2011. "Insert.tree_depth: %d\n",
  2012. insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
  2013. insert.ins_free_records, insert.ins_tree_depth);
  2014. /*
  2015. * Avoid growing the tree unless we're out of records and the
  2016. * insert type requres one.
  2017. */
  2018. if (insert.ins_contig != CONTIG_NONE || insert.ins_free_records)
  2019. goto out_add;
  2020. shift = ocfs2_find_branch_target(osb, inode, fe_bh, &bh);
  2021. if (shift < 0) {
  2022. status = shift;
  2023. mlog_errno(status);
  2024. goto bail;
  2025. }
  2026. /* We traveled all the way to the bottom of the allocation tree
  2027. * and didn't find room for any more extents - we need to add
  2028. * another tree level */
  2029. if (shift) {
  2030. BUG_ON(bh);
  2031. mlog(0, "need to shift tree depth "
  2032. "(current = %d)\n", insert.ins_tree_depth);
  2033. /* ocfs2_shift_tree_depth will return us a buffer with
  2034. * the new extent block (so we can pass that to
  2035. * ocfs2_add_branch). */
  2036. status = ocfs2_shift_tree_depth(osb, handle, inode, fe_bh,
  2037. meta_ac, &bh);
  2038. if (status < 0) {
  2039. mlog_errno(status);
  2040. goto bail;
  2041. }
  2042. insert.ins_tree_depth++;
  2043. /* Special case: we have room now if we shifted from
  2044. * tree_depth 0 */
  2045. if (insert.ins_tree_depth == 1)
  2046. goto out_add;
  2047. }
  2048. /* call ocfs2_add_branch to add the final part of the tree with
  2049. * the new data. */
  2050. mlog(0, "add branch. bh = %p\n", bh);
  2051. status = ocfs2_add_branch(osb, handle, inode, fe_bh, bh, last_eb_bh,
  2052. meta_ac);
  2053. if (status < 0) {
  2054. mlog_errno(status);
  2055. goto bail;
  2056. }
  2057. out_add:
  2058. /* Finally, we can add clusters. This might rotate the tree for us. */
  2059. status = ocfs2_do_insert_extent(inode, handle, fe_bh, &rec, &insert);
  2060. if (status < 0)
  2061. mlog_errno(status);
  2062. bail:
  2063. if (bh)
  2064. brelse(bh);
  2065. if (last_eb_bh)
  2066. brelse(last_eb_bh);
  2067. mlog_exit(status);
  2068. return status;
  2069. }
  2070. static inline int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
  2071. {
  2072. struct buffer_head *tl_bh = osb->osb_tl_bh;
  2073. struct ocfs2_dinode *di;
  2074. struct ocfs2_truncate_log *tl;
  2075. di = (struct ocfs2_dinode *) tl_bh->b_data;
  2076. tl = &di->id2.i_dealloc;
  2077. mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
  2078. "slot %d, invalid truncate log parameters: used = "
  2079. "%u, count = %u\n", osb->slot_num,
  2080. le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
  2081. return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
  2082. }
  2083. static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
  2084. unsigned int new_start)
  2085. {
  2086. unsigned int tail_index;
  2087. unsigned int current_tail;
  2088. /* No records, nothing to coalesce */
  2089. if (!le16_to_cpu(tl->tl_used))
  2090. return 0;
  2091. tail_index = le16_to_cpu(tl->tl_used) - 1;
  2092. current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
  2093. current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
  2094. return current_tail == new_start;
  2095. }
  2096. static int ocfs2_truncate_log_append(struct ocfs2_super *osb,
  2097. handle_t *handle,
  2098. u64 start_blk,
  2099. unsigned int num_clusters)
  2100. {
  2101. int status, index;
  2102. unsigned int start_cluster, tl_count;
  2103. struct inode *tl_inode = osb->osb_tl_inode;
  2104. struct buffer_head *tl_bh = osb->osb_tl_bh;
  2105. struct ocfs2_dinode *di;
  2106. struct ocfs2_truncate_log *tl;
  2107. mlog_entry("start_blk = %llu, num_clusters = %u\n",
  2108. (unsigned long long)start_blk, num_clusters);
  2109. BUG_ON(mutex_trylock(&tl_inode->i_mutex));
  2110. start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
  2111. di = (struct ocfs2_dinode *) tl_bh->b_data;
  2112. tl = &di->id2.i_dealloc;
  2113. if (!OCFS2_IS_VALID_DINODE(di)) {
  2114. OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
  2115. status = -EIO;
  2116. goto bail;
  2117. }
  2118. tl_count = le16_to_cpu(tl->tl_count);
  2119. mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
  2120. tl_count == 0,
  2121. "Truncate record count on #%llu invalid "
  2122. "wanted %u, actual %u\n",
  2123. (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
  2124. ocfs2_truncate_recs_per_inode(osb->sb),
  2125. le16_to_cpu(tl->tl_count));
  2126. /* Caller should have known to flush before calling us. */
  2127. index = le16_to_cpu(tl->tl_used);
  2128. if (index >= tl_count) {
  2129. status = -ENOSPC;
  2130. mlog_errno(status);
  2131. goto bail;
  2132. }
  2133. status = ocfs2_journal_access(handle, tl_inode, tl_bh,
  2134. OCFS2_JOURNAL_ACCESS_WRITE);
  2135. if (status < 0) {
  2136. mlog_errno(status);
  2137. goto bail;
  2138. }
  2139. mlog(0, "Log truncate of %u clusters starting at cluster %u to "
  2140. "%llu (index = %d)\n", num_clusters, start_cluster,
  2141. (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
  2142. if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
  2143. /*
  2144. * Move index back to the record we are coalescing with.
  2145. * ocfs2_truncate_log_can_coalesce() guarantees nonzero
  2146. */
  2147. index--;
  2148. num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
  2149. mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
  2150. index, le32_to_cpu(tl->tl_recs[index].t_start),
  2151. num_clusters);
  2152. } else {
  2153. tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
  2154. tl->tl_used = cpu_to_le16(index + 1);
  2155. }
  2156. tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
  2157. status = ocfs2_journal_dirty(handle, tl_bh);
  2158. if (status < 0) {
  2159. mlog_errno(status);
  2160. goto bail;
  2161. }
  2162. bail:
  2163. mlog_exit(status);
  2164. return status;
  2165. }
  2166. static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
  2167. handle_t *handle,
  2168. struct inode *data_alloc_inode,
  2169. struct buffer_head *data_alloc_bh)
  2170. {
  2171. int status = 0;
  2172. int i;
  2173. unsigned int num_clusters;
  2174. u64 start_blk;
  2175. struct ocfs2_truncate_rec rec;
  2176. struct ocfs2_dinode *di;
  2177. struct ocfs2_truncate_log *tl;
  2178. struct inode *tl_inode = osb->osb_tl_inode;
  2179. struct buffer_head *tl_bh = osb->osb_tl_bh;
  2180. mlog_entry_void();
  2181. di = (struct ocfs2_dinode *) tl_bh->b_data;
  2182. tl = &di->id2.i_dealloc;
  2183. i = le16_to_cpu(tl->tl_used) - 1;
  2184. while (i >= 0) {
  2185. /* Caller has given us at least enough credits to
  2186. * update the truncate log dinode */
  2187. status = ocfs2_journal_access(handle, tl_inode, tl_bh,
  2188. OCFS2_JOURNAL_ACCESS_WRITE);
  2189. if (status < 0) {
  2190. mlog_errno(status);
  2191. goto bail;
  2192. }
  2193. tl->tl_used = cpu_to_le16(i);
  2194. status = ocfs2_journal_dirty(handle, tl_bh);
  2195. if (status < 0) {
  2196. mlog_errno(status);
  2197. goto bail;
  2198. }
  2199. /* TODO: Perhaps we can calculate the bulk of the
  2200. * credits up front rather than extending like
  2201. * this. */
  2202. status = ocfs2_extend_trans(handle,
  2203. OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
  2204. if (status < 0) {
  2205. mlog_errno(status);
  2206. goto bail;
  2207. }
  2208. rec = tl->tl_recs[i];
  2209. start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
  2210. le32_to_cpu(rec.t_start));
  2211. num_clusters = le32_to_cpu(rec.t_clusters);
  2212. /* if start_blk is not set, we ignore the record as
  2213. * invalid. */
  2214. if (start_blk) {
  2215. mlog(0, "free record %d, start = %u, clusters = %u\n",
  2216. i, le32_to_cpu(rec.t_start), num_clusters);
  2217. status = ocfs2_free_clusters(handle, data_alloc_inode,
  2218. data_alloc_bh, start_blk,
  2219. num_clusters);
  2220. if (status < 0) {
  2221. mlog_errno(status);
  2222. goto bail;
  2223. }
  2224. }
  2225. i--;
  2226. }
  2227. bail:
  2228. mlog_exit(status);
  2229. return status;
  2230. }
  2231. /* Expects you to already be holding tl_inode->i_mutex */
  2232. static int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
  2233. {
  2234. int status;
  2235. unsigned int num_to_flush;
  2236. handle_t *handle;
  2237. struct inode *tl_inode = osb->osb_tl_inode;
  2238. struct inode *data_alloc_inode = NULL;
  2239. struct buffer_head *tl_bh = osb->osb_tl_bh;
  2240. struct buffer_head *data_alloc_bh = NULL;
  2241. struct ocfs2_dinode *di;
  2242. struct ocfs2_truncate_log *tl;
  2243. mlog_entry_void();
  2244. BUG_ON(mutex_trylock(&tl_inode->i_mutex));
  2245. di = (struct ocfs2_dinode *) tl_bh->b_data;
  2246. tl = &di->id2.i_dealloc;
  2247. if (!OCFS2_IS_VALID_DINODE(di)) {
  2248. OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
  2249. status = -EIO;
  2250. goto out;
  2251. }
  2252. num_to_flush = le16_to_cpu(tl->tl_used);
  2253. mlog(0, "Flush %u records from truncate log #%llu\n",
  2254. num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
  2255. if (!num_to_flush) {
  2256. status = 0;
  2257. goto out;
  2258. }
  2259. data_alloc_inode = ocfs2_get_system_file_inode(osb,
  2260. GLOBAL_BITMAP_SYSTEM_INODE,
  2261. OCFS2_INVALID_SLOT);
  2262. if (!data_alloc_inode) {
  2263. status = -EINVAL;
  2264. mlog(ML_ERROR, "Could not get bitmap inode!\n");
  2265. goto out;
  2266. }
  2267. mutex_lock(&data_alloc_inode->i_mutex);
  2268. status = ocfs2_meta_lock(data_alloc_inode, &data_alloc_bh, 1);
  2269. if (status < 0) {
  2270. mlog_errno(status);
  2271. goto out_mutex;
  2272. }
  2273. handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
  2274. if (IS_ERR(handle)) {
  2275. status = PTR_ERR(handle);
  2276. mlog_errno(status);
  2277. goto out_unlock;
  2278. }
  2279. status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
  2280. data_alloc_bh);
  2281. if (status < 0)
  2282. mlog_errno(status);
  2283. ocfs2_commit_trans(osb, handle);
  2284. out_unlock:
  2285. brelse(data_alloc_bh);
  2286. ocfs2_meta_unlock(data_alloc_inode, 1);
  2287. out_mutex:
  2288. mutex_unlock(&data_alloc_inode->i_mutex);
  2289. iput(data_alloc_inode);
  2290. out:
  2291. mlog_exit(status);
  2292. return status;
  2293. }
  2294. int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
  2295. {
  2296. int status;
  2297. struct inode *tl_inode = osb->osb_tl_inode;
  2298. mutex_lock(&tl_inode->i_mutex);
  2299. status = __ocfs2_flush_truncate_log(osb);
  2300. mutex_unlock(&tl_inode->i_mutex);
  2301. return status;
  2302. }
  2303. static void ocfs2_truncate_log_worker(struct work_struct *work)
  2304. {
  2305. int status;
  2306. struct ocfs2_super *osb =
  2307. container_of(work, struct ocfs2_super,
  2308. osb_truncate_log_wq.work);
  2309. mlog_entry_void();
  2310. status = ocfs2_flush_truncate_log(osb);
  2311. if (status < 0)
  2312. mlog_errno(status);
  2313. mlog_exit(status);
  2314. }
  2315. #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
  2316. void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
  2317. int cancel)
  2318. {
  2319. if (osb->osb_tl_inode) {
  2320. /* We want to push off log flushes while truncates are
  2321. * still running. */
  2322. if (cancel)
  2323. cancel_delayed_work(&osb->osb_truncate_log_wq);
  2324. queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
  2325. OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
  2326. }
  2327. }
  2328. static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
  2329. int slot_num,
  2330. struct inode **tl_inode,
  2331. struct buffer_head **tl_bh)
  2332. {
  2333. int status;
  2334. struct inode *inode = NULL;
  2335. struct buffer_head *bh = NULL;
  2336. inode = ocfs2_get_system_file_inode(osb,
  2337. TRUNCATE_LOG_SYSTEM_INODE,
  2338. slot_num);
  2339. if (!inode) {
  2340. status = -EINVAL;
  2341. mlog(ML_ERROR, "Could not get load truncate log inode!\n");
  2342. goto bail;
  2343. }
  2344. status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
  2345. OCFS2_BH_CACHED, inode);
  2346. if (status < 0) {
  2347. iput(inode);
  2348. mlog_errno(status);
  2349. goto bail;
  2350. }
  2351. *tl_inode = inode;
  2352. *tl_bh = bh;
  2353. bail:
  2354. mlog_exit(status);
  2355. return status;
  2356. }
  2357. /* called during the 1st stage of node recovery. we stamp a clean
  2358. * truncate log and pass back a copy for processing later. if the
  2359. * truncate log does not require processing, a *tl_copy is set to
  2360. * NULL. */
  2361. int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
  2362. int slot_num,
  2363. struct ocfs2_dinode **tl_copy)
  2364. {
  2365. int status;
  2366. struct inode *tl_inode = NULL;
  2367. struct buffer_head *tl_bh = NULL;
  2368. struct ocfs2_dinode *di;
  2369. struct ocfs2_truncate_log *tl;
  2370. *tl_copy = NULL;
  2371. mlog(0, "recover truncate log from slot %d\n", slot_num);
  2372. status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
  2373. if (status < 0) {
  2374. mlog_errno(status);
  2375. goto bail;
  2376. }
  2377. di = (struct ocfs2_dinode *) tl_bh->b_data;
  2378. tl = &di->id2.i_dealloc;
  2379. if (!OCFS2_IS_VALID_DINODE(di)) {
  2380. OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
  2381. status = -EIO;
  2382. goto bail;
  2383. }
  2384. if (le16_to_cpu(tl->tl_used)) {
  2385. mlog(0, "We'll have %u logs to recover\n",
  2386. le16_to_cpu(tl->tl_used));
  2387. *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
  2388. if (!(*tl_copy)) {
  2389. status = -ENOMEM;
  2390. mlog_errno(status);
  2391. goto bail;
  2392. }
  2393. /* Assuming the write-out below goes well, this copy
  2394. * will be passed back to recovery for processing. */
  2395. memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
  2396. /* All we need to do to clear the truncate log is set
  2397. * tl_used. */
  2398. tl->tl_used = 0;
  2399. status = ocfs2_write_block(osb, tl_bh, tl_inode);
  2400. if (status < 0) {
  2401. mlog_errno(status);
  2402. goto bail;
  2403. }
  2404. }
  2405. bail:
  2406. if (tl_inode)
  2407. iput(tl_inode);
  2408. if (tl_bh)
  2409. brelse(tl_bh);
  2410. if (status < 0 && (*tl_copy)) {
  2411. kfree(*tl_copy);
  2412. *tl_copy = NULL;
  2413. }
  2414. mlog_exit(status);
  2415. return status;
  2416. }
  2417. int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
  2418. struct ocfs2_dinode *tl_copy)
  2419. {
  2420. int status = 0;
  2421. int i;
  2422. unsigned int clusters, num_recs, start_cluster;
  2423. u64 start_blk;
  2424. handle_t *handle;
  2425. struct inode *tl_inode = osb->osb_tl_inode;
  2426. struct ocfs2_truncate_log *tl;
  2427. mlog_entry_void();
  2428. if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
  2429. mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
  2430. return -EINVAL;
  2431. }
  2432. tl = &tl_copy->id2.i_dealloc;
  2433. num_recs = le16_to_cpu(tl->tl_used);
  2434. mlog(0, "cleanup %u records from %llu\n", num_recs,
  2435. (unsigned long long)tl_copy->i_blkno);
  2436. mutex_lock(&tl_inode->i_mutex);
  2437. for(i = 0; i < num_recs; i++) {
  2438. if (ocfs2_truncate_log_needs_flush(osb)) {
  2439. status = __ocfs2_flush_truncate_log(osb);
  2440. if (status < 0) {
  2441. mlog_errno(status);
  2442. goto bail_up;
  2443. }
  2444. }
  2445. handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
  2446. if (IS_ERR(handle)) {
  2447. status = PTR_ERR(handle);
  2448. mlog_errno(status);
  2449. goto bail_up;
  2450. }
  2451. clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
  2452. start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
  2453. start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
  2454. status = ocfs2_truncate_log_append(osb, handle,
  2455. start_blk, clusters);
  2456. ocfs2_commit_trans(osb, handle);
  2457. if (status < 0) {
  2458. mlog_errno(status);
  2459. goto bail_up;
  2460. }
  2461. }
  2462. bail_up:
  2463. mutex_unlock(&tl_inode->i_mutex);
  2464. mlog_exit(status);
  2465. return status;
  2466. }
  2467. void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
  2468. {
  2469. int status;
  2470. struct inode *tl_inode = osb->osb_tl_inode;
  2471. mlog_entry_void();
  2472. if (tl_inode) {
  2473. cancel_delayed_work(&osb->osb_truncate_log_wq);
  2474. flush_workqueue(ocfs2_wq);
  2475. status = ocfs2_flush_truncate_log(osb);
  2476. if (status < 0)
  2477. mlog_errno(status);
  2478. brelse(osb->osb_tl_bh);
  2479. iput(osb->osb_tl_inode);
  2480. }
  2481. mlog_exit_void();
  2482. }
  2483. int ocfs2_truncate_log_init(struct ocfs2_super *osb)
  2484. {
  2485. int status;
  2486. struct inode *tl_inode = NULL;
  2487. struct buffer_head *tl_bh = NULL;
  2488. mlog_entry_void();
  2489. status = ocfs2_get_truncate_log_info(osb,
  2490. osb->slot_num,
  2491. &tl_inode,
  2492. &tl_bh);
  2493. if (status < 0)
  2494. mlog_errno(status);
  2495. /* ocfs2_truncate_log_shutdown keys on the existence of
  2496. * osb->osb_tl_inode so we don't set any of the osb variables
  2497. * until we're sure all is well. */
  2498. INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
  2499. ocfs2_truncate_log_worker);
  2500. osb->osb_tl_bh = tl_bh;
  2501. osb->osb_tl_inode = tl_inode;
  2502. mlog_exit(status);
  2503. return status;
  2504. }
  2505. /* This function will figure out whether the currently last extent
  2506. * block will be deleted, and if it will, what the new last extent
  2507. * block will be so we can update his h_next_leaf_blk field, as well
  2508. * as the dinodes i_last_eb_blk */
  2509. static int ocfs2_find_new_last_ext_blk(struct inode *inode,
  2510. unsigned int clusters_to_del,
  2511. struct ocfs2_path *path,
  2512. struct buffer_head **new_last_eb)
  2513. {
  2514. int next_free, ret = 0;
  2515. u32 cpos;
  2516. struct ocfs2_extent_rec *rec;
  2517. struct ocfs2_extent_block *eb;
  2518. struct ocfs2_extent_list *el;
  2519. struct buffer_head *bh = NULL;
  2520. *new_last_eb = NULL;
  2521. /* we have no tree, so of course, no last_eb. */
  2522. if (!path->p_tree_depth)
  2523. goto out;
  2524. /* trunc to zero special case - this makes tree_depth = 0
  2525. * regardless of what it is. */
  2526. if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
  2527. goto out;
  2528. el = path_leaf_el(path);
  2529. BUG_ON(!el->l_next_free_rec);
  2530. /*
  2531. * Make sure that this extent list will actually be empty
  2532. * after we clear away the data. We can shortcut out if
  2533. * there's more than one non-empty extent in the
  2534. * list. Otherwise, a check of the remaining extent is
  2535. * necessary.
  2536. */
  2537. next_free = le16_to_cpu(el->l_next_free_rec);
  2538. rec = NULL;
  2539. if (ocfs2_is_empty_extent(&el->l_recs[0])) {
  2540. if (next_free > 2)
  2541. goto out;
  2542. /* We may have a valid extent in index 1, check it. */
  2543. if (next_free == 2)
  2544. rec = &el->l_recs[1];
  2545. /*
  2546. * Fall through - no more nonempty extents, so we want
  2547. * to delete this leaf.
  2548. */
  2549. } else {
  2550. if (next_free > 1)
  2551. goto out;
  2552. rec = &el->l_recs[0];
  2553. }
  2554. if (rec) {
  2555. /*
  2556. * Check it we'll only be trimming off the end of this
  2557. * cluster.
  2558. */
  2559. if (le16_to_cpu(rec->e_clusters) > clusters_to_del)
  2560. goto out;
  2561. }
  2562. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
  2563. if (ret) {
  2564. mlog_errno(ret);
  2565. goto out;
  2566. }
  2567. ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
  2568. if (ret) {
  2569. mlog_errno(ret);
  2570. goto out;
  2571. }
  2572. eb = (struct ocfs2_extent_block *) bh->b_data;
  2573. el = &eb->h_list;
  2574. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  2575. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  2576. ret = -EROFS;
  2577. goto out;
  2578. }
  2579. *new_last_eb = bh;
  2580. get_bh(*new_last_eb);
  2581. mlog(0, "returning block %llu, (cpos: %u)\n",
  2582. (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
  2583. out:
  2584. brelse(bh);
  2585. return ret;
  2586. }
  2587. /*
  2588. * Trim some clusters off the rightmost edge of a tree. Only called
  2589. * during truncate.
  2590. *
  2591. * The caller needs to:
  2592. * - start journaling of each path component.
  2593. * - compute and fully set up any new last ext block
  2594. */
  2595. static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
  2596. handle_t *handle, struct ocfs2_truncate_context *tc,
  2597. u32 clusters_to_del, u64 *delete_start)
  2598. {
  2599. int ret, i, index = path->p_tree_depth;
  2600. u32 new_edge = 0;
  2601. u64 deleted_eb = 0;
  2602. struct buffer_head *bh;
  2603. struct ocfs2_extent_list *el;
  2604. struct ocfs2_extent_rec *rec;
  2605. *delete_start = 0;
  2606. while (index >= 0) {
  2607. bh = path->p_node[index].bh;
  2608. el = path->p_node[index].el;
  2609. mlog(0, "traveling tree (index = %d, block = %llu)\n",
  2610. index, (unsigned long long)bh->b_blocknr);
  2611. BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
  2612. if (index !=
  2613. (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
  2614. ocfs2_error(inode->i_sb,
  2615. "Inode %lu has invalid ext. block %llu",
  2616. inode->i_ino,
  2617. (unsigned long long)bh->b_blocknr);
  2618. ret = -EROFS;
  2619. goto out;
  2620. }
  2621. find_tail_record:
  2622. i = le16_to_cpu(el->l_next_free_rec) - 1;
  2623. rec = &el->l_recs[i];
  2624. mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
  2625. "next = %u\n", i, le32_to_cpu(rec->e_cpos),
  2626. le32_to_cpu(rec->e_clusters),
  2627. (unsigned long long)le64_to_cpu(rec->e_blkno),
  2628. le16_to_cpu(el->l_next_free_rec));
  2629. BUG_ON(le32_to_cpu(rec->e_clusters) < clusters_to_del);
  2630. if (le16_to_cpu(el->l_tree_depth) == 0) {
  2631. /*
  2632. * If the leaf block contains a single empty
  2633. * extent and no records, we can just remove
  2634. * the block.
  2635. */
  2636. if (i == 0 && ocfs2_is_empty_extent(rec)) {
  2637. memset(rec, 0,
  2638. sizeof(struct ocfs2_extent_rec));
  2639. el->l_next_free_rec = cpu_to_le16(0);
  2640. goto delete;
  2641. }
  2642. /*
  2643. * Remove any empty extents by shifting things
  2644. * left. That should make life much easier on
  2645. * the code below. This condition is rare
  2646. * enough that we shouldn't see a performance
  2647. * hit.
  2648. */
  2649. if (ocfs2_is_empty_extent(&el->l_recs[0])) {
  2650. le16_add_cpu(&el->l_next_free_rec, -1);
  2651. for(i = 0;
  2652. i < le16_to_cpu(el->l_next_free_rec); i++)
  2653. el->l_recs[i] = el->l_recs[i + 1];
  2654. memset(&el->l_recs[i], 0,
  2655. sizeof(struct ocfs2_extent_rec));
  2656. /*
  2657. * We've modified our extent list. The
  2658. * simplest way to handle this change
  2659. * is to being the search from the
  2660. * start again.
  2661. */
  2662. goto find_tail_record;
  2663. }
  2664. le32_add_cpu(&rec->e_clusters, -clusters_to_del);
  2665. /*
  2666. * We'll use "new_edge" on our way back up the
  2667. * tree to know what our rightmost cpos is.
  2668. */
  2669. new_edge = le32_to_cpu(rec->e_clusters);
  2670. new_edge += le32_to_cpu(rec->e_cpos);
  2671. /*
  2672. * The caller will use this to delete data blocks.
  2673. */
  2674. *delete_start = le64_to_cpu(rec->e_blkno)
  2675. + ocfs2_clusters_to_blocks(inode->i_sb,
  2676. le32_to_cpu(rec->e_clusters));
  2677. /*
  2678. * If it's now empty, remove this record.
  2679. */
  2680. if (le32_to_cpu(rec->e_clusters) == 0) {
  2681. memset(rec, 0,
  2682. sizeof(struct ocfs2_extent_rec));
  2683. le16_add_cpu(&el->l_next_free_rec, -1);
  2684. }
  2685. } else {
  2686. if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
  2687. memset(rec, 0,
  2688. sizeof(struct ocfs2_extent_rec));
  2689. le16_add_cpu(&el->l_next_free_rec, -1);
  2690. goto delete;
  2691. }
  2692. /* Can this actually happen? */
  2693. if (le16_to_cpu(el->l_next_free_rec) == 0)
  2694. goto delete;
  2695. /*
  2696. * We never actually deleted any clusters
  2697. * because our leaf was empty. There's no
  2698. * reason to adjust the rightmost edge then.
  2699. */
  2700. if (new_edge == 0)
  2701. goto delete;
  2702. rec->e_clusters = cpu_to_le32(new_edge);
  2703. le32_add_cpu(&rec->e_clusters,
  2704. -le32_to_cpu(rec->e_cpos));
  2705. /*
  2706. * A deleted child record should have been
  2707. * caught above.
  2708. */
  2709. BUG_ON(le32_to_cpu(rec->e_clusters) == 0);
  2710. }
  2711. delete:
  2712. ret = ocfs2_journal_dirty(handle, bh);
  2713. if (ret) {
  2714. mlog_errno(ret);
  2715. goto out;
  2716. }
  2717. mlog(0, "extent list container %llu, after: record %d: "
  2718. "(%u, %u, %llu), next = %u.\n",
  2719. (unsigned long long)bh->b_blocknr, i,
  2720. le32_to_cpu(rec->e_cpos), le32_to_cpu(rec->e_clusters),
  2721. (unsigned long long)le64_to_cpu(rec->e_blkno),
  2722. le16_to_cpu(el->l_next_free_rec));
  2723. /*
  2724. * We must be careful to only attempt delete of an
  2725. * extent block (and not the root inode block).
  2726. */
  2727. if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
  2728. struct ocfs2_extent_block *eb =
  2729. (struct ocfs2_extent_block *)bh->b_data;
  2730. /*
  2731. * Save this for use when processing the
  2732. * parent block.
  2733. */
  2734. deleted_eb = le64_to_cpu(eb->h_blkno);
  2735. mlog(0, "deleting this extent block.\n");
  2736. ocfs2_remove_from_cache(inode, bh);
  2737. BUG_ON(le32_to_cpu(el->l_recs[0].e_clusters));
  2738. BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
  2739. BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
  2740. if (le16_to_cpu(eb->h_suballoc_slot) == 0) {
  2741. /*
  2742. * This code only understands how to
  2743. * lock the suballocator in slot 0,
  2744. * which is fine because allocation is
  2745. * only ever done out of that
  2746. * suballocator too. A future version
  2747. * might change that however, so avoid
  2748. * a free if we don't know how to
  2749. * handle it. This way an fs incompat
  2750. * bit will not be necessary.
  2751. */
  2752. ret = ocfs2_free_extent_block(handle,
  2753. tc->tc_ext_alloc_inode,
  2754. tc->tc_ext_alloc_bh,
  2755. eb);
  2756. /* An error here is not fatal. */
  2757. if (ret < 0)
  2758. mlog_errno(ret);
  2759. }
  2760. } else {
  2761. deleted_eb = 0;
  2762. }
  2763. index--;
  2764. }
  2765. ret = 0;
  2766. out:
  2767. return ret;
  2768. }
  2769. static int ocfs2_do_truncate(struct ocfs2_super *osb,
  2770. unsigned int clusters_to_del,
  2771. struct inode *inode,
  2772. struct buffer_head *fe_bh,
  2773. handle_t *handle,
  2774. struct ocfs2_truncate_context *tc,
  2775. struct ocfs2_path *path)
  2776. {
  2777. int status;
  2778. struct ocfs2_dinode *fe;
  2779. struct ocfs2_extent_block *last_eb = NULL;
  2780. struct ocfs2_extent_list *el;
  2781. struct buffer_head *last_eb_bh = NULL;
  2782. u64 delete_blk = 0;
  2783. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  2784. status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
  2785. path, &last_eb_bh);
  2786. if (status < 0) {
  2787. mlog_errno(status);
  2788. goto bail;
  2789. }
  2790. /*
  2791. * Each component will be touched, so we might as well journal
  2792. * here to avoid having to handle errors later.
  2793. */
  2794. status = ocfs2_journal_access_path(inode, handle, path);
  2795. if (status < 0) {
  2796. mlog_errno(status);
  2797. goto bail;
  2798. }
  2799. if (last_eb_bh) {
  2800. status = ocfs2_journal_access(handle, inode, last_eb_bh,
  2801. OCFS2_JOURNAL_ACCESS_WRITE);
  2802. if (status < 0) {
  2803. mlog_errno(status);
  2804. goto bail;
  2805. }
  2806. last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
  2807. }
  2808. el = &(fe->id2.i_list);
  2809. /*
  2810. * Lower levels depend on this never happening, but it's best
  2811. * to check it up here before changing the tree.
  2812. */
  2813. if (el->l_tree_depth && ocfs2_is_empty_extent(&el->l_recs[0])) {
  2814. ocfs2_error(inode->i_sb,
  2815. "Inode %lu has an empty extent record, depth %u\n",
  2816. inode->i_ino, le16_to_cpu(el->l_tree_depth));
  2817. status = -EROFS;
  2818. goto bail;
  2819. }
  2820. spin_lock(&OCFS2_I(inode)->ip_lock);
  2821. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
  2822. clusters_to_del;
  2823. spin_unlock(&OCFS2_I(inode)->ip_lock);
  2824. le32_add_cpu(&fe->i_clusters, -clusters_to_del);
  2825. status = ocfs2_trim_tree(inode, path, handle, tc,
  2826. clusters_to_del, &delete_blk);
  2827. if (status) {
  2828. mlog_errno(status);
  2829. goto bail;
  2830. }
  2831. if (le32_to_cpu(fe->i_clusters) == 0) {
  2832. /* trunc to zero is a special case. */
  2833. el->l_tree_depth = 0;
  2834. fe->i_last_eb_blk = 0;
  2835. } else if (last_eb)
  2836. fe->i_last_eb_blk = last_eb->h_blkno;
  2837. status = ocfs2_journal_dirty(handle, fe_bh);
  2838. if (status < 0) {
  2839. mlog_errno(status);
  2840. goto bail;
  2841. }
  2842. if (last_eb) {
  2843. /* If there will be a new last extent block, then by
  2844. * definition, there cannot be any leaves to the right of
  2845. * him. */
  2846. last_eb->h_next_leaf_blk = 0;
  2847. status = ocfs2_journal_dirty(handle, last_eb_bh);
  2848. if (status < 0) {
  2849. mlog_errno(status);
  2850. goto bail;
  2851. }
  2852. }
  2853. if (delete_blk) {
  2854. status = ocfs2_truncate_log_append(osb, handle, delete_blk,
  2855. clusters_to_del);
  2856. if (status < 0) {
  2857. mlog_errno(status);
  2858. goto bail;
  2859. }
  2860. }
  2861. status = 0;
  2862. bail:
  2863. mlog_exit(status);
  2864. return status;
  2865. }
  2866. static int ocfs2_writeback_zero_func(handle_t *handle, struct buffer_head *bh)
  2867. {
  2868. set_buffer_uptodate(bh);
  2869. mark_buffer_dirty(bh);
  2870. return 0;
  2871. }
  2872. static int ocfs2_ordered_zero_func(handle_t *handle, struct buffer_head *bh)
  2873. {
  2874. set_buffer_uptodate(bh);
  2875. mark_buffer_dirty(bh);
  2876. return ocfs2_journal_dirty_data(handle, bh);
  2877. }
  2878. static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t isize,
  2879. struct page **pages, int numpages,
  2880. u64 phys, handle_t *handle)
  2881. {
  2882. int i, ret, partial = 0;
  2883. void *kaddr;
  2884. struct page *page;
  2885. unsigned int from, to = PAGE_CACHE_SIZE;
  2886. struct super_block *sb = inode->i_sb;
  2887. BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
  2888. if (numpages == 0)
  2889. goto out;
  2890. from = isize & (PAGE_CACHE_SIZE - 1); /* 1st page offset */
  2891. if (PAGE_CACHE_SHIFT > OCFS2_SB(sb)->s_clustersize_bits) {
  2892. /*
  2893. * Since 'from' has been capped to a value below page
  2894. * size, this calculation won't be able to overflow
  2895. * 'to'
  2896. */
  2897. to = ocfs2_align_bytes_to_clusters(sb, from);
  2898. /*
  2899. * The truncate tail in this case should never contain
  2900. * more than one page at maximum. The loop below also
  2901. * assumes this.
  2902. */
  2903. BUG_ON(numpages != 1);
  2904. }
  2905. for(i = 0; i < numpages; i++) {
  2906. page = pages[i];
  2907. BUG_ON(from > PAGE_CACHE_SIZE);
  2908. BUG_ON(to > PAGE_CACHE_SIZE);
  2909. ret = ocfs2_map_page_blocks(page, &phys, inode, from, to, 0);
  2910. if (ret)
  2911. mlog_errno(ret);
  2912. kaddr = kmap_atomic(page, KM_USER0);
  2913. memset(kaddr + from, 0, to - from);
  2914. kunmap_atomic(kaddr, KM_USER0);
  2915. /*
  2916. * Need to set the buffers we zero'd into uptodate
  2917. * here if they aren't - ocfs2_map_page_blocks()
  2918. * might've skipped some
  2919. */
  2920. if (ocfs2_should_order_data(inode)) {
  2921. ret = walk_page_buffers(handle,
  2922. page_buffers(page),
  2923. from, to, &partial,
  2924. ocfs2_ordered_zero_func);
  2925. if (ret < 0)
  2926. mlog_errno(ret);
  2927. } else {
  2928. ret = walk_page_buffers(handle, page_buffers(page),
  2929. from, to, &partial,
  2930. ocfs2_writeback_zero_func);
  2931. if (ret < 0)
  2932. mlog_errno(ret);
  2933. }
  2934. if (!partial)
  2935. SetPageUptodate(page);
  2936. flush_dcache_page(page);
  2937. /*
  2938. * Every page after the 1st one should be completely zero'd.
  2939. */
  2940. from = 0;
  2941. }
  2942. out:
  2943. if (pages) {
  2944. for (i = 0; i < numpages; i++) {
  2945. page = pages[i];
  2946. unlock_page(page);
  2947. mark_page_accessed(page);
  2948. page_cache_release(page);
  2949. }
  2950. }
  2951. }
  2952. static int ocfs2_grab_eof_pages(struct inode *inode, loff_t isize, struct page **pages,
  2953. int *num, u64 *phys)
  2954. {
  2955. int i, numpages = 0, ret = 0;
  2956. unsigned int csize = OCFS2_SB(inode->i_sb)->s_clustersize;
  2957. struct super_block *sb = inode->i_sb;
  2958. struct address_space *mapping = inode->i_mapping;
  2959. unsigned long index;
  2960. u64 next_cluster_bytes;
  2961. BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
  2962. /* Cluster boundary, so we don't need to grab any pages. */
  2963. if ((isize & (csize - 1)) == 0)
  2964. goto out;
  2965. ret = ocfs2_extent_map_get_blocks(inode, isize >> sb->s_blocksize_bits,
  2966. phys, NULL);
  2967. if (ret) {
  2968. mlog_errno(ret);
  2969. goto out;
  2970. }
  2971. /* Tail is a hole. */
  2972. if (*phys == 0)
  2973. goto out;
  2974. next_cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, isize);
  2975. index = isize >> PAGE_CACHE_SHIFT;
  2976. do {
  2977. pages[numpages] = grab_cache_page(mapping, index);
  2978. if (!pages[numpages]) {
  2979. ret = -ENOMEM;
  2980. mlog_errno(ret);
  2981. goto out;
  2982. }
  2983. numpages++;
  2984. index++;
  2985. } while (index < (next_cluster_bytes >> PAGE_CACHE_SHIFT));
  2986. out:
  2987. if (ret != 0) {
  2988. if (pages) {
  2989. for (i = 0; i < numpages; i++) {
  2990. if (pages[i]) {
  2991. unlock_page(pages[i]);
  2992. page_cache_release(pages[i]);
  2993. }
  2994. }
  2995. }
  2996. numpages = 0;
  2997. }
  2998. *num = numpages;
  2999. return ret;
  3000. }
  3001. /*
  3002. * Zero the area past i_size but still within an allocated
  3003. * cluster. This avoids exposing nonzero data on subsequent file
  3004. * extends.
  3005. *
  3006. * We need to call this before i_size is updated on the inode because
  3007. * otherwise block_write_full_page() will skip writeout of pages past
  3008. * i_size. The new_i_size parameter is passed for this reason.
  3009. */
  3010. int ocfs2_zero_tail_for_truncate(struct inode *inode, handle_t *handle,
  3011. u64 new_i_size)
  3012. {
  3013. int ret, numpages;
  3014. struct page **pages = NULL;
  3015. u64 phys;
  3016. /*
  3017. * File systems which don't support sparse files zero on every
  3018. * extend.
  3019. */
  3020. if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  3021. return 0;
  3022. pages = kcalloc(ocfs2_pages_per_cluster(inode->i_sb),
  3023. sizeof(struct page *), GFP_NOFS);
  3024. if (pages == NULL) {
  3025. ret = -ENOMEM;
  3026. mlog_errno(ret);
  3027. goto out;
  3028. }
  3029. ret = ocfs2_grab_eof_pages(inode, new_i_size, pages, &numpages, &phys);
  3030. if (ret) {
  3031. mlog_errno(ret);
  3032. goto out;
  3033. }
  3034. /*
  3035. * Truncate on an i_size boundary - nothing more to do.
  3036. */
  3037. if (numpages == 0)
  3038. goto out;
  3039. ocfs2_zero_cluster_pages(inode, new_i_size, pages, numpages, phys,
  3040. handle);
  3041. /*
  3042. * Initiate writeout of the pages we zero'd here. We don't
  3043. * wait on them - the truncate_inode_pages() call later will
  3044. * do that for us.
  3045. */
  3046. ret = filemap_fdatawrite(inode->i_mapping);
  3047. if (ret)
  3048. mlog_errno(ret);
  3049. out:
  3050. if (pages)
  3051. kfree(pages);
  3052. return ret;
  3053. }
  3054. /*
  3055. * It is expected, that by the time you call this function,
  3056. * inode->i_size and fe->i_size have been adjusted.
  3057. *
  3058. * WARNING: This will kfree the truncate context
  3059. */
  3060. int ocfs2_commit_truncate(struct ocfs2_super *osb,
  3061. struct inode *inode,
  3062. struct buffer_head *fe_bh,
  3063. struct ocfs2_truncate_context *tc)
  3064. {
  3065. int status, i, credits, tl_sem = 0;
  3066. u32 clusters_to_del, new_highest_cpos, range;
  3067. struct ocfs2_extent_list *el;
  3068. handle_t *handle = NULL;
  3069. struct inode *tl_inode = osb->osb_tl_inode;
  3070. struct ocfs2_path *path = NULL;
  3071. mlog_entry_void();
  3072. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  3073. new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
  3074. i_size_read(inode));
  3075. path = ocfs2_new_inode_path(fe_bh);
  3076. if (!path) {
  3077. status = -ENOMEM;
  3078. mlog_errno(status);
  3079. goto bail;
  3080. }
  3081. start:
  3082. /*
  3083. * Check that we still have allocation to delete.
  3084. */
  3085. if (OCFS2_I(inode)->ip_clusters == 0) {
  3086. status = 0;
  3087. goto bail;
  3088. }
  3089. /*
  3090. * Truncate always works against the rightmost tree branch.
  3091. */
  3092. status = ocfs2_find_path(inode, path, UINT_MAX);
  3093. if (status) {
  3094. mlog_errno(status);
  3095. goto bail;
  3096. }
  3097. mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
  3098. OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
  3099. /*
  3100. * By now, el will point to the extent list on the bottom most
  3101. * portion of this tree. Only the tail record is considered in
  3102. * each pass.
  3103. *
  3104. * We handle the following cases, in order:
  3105. * - empty extent: delete the remaining branch
  3106. * - remove the entire record
  3107. * - remove a partial record
  3108. * - no record needs to be removed (truncate has completed)
  3109. */
  3110. el = path_leaf_el(path);
  3111. if (le16_to_cpu(el->l_next_free_rec) == 0) {
  3112. ocfs2_error(inode->i_sb,
  3113. "Inode %llu has empty extent block at %llu\n",
  3114. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  3115. (unsigned long long)path_leaf_bh(path)->b_blocknr);
  3116. status = -EROFS;
  3117. goto bail;
  3118. }
  3119. i = le16_to_cpu(el->l_next_free_rec) - 1;
  3120. range = le32_to_cpu(el->l_recs[i].e_cpos) +
  3121. le32_to_cpu(el->l_recs[i].e_clusters);
  3122. if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
  3123. clusters_to_del = 0;
  3124. } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
  3125. clusters_to_del = le32_to_cpu(el->l_recs[i].e_clusters);
  3126. } else if (range > new_highest_cpos) {
  3127. clusters_to_del = (le32_to_cpu(el->l_recs[i].e_clusters) +
  3128. le32_to_cpu(el->l_recs[i].e_cpos)) -
  3129. new_highest_cpos;
  3130. } else {
  3131. status = 0;
  3132. goto bail;
  3133. }
  3134. mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
  3135. clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
  3136. BUG_ON(clusters_to_del == 0);
  3137. mutex_lock(&tl_inode->i_mutex);
  3138. tl_sem = 1;
  3139. /* ocfs2_truncate_log_needs_flush guarantees us at least one
  3140. * record is free for use. If there isn't any, we flush to get
  3141. * an empty truncate log. */
  3142. if (ocfs2_truncate_log_needs_flush(osb)) {
  3143. status = __ocfs2_flush_truncate_log(osb);
  3144. if (status < 0) {
  3145. mlog_errno(status);
  3146. goto bail;
  3147. }
  3148. }
  3149. credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
  3150. (struct ocfs2_dinode *)fe_bh->b_data,
  3151. el);
  3152. handle = ocfs2_start_trans(osb, credits);
  3153. if (IS_ERR(handle)) {
  3154. status = PTR_ERR(handle);
  3155. handle = NULL;
  3156. mlog_errno(status);
  3157. goto bail;
  3158. }
  3159. status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
  3160. tc, path);
  3161. if (status < 0) {
  3162. mlog_errno(status);
  3163. goto bail;
  3164. }
  3165. mutex_unlock(&tl_inode->i_mutex);
  3166. tl_sem = 0;
  3167. ocfs2_commit_trans(osb, handle);
  3168. handle = NULL;
  3169. ocfs2_reinit_path(path, 1);
  3170. /*
  3171. * The check above will catch the case where we've truncated
  3172. * away all allocation.
  3173. */
  3174. goto start;
  3175. bail:
  3176. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  3177. ocfs2_schedule_truncate_log_flush(osb, 1);
  3178. if (tl_sem)
  3179. mutex_unlock(&tl_inode->i_mutex);
  3180. if (handle)
  3181. ocfs2_commit_trans(osb, handle);
  3182. ocfs2_free_path(path);
  3183. /* This will drop the ext_alloc cluster lock for us */
  3184. ocfs2_free_truncate_context(tc);
  3185. mlog_exit(status);
  3186. return status;
  3187. }
  3188. /*
  3189. * Expects the inode to already be locked. This will figure out which
  3190. * inodes need to be locked and will put them on the returned truncate
  3191. * context.
  3192. */
  3193. int ocfs2_prepare_truncate(struct ocfs2_super *osb,
  3194. struct inode *inode,
  3195. struct buffer_head *fe_bh,
  3196. struct ocfs2_truncate_context **tc)
  3197. {
  3198. int status, metadata_delete, i;
  3199. unsigned int new_i_clusters;
  3200. struct ocfs2_dinode *fe;
  3201. struct ocfs2_extent_block *eb;
  3202. struct ocfs2_extent_list *el;
  3203. struct buffer_head *last_eb_bh = NULL;
  3204. struct inode *ext_alloc_inode = NULL;
  3205. struct buffer_head *ext_alloc_bh = NULL;
  3206. mlog_entry_void();
  3207. *tc = NULL;
  3208. new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
  3209. i_size_read(inode));
  3210. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  3211. mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
  3212. "%llu\n", fe->i_clusters, new_i_clusters,
  3213. (unsigned long long)fe->i_size);
  3214. *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
  3215. if (!(*tc)) {
  3216. status = -ENOMEM;
  3217. mlog_errno(status);
  3218. goto bail;
  3219. }
  3220. metadata_delete = 0;
  3221. if (fe->id2.i_list.l_tree_depth) {
  3222. /* If we have a tree, then the truncate may result in
  3223. * metadata deletes. Figure this out from the
  3224. * rightmost leaf block.*/
  3225. status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
  3226. &last_eb_bh, OCFS2_BH_CACHED, inode);
  3227. if (status < 0) {
  3228. mlog_errno(status);
  3229. goto bail;
  3230. }
  3231. eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
  3232. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  3233. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  3234. brelse(last_eb_bh);
  3235. status = -EIO;
  3236. goto bail;
  3237. }
  3238. el = &(eb->h_list);
  3239. i = 0;
  3240. if (ocfs2_is_empty_extent(&el->l_recs[0]))
  3241. i = 1;
  3242. /*
  3243. * XXX: Should we check that next_free_rec contains
  3244. * the extent?
  3245. */
  3246. if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_i_clusters)
  3247. metadata_delete = 1;
  3248. }
  3249. (*tc)->tc_last_eb_bh = last_eb_bh;
  3250. if (metadata_delete) {
  3251. mlog(0, "Will have to delete metadata for this trunc. "
  3252. "locking allocator.\n");
  3253. ext_alloc_inode = ocfs2_get_system_file_inode(osb, EXTENT_ALLOC_SYSTEM_INODE, 0);
  3254. if (!ext_alloc_inode) {
  3255. status = -ENOMEM;
  3256. mlog_errno(status);
  3257. goto bail;
  3258. }
  3259. mutex_lock(&ext_alloc_inode->i_mutex);
  3260. (*tc)->tc_ext_alloc_inode = ext_alloc_inode;
  3261. status = ocfs2_meta_lock(ext_alloc_inode, &ext_alloc_bh, 1);
  3262. if (status < 0) {
  3263. mlog_errno(status);
  3264. goto bail;
  3265. }
  3266. (*tc)->tc_ext_alloc_bh = ext_alloc_bh;
  3267. (*tc)->tc_ext_alloc_locked = 1;
  3268. }
  3269. status = 0;
  3270. bail:
  3271. if (status < 0) {
  3272. if (*tc)
  3273. ocfs2_free_truncate_context(*tc);
  3274. *tc = NULL;
  3275. }
  3276. mlog_exit_void();
  3277. return status;
  3278. }
  3279. static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
  3280. {
  3281. if (tc->tc_ext_alloc_inode) {
  3282. if (tc->tc_ext_alloc_locked)
  3283. ocfs2_meta_unlock(tc->tc_ext_alloc_inode, 1);
  3284. mutex_unlock(&tc->tc_ext_alloc_inode->i_mutex);
  3285. iput(tc->tc_ext_alloc_inode);
  3286. }
  3287. if (tc->tc_ext_alloc_bh)
  3288. brelse(tc->tc_ext_alloc_bh);
  3289. if (tc->tc_last_eb_bh)
  3290. brelse(tc->tc_last_eb_bh);
  3291. kfree(tc);
  3292. }