localalloc.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  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. int i;
  761. alloc->id1.bitmap1.i_total = 0;
  762. alloc->id1.bitmap1.i_used = 0;
  763. la->la_bm_off = 0;
  764. for(i = 0; i < le16_to_cpu(la->la_size); i++)
  765. la->la_bitmap[i] = 0;
  766. }
  767. #if 0
  768. /* turn this on and uncomment below to aid debugging window shifts. */
  769. static void ocfs2_verify_zero_bits(unsigned long *bitmap,
  770. unsigned int start,
  771. unsigned int count)
  772. {
  773. unsigned int tmp = count;
  774. while(tmp--) {
  775. if (ocfs2_test_bit(start + tmp, bitmap)) {
  776. printk("ocfs2_verify_zero_bits: start = %u, count = "
  777. "%u\n", start, count);
  778. printk("ocfs2_verify_zero_bits: bit %u is set!",
  779. start + tmp);
  780. BUG();
  781. }
  782. }
  783. }
  784. #endif
  785. /*
  786. * sync the local alloc to main bitmap.
  787. *
  788. * assumes you've already locked the main bitmap -- the bitmap inode
  789. * passed is used for caching.
  790. */
  791. static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
  792. handle_t *handle,
  793. struct ocfs2_dinode *alloc,
  794. struct inode *main_bm_inode,
  795. struct buffer_head *main_bm_bh)
  796. {
  797. int status = 0;
  798. int bit_off, left, count, start;
  799. u64 la_start_blk;
  800. u64 blkno;
  801. void *bitmap;
  802. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  803. trace_ocfs2_sync_local_to_main(
  804. le32_to_cpu(alloc->id1.bitmap1.i_total),
  805. le32_to_cpu(alloc->id1.bitmap1.i_used));
  806. if (!alloc->id1.bitmap1.i_total) {
  807. goto bail;
  808. }
  809. if (le32_to_cpu(alloc->id1.bitmap1.i_used) ==
  810. le32_to_cpu(alloc->id1.bitmap1.i_total)) {
  811. goto bail;
  812. }
  813. la_start_blk = ocfs2_clusters_to_blocks(osb->sb,
  814. le32_to_cpu(la->la_bm_off));
  815. bitmap = la->la_bitmap;
  816. start = count = 0;
  817. left = le32_to_cpu(alloc->id1.bitmap1.i_total);
  818. while (1) {
  819. bit_off = ocfs2_find_next_zero_bit(bitmap, left, start);
  820. if ((bit_off < left) && (bit_off == start)) {
  821. count++;
  822. start++;
  823. continue;
  824. }
  825. if (count) {
  826. blkno = la_start_blk +
  827. ocfs2_clusters_to_blocks(osb->sb,
  828. start - count);
  829. trace_ocfs2_sync_local_to_main_free(
  830. count, start - count,
  831. (unsigned long long)la_start_blk,
  832. (unsigned long long)blkno);
  833. status = ocfs2_release_clusters(handle,
  834. main_bm_inode,
  835. main_bm_bh, blkno,
  836. count);
  837. if (status < 0) {
  838. mlog_errno(status);
  839. goto bail;
  840. }
  841. }
  842. if (bit_off >= left)
  843. break;
  844. count = 1;
  845. start = bit_off + 1;
  846. }
  847. bail:
  848. if (status)
  849. mlog_errno(status);
  850. return status;
  851. }
  852. enum ocfs2_la_event {
  853. OCFS2_LA_EVENT_SLIDE, /* Normal window slide. */
  854. OCFS2_LA_EVENT_FRAGMENTED, /* The global bitmap has
  855. * enough bits theoretically
  856. * free, but a contiguous
  857. * allocation could not be
  858. * found. */
  859. OCFS2_LA_EVENT_ENOSPC, /* Global bitmap doesn't have
  860. * enough bits free to satisfy
  861. * our request. */
  862. };
  863. #define OCFS2_LA_ENABLE_INTERVAL (30 * HZ)
  864. /*
  865. * Given an event, calculate the size of our next local alloc window.
  866. *
  867. * This should always be called under i_rwsem of the local alloc inode
  868. * so that local alloc disabling doesn't race with processes trying to
  869. * use the allocator.
  870. *
  871. * Returns the state which the local alloc was left in. This value can
  872. * be ignored by some paths.
  873. */
  874. static int ocfs2_recalc_la_window(struct ocfs2_super *osb,
  875. enum ocfs2_la_event event)
  876. {
  877. unsigned int bits;
  878. int state;
  879. spin_lock(&osb->osb_lock);
  880. if (osb->local_alloc_state == OCFS2_LA_DISABLED) {
  881. WARN_ON_ONCE(osb->local_alloc_state == OCFS2_LA_DISABLED);
  882. goto out_unlock;
  883. }
  884. /*
  885. * ENOSPC and fragmentation are treated similarly for now.
  886. */
  887. if (event == OCFS2_LA_EVENT_ENOSPC ||
  888. event == OCFS2_LA_EVENT_FRAGMENTED) {
  889. /*
  890. * We ran out of contiguous space in the primary
  891. * bitmap. Drastically reduce the number of bits used
  892. * by local alloc until we have to disable it.
  893. */
  894. bits = osb->local_alloc_bits >> 1;
  895. if (bits > ocfs2_megabytes_to_clusters(osb->sb, 1)) {
  896. /*
  897. * By setting state to THROTTLED, we'll keep
  898. * the number of local alloc bits used down
  899. * until an event occurs which would give us
  900. * reason to assume the bitmap situation might
  901. * have changed.
  902. */
  903. osb->local_alloc_state = OCFS2_LA_THROTTLED;
  904. osb->local_alloc_bits = bits;
  905. } else {
  906. osb->local_alloc_state = OCFS2_LA_DISABLED;
  907. }
  908. queue_delayed_work(osb->ocfs2_wq, &osb->la_enable_wq,
  909. OCFS2_LA_ENABLE_INTERVAL);
  910. goto out_unlock;
  911. }
  912. /*
  913. * Don't increase the size of the local alloc window until we
  914. * know we might be able to fulfill the request. Otherwise, we
  915. * risk bouncing around the global bitmap during periods of
  916. * low space.
  917. */
  918. if (osb->local_alloc_state != OCFS2_LA_THROTTLED)
  919. osb->local_alloc_bits = osb->local_alloc_default_bits;
  920. out_unlock:
  921. state = osb->local_alloc_state;
  922. spin_unlock(&osb->osb_lock);
  923. return state;
  924. }
  925. static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
  926. struct ocfs2_alloc_context **ac,
  927. struct inode **bitmap_inode,
  928. struct buffer_head **bitmap_bh)
  929. {
  930. int status;
  931. *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
  932. if (!(*ac)) {
  933. status = -ENOMEM;
  934. mlog_errno(status);
  935. goto bail;
  936. }
  937. retry_enospc:
  938. (*ac)->ac_bits_wanted = osb->local_alloc_bits;
  939. status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
  940. if (status == -ENOSPC) {
  941. if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_ENOSPC) ==
  942. OCFS2_LA_DISABLED)
  943. goto bail;
  944. ocfs2_free_ac_resource(*ac);
  945. memset(*ac, 0, sizeof(struct ocfs2_alloc_context));
  946. goto retry_enospc;
  947. }
  948. if (status < 0) {
  949. mlog_errno(status);
  950. goto bail;
  951. }
  952. *bitmap_inode = (*ac)->ac_inode;
  953. igrab(*bitmap_inode);
  954. *bitmap_bh = (*ac)->ac_bh;
  955. get_bh(*bitmap_bh);
  956. status = 0;
  957. bail:
  958. if ((status < 0) && *ac) {
  959. ocfs2_free_alloc_context(*ac);
  960. *ac = NULL;
  961. }
  962. if (status)
  963. mlog_errno(status);
  964. return status;
  965. }
  966. /*
  967. * pass it the bitmap lock in lock_bh if you have it.
  968. */
  969. static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
  970. handle_t *handle,
  971. struct ocfs2_alloc_context *ac)
  972. {
  973. int status = 0;
  974. u32 cluster_off, cluster_count;
  975. struct ocfs2_dinode *alloc = NULL;
  976. struct ocfs2_local_alloc *la;
  977. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  978. la = OCFS2_LOCAL_ALLOC(alloc);
  979. trace_ocfs2_local_alloc_new_window(
  980. le32_to_cpu(alloc->id1.bitmap1.i_total),
  981. osb->local_alloc_bits);
  982. /* Instruct the allocation code to try the most recently used
  983. * cluster group. We'll re-record the group used this pass
  984. * below. */
  985. ac->ac_last_group = osb->la_last_gd;
  986. /* we used the generic suballoc reserve function, but we set
  987. * everything up nicely, so there's no reason why we can't use
  988. * the more specific cluster api to claim bits. */
  989. status = ocfs2_claim_clusters(handle, ac, osb->local_alloc_bits,
  990. &cluster_off, &cluster_count);
  991. if (status == -ENOSPC) {
  992. retry_enospc:
  993. /*
  994. * Note: We could also try syncing the journal here to
  995. * allow use of any free bits which the current
  996. * transaction can't give us access to. --Mark
  997. */
  998. if (ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_FRAGMENTED) ==
  999. OCFS2_LA_DISABLED)
  1000. goto bail;
  1001. ac->ac_bits_wanted = osb->local_alloc_bits;
  1002. status = ocfs2_claim_clusters(handle, ac,
  1003. osb->local_alloc_bits,
  1004. &cluster_off,
  1005. &cluster_count);
  1006. if (status == -ENOSPC)
  1007. goto retry_enospc;
  1008. /*
  1009. * We only shrunk the *minimum* number of in our
  1010. * request - it's entirely possible that the allocator
  1011. * might give us more than we asked for.
  1012. */
  1013. if (status == 0) {
  1014. spin_lock(&osb->osb_lock);
  1015. osb->local_alloc_bits = cluster_count;
  1016. spin_unlock(&osb->osb_lock);
  1017. }
  1018. }
  1019. if (status < 0) {
  1020. if (status != -ENOSPC)
  1021. mlog_errno(status);
  1022. goto bail;
  1023. }
  1024. osb->la_last_gd = ac->ac_last_group;
  1025. la->la_bm_off = cpu_to_le32(cluster_off);
  1026. alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count);
  1027. /* just in case... In the future when we find space ourselves,
  1028. * we don't have to get all contiguous -- but we'll have to
  1029. * set all previously used bits in bitmap and update
  1030. * la_bits_set before setting the bits in the main bitmap. */
  1031. alloc->id1.bitmap1.i_used = 0;
  1032. memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0,
  1033. le16_to_cpu(la->la_size));
  1034. ocfs2_resmap_restart(&osb->osb_la_resmap, cluster_count,
  1035. OCFS2_LOCAL_ALLOC(alloc)->la_bitmap);
  1036. trace_ocfs2_local_alloc_new_window_result(
  1037. le32_to_cpu(OCFS2_LOCAL_ALLOC(alloc)->la_bm_off),
  1038. le32_to_cpu(alloc->id1.bitmap1.i_total));
  1039. bail:
  1040. if (status)
  1041. mlog_errno(status);
  1042. return status;
  1043. }
  1044. /* Note that we do *NOT* lock the local alloc inode here as
  1045. * it's been locked already for us. */
  1046. static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
  1047. struct inode *local_alloc_inode)
  1048. {
  1049. int status = 0;
  1050. struct buffer_head *main_bm_bh = NULL;
  1051. struct inode *main_bm_inode = NULL;
  1052. handle_t *handle = NULL;
  1053. struct ocfs2_dinode *alloc;
  1054. struct ocfs2_dinode *alloc_copy = NULL;
  1055. struct ocfs2_alloc_context *ac = NULL;
  1056. ocfs2_recalc_la_window(osb, OCFS2_LA_EVENT_SLIDE);
  1057. /* This will lock the main bitmap for us. */
  1058. status = ocfs2_local_alloc_reserve_for_window(osb,
  1059. &ac,
  1060. &main_bm_inode,
  1061. &main_bm_bh);
  1062. if (status < 0) {
  1063. if (status != -ENOSPC)
  1064. mlog_errno(status);
  1065. goto bail;
  1066. }
  1067. handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
  1068. if (IS_ERR(handle)) {
  1069. status = PTR_ERR(handle);
  1070. handle = NULL;
  1071. mlog_errno(status);
  1072. goto bail;
  1073. }
  1074. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  1075. /* We want to clear the local alloc before doing anything
  1076. * else, so that if we error later during this operation,
  1077. * local alloc shutdown won't try to double free main bitmap
  1078. * bits. Make a copy so the sync function knows which bits to
  1079. * free. */
  1080. alloc_copy = kmemdup(alloc, osb->local_alloc_bh->b_size, GFP_NOFS);
  1081. if (!alloc_copy) {
  1082. status = -ENOMEM;
  1083. mlog_errno(status);
  1084. goto bail;
  1085. }
  1086. status = ocfs2_journal_access_di(handle,
  1087. INODE_CACHE(local_alloc_inode),
  1088. osb->local_alloc_bh,
  1089. OCFS2_JOURNAL_ACCESS_WRITE);
  1090. if (status < 0) {
  1091. mlog_errno(status);
  1092. goto bail;
  1093. }
  1094. ocfs2_clear_local_alloc(alloc);
  1095. ocfs2_journal_dirty(handle, osb->local_alloc_bh);
  1096. status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
  1097. main_bm_inode, main_bm_bh);
  1098. if (status < 0) {
  1099. mlog_errno(status);
  1100. goto bail;
  1101. }
  1102. status = ocfs2_local_alloc_new_window(osb, handle, ac);
  1103. if (status < 0) {
  1104. if (status != -ENOSPC)
  1105. mlog_errno(status);
  1106. goto bail;
  1107. }
  1108. atomic_inc(&osb->alloc_stats.moves);
  1109. bail:
  1110. if (handle)
  1111. ocfs2_commit_trans(osb, handle);
  1112. brelse(main_bm_bh);
  1113. iput(main_bm_inode);
  1114. kfree(alloc_copy);
  1115. if (ac)
  1116. ocfs2_free_alloc_context(ac);
  1117. if (status)
  1118. mlog_errno(status);
  1119. return status;
  1120. }