localalloc.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * localalloc.c
  5. *
  6. * Node local data allocation
  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/bitops.h>
  30. #include <cluster/masklog.h>
  31. #include "ocfs2.h"
  32. #include "alloc.h"
  33. #include "blockcheck.h"
  34. #include "dlmglue.h"
  35. #include "inode.h"
  36. #include "journal.h"
  37. #include "localalloc.h"
  38. #include "suballoc.h"
  39. #include "super.h"
  40. #include "sysfile.h"
  41. #include "ocfs2_trace.h"
  42. #include "buffer_head_io.h"
  43. #define OCFS2_LOCAL_ALLOC(dinode) (&((dinode)->id2.i_lab))
  44. static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc);
  45. static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
  46. struct ocfs2_dinode *alloc,
  47. u32 *numbits,
  48. struct ocfs2_alloc_reservation *resv);
  49. static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc);
  50. static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
  51. handle_t *handle,
  52. struct ocfs2_dinode *alloc,
  53. struct inode *main_bm_inode,
  54. struct buffer_head *main_bm_bh);
  55. static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
  56. struct ocfs2_alloc_context **ac,
  57. struct inode **bitmap_inode,
  58. struct buffer_head **bitmap_bh);
  59. static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
  60. handle_t *handle,
  61. struct ocfs2_alloc_context *ac);
  62. static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
  63. struct inode *local_alloc_inode);
  64. /*
  65. * ocfs2_la_default_mb() - determine a default size, in megabytes of
  66. * the local alloc.
  67. *
  68. * Generally, we'd like to pick as large a local alloc as
  69. * possible. Performance on large workloads tends to scale
  70. * proportionally to la size. In addition to that, the reservations
  71. * code functions more efficiently as it can reserve more windows for
  72. * write.
  73. *
  74. * Some things work against us when trying to choose a large local alloc:
  75. *
  76. * - We need to ensure our sizing is picked to leave enough space in
  77. * group descriptors for other allocations (such as block groups,
  78. * etc). Picking default sizes which are a multiple of 4 could help
  79. * - block groups are allocated in 2mb and 4mb chunks.
  80. *
  81. * - Likewise, we don't want to starve other nodes of bits on small
  82. * file systems. This can easily be taken care of by limiting our
  83. * default to a reasonable size (256M) on larger cluster sizes.
  84. *
  85. * - Some file systems can't support very large sizes - 4k and 8k in
  86. * particular are limited to less than 128 and 256 megabytes respectively.
  87. *
  88. * The following reference table shows group descriptor and local
  89. * alloc maximums at various cluster sizes (4k blocksize)
  90. *
  91. * csize: 4K group: 126M la: 121M
  92. * csize: 8K group: 252M la: 243M
  93. * csize: 16K group: 504M la: 486M
  94. * csize: 32K group: 1008M la: 972M
  95. * csize: 64K group: 2016M la: 1944M
  96. * csize: 128K group: 4032M la: 3888M
  97. * csize: 256K group: 8064M la: 7776M
  98. * csize: 512K group: 16128M la: 15552M
  99. * csize: 1024K group: 32256M la: 31104M
  100. */
  101. #define OCFS2_LA_MAX_DEFAULT_MB 256
  102. #define OCFS2_LA_OLD_DEFAULT 8
  103. unsigned int ocfs2_la_default_mb(struct ocfs2_super *osb)
  104. {
  105. unsigned int la_mb;
  106. unsigned int gd_mb;
  107. unsigned int la_max_mb;
  108. unsigned int megs_per_slot;
  109. struct super_block *sb = osb->sb;
  110. gd_mb = ocfs2_clusters_to_megabytes(osb->sb,
  111. 8 * ocfs2_group_bitmap_size(sb, 0, osb->s_feature_incompat));
  112. /*
  113. * This takes care of files systems with very small group
  114. * descriptors - 512 byte blocksize at cluster sizes lower
  115. * than 16K and also 1k blocksize with 4k cluster size.
  116. */
  117. if ((sb->s_blocksize == 512 && osb->s_clustersize <= 8192)
  118. || (sb->s_blocksize == 1024 && osb->s_clustersize == 4096))
  119. return OCFS2_LA_OLD_DEFAULT;
  120. /*
  121. * Leave enough room for some block groups and make the final
  122. * value we work from a multiple of 4.
  123. */
  124. gd_mb -= 16;
  125. gd_mb &= 0xFFFFFFFB;
  126. la_mb = gd_mb;
  127. /*
  128. * Keep window sizes down to a reasonable default
  129. */
  130. if (la_mb > OCFS2_LA_MAX_DEFAULT_MB) {
  131. /*
  132. * Some clustersize / blocksize combinations will have
  133. * given us a larger than OCFS2_LA_MAX_DEFAULT_MB
  134. * default size, but get poor distribution when
  135. * limited to exactly 256 megabytes.
  136. *
  137. * As an example, 16K clustersize at 4K blocksize
  138. * gives us a cluster group size of 504M. Paring the
  139. * local alloc size down to 256 however, would give us
  140. * only one window and around 200MB left in the
  141. * cluster group. Instead, find the first size below
  142. * 256 which would give us an even distribution.
  143. *
  144. * Larger cluster group sizes actually work out pretty
  145. * well when pared to 256, so we don't have to do this
  146. * for any group that fits more than two
  147. * OCFS2_LA_MAX_DEFAULT_MB windows.
  148. */
  149. if (gd_mb > (2 * OCFS2_LA_MAX_DEFAULT_MB))
  150. la_mb = 256;
  151. else {
  152. unsigned int gd_mult = gd_mb;
  153. while (gd_mult > 256)
  154. gd_mult = gd_mult >> 1;
  155. la_mb = gd_mult;
  156. }
  157. }
  158. megs_per_slot = osb->osb_clusters_at_boot / osb->max_slots;
  159. megs_per_slot = ocfs2_clusters_to_megabytes(osb->sb, megs_per_slot);
  160. /* Too many nodes, too few disk clusters. */
  161. if (megs_per_slot < la_mb)
  162. la_mb = megs_per_slot;
  163. /* We can't store more bits than we can in a block. */
  164. la_max_mb = ocfs2_clusters_to_megabytes(osb->sb,
  165. ocfs2_local_alloc_size(sb) * 8);
  166. if (la_mb > la_max_mb)
  167. la_mb = la_max_mb;
  168. return la_mb;
  169. }
  170. void ocfs2_la_set_sizes(struct ocfs2_super *osb, int requested_mb)
  171. {
  172. struct super_block *sb = osb->sb;
  173. unsigned int la_default_mb = ocfs2_la_default_mb(osb);
  174. unsigned int la_max_mb;
  175. la_max_mb = ocfs2_clusters_to_megabytes(sb,
  176. ocfs2_local_alloc_size(sb) * 8);
  177. trace_ocfs2_la_set_sizes(requested_mb, la_max_mb, la_default_mb);
  178. if (requested_mb == -1) {
  179. /* No user request - use defaults */
  180. osb->local_alloc_default_bits =
  181. ocfs2_megabytes_to_clusters(sb, la_default_mb);
  182. } else if (requested_mb > la_max_mb) {
  183. /* Request is too big, we give the maximum available */
  184. osb->local_alloc_default_bits =
  185. ocfs2_megabytes_to_clusters(sb, la_max_mb);
  186. } else {
  187. osb->local_alloc_default_bits =
  188. ocfs2_megabytes_to_clusters(sb, requested_mb);
  189. }
  190. osb->local_alloc_bits = osb->local_alloc_default_bits;
  191. }
  192. static inline int ocfs2_la_state_enabled(struct ocfs2_super *osb)
  193. {
  194. return (osb->local_alloc_state == OCFS2_LA_THROTTLED ||
  195. osb->local_alloc_state == OCFS2_LA_ENABLED);
  196. }
  197. void ocfs2_local_alloc_seen_free_bits(struct ocfs2_super *osb,
  198. unsigned int num_clusters)
  199. {
  200. spin_lock(&osb->osb_lock);
  201. if (osb->local_alloc_state == OCFS2_LA_DISABLED ||
  202. osb->local_alloc_state == OCFS2_LA_THROTTLED)
  203. if (num_clusters >= osb->local_alloc_default_bits) {
  204. cancel_delayed_work(&osb->la_enable_wq);
  205. osb->local_alloc_state = OCFS2_LA_ENABLED;
  206. }
  207. spin_unlock(&osb->osb_lock);
  208. }
  209. void ocfs2_la_enable_worker(struct work_struct *work)
  210. {
  211. struct ocfs2_super *osb =
  212. container_of(work, struct ocfs2_super,
  213. la_enable_wq.work);
  214. spin_lock(&osb->osb_lock);
  215. osb->local_alloc_state = OCFS2_LA_ENABLED;
  216. spin_unlock(&osb->osb_lock);
  217. }
  218. /*
  219. * Tell us whether a given allocation should use the local alloc
  220. * file. Otherwise, it has to go to the main bitmap.
  221. *
  222. * This function does semi-dirty reads of local alloc size and state!
  223. * This is ok however, as the values are re-checked once under mutex.
  224. */
  225. int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
  226. {
  227. int ret = 0;
  228. int la_bits;
  229. spin_lock(&osb->osb_lock);
  230. la_bits = osb->local_alloc_bits;
  231. if (!ocfs2_la_state_enabled(osb))
  232. goto bail;
  233. /* la_bits should be at least twice the size (in clusters) of
  234. * a new block group. We want to be sure block group
  235. * allocations go through the local alloc, so allow an
  236. * allocation to take up to half the bitmap. */
  237. if (bits > (la_bits / 2))
  238. goto bail;
  239. ret = 1;
  240. bail:
  241. trace_ocfs2_alloc_should_use_local(
  242. (unsigned long long)bits, osb->local_alloc_state, la_bits, ret);
  243. spin_unlock(&osb->osb_lock);
  244. return ret;
  245. }
  246. int ocfs2_load_local_alloc(struct ocfs2_super *osb)
  247. {
  248. int status = 0;
  249. struct ocfs2_dinode *alloc = NULL;
  250. struct buffer_head *alloc_bh = NULL;
  251. u32 num_used;
  252. struct inode *inode = NULL;
  253. struct ocfs2_local_alloc *la;
  254. if (osb->local_alloc_bits == 0)
  255. goto bail;
  256. if (osb->local_alloc_bits >= osb->bitmap_cpg) {
  257. mlog(ML_NOTICE, "Requested local alloc window %d is larger "
  258. "than max possible %u. Using defaults.\n",
  259. osb->local_alloc_bits, (osb->bitmap_cpg - 1));
  260. osb->local_alloc_bits =
  261. ocfs2_megabytes_to_clusters(osb->sb,
  262. ocfs2_la_default_mb(osb));
  263. }
  264. /* read the alloc off disk */
  265. inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE,
  266. osb->slot_num);
  267. if (!inode) {
  268. status = -EINVAL;
  269. mlog_errno(status);
  270. goto bail;
  271. }
  272. status = ocfs2_read_inode_block_full(inode, &alloc_bh,
  273. OCFS2_BH_IGNORE_CACHE);
  274. if (status < 0) {
  275. mlog_errno(status);
  276. goto bail;
  277. }
  278. alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
  279. la = OCFS2_LOCAL_ALLOC(alloc);
  280. if (!(le32_to_cpu(alloc->i_flags) &
  281. (OCFS2_LOCAL_ALLOC_FL|OCFS2_BITMAP_FL))) {
  282. mlog(ML_ERROR, "Invalid local alloc inode, %llu\n",
  283. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  284. status = -EINVAL;
  285. goto bail;
  286. }
  287. if ((la->la_size == 0) ||
  288. (le16_to_cpu(la->la_size) > ocfs2_local_alloc_size(inode->i_sb))) {
  289. mlog(ML_ERROR, "Local alloc size is invalid (la_size = %u)\n",
  290. le16_to_cpu(la->la_size));
  291. status = -EINVAL;
  292. goto bail;
  293. }
  294. /* do a little verification. */
  295. num_used = ocfs2_local_alloc_count_bits(alloc);
  296. /* hopefully the local alloc has always been recovered before
  297. * we load it. */
  298. if (num_used
  299. || alloc->id1.bitmap1.i_used
  300. || alloc->id1.bitmap1.i_total
  301. || la->la_bm_off)
  302. mlog(ML_ERROR, "Local alloc hasn't been recovered!\n"
  303. "found = %u, set = %u, taken = %u, off = %u\n",
  304. num_used, le32_to_cpu(alloc->id1.bitmap1.i_used),
  305. le32_to_cpu(alloc->id1.bitmap1.i_total),
  306. OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
  307. osb->local_alloc_bh = alloc_bh;
  308. osb->local_alloc_state = OCFS2_LA_ENABLED;
  309. bail:
  310. if (status < 0)
  311. brelse(alloc_bh);
  312. if (inode)
  313. iput(inode);
  314. trace_ocfs2_load_local_alloc(osb->local_alloc_bits);
  315. if (status)
  316. mlog_errno(status);
  317. return status;
  318. }
  319. /*
  320. * return any unused bits to the bitmap and write out a clean
  321. * local_alloc.
  322. *
  323. * local_alloc_bh is optional. If not passed, we will simply use the
  324. * one off osb. If you do pass it however, be warned that it *will* be
  325. * returned brelse'd and NULL'd out.*/
  326. void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
  327. {
  328. int status;
  329. handle_t *handle;
  330. struct inode *local_alloc_inode = NULL;
  331. struct buffer_head *bh = NULL;
  332. struct buffer_head *main_bm_bh = NULL;
  333. struct inode *main_bm_inode = NULL;
  334. struct ocfs2_dinode *alloc_copy = NULL;
  335. struct ocfs2_dinode *alloc = NULL;
  336. cancel_delayed_work(&osb->la_enable_wq);
  337. flush_workqueue(ocfs2_wq);
  338. if (osb->local_alloc_state == OCFS2_LA_UNUSED)
  339. goto out;
  340. local_alloc_inode =
  341. ocfs2_get_system_file_inode(osb,
  342. LOCAL_ALLOC_SYSTEM_INODE,
  343. osb->slot_num);
  344. if (!local_alloc_inode) {
  345. status = -ENOENT;
  346. mlog_errno(status);
  347. goto out;
  348. }
  349. osb->local_alloc_state = OCFS2_LA_DISABLED;
  350. ocfs2_resmap_uninit(&osb->osb_la_resmap);
  351. main_bm_inode = ocfs2_get_system_file_inode(osb,
  352. GLOBAL_BITMAP_SYSTEM_INODE,
  353. OCFS2_INVALID_SLOT);
  354. if (!main_bm_inode) {
  355. status = -EINVAL;
  356. mlog_errno(status);
  357. goto out;
  358. }
  359. mutex_lock(&main_bm_inode->i_mutex);
  360. status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
  361. if (status < 0) {
  362. mlog_errno(status);
  363. goto out_mutex;
  364. }
  365. /* WINDOW_MOVE_CREDITS is a bit heavy... */
  366. handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
  367. if (IS_ERR(handle)) {
  368. mlog_errno(PTR_ERR(handle));
  369. handle = NULL;
  370. goto out_unlock;
  371. }
  372. bh = osb->local_alloc_bh;
  373. alloc = (struct ocfs2_dinode *) bh->b_data;
  374. alloc_copy = kmalloc(bh->b_size, GFP_NOFS);
  375. if (!alloc_copy) {
  376. status = -ENOMEM;
  377. goto out_commit;
  378. }
  379. memcpy(alloc_copy, alloc, bh->b_size);
  380. status = ocfs2_journal_access_di(handle, INODE_CACHE(local_alloc_inode),
  381. bh, OCFS2_JOURNAL_ACCESS_WRITE);
  382. if (status < 0) {
  383. mlog_errno(status);
  384. goto out_commit;
  385. }
  386. ocfs2_clear_local_alloc(alloc);
  387. ocfs2_journal_dirty(handle, bh);
  388. brelse(bh);
  389. osb->local_alloc_bh = NULL;
  390. osb->local_alloc_state = OCFS2_LA_UNUSED;
  391. status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
  392. main_bm_inode, main_bm_bh);
  393. if (status < 0)
  394. mlog_errno(status);
  395. out_commit:
  396. ocfs2_commit_trans(osb, handle);
  397. out_unlock:
  398. brelse(main_bm_bh);
  399. ocfs2_inode_unlock(main_bm_inode, 1);
  400. out_mutex:
  401. mutex_unlock(&main_bm_inode->i_mutex);
  402. iput(main_bm_inode);
  403. out:
  404. if (local_alloc_inode)
  405. iput(local_alloc_inode);
  406. if (alloc_copy)
  407. kfree(alloc_copy);
  408. }
  409. /*
  410. * We want to free the bitmap bits outside of any recovery context as
  411. * we'll need a cluster lock to do so, but we must clear the local
  412. * alloc before giving up the recovered nodes journal. To solve this,
  413. * we kmalloc a copy of the local alloc before it's change for the
  414. * caller to process with ocfs2_complete_local_alloc_recovery
  415. */
  416. int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb,
  417. int slot_num,
  418. struct ocfs2_dinode **alloc_copy)
  419. {
  420. int status = 0;
  421. struct buffer_head *alloc_bh = NULL;
  422. struct inode *inode = NULL;
  423. struct ocfs2_dinode *alloc;
  424. trace_ocfs2_begin_local_alloc_recovery(slot_num);
  425. *alloc_copy = NULL;
  426. inode = ocfs2_get_system_file_inode(osb,
  427. LOCAL_ALLOC_SYSTEM_INODE,
  428. slot_num);
  429. if (!inode) {
  430. status = -EINVAL;
  431. mlog_errno(status);
  432. goto bail;
  433. }
  434. mutex_lock(&inode->i_mutex);
  435. status = ocfs2_read_inode_block_full(inode, &alloc_bh,
  436. OCFS2_BH_IGNORE_CACHE);
  437. if (status < 0) {
  438. mlog_errno(status);
  439. goto bail;
  440. }
  441. *alloc_copy = kmalloc(alloc_bh->b_size, GFP_KERNEL);
  442. if (!(*alloc_copy)) {
  443. status = -ENOMEM;
  444. goto bail;
  445. }
  446. memcpy((*alloc_copy), alloc_bh->b_data, alloc_bh->b_size);
  447. alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
  448. ocfs2_clear_local_alloc(alloc);
  449. ocfs2_compute_meta_ecc(osb->sb, alloc_bh->b_data, &alloc->i_check);
  450. status = ocfs2_write_block(osb, alloc_bh, INODE_CACHE(inode));
  451. if (status < 0)
  452. mlog_errno(status);
  453. bail:
  454. if ((status < 0) && (*alloc_copy)) {
  455. kfree(*alloc_copy);
  456. *alloc_copy = NULL;
  457. }
  458. brelse(alloc_bh);
  459. if (inode) {
  460. mutex_unlock(&inode->i_mutex);
  461. iput(inode);
  462. }
  463. if (status)
  464. mlog_errno(status);
  465. return status;
  466. }
  467. /*
  468. * Step 2: By now, we've completed the journal recovery, we've stamped
  469. * a clean local alloc on disk and dropped the node out of the
  470. * recovery map. Dlm locks will no longer stall, so lets clear out the
  471. * main bitmap.
  472. */
  473. int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb,
  474. struct ocfs2_dinode *alloc)
  475. {
  476. int status;
  477. handle_t *handle;
  478. struct buffer_head *main_bm_bh = NULL;
  479. struct inode *main_bm_inode;
  480. main_bm_inode = ocfs2_get_system_file_inode(osb,
  481. GLOBAL_BITMAP_SYSTEM_INODE,
  482. OCFS2_INVALID_SLOT);
  483. if (!main_bm_inode) {
  484. status = -EINVAL;
  485. mlog_errno(status);
  486. goto out;
  487. }
  488. mutex_lock(&main_bm_inode->i_mutex);
  489. status = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 1);
  490. if (status < 0) {
  491. mlog_errno(status);
  492. goto out_mutex;
  493. }
  494. handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
  495. if (IS_ERR(handle)) {
  496. status = PTR_ERR(handle);
  497. handle = NULL;
  498. mlog_errno(status);
  499. goto out_unlock;
  500. }
  501. /* we want the bitmap change to be recorded on disk asap */
  502. handle->h_sync = 1;
  503. status = ocfs2_sync_local_to_main(osb, handle, alloc,
  504. main_bm_inode, main_bm_bh);
  505. if (status < 0)
  506. mlog_errno(status);
  507. ocfs2_commit_trans(osb, handle);
  508. out_unlock:
  509. ocfs2_inode_unlock(main_bm_inode, 1);
  510. out_mutex:
  511. mutex_unlock(&main_bm_inode->i_mutex);
  512. brelse(main_bm_bh);
  513. iput(main_bm_inode);
  514. out:
  515. if (!status)
  516. ocfs2_init_steal_slots(osb);
  517. if (status)
  518. mlog_errno(status);
  519. return status;
  520. }
  521. /*
  522. * make sure we've got at least bits_wanted contiguous bits in the
  523. * local alloc. You lose them when you drop i_mutex.
  524. *
  525. * We will add ourselves to the transaction passed in, but may start
  526. * our own in order to shift windows.
  527. */
  528. int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
  529. u32 bits_wanted,
  530. struct ocfs2_alloc_context *ac)
  531. {
  532. int status;
  533. struct ocfs2_dinode *alloc;
  534. struct inode *local_alloc_inode;
  535. unsigned int free_bits;
  536. BUG_ON(!ac);
  537. local_alloc_inode =
  538. ocfs2_get_system_file_inode(osb,
  539. LOCAL_ALLOC_SYSTEM_INODE,
  540. osb->slot_num);
  541. if (!local_alloc_inode) {
  542. status = -ENOENT;
  543. mlog_errno(status);
  544. goto bail;
  545. }
  546. mutex_lock(&local_alloc_inode->i_mutex);
  547. /*
  548. * We must double check state and allocator bits because
  549. * another process may have changed them while holding i_mutex.
  550. */
  551. spin_lock(&osb->osb_lock);
  552. if (!ocfs2_la_state_enabled(osb) ||
  553. (bits_wanted > osb->local_alloc_bits)) {
  554. spin_unlock(&osb->osb_lock);
  555. status = -ENOSPC;
  556. goto bail;
  557. }
  558. spin_unlock(&osb->osb_lock);
  559. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  560. #ifdef CONFIG_OCFS2_DEBUG_FS
  561. if (le32_to_cpu(alloc->id1.bitmap1.i_used) !=
  562. ocfs2_local_alloc_count_bits(alloc)) {
  563. ocfs2_error(osb->sb, "local alloc inode %llu says it has "
  564. "%u free bits, but a count shows %u",
  565. (unsigned long long)le64_to_cpu(alloc->i_blkno),
  566. le32_to_cpu(alloc->id1.bitmap1.i_used),
  567. ocfs2_local_alloc_count_bits(alloc));
  568. status = -EIO;
  569. goto bail;
  570. }
  571. #endif
  572. free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
  573. le32_to_cpu(alloc->id1.bitmap1.i_used);
  574. if (bits_wanted > free_bits) {
  575. /* uhoh, window change time. */
  576. status =
  577. ocfs2_local_alloc_slide_window(osb, local_alloc_inode);
  578. if (status < 0) {
  579. if (status != -ENOSPC)
  580. mlog_errno(status);
  581. goto bail;
  582. }
  583. /*
  584. * Under certain conditions, the window slide code
  585. * might have reduced the number of bits available or
  586. * disabled the the local alloc entirely. Re-check
  587. * here and return -ENOSPC if necessary.
  588. */
  589. status = -ENOSPC;
  590. if (!ocfs2_la_state_enabled(osb))
  591. goto bail;
  592. free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
  593. le32_to_cpu(alloc->id1.bitmap1.i_used);
  594. if (bits_wanted > free_bits)
  595. goto bail;
  596. }
  597. ac->ac_inode = local_alloc_inode;
  598. /* We should never use localalloc from another slot */
  599. ac->ac_alloc_slot = osb->slot_num;
  600. ac->ac_which = OCFS2_AC_USE_LOCAL;
  601. get_bh(osb->local_alloc_bh);
  602. ac->ac_bh = osb->local_alloc_bh;
  603. status = 0;
  604. bail:
  605. if (status < 0 && local_alloc_inode) {
  606. mutex_unlock(&local_alloc_inode->i_mutex);
  607. iput(local_alloc_inode);
  608. }
  609. trace_ocfs2_reserve_local_alloc_bits(
  610. (unsigned long long)ac->ac_max_block,
  611. bits_wanted, osb->slot_num, status);
  612. if (status)
  613. mlog_errno(status);
  614. return status;
  615. }
  616. int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
  617. handle_t *handle,
  618. struct ocfs2_alloc_context *ac,
  619. u32 bits_wanted,
  620. u32 *bit_off,
  621. u32 *num_bits)
  622. {
  623. int status, start;
  624. struct inode *local_alloc_inode;
  625. void *bitmap;
  626. struct ocfs2_dinode *alloc;
  627. struct ocfs2_local_alloc *la;
  628. BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
  629. local_alloc_inode = ac->ac_inode;
  630. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  631. la = OCFS2_LOCAL_ALLOC(alloc);
  632. start = ocfs2_local_alloc_find_clear_bits(osb, alloc, &bits_wanted,
  633. ac->ac_resv);
  634. if (start == -1) {
  635. /* TODO: Shouldn't we just BUG here? */
  636. status = -ENOSPC;
  637. mlog_errno(status);
  638. goto bail;
  639. }
  640. bitmap = la->la_bitmap;
  641. *bit_off = le32_to_cpu(la->la_bm_off) + start;
  642. *num_bits = bits_wanted;
  643. status = ocfs2_journal_access_di(handle,
  644. INODE_CACHE(local_alloc_inode),
  645. osb->local_alloc_bh,
  646. OCFS2_JOURNAL_ACCESS_WRITE);
  647. if (status < 0) {
  648. mlog_errno(status);
  649. goto bail;
  650. }
  651. ocfs2_resmap_claimed_bits(&osb->osb_la_resmap, ac->ac_resv, start,
  652. bits_wanted);
  653. while(bits_wanted--)
  654. ocfs2_set_bit(start++, bitmap);
  655. le32_add_cpu(&alloc->id1.bitmap1.i_used, *num_bits);
  656. ocfs2_journal_dirty(handle, osb->local_alloc_bh);
  657. bail:
  658. if (status)
  659. mlog_errno(status);
  660. return status;
  661. }
  662. static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc)
  663. {
  664. u32 count;
  665. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  666. count = memweight(la->la_bitmap, le16_to_cpu(la->la_size));
  667. trace_ocfs2_local_alloc_count_bits(count);
  668. return count;
  669. }
  670. static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
  671. struct ocfs2_dinode *alloc,
  672. u32 *numbits,
  673. struct ocfs2_alloc_reservation *resv)
  674. {
  675. int numfound, bitoff, left, startoff, lastzero;
  676. int local_resv = 0;
  677. struct ocfs2_alloc_reservation r;
  678. void *bitmap = NULL;
  679. struct ocfs2_reservation_map *resmap = &osb->osb_la_resmap;
  680. if (!alloc->id1.bitmap1.i_total) {
  681. bitoff = -1;
  682. goto bail;
  683. }
  684. if (!resv) {
  685. local_resv = 1;
  686. ocfs2_resv_init_once(&r);
  687. ocfs2_resv_set_type(&r, OCFS2_RESV_FLAG_TMP);
  688. resv = &r;
  689. }
  690. numfound = *numbits;
  691. if (ocfs2_resmap_resv_bits(resmap, resv, &bitoff, &numfound) == 0) {
  692. if (numfound < *numbits)
  693. *numbits = numfound;
  694. goto bail;
  695. }
  696. /*
  697. * Code error. While reservations are enabled, local
  698. * allocation should _always_ go through them.
  699. */
  700. BUG_ON(osb->osb_resv_level != 0);
  701. /*
  702. * Reservations are disabled. Handle this the old way.
  703. */
  704. bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap;
  705. numfound = bitoff = startoff = 0;
  706. lastzero = -1;
  707. left = le32_to_cpu(alloc->id1.bitmap1.i_total);
  708. while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) {
  709. if (bitoff == left) {
  710. /* mlog(0, "bitoff (%d) == left", bitoff); */
  711. break;
  712. }
  713. /* mlog(0, "Found a zero: bitoff = %d, startoff = %d, "
  714. "numfound = %d\n", bitoff, startoff, numfound);*/
  715. /* Ok, we found a zero bit... is it contig. or do we
  716. * start over?*/
  717. if (bitoff == startoff) {
  718. /* we found a zero */
  719. numfound++;
  720. startoff++;
  721. } else {
  722. /* got a zero after some ones */
  723. numfound = 1;
  724. startoff = bitoff+1;
  725. }
  726. /* we got everything we needed */
  727. if (numfound == *numbits) {
  728. /* mlog(0, "Found it all!\n"); */
  729. break;
  730. }
  731. }
  732. trace_ocfs2_local_alloc_find_clear_bits_search_bitmap(bitoff, numfound);
  733. if (numfound == *numbits)
  734. bitoff = startoff - numfound;
  735. else
  736. bitoff = -1;
  737. bail:
  738. if (local_resv)
  739. ocfs2_resv_discard(resmap, resv);
  740. trace_ocfs2_local_alloc_find_clear_bits(*numbits,
  741. le32_to_cpu(alloc->id1.bitmap1.i_total),
  742. bitoff, numfound);
  743. return bitoff;
  744. }
  745. static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc)
  746. {
  747. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  748. int i;
  749. alloc->id1.bitmap1.i_total = 0;
  750. alloc->id1.bitmap1.i_used = 0;
  751. la->la_bm_off = 0;
  752. for(i = 0; i < le16_to_cpu(la->la_size); i++)
  753. la->la_bitmap[i] = 0;
  754. }
  755. #if 0
  756. /* turn this on and uncomment below to aid debugging window shifts. */
  757. static void ocfs2_verify_zero_bits(unsigned long *bitmap,
  758. unsigned int start,
  759. unsigned int count)
  760. {
  761. unsigned int tmp = count;
  762. while(tmp--) {
  763. if (ocfs2_test_bit(start + tmp, bitmap)) {
  764. printk("ocfs2_verify_zero_bits: start = %u, count = "
  765. "%u\n", start, count);
  766. printk("ocfs2_verify_zero_bits: bit %u is set!",
  767. start + tmp);
  768. BUG();
  769. }
  770. }
  771. }
  772. #endif
  773. /*
  774. * sync the local alloc to main bitmap.
  775. *
  776. * assumes you've already locked the main bitmap -- the bitmap inode
  777. * passed is used for caching.
  778. */
  779. static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
  780. handle_t *handle,
  781. struct ocfs2_dinode *alloc,
  782. struct inode *main_bm_inode,
  783. struct buffer_head *main_bm_bh)
  784. {
  785. int status = 0;
  786. int bit_off, left, count, start;
  787. u64 la_start_blk;
  788. u64 blkno;
  789. void *bitmap;
  790. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  791. trace_ocfs2_sync_local_to_main(
  792. le32_to_cpu(alloc->id1.bitmap1.i_total),
  793. le32_to_cpu(alloc->id1.bitmap1.i_used));
  794. if (!alloc->id1.bitmap1.i_total) {
  795. goto bail;
  796. }
  797. if (le32_to_cpu(alloc->id1.bitmap1.i_used) ==
  798. le32_to_cpu(alloc->id1.bitmap1.i_total)) {
  799. goto bail;
  800. }
  801. la_start_blk = ocfs2_clusters_to_blocks(osb->sb,
  802. le32_to_cpu(la->la_bm_off));
  803. bitmap = la->la_bitmap;
  804. start = count = bit_off = 0;
  805. left = le32_to_cpu(alloc->id1.bitmap1.i_total);
  806. while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start))
  807. != -1) {
  808. if ((bit_off < left) && (bit_off == start)) {
  809. count++;
  810. start++;
  811. continue;
  812. }
  813. if (count) {
  814. blkno = la_start_blk +
  815. ocfs2_clusters_to_blocks(osb->sb,
  816. start - count);
  817. trace_ocfs2_sync_local_to_main_free(
  818. count, start - count,
  819. (unsigned long long)la_start_blk,
  820. (unsigned long long)blkno);
  821. status = ocfs2_release_clusters(handle,
  822. main_bm_inode,
  823. main_bm_bh, blkno,
  824. count);
  825. if (status < 0) {
  826. mlog_errno(status);
  827. goto bail;
  828. }
  829. }
  830. if (bit_off >= left)
  831. break;
  832. count = 1;
  833. start = bit_off + 1;
  834. }
  835. bail:
  836. if (status)
  837. mlog_errno(status);
  838. return status;
  839. }
  840. enum ocfs2_la_event {
  841. OCFS2_LA_EVENT_SLIDE, /* Normal window slide. */
  842. OCFS2_LA_EVENT_FRAGMENTED, /* The global bitmap has
  843. * enough bits theoretically
  844. * free, but a contiguous
  845. * allocation could not be
  846. * found. */
  847. OCFS2_LA_EVENT_ENOSPC, /* Global bitmap doesn't have
  848. * enough bits free to satisfy
  849. * our request. */
  850. };
  851. #define OCFS2_LA_ENABLE_INTERVAL (30 * HZ)
  852. /*
  853. * Given an event, calculate the size of our next local alloc window.
  854. *
  855. * This should always be called under i_mutex of the local alloc inode
  856. * so that local alloc disabling doesn't race with processes trying to
  857. * use the allocator.
  858. *
  859. * Returns the state which the local alloc was left in. This value can
  860. * be ignored by some paths.
  861. */
  862. static int ocfs2_recalc_la_window(struct ocfs2_super *osb,
  863. enum ocfs2_la_event event)
  864. {
  865. unsigned int bits;
  866. int state;
  867. spin_lock(&osb->osb_lock);
  868. if (osb->local_alloc_state == OCFS2_LA_DISABLED) {
  869. WARN_ON_ONCE(osb->local_alloc_state == OCFS2_LA_DISABLED);
  870. goto out_unlock;
  871. }
  872. /*
  873. * ENOSPC and fragmentation are treated similarly for now.
  874. */
  875. if (event == OCFS2_LA_EVENT_ENOSPC ||
  876. event == OCFS2_LA_EVENT_FRAGMENTED) {
  877. /*
  878. * We ran out of contiguous space in the primary
  879. * bitmap. Drastically reduce the number of bits used
  880. * by local alloc until we have to disable it.
  881. */
  882. bits = osb->local_alloc_bits >> 1;
  883. if (bits > ocfs2_megabytes_to_clusters(osb->sb, 1)) {
  884. /*
  885. * By setting state to THROTTLED, we'll keep
  886. * the number of local alloc bits used down
  887. * until an event occurs which would give us
  888. * reason to assume the bitmap situation might
  889. * have changed.
  890. */
  891. osb->local_alloc_state = OCFS2_LA_THROTTLED;
  892. osb->local_alloc_bits = bits;
  893. } else {
  894. osb->local_alloc_state = OCFS2_LA_DISABLED;
  895. }
  896. queue_delayed_work(ocfs2_wq, &osb->la_enable_wq,
  897. OCFS2_LA_ENABLE_INTERVAL);
  898. goto out_unlock;
  899. }
  900. /*
  901. * Don't increase the size of the local alloc window until we
  902. * know we might be able to fulfill the request. Otherwise, we
  903. * risk bouncing around the global bitmap during periods of
  904. * low space.
  905. */
  906. if (osb->local_alloc_state != OCFS2_LA_THROTTLED)
  907. osb->local_alloc_bits = osb->local_alloc_default_bits;
  908. out_unlock:
  909. state = osb->local_alloc_state;
  910. spin_unlock(&osb->osb_lock);
  911. return state;
  912. }
  913. static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
  914. struct ocfs2_alloc_context **ac,
  915. struct inode **bitmap_inode,
  916. struct buffer_head **bitmap_bh)
  917. {
  918. int status;
  919. *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
  920. if (!(*ac)) {
  921. status = -ENOMEM;
  922. mlog_errno(status);
  923. goto bail;
  924. }
  925. retry_enospc:
  926. (*ac)->ac_bits_wanted = osb->local_alloc_default_bits;
  927. status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
  928. if (status == -ENOSPC) {
  929. if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_ENOSPC) ==
  930. OCFS2_LA_DISABLED)
  931. goto bail;
  932. ocfs2_free_ac_resource(*ac);
  933. memset(*ac, 0, sizeof(struct ocfs2_alloc_context));
  934. goto retry_enospc;
  935. }
  936. if (status < 0) {
  937. mlog_errno(status);
  938. goto bail;
  939. }
  940. *bitmap_inode = (*ac)->ac_inode;
  941. igrab(*bitmap_inode);
  942. *bitmap_bh = (*ac)->ac_bh;
  943. get_bh(*bitmap_bh);
  944. status = 0;
  945. bail:
  946. if ((status < 0) && *ac) {
  947. ocfs2_free_alloc_context(*ac);
  948. *ac = NULL;
  949. }
  950. if (status)
  951. mlog_errno(status);
  952. return status;
  953. }
  954. /*
  955. * pass it the bitmap lock in lock_bh if you have it.
  956. */
  957. static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
  958. handle_t *handle,
  959. struct ocfs2_alloc_context *ac)
  960. {
  961. int status = 0;
  962. u32 cluster_off, cluster_count;
  963. struct ocfs2_dinode *alloc = NULL;
  964. struct ocfs2_local_alloc *la;
  965. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  966. la = OCFS2_LOCAL_ALLOC(alloc);
  967. trace_ocfs2_local_alloc_new_window(
  968. le32_to_cpu(alloc->id1.bitmap1.i_total),
  969. osb->local_alloc_bits);
  970. /* Instruct the allocation code to try the most recently used
  971. * cluster group. We'll re-record the group used this pass
  972. * below. */
  973. ac->ac_last_group = osb->la_last_gd;
  974. /* we used the generic suballoc reserve function, but we set
  975. * everything up nicely, so there's no reason why we can't use
  976. * the more specific cluster api to claim bits. */
  977. status = ocfs2_claim_clusters(handle, ac, osb->local_alloc_bits,
  978. &cluster_off, &cluster_count);
  979. if (status == -ENOSPC) {
  980. retry_enospc:
  981. /*
  982. * Note: We could also try syncing the journal here to
  983. * allow use of any free bits which the current
  984. * transaction can't give us access to. --Mark
  985. */
  986. if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_FRAGMENTED) ==
  987. OCFS2_LA_DISABLED)
  988. goto bail;
  989. ac->ac_bits_wanted = osb->local_alloc_default_bits;
  990. status = ocfs2_claim_clusters(handle, ac,
  991. osb->local_alloc_bits,
  992. &cluster_off,
  993. &cluster_count);
  994. if (status == -ENOSPC)
  995. goto retry_enospc;
  996. /*
  997. * We only shrunk the *minimum* number of in our
  998. * request - it's entirely possible that the allocator
  999. * might give us more than we asked for.
  1000. */
  1001. if (status == 0) {
  1002. spin_lock(&osb->osb_lock);
  1003. osb->local_alloc_bits = cluster_count;
  1004. spin_unlock(&osb->osb_lock);
  1005. }
  1006. }
  1007. if (status < 0) {
  1008. if (status != -ENOSPC)
  1009. mlog_errno(status);
  1010. goto bail;
  1011. }
  1012. osb->la_last_gd = ac->ac_last_group;
  1013. la->la_bm_off = cpu_to_le32(cluster_off);
  1014. alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count);
  1015. /* just in case... In the future when we find space ourselves,
  1016. * we don't have to get all contiguous -- but we'll have to
  1017. * set all previously used bits in bitmap and update
  1018. * la_bits_set before setting the bits in the main bitmap. */
  1019. alloc->id1.bitmap1.i_used = 0;
  1020. memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0,
  1021. le16_to_cpu(la->la_size));
  1022. ocfs2_resmap_restart(&osb->osb_la_resmap, cluster_count,
  1023. OCFS2_LOCAL_ALLOC(alloc)->la_bitmap);
  1024. trace_ocfs2_local_alloc_new_window_result(
  1025. OCFS2_LOCAL_ALLOC(alloc)->la_bm_off,
  1026. le32_to_cpu(alloc->id1.bitmap1.i_total));
  1027. bail:
  1028. if (status)
  1029. mlog_errno(status);
  1030. return status;
  1031. }
  1032. /* Note that we do *NOT* lock the local alloc inode here as
  1033. * it's been locked already for us. */
  1034. static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
  1035. struct inode *local_alloc_inode)
  1036. {
  1037. int status = 0;
  1038. struct buffer_head *main_bm_bh = NULL;
  1039. struct inode *main_bm_inode = NULL;
  1040. handle_t *handle = NULL;
  1041. struct ocfs2_dinode *alloc;
  1042. struct ocfs2_dinode *alloc_copy = NULL;
  1043. struct ocfs2_alloc_context *ac = NULL;
  1044. ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_SLIDE);
  1045. /* This will lock the main bitmap for us. */
  1046. status = ocfs2_local_alloc_reserve_for_window(osb,
  1047. &ac,
  1048. &main_bm_inode,
  1049. &main_bm_bh);
  1050. if (status < 0) {
  1051. if (status != -ENOSPC)
  1052. mlog_errno(status);
  1053. goto bail;
  1054. }
  1055. handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
  1056. if (IS_ERR(handle)) {
  1057. status = PTR_ERR(handle);
  1058. handle = NULL;
  1059. mlog_errno(status);
  1060. goto bail;
  1061. }
  1062. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  1063. /* We want to clear the local alloc before doing anything
  1064. * else, so that if we error later during this operation,
  1065. * local alloc shutdown won't try to double free main bitmap
  1066. * bits. Make a copy so the sync function knows which bits to
  1067. * free. */
  1068. alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_NOFS);
  1069. if (!alloc_copy) {
  1070. status = -ENOMEM;
  1071. mlog_errno(status);
  1072. goto bail;
  1073. }
  1074. memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size);
  1075. status = ocfs2_journal_access_di(handle,
  1076. INODE_CACHE(local_alloc_inode),
  1077. osb->local_alloc_bh,
  1078. OCFS2_JOURNAL_ACCESS_WRITE);
  1079. if (status < 0) {
  1080. mlog_errno(status);
  1081. goto bail;
  1082. }
  1083. ocfs2_clear_local_alloc(alloc);
  1084. ocfs2_journal_dirty(handle, osb->local_alloc_bh);
  1085. status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
  1086. main_bm_inode, main_bm_bh);
  1087. if (status < 0) {
  1088. mlog_errno(status);
  1089. goto bail;
  1090. }
  1091. status = ocfs2_local_alloc_new_window(osb, handle, ac);
  1092. if (status < 0) {
  1093. if (status != -ENOSPC)
  1094. mlog_errno(status);
  1095. goto bail;
  1096. }
  1097. atomic_inc(&osb->alloc_stats.moves);
  1098. bail:
  1099. if (handle)
  1100. ocfs2_commit_trans(osb, handle);
  1101. brelse(main_bm_bh);
  1102. if (main_bm_inode)
  1103. iput(main_bm_inode);
  1104. if (alloc_copy)
  1105. kfree(alloc_copy);
  1106. if (ac)
  1107. ocfs2_free_alloc_context(ac);
  1108. if (status)
  1109. mlog_errno(status);
  1110. return status;
  1111. }