localalloc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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. ac->ac_inode = local_alloc_inode;
  387. ac->ac_which = OCFS2_AC_USE_LOCAL;
  388. if (osb->local_alloc_state != OCFS2_LA_ENABLED) {
  389. status = -ENOSPC;
  390. goto bail;
  391. }
  392. if (bits_wanted > ocfs2_local_alloc_window_bits(osb)) {
  393. mlog(0, "Asking for more than my max window size!\n");
  394. status = -ENOSPC;
  395. goto bail;
  396. }
  397. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  398. if (le32_to_cpu(alloc->id1.bitmap1.i_used) !=
  399. ocfs2_local_alloc_count_bits(alloc)) {
  400. ocfs2_error(osb->sb, "local alloc inode %llu says it has "
  401. "%u free bits, but a count shows %u",
  402. (unsigned long long)le64_to_cpu(alloc->i_blkno),
  403. le32_to_cpu(alloc->id1.bitmap1.i_used),
  404. ocfs2_local_alloc_count_bits(alloc));
  405. status = -EIO;
  406. goto bail;
  407. }
  408. free_bits = le32_to_cpu(alloc->id1.bitmap1.i_total) -
  409. le32_to_cpu(alloc->id1.bitmap1.i_used);
  410. if (bits_wanted > free_bits) {
  411. /* uhoh, window change time. */
  412. status =
  413. ocfs2_local_alloc_slide_window(osb, local_alloc_inode);
  414. if (status < 0) {
  415. if (status != -ENOSPC)
  416. mlog_errno(status);
  417. goto bail;
  418. }
  419. }
  420. get_bh(osb->local_alloc_bh);
  421. ac->ac_bh = osb->local_alloc_bh;
  422. status = 0;
  423. bail:
  424. mlog_exit(status);
  425. return status;
  426. }
  427. int ocfs2_claim_local_alloc_bits(struct ocfs2_super *osb,
  428. handle_t *handle,
  429. struct ocfs2_alloc_context *ac,
  430. u32 min_bits,
  431. u32 *bit_off,
  432. u32 *num_bits)
  433. {
  434. int status, start;
  435. struct inode *local_alloc_inode;
  436. u32 bits_wanted;
  437. void *bitmap;
  438. struct ocfs2_dinode *alloc;
  439. struct ocfs2_local_alloc *la;
  440. mlog_entry_void();
  441. BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL);
  442. bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
  443. local_alloc_inode = ac->ac_inode;
  444. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  445. la = OCFS2_LOCAL_ALLOC(alloc);
  446. start = ocfs2_local_alloc_find_clear_bits(osb, alloc, bits_wanted);
  447. if (start == -1) {
  448. /* TODO: Shouldn't we just BUG here? */
  449. status = -ENOSPC;
  450. mlog_errno(status);
  451. goto bail;
  452. }
  453. bitmap = la->la_bitmap;
  454. *bit_off = le32_to_cpu(la->la_bm_off) + start;
  455. /* local alloc is always contiguous by nature -- we never
  456. * delete bits from it! */
  457. *num_bits = bits_wanted;
  458. status = ocfs2_journal_access(handle, local_alloc_inode,
  459. osb->local_alloc_bh,
  460. OCFS2_JOURNAL_ACCESS_WRITE);
  461. if (status < 0) {
  462. mlog_errno(status);
  463. goto bail;
  464. }
  465. while(bits_wanted--)
  466. ocfs2_set_bit(start++, bitmap);
  467. alloc->id1.bitmap1.i_used = cpu_to_le32(*num_bits +
  468. le32_to_cpu(alloc->id1.bitmap1.i_used));
  469. status = ocfs2_journal_dirty(handle, osb->local_alloc_bh);
  470. if (status < 0) {
  471. mlog_errno(status);
  472. goto bail;
  473. }
  474. status = 0;
  475. bail:
  476. mlog_exit(status);
  477. return status;
  478. }
  479. static u32 ocfs2_local_alloc_count_bits(struct ocfs2_dinode *alloc)
  480. {
  481. int i;
  482. u8 *buffer;
  483. u32 count = 0;
  484. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  485. mlog_entry_void();
  486. buffer = la->la_bitmap;
  487. for (i = 0; i < le16_to_cpu(la->la_size); i++)
  488. count += hweight8(buffer[i]);
  489. mlog_exit(count);
  490. return count;
  491. }
  492. static int ocfs2_local_alloc_find_clear_bits(struct ocfs2_super *osb,
  493. struct ocfs2_dinode *alloc,
  494. u32 numbits)
  495. {
  496. int numfound, bitoff, left, startoff, lastzero;
  497. void *bitmap = NULL;
  498. mlog_entry("(numbits wanted = %u)\n", numbits);
  499. if (!alloc->id1.bitmap1.i_total) {
  500. mlog(0, "No bits in my window!\n");
  501. bitoff = -1;
  502. goto bail;
  503. }
  504. bitmap = OCFS2_LOCAL_ALLOC(alloc)->la_bitmap;
  505. numfound = bitoff = startoff = 0;
  506. lastzero = -1;
  507. left = le32_to_cpu(alloc->id1.bitmap1.i_total);
  508. while ((bitoff = ocfs2_find_next_zero_bit(bitmap, left, startoff)) != -1) {
  509. if (bitoff == left) {
  510. /* mlog(0, "bitoff (%d) == left", bitoff); */
  511. break;
  512. }
  513. /* mlog(0, "Found a zero: bitoff = %d, startoff = %d, "
  514. "numfound = %d\n", bitoff, startoff, numfound);*/
  515. /* Ok, we found a zero bit... is it contig. or do we
  516. * start over?*/
  517. if (bitoff == startoff) {
  518. /* we found a zero */
  519. numfound++;
  520. startoff++;
  521. } else {
  522. /* got a zero after some ones */
  523. numfound = 1;
  524. startoff = bitoff+1;
  525. }
  526. /* we got everything we needed */
  527. if (numfound == numbits) {
  528. /* mlog(0, "Found it all!\n"); */
  529. break;
  530. }
  531. }
  532. mlog(0, "Exiting loop, bitoff = %d, numfound = %d\n", bitoff,
  533. numfound);
  534. if (numfound == numbits)
  535. bitoff = startoff - numfound;
  536. else
  537. bitoff = -1;
  538. bail:
  539. mlog_exit(bitoff);
  540. return bitoff;
  541. }
  542. static void ocfs2_clear_local_alloc(struct ocfs2_dinode *alloc)
  543. {
  544. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  545. int i;
  546. mlog_entry_void();
  547. alloc->id1.bitmap1.i_total = 0;
  548. alloc->id1.bitmap1.i_used = 0;
  549. la->la_bm_off = 0;
  550. for(i = 0; i < le16_to_cpu(la->la_size); i++)
  551. la->la_bitmap[i] = 0;
  552. mlog_exit_void();
  553. }
  554. #if 0
  555. /* turn this on and uncomment below to aid debugging window shifts. */
  556. static void ocfs2_verify_zero_bits(unsigned long *bitmap,
  557. unsigned int start,
  558. unsigned int count)
  559. {
  560. unsigned int tmp = count;
  561. while(tmp--) {
  562. if (ocfs2_test_bit(start + tmp, bitmap)) {
  563. printk("ocfs2_verify_zero_bits: start = %u, count = "
  564. "%u\n", start, count);
  565. printk("ocfs2_verify_zero_bits: bit %u is set!",
  566. start + tmp);
  567. BUG();
  568. }
  569. }
  570. }
  571. #endif
  572. /*
  573. * sync the local alloc to main bitmap.
  574. *
  575. * assumes you've already locked the main bitmap -- the bitmap inode
  576. * passed is used for caching.
  577. */
  578. static int ocfs2_sync_local_to_main(struct ocfs2_super *osb,
  579. handle_t *handle,
  580. struct ocfs2_dinode *alloc,
  581. struct inode *main_bm_inode,
  582. struct buffer_head *main_bm_bh)
  583. {
  584. int status = 0;
  585. int bit_off, left, count, start;
  586. u64 la_start_blk;
  587. u64 blkno;
  588. void *bitmap;
  589. struct ocfs2_local_alloc *la = OCFS2_LOCAL_ALLOC(alloc);
  590. mlog_entry("total = %u, COUNT = %u, used = %u\n",
  591. le32_to_cpu(alloc->id1.bitmap1.i_total),
  592. ocfs2_local_alloc_count_bits(alloc),
  593. le32_to_cpu(alloc->id1.bitmap1.i_used));
  594. if (!alloc->id1.bitmap1.i_total) {
  595. mlog(0, "nothing to sync!\n");
  596. goto bail;
  597. }
  598. if (le32_to_cpu(alloc->id1.bitmap1.i_used) ==
  599. le32_to_cpu(alloc->id1.bitmap1.i_total)) {
  600. mlog(0, "all bits were taken!\n");
  601. goto bail;
  602. }
  603. la_start_blk = ocfs2_clusters_to_blocks(osb->sb,
  604. le32_to_cpu(la->la_bm_off));
  605. bitmap = la->la_bitmap;
  606. start = count = bit_off = 0;
  607. left = le32_to_cpu(alloc->id1.bitmap1.i_total);
  608. while ((bit_off = ocfs2_find_next_zero_bit(bitmap, left, start))
  609. != -1) {
  610. if ((bit_off < left) && (bit_off == start)) {
  611. count++;
  612. start++;
  613. continue;
  614. }
  615. if (count) {
  616. blkno = la_start_blk +
  617. ocfs2_clusters_to_blocks(osb->sb,
  618. start - count);
  619. mlog(0, "freeing %u bits starting at local alloc bit "
  620. "%u (la_start_blk = %llu, blkno = %llu)\n",
  621. count, start - count,
  622. (unsigned long long)la_start_blk,
  623. (unsigned long long)blkno);
  624. status = ocfs2_free_clusters(handle, main_bm_inode,
  625. main_bm_bh, blkno, count);
  626. if (status < 0) {
  627. mlog_errno(status);
  628. goto bail;
  629. }
  630. }
  631. if (bit_off >= left)
  632. break;
  633. count = 1;
  634. start = bit_off + 1;
  635. }
  636. bail:
  637. mlog_exit(status);
  638. return status;
  639. }
  640. static int ocfs2_local_alloc_reserve_for_window(struct ocfs2_super *osb,
  641. struct ocfs2_alloc_context **ac,
  642. struct inode **bitmap_inode,
  643. struct buffer_head **bitmap_bh)
  644. {
  645. int status;
  646. *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
  647. if (!(*ac)) {
  648. status = -ENOMEM;
  649. mlog_errno(status);
  650. goto bail;
  651. }
  652. (*ac)->ac_bits_wanted = ocfs2_local_alloc_window_bits(osb);
  653. status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
  654. if (status < 0) {
  655. if (status != -ENOSPC)
  656. mlog_errno(status);
  657. goto bail;
  658. }
  659. *bitmap_inode = (*ac)->ac_inode;
  660. igrab(*bitmap_inode);
  661. *bitmap_bh = (*ac)->ac_bh;
  662. get_bh(*bitmap_bh);
  663. status = 0;
  664. bail:
  665. if ((status < 0) && *ac) {
  666. ocfs2_free_alloc_context(*ac);
  667. *ac = NULL;
  668. }
  669. mlog_exit(status);
  670. return status;
  671. }
  672. /*
  673. * pass it the bitmap lock in lock_bh if you have it.
  674. */
  675. static int ocfs2_local_alloc_new_window(struct ocfs2_super *osb,
  676. handle_t *handle,
  677. struct ocfs2_alloc_context *ac)
  678. {
  679. int status = 0;
  680. u32 cluster_off, cluster_count;
  681. struct ocfs2_dinode *alloc = NULL;
  682. struct ocfs2_local_alloc *la;
  683. mlog_entry_void();
  684. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  685. la = OCFS2_LOCAL_ALLOC(alloc);
  686. if (alloc->id1.bitmap1.i_total)
  687. mlog(0, "asking me to alloc a new window over a non-empty "
  688. "one\n");
  689. mlog(0, "Allocating %u clusters for a new window.\n",
  690. ocfs2_local_alloc_window_bits(osb));
  691. /* Instruct the allocation code to try the most recently used
  692. * cluster group. We'll re-record the group used this pass
  693. * below. */
  694. ac->ac_last_group = osb->la_last_gd;
  695. /* we used the generic suballoc reserve function, but we set
  696. * everything up nicely, so there's no reason why we can't use
  697. * the more specific cluster api to claim bits. */
  698. status = ocfs2_claim_clusters(osb, handle, ac,
  699. ocfs2_local_alloc_window_bits(osb),
  700. &cluster_off, &cluster_count);
  701. if (status < 0) {
  702. if (status != -ENOSPC)
  703. mlog_errno(status);
  704. goto bail;
  705. }
  706. osb->la_last_gd = ac->ac_last_group;
  707. la->la_bm_off = cpu_to_le32(cluster_off);
  708. alloc->id1.bitmap1.i_total = cpu_to_le32(cluster_count);
  709. /* just in case... In the future when we find space ourselves,
  710. * we don't have to get all contiguous -- but we'll have to
  711. * set all previously used bits in bitmap and update
  712. * la_bits_set before setting the bits in the main bitmap. */
  713. alloc->id1.bitmap1.i_used = 0;
  714. memset(OCFS2_LOCAL_ALLOC(alloc)->la_bitmap, 0,
  715. le16_to_cpu(la->la_size));
  716. mlog(0, "New window allocated:\n");
  717. mlog(0, "window la_bm_off = %u\n",
  718. OCFS2_LOCAL_ALLOC(alloc)->la_bm_off);
  719. mlog(0, "window bits = %u\n", le32_to_cpu(alloc->id1.bitmap1.i_total));
  720. bail:
  721. mlog_exit(status);
  722. return status;
  723. }
  724. /* Note that we do *NOT* lock the local alloc inode here as
  725. * it's been locked already for us. */
  726. static int ocfs2_local_alloc_slide_window(struct ocfs2_super *osb,
  727. struct inode *local_alloc_inode)
  728. {
  729. int status = 0;
  730. struct buffer_head *main_bm_bh = NULL;
  731. struct inode *main_bm_inode = NULL;
  732. handle_t *handle = NULL;
  733. struct ocfs2_dinode *alloc;
  734. struct ocfs2_dinode *alloc_copy = NULL;
  735. struct ocfs2_alloc_context *ac = NULL;
  736. mlog_entry_void();
  737. /* This will lock the main bitmap for us. */
  738. status = ocfs2_local_alloc_reserve_for_window(osb,
  739. &ac,
  740. &main_bm_inode,
  741. &main_bm_bh);
  742. if (status < 0) {
  743. if (status != -ENOSPC)
  744. mlog_errno(status);
  745. goto bail;
  746. }
  747. handle = ocfs2_start_trans(osb, OCFS2_WINDOW_MOVE_CREDITS);
  748. if (IS_ERR(handle)) {
  749. status = PTR_ERR(handle);
  750. handle = NULL;
  751. mlog_errno(status);
  752. goto bail;
  753. }
  754. alloc = (struct ocfs2_dinode *) osb->local_alloc_bh->b_data;
  755. /* We want to clear the local alloc before doing anything
  756. * else, so that if we error later during this operation,
  757. * local alloc shutdown won't try to double free main bitmap
  758. * bits. Make a copy so the sync function knows which bits to
  759. * free. */
  760. alloc_copy = kmalloc(osb->local_alloc_bh->b_size, GFP_KERNEL);
  761. if (!alloc_copy) {
  762. status = -ENOMEM;
  763. mlog_errno(status);
  764. goto bail;
  765. }
  766. memcpy(alloc_copy, alloc, osb->local_alloc_bh->b_size);
  767. status = ocfs2_journal_access(handle, local_alloc_inode,
  768. osb->local_alloc_bh,
  769. OCFS2_JOURNAL_ACCESS_WRITE);
  770. if (status < 0) {
  771. mlog_errno(status);
  772. goto bail;
  773. }
  774. ocfs2_clear_local_alloc(alloc);
  775. status = ocfs2_journal_dirty(handle, osb->local_alloc_bh);
  776. if (status < 0) {
  777. mlog_errno(status);
  778. goto bail;
  779. }
  780. status = ocfs2_sync_local_to_main(osb, handle, alloc_copy,
  781. main_bm_inode, main_bm_bh);
  782. if (status < 0) {
  783. mlog_errno(status);
  784. goto bail;
  785. }
  786. status = ocfs2_local_alloc_new_window(osb, handle, ac);
  787. if (status < 0) {
  788. if (status != -ENOSPC)
  789. mlog_errno(status);
  790. goto bail;
  791. }
  792. atomic_inc(&osb->alloc_stats.moves);
  793. status = 0;
  794. bail:
  795. if (handle)
  796. ocfs2_commit_trans(osb, handle);
  797. if (main_bm_bh)
  798. brelse(main_bm_bh);
  799. if (main_bm_inode)
  800. iput(main_bm_inode);
  801. if (alloc_copy)
  802. kfree(alloc_copy);
  803. if (ac)
  804. ocfs2_free_alloc_context(ac);
  805. mlog_exit(status);
  806. return status;
  807. }