localalloc.c 32 KB

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