localalloc.c 24 KB

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