localalloc.c 23 KB

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