localalloc.c 23 KB

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