localalloc.c 24 KB

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