localalloc.c 33 KB

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