localalloc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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. #define MLOG_MASK_PREFIX ML_DISK_ALLOC
  31. #include <cluster/masklog.h>
  32. #include "ocfs2.h"
  33. #include "alloc.h"
  34. #include "dlmglue.h"
  35. #include "inode.h"
  36. #include "journal.h"
  37. #include "localalloc.h"
  38. #include "suballoc.h"
  39. #include "super.h"
  40. #include "sysfile.h"
  41. #include "buffer_head_io.h"
  42. #define OCFS2_LOCAL_ALLOC(dinode) (&((dinode)->id2.i_lab))
  43. static inline int ocfs2_local_alloc_window_bits(struct ocfs2_super *osb);
  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. struct ocfs2_journal_handle *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_journal_handle *handle,
  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. struct ocfs2_journal_handle *handle,
  61. struct ocfs2_alloc_context *ac);
  62. static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
  63. struct inode *local_alloc_inode);
  64. /*
  65. * Determine how large our local alloc window should be, in bits.
  66. *
  67. * These values (and the behavior in ocfs2_alloc_should_use_local) have
  68. * been chosen so that most allocations, including new block groups go
  69. * through local alloc.
  70. */
  71. static inline int ocfs2_local_alloc_window_bits(struct ocfs2_super *osb)
  72. {
  73. BUG_ON(osb->s_clustersize_bits < 12);
  74. return 2048 >> (osb->s_clustersize_bits - 12);
  75. }
  76. /*
  77. * Tell us whether a given allocation should use the local alloc
  78. * file. Otherwise, it has to go to the main bitmap.
  79. */
  80. int ocfs2_alloc_should_use_local(struct ocfs2_super *osb, u64 bits)
  81. {
  82. int la_bits = ocfs2_local_alloc_window_bits(osb);
  83. if (osb->local_alloc_state != OCFS2_LA_ENABLED)
  84. return 0;
  85. /* la_bits should be at least twice the size (in clusters) of
  86. * a new block group. We want to be sure block group
  87. * allocations go through the local alloc, so allow an
  88. * allocation to take up to half the bitmap. */
  89. if (bits > (la_bits / 2))
  90. return 0;
  91. return 1;
  92. }
  93. int ocfs2_load_local_alloc(struct ocfs2_super *osb)
  94. {
  95. int status = 0;
  96. struct ocfs2_dinode *alloc = NULL;
  97. struct buffer_head *alloc_bh = NULL;
  98. u32 num_used;
  99. struct inode *inode = NULL;
  100. struct ocfs2_local_alloc *la;
  101. mlog_entry_void();
  102. /* read the alloc off disk */
  103. inode = ocfs2_get_system_file_inode(osb, LOCAL_ALLOC_SYSTEM_INODE,
  104. osb->slot_num);
  105. if (!inode) {
  106. status = -EINVAL;
  107. mlog_errno(status);
  108. goto bail;
  109. }
  110. status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno,
  111. &alloc_bh, 0, inode);
  112. if (status < 0) {
  113. mlog_errno(status);
  114. goto bail;
  115. }
  116. alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
  117. la = OCFS2_LOCAL_ALLOC(alloc);
  118. if (!(le32_to_cpu(alloc->i_flags) &
  119. (OCFS2_LOCAL_ALLOC_FL|OCFS2_BITMAP_FL))) {
  120. mlog(ML_ERROR, "Invalid local alloc inode, %llu\n",
  121. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  122. status = -EINVAL;
  123. goto bail;
  124. }
  125. if ((la->la_size == 0) ||
  126. (le16_to_cpu(la->la_size) > ocfs2_local_alloc_size(inode->i_sb))) {
  127. mlog(ML_ERROR, "Local alloc size is invalid (la_size = %u)\n",
  128. le16_to_cpu(la->la_size));
  129. status = -EINVAL;
  130. goto bail;
  131. }
  132. /* do a little verification. */
  133. num_used = ocfs2_local_alloc_count_bits(alloc);
  134. /* hopefully the local alloc has always been recovered before
  135. * we load it. */
  136. if (num_used
  137. || alloc->id1.bitmap1.i_used
  138. || alloc->id1.bitmap1.i_total
  139. || la->la_bm_off)
  140. mlog(ML_ERROR, "Local alloc hasn't been recovered!\n"
  141. "found = %u, set = %u, taken = %u, off = %u\n",
  142. num_used, le32_to_cpu(alloc->id1.bitmap1.i_used),
  143. le32_to_cpu(alloc->id1.bitmap1.i_total),
  144. OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
  145. osb->local_alloc_bh = alloc_bh;
  146. osb->local_alloc_state = OCFS2_LA_ENABLED;
  147. bail:
  148. if (status < 0)
  149. if (alloc_bh)
  150. brelse(alloc_bh);
  151. if (inode)
  152. iput(inode);
  153. mlog_exit(status);
  154. return status;
  155. }
  156. /*
  157. * return any unused bits to the bitmap and write out a clean
  158. * local_alloc.
  159. *
  160. * local_alloc_bh is optional. If not passed, we will simply use the
  161. * one off osb. If you do pass it however, be warned that it *will* be
  162. * returned brelse'd and NULL'd out.*/
  163. void ocfs2_shutdown_local_alloc(struct ocfs2_super *osb)
  164. {
  165. int status;
  166. struct ocfs2_journal_handle *handle = NULL;
  167. struct inode *local_alloc_inode = NULL;
  168. struct buffer_head *bh = NULL;
  169. struct buffer_head *main_bm_bh = NULL;
  170. struct inode *main_bm_inode = NULL;
  171. struct ocfs2_dinode *alloc_copy = NULL;
  172. struct ocfs2_dinode *alloc = NULL;
  173. mlog_entry_void();
  174. if (osb->local_alloc_state == OCFS2_LA_UNUSED)
  175. goto bail;
  176. local_alloc_inode =
  177. ocfs2_get_system_file_inode(osb,
  178. LOCAL_ALLOC_SYSTEM_INODE,
  179. osb->slot_num);
  180. if (!local_alloc_inode) {
  181. status = -ENOENT;
  182. mlog_errno(status);
  183. goto bail;
  184. }
  185. osb->local_alloc_state = OCFS2_LA_DISABLED;
  186. handle = ocfs2_alloc_handle(osb);
  187. if (!handle) {
  188. status = -ENOMEM;
  189. mlog_errno(status);
  190. goto bail;
  191. }
  192. main_bm_inode = ocfs2_get_system_file_inode(osb,
  193. GLOBAL_BITMAP_SYSTEM_INODE,
  194. OCFS2_INVALID_SLOT);
  195. if (!main_bm_inode) {
  196. status = -EINVAL;
  197. mlog_errno(status);
  198. goto bail;
  199. }
  200. ocfs2_handle_add_inode(handle, main_bm_inode);
  201. status = ocfs2_meta_lock(main_bm_inode, handle, &main_bm_bh, 1);
  202. if (status < 0) {
  203. mlog_errno(status);
  204. goto bail;
  205. }
  206. /* WINDOW_MOVE_CREDITS is a bit heavy... */
  207. handle = ocfs2_start_trans(osb, handle, OCFS2_WINDOW_MOVE_CREDITS);
  208. if (IS_ERR(handle)) {
  209. mlog_errno(PTR_ERR(handle));
  210. handle = NULL;
  211. goto bail;
  212. }
  213. bh = osb->local_alloc_bh;
  214. alloc = (struct ocfs2_dinode *) bh->b_data;
  215. alloc_copy = kmalloc(bh->b_size, GFP_KERNEL);
  216. if (!alloc_copy) {
  217. status = -ENOMEM;
  218. goto bail;
  219. }
  220. memcpy(alloc_copy, alloc, bh->b_size);
  221. status = ocfs2_journal_access(handle, local_alloc_inode, bh,
  222. OCFS2_JOURNAL_ACCESS_WRITE);
  223. if (status < 0) {
  224. mlog_errno(status);
  225. goto bail;
  226. }
  227. ocfs2_clear_local_alloc(alloc);
  228. status = ocfs2_journal_dirty(handle, bh);
  229. if (status < 0) {
  230. mlog_errno(status);
  231. goto bail;
  232. }
  233. brelse(bh);
  234. osb->local_alloc_bh = NULL;
  235. osb->local_alloc_state = OCFS2_LA_UNUSED;
  236. status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
  237. main_bm_inode, main_bm_bh);
  238. if (status < 0)
  239. mlog_errno(status);
  240. bail:
  241. if (handle)
  242. ocfs2_commit_trans(handle);
  243. if (main_bm_bh)
  244. brelse(main_bm_bh);
  245. if (main_bm_inode)
  246. iput(main_bm_inode);
  247. if (local_alloc_inode)
  248. iput(local_alloc_inode);
  249. if (alloc_copy)
  250. kfree(alloc_copy);
  251. mlog_exit_void();
  252. }
  253. /*
  254. * We want to free the bitmap bits outside of any recovery context as
  255. * we'll need a cluster lock to do so, but we must clear the local
  256. * alloc before giving up the recovered nodes journal. To solve this,
  257. * we kmalloc a copy of the local alloc before it's change for the
  258. * caller to process with ocfs2_complete_local_alloc_recovery
  259. */
  260. int ocfs2_begin_local_alloc_recovery(struct ocfs2_super *osb,
  261. int slot_num,
  262. struct ocfs2_dinode **alloc_copy)
  263. {
  264. int status = 0;
  265. struct buffer_head *alloc_bh = NULL;
  266. struct inode *inode = NULL;
  267. struct ocfs2_dinode *alloc;
  268. mlog_entry("(slot_num = %d)\n", slot_num);
  269. *alloc_copy = NULL;
  270. inode = ocfs2_get_system_file_inode(osb,
  271. LOCAL_ALLOC_SYSTEM_INODE,
  272. slot_num);
  273. if (!inode) {
  274. status = -EINVAL;
  275. mlog_errno(status);
  276. goto bail;
  277. }
  278. mutex_lock(&inode->i_mutex);
  279. status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno,
  280. &alloc_bh, 0, inode);
  281. if (status < 0) {
  282. mlog_errno(status);
  283. goto bail;
  284. }
  285. *alloc_copy = kmalloc(alloc_bh->b_size, GFP_KERNEL);
  286. if (!(*alloc_copy)) {
  287. status = -ENOMEM;
  288. goto bail;
  289. }
  290. memcpy((*alloc_copy), alloc_bh->b_data, alloc_bh->b_size);
  291. alloc = (struct ocfs2_dinode *) alloc_bh->b_data;
  292. ocfs2_clear_local_alloc(alloc);
  293. status = ocfs2_write_block(osb, alloc_bh, inode);
  294. if (status < 0)
  295. mlog_errno(status);
  296. bail:
  297. if ((status < 0) && (*alloc_copy)) {
  298. kfree(*alloc_copy);
  299. *alloc_copy = NULL;
  300. }
  301. if (alloc_bh)
  302. brelse(alloc_bh);
  303. if (inode) {
  304. mutex_unlock(&inode->i_mutex);
  305. iput(inode);
  306. }
  307. mlog_exit(status);
  308. return status;
  309. }
  310. /*
  311. * Step 2: By now, we've completed the journal recovery, we've stamped
  312. * a clean local alloc on disk and dropped the node out of the
  313. * recovery map. Dlm locks will no longer stall, so lets clear out the
  314. * main bitmap.
  315. */
  316. int ocfs2_complete_local_alloc_recovery(struct ocfs2_super *osb,
  317. struct ocfs2_dinode *alloc)
  318. {
  319. int status;
  320. struct ocfs2_journal_handle *handle = NULL;
  321. struct buffer_head *main_bm_bh = NULL;
  322. struct inode *main_bm_inode = NULL;
  323. mlog_entry_void();
  324. handle = ocfs2_alloc_handle(osb);
  325. if (!handle) {
  326. status = -ENOMEM;
  327. mlog_errno(status);
  328. goto bail;
  329. }
  330. main_bm_inode = ocfs2_get_system_file_inode(osb,
  331. GLOBAL_BITMAP_SYSTEM_INODE,
  332. OCFS2_INVALID_SLOT);
  333. if (!main_bm_inode) {
  334. status = -EINVAL;
  335. mlog_errno(status);
  336. goto bail;
  337. }
  338. ocfs2_handle_add_inode(handle, main_bm_inode);
  339. status = ocfs2_meta_lock(main_bm_inode, handle, &main_bm_bh, 1);
  340. if (status < 0) {
  341. mlog_errno(status);
  342. goto bail;
  343. }
  344. handle = ocfs2_start_trans(osb, handle, OCFS2_WINDOW_MOVE_CREDITS);
  345. if (IS_ERR(handle)) {
  346. status = PTR_ERR(handle);
  347. handle = NULL;
  348. mlog_errno(status);
  349. goto bail;
  350. }
  351. /* we want the bitmap change to be recorded on disk asap */
  352. ocfs2_handle_set_sync(handle, 1);
  353. status = ocfs2_sync_local_to_main(osb, handle, alloc,
  354. main_bm_inode, main_bm_bh);
  355. if (status < 0)
  356. mlog_errno(status);
  357. bail:
  358. if (handle)
  359. ocfs2_commit_trans(handle);
  360. if (main_bm_bh)
  361. brelse(main_bm_bh);
  362. if (main_bm_inode)
  363. iput(main_bm_inode);
  364. mlog_exit(status);
  365. return status;
  366. }
  367. /*
  368. * make sure we've got at least bitswanted contiguous bits in the
  369. * local alloc. You lose them when you drop i_mutex.
  370. *
  371. * We will add ourselves to the transaction passed in, but may start
  372. * our own in order to shift windows.
  373. */
  374. int ocfs2_reserve_local_alloc_bits(struct ocfs2_super *osb,
  375. struct ocfs2_journal_handle *passed_handle,
  376. u32 bits_wanted,
  377. struct ocfs2_alloc_context *ac)
  378. {
  379. int status;
  380. struct ocfs2_dinode *alloc;
  381. struct inode *local_alloc_inode;
  382. unsigned int free_bits;
  383. mlog_entry_void();
  384. BUG_ON(!passed_handle);
  385. BUG_ON(!ac);
  386. BUG_ON(passed_handle->flags & OCFS2_HANDLE_STARTED);
  387. local_alloc_inode =
  388. ocfs2_get_system_file_inode(osb,
  389. LOCAL_ALLOC_SYSTEM_INODE,
  390. osb->slot_num);
  391. if (!local_alloc_inode) {
  392. status = -ENOENT;
  393. mlog_errno(status);
  394. goto bail;
  395. }
  396. ocfs2_handle_add_inode(passed_handle, local_alloc_inode);
  397. if (osb->local_alloc_state != OCFS2_LA_ENABLED) {
  398. status = -ENOSPC;
  399. goto bail;
  400. }
  401. if (bits_wanted > ocfs2_local_alloc_window_bits(osb)) {
  402. mlog(0, "Asking for more than my max window size!\n");
  403. status = -ENOSPC;
  404. goto bail;
  405. }
  406. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  407. if (le32_to_cpu(alloc->id1.bitmap1.i_used) !=
  408. ocfs2_local_alloc_count_bits(alloc)) {
  409. ocfs2_error(osb->sb, "local alloc inode %llu says it has "
  410. "%u free bits, but a count shows %u",
  411. (unsigned long long)le64_to_cpu(alloc->i_blkno),
  412. le32_to_cpu(alloc->id1.bitmap1.i_used),
  413. ocfs2_local_alloc_count_bits(alloc));
  414. status = -EIO;
  415. goto bail;
  416. }
  417. free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
  418. le32_to_cpu(alloc->id1.bitmap1.i_used);
  419. if (bits_wanted > free_bits) {
  420. /* uhoh, window change time. */
  421. status =
  422. ocfs2_local_alloc_slide_window(osb, local_alloc_inode);
  423. if (status < 0) {
  424. if (status != -ENOSPC)
  425. mlog_errno(status);
  426. goto bail;
  427. }
  428. }
  429. ac->ac_inode = igrab(local_alloc_inode);
  430. get_bh(osb->local_alloc_bh);
  431. ac->ac_bh = osb->local_alloc_bh;
  432. ac->ac_which = OCFS2_AC_USE_LOCAL;
  433. status = 0;
  434. bail:
  435. if (local_alloc_inode)
  436. iput(local_alloc_inode);
  437. mlog_exit(status);
  438. return status;
  439. }
  440. int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
  441. struct ocfs2_journal_handle *handle,
  442. struct ocfs2_alloc_context *ac,
  443. u32 min_bits,
  444. u32 *bit_off,
  445. u32 *num_bits)
  446. {
  447. int status, start;
  448. struct inode *local_alloc_inode;
  449. u32 bits_wanted;
  450. void *bitmap;
  451. struct ocfs2_dinode *alloc;
  452. struct ocfs2_local_alloc *la;
  453. mlog_entry_void();
  454. BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
  455. bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
  456. local_alloc_inode = ac->ac_inode;
  457. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  458. la = OCFS2_LOCAL_ALLOC(alloc);
  459. start = ocfs2_local_alloc_find_clear_bits(osb, alloc, bits_wanted);
  460. if (start == -1) {
  461. /* TODO: Shouldn't we just BUG here? */
  462. status = -ENOSPC;
  463. mlog_errno(status);
  464. goto bail;
  465. }
  466. bitmap = la->la_bitmap;
  467. *bit_off = le32_to_cpu(la->la_bm_off) + start;
  468. /* local alloc is always contiguous by nature -- we never
  469. * delete bits from it! */
  470. *num_bits = bits_wanted;
  471. status = ocfs2_journal_access(handle, local_alloc_inode,
  472. osb->local_alloc_bh,
  473. OCFS2_JOURNAL_ACCESS_WRITE);
  474. if (status < 0) {
  475. mlog_errno(status);
  476. goto bail;
  477. }
  478. while(bits_wanted--)
  479. ocfs2_set_bit(start++, bitmap);
  480. alloc->id1.bitmap1.i_used = cpu_to_le32(*num_bits +
  481. le32_to_cpu(alloc->id1.bitmap1.i_used));
  482. status = ocfs2_journal_dirty(handle, osb->local_alloc_bh);
  483. if (status < 0) {
  484. mlog_errno(status);
  485. goto bail;
  486. }
  487. status = 0;
  488. bail:
  489. mlog_exit(status);
  490. return status;
  491. }
  492. static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc)
  493. {
  494. int i;
  495. u8 *buffer;
  496. u32 count = 0;
  497. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  498. mlog_entry_void();
  499. buffer = la->la_bitmap;
  500. for (i = 0; i < le16_to_cpu(la->la_size); i++)
  501. count += hweight8(buffer[i]);
  502. mlog_exit(count);
  503. return count;
  504. }
  505. static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
  506. struct ocfs2_dinode *alloc,
  507. u32 numbits)
  508. {
  509. int numfound, bitoff, left, startoff, lastzero;
  510. void *bitmap = NULL;
  511. mlog_entry("(numbits wanted = %u)\n", numbits);
  512. if (!alloc->id1.bitmap1.i_total) {
  513. mlog(0, "No bits in my window!\n");
  514. bitoff = -1;
  515. goto bail;
  516. }
  517. bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap;
  518. numfound = bitoff = startoff = 0;
  519. lastzero = -1;
  520. left = le32_to_cpu(alloc->id1.bitmap1.i_total);
  521. while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) {
  522. if (bitoff == left) {
  523. /* mlog(0, "bitoff (%d) == left", bitoff); */
  524. break;
  525. }
  526. /* mlog(0, "Found a zero: bitoff = %d, startoff = %d, "
  527. "numfound = %d\n", bitoff, startoff, numfound);*/
  528. /* Ok, we found a zero bit... is it contig. or do we
  529. * start over?*/
  530. if (bitoff == startoff) {
  531. /* we found a zero */
  532. numfound++;
  533. startoff++;
  534. } else {
  535. /* got a zero after some ones */
  536. numfound = 1;
  537. startoff = bitoff+1;
  538. }
  539. /* we got everything we needed */
  540. if (numfound == numbits) {
  541. /* mlog(0, "Found it all!\n"); */
  542. break;
  543. }
  544. }
  545. mlog(0, "Exiting loop, bitoff = %d, numfound = %d\n", bitoff,
  546. numfound);
  547. if (numfound == numbits)
  548. bitoff = startoff - numfound;
  549. else
  550. bitoff = -1;
  551. bail:
  552. mlog_exit(bitoff);
  553. return bitoff;
  554. }
  555. static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc)
  556. {
  557. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  558. int i;
  559. mlog_entry_void();
  560. alloc->id1.bitmap1.i_total = 0;
  561. alloc->id1.bitmap1.i_used = 0;
  562. la->la_bm_off = 0;
  563. for(i = 0; i < le16_to_cpu(la->la_size); i++)
  564. la->la_bitmap[i] = 0;
  565. mlog_exit_void();
  566. }
  567. #if 0
  568. /* turn this on and uncomment below to aid debugging window shifts. */
  569. static void ocfs2_verify_zero_bits(unsigned long *bitmap,
  570. unsigned int start,
  571. unsigned int count)
  572. {
  573. unsigned int tmp = count;
  574. while(tmp--) {
  575. if (ocfs2_test_bit(start + tmp, bitmap)) {
  576. printk("ocfs2_verify_zero_bits: start = %u, count = "
  577. "%u\n", start, count);
  578. printk("ocfs2_verify_zero_bits: bit %u is set!",
  579. start + tmp);
  580. BUG();
  581. }
  582. }
  583. }
  584. #endif
  585. /*
  586. * sync the local alloc to main bitmap.
  587. *
  588. * assumes you've already locked the main bitmap -- the bitmap inode
  589. * passed is used for caching.
  590. */
  591. static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
  592. struct ocfs2_journal_handle *handle,
  593. struct ocfs2_dinode *alloc,
  594. struct inode *main_bm_inode,
  595. struct buffer_head *main_bm_bh)
  596. {
  597. int status = 0;
  598. int bit_off, left, count, start;
  599. u64 la_start_blk;
  600. u64 blkno;
  601. void *bitmap;
  602. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  603. mlog_entry("total = %u, COUNT = %u, used = %u\n",
  604. le32_to_cpu(alloc->id1.bitmap1.i_total),
  605. ocfs2_local_alloc_count_bits(alloc),
  606. le32_to_cpu(alloc->id1.bitmap1.i_used));
  607. if (!alloc->id1.bitmap1.i_total) {
  608. mlog(0, "nothing to sync!\n");
  609. goto bail;
  610. }
  611. if (le32_to_cpu(alloc->id1.bitmap1.i_used) ==
  612. le32_to_cpu(alloc->id1.bitmap1.i_total)) {
  613. mlog(0, "all bits were taken!\n");
  614. goto bail;
  615. }
  616. la_start_blk = ocfs2_clusters_to_blocks(osb->sb,
  617. le32_to_cpu(la->la_bm_off));
  618. bitmap = la->la_bitmap;
  619. start = count = bit_off = 0;
  620. left = le32_to_cpu(alloc->id1.bitmap1.i_total);
  621. while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start))
  622. != -1) {
  623. if ((bit_off < left) && (bit_off == start)) {
  624. count++;
  625. start++;
  626. continue;
  627. }
  628. if (count) {
  629. blkno = la_start_blk +
  630. ocfs2_clusters_to_blocks(osb->sb,
  631. start - count);
  632. mlog(0, "freeing %u bits starting at local alloc bit "
  633. "%u (la_start_blk = %llu, blkno = %llu)\n",
  634. count, start - count,
  635. (unsigned long long)la_start_blk,
  636. (unsigned long long)blkno);
  637. status = ocfs2_free_clusters(handle, main_bm_inode,
  638. main_bm_bh, blkno, count);
  639. if (status < 0) {
  640. mlog_errno(status);
  641. goto bail;
  642. }
  643. }
  644. if (bit_off >= left)
  645. break;
  646. count = 1;
  647. start = bit_off + 1;
  648. }
  649. bail:
  650. mlog_exit(status);
  651. return status;
  652. }
  653. static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
  654. struct ocfs2_journal_handle *handle,
  655. struct ocfs2_alloc_context **ac,
  656. struct inode **bitmap_inode,
  657. struct buffer_head **bitmap_bh)
  658. {
  659. int status;
  660. *ac = kcalloc(1, sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
  661. if (!(*ac)) {
  662. status = -ENOMEM;
  663. mlog_errno(status);
  664. goto bail;
  665. }
  666. (*ac)->ac_handle = handle;
  667. (*ac)->ac_bits_wanted = ocfs2_local_alloc_window_bits(osb);
  668. status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
  669. if (status < 0) {
  670. if (status != -ENOSPC)
  671. mlog_errno(status);
  672. goto bail;
  673. }
  674. *bitmap_inode = (*ac)->ac_inode;
  675. igrab(*bitmap_inode);
  676. *bitmap_bh = (*ac)->ac_bh;
  677. get_bh(*bitmap_bh);
  678. status = 0;
  679. bail:
  680. if ((status < 0) && *ac) {
  681. ocfs2_free_alloc_context(*ac);
  682. *ac = NULL;
  683. }
  684. mlog_exit(status);
  685. return status;
  686. }
  687. /*
  688. * pass it the bitmap lock in lock_bh if you have it.
  689. */
  690. static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
  691. struct ocfs2_journal_handle *handle,
  692. struct ocfs2_alloc_context *ac)
  693. {
  694. int status = 0;
  695. u32 cluster_off, cluster_count;
  696. struct ocfs2_dinode *alloc = NULL;
  697. struct ocfs2_local_alloc *la;
  698. mlog_entry_void();
  699. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  700. la = OCFS2_LOCAL_ALLOC(alloc);
  701. if (alloc->id1.bitmap1.i_total)
  702. mlog(0, "asking me to alloc a new window over a non-empty "
  703. "one\n");
  704. mlog(0, "Allocating %u clusters for a new window.\n",
  705. ocfs2_local_alloc_window_bits(osb));
  706. /* we used the generic suballoc reserve function, but we set
  707. * everything up nicely, so there's no reason why we can't use
  708. * the more specific cluster api to claim bits. */
  709. status = ocfs2_claim_clusters(osb, handle, ac,
  710. ocfs2_local_alloc_window_bits(osb),
  711. &cluster_off, &cluster_count);
  712. if (status < 0) {
  713. if (status != -ENOSPC)
  714. mlog_errno(status);
  715. goto bail;
  716. }
  717. la->la_bm_off = cpu_to_le32(cluster_off);
  718. alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count);
  719. /* just in case... In the future when we find space ourselves,
  720. * we don't have to get all contiguous -- but we'll have to
  721. * set all previously used bits in bitmap and update
  722. * la_bits_set before setting the bits in the main bitmap. */
  723. alloc->id1.bitmap1.i_used = 0;
  724. memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0,
  725. le16_to_cpu(la->la_size));
  726. mlog(0, "New window allocated:\n");
  727. mlog(0, "window la_bm_off = %u\n",
  728. OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
  729. mlog(0, "window bits = %u\n", le32_to_cpu(alloc->id1.bitmap1.i_total));
  730. bail:
  731. mlog_exit(status);
  732. return status;
  733. }
  734. /* Note that we do *NOT* lock the local alloc inode here as
  735. * it's been locked already for us. */
  736. static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
  737. struct inode *local_alloc_inode)
  738. {
  739. int status = 0;
  740. struct buffer_head *main_bm_bh = NULL;
  741. struct inode *main_bm_inode = NULL;
  742. struct ocfs2_journal_handle *handle = NULL;
  743. struct ocfs2_dinode *alloc;
  744. struct ocfs2_dinode *alloc_copy = NULL;
  745. struct ocfs2_alloc_context *ac = NULL;
  746. mlog_entry_void();
  747. handle = ocfs2_alloc_handle(osb);
  748. if (!handle) {
  749. status = -ENOMEM;
  750. mlog_errno(status);
  751. goto bail;
  752. }
  753. /* This will lock the main bitmap for us. */
  754. status = ocfs2_local_alloc_reserve_for_window(osb,
  755. handle,
  756. &ac,
  757. &main_bm_inode,
  758. &main_bm_bh);
  759. if (status < 0) {
  760. if (status != -ENOSPC)
  761. mlog_errno(status);
  762. goto bail;
  763. }
  764. handle = ocfs2_start_trans(osb, handle, OCFS2_WINDOW_MOVE_CREDITS);
  765. if (IS_ERR(handle)) {
  766. status = PTR_ERR(handle);
  767. handle = NULL;
  768. mlog_errno(status);
  769. goto bail;
  770. }
  771. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  772. /* We want to clear the local alloc before doing anything
  773. * else, so that if we error later during this operation,
  774. * local alloc shutdown won't try to double free main bitmap
  775. * bits. Make a copy so the sync function knows which bits to
  776. * free. */
  777. alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_KERNEL);
  778. if (!alloc_copy) {
  779. status = -ENOMEM;
  780. mlog_errno(status);
  781. goto bail;
  782. }
  783. memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size);
  784. status = ocfs2_journal_access(handle, local_alloc_inode,
  785. osb->local_alloc_bh,
  786. OCFS2_JOURNAL_ACCESS_WRITE);
  787. if (status < 0) {
  788. mlog_errno(status);
  789. goto bail;
  790. }
  791. ocfs2_clear_local_alloc(alloc);
  792. status = ocfs2_journal_dirty(handle, osb->local_alloc_bh);
  793. if (status < 0) {
  794. mlog_errno(status);
  795. goto bail;
  796. }
  797. status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
  798. main_bm_inode, main_bm_bh);
  799. if (status < 0) {
  800. mlog_errno(status);
  801. goto bail;
  802. }
  803. status = ocfs2_local_alloc_new_window(osb, handle, ac);
  804. if (status < 0) {
  805. if (status != -ENOSPC)
  806. mlog_errno(status);
  807. goto bail;
  808. }
  809. atomic_inc(&osb->alloc_stats.moves);
  810. status = 0;
  811. bail:
  812. if (handle)
  813. ocfs2_commit_trans(handle);
  814. if (main_bm_bh)
  815. brelse(main_bm_bh);
  816. if (main_bm_inode)
  817. iput(main_bm_inode);
  818. if (alloc_copy)
  819. kfree(alloc_copy);
  820. if (ac)
  821. ocfs2_free_alloc_context(ac);
  822. mlog_exit(status);
  823. return status;
  824. }