localalloc.c 32 KB

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