reservations.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * reservations.c
  5. *
  6. * Allocation reservations implementation
  7. *
  8. * Some code borrowed from fs/ext3/balloc.c and is:
  9. *
  10. * Copyright (C) 1992, 1993, 1994, 1995
  11. * Remy Card (card@masi.ibp.fr)
  12. * Laboratoire MASI - Institut Blaise Pascal
  13. * Universite Pierre et Marie Curie (Paris VI)
  14. *
  15. * The rest is copyright (C) 2010 Novell. All rights reserved.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public
  19. * License version 2 as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  24. * General Public License for more details.
  25. */
  26. #include <linux/fs.h>
  27. #include <linux/types.h>
  28. #include <linux/slab.h>
  29. #include <linux/highmem.h>
  30. #include <linux/bitops.h>
  31. #include <linux/list.h>
  32. #define MLOG_MASK_PREFIX ML_RESERVATIONS
  33. #include <cluster/masklog.h>
  34. #include "ocfs2.h"
  35. #ifdef CONFIG_OCFS2_DEBUG_FS
  36. #define OCFS2_CHECK_RESERVATIONS
  37. #endif
  38. DEFINE_SPINLOCK(resv_lock);
  39. #define OCFS2_MIN_RESV_WINDOW_BITS 8
  40. #define OCFS2_MAX_RESV_WINDOW_BITS 1024
  41. #define OCFS2_RESV_DIR_WINDOW_BITS OCFS2_MIN_RESV_WINDOW_BITS
  42. static unsigned int ocfs2_resv_window_bits(struct ocfs2_reservation_map *resmap,
  43. struct ocfs2_alloc_reservation *resv)
  44. {
  45. struct ocfs2_super *osb = resmap->m_osb;
  46. unsigned int bits;
  47. if (!(resv->r_flags & OCFS2_RESV_FLAG_DIR)) {
  48. /* 8, 16, 32, 64, 128, 256, 512, 1024 */
  49. bits = 4 << osb->osb_resv_level;
  50. } else {
  51. /* For now, treat directories the same as files. */
  52. bits = 4 << osb->osb_resv_level;
  53. }
  54. return bits;
  55. }
  56. static inline unsigned int ocfs2_resv_end(struct ocfs2_alloc_reservation *resv)
  57. {
  58. if (resv->r_len)
  59. return resv->r_start + resv->r_len - 1;
  60. return resv->r_start;
  61. }
  62. static inline int ocfs2_resv_empty(struct ocfs2_alloc_reservation *resv)
  63. {
  64. return !!(resv->r_len == 0);
  65. }
  66. static inline int ocfs2_resmap_disabled(struct ocfs2_reservation_map *resmap)
  67. {
  68. if (resmap->m_osb->osb_resv_level == 0)
  69. return 1;
  70. return 0;
  71. }
  72. static void ocfs2_dump_resv(struct ocfs2_reservation_map *resmap)
  73. {
  74. struct ocfs2_super *osb = resmap->m_osb;
  75. struct rb_node *node;
  76. struct ocfs2_alloc_reservation *resv;
  77. int i = 0;
  78. mlog(ML_NOTICE, "Dumping resmap for device %s. Bitmap length: %u\n",
  79. osb->dev_str, resmap->m_bitmap_len);
  80. node = rb_first(&resmap->m_reservations);
  81. while (node) {
  82. resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
  83. mlog(ML_NOTICE, "start: %u\tend: %u\tlen: %u\tlast_start: %u"
  84. "\tlast_len: %u\n", resv->r_start,
  85. ocfs2_resv_end(resv), resv->r_len, resv->r_last_start,
  86. resv->r_last_len);
  87. node = rb_next(node);
  88. i++;
  89. }
  90. mlog(ML_NOTICE, "%d reservations found. LRU follows\n", i);
  91. i = 0;
  92. list_for_each_entry(resv, &resmap->m_lru, r_lru) {
  93. mlog(ML_NOTICE, "LRU(%d) start: %u\tend: %u\tlen: %u\t"
  94. "last_start: %u\tlast_len: %u\n", i, resv->r_start,
  95. ocfs2_resv_end(resv), resv->r_len, resv->r_last_start,
  96. resv->r_last_len);
  97. i++;
  98. }
  99. }
  100. #ifdef OCFS2_CHECK_RESERVATIONS
  101. static int ocfs2_validate_resmap_bits(struct ocfs2_reservation_map *resmap,
  102. int i,
  103. struct ocfs2_alloc_reservation *resv)
  104. {
  105. char *disk_bitmap = resmap->m_disk_bitmap;
  106. unsigned int start = resv->r_start;
  107. unsigned int end = ocfs2_resv_end(resv);
  108. while (start <= end) {
  109. if (ocfs2_test_bit(start, disk_bitmap)) {
  110. mlog(ML_ERROR,
  111. "reservation %d covers an allocated area "
  112. "starting at bit %u!\n", i, start);
  113. return 1;
  114. }
  115. start++;
  116. }
  117. return 0;
  118. }
  119. static void ocfs2_check_resmap(struct ocfs2_reservation_map *resmap)
  120. {
  121. unsigned int off = 0;
  122. int i = 0;
  123. struct rb_node *node;
  124. struct ocfs2_alloc_reservation *resv;
  125. node = rb_first(&resmap->m_reservations);
  126. while (node) {
  127. resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
  128. if (i > 0 && resv->r_start <= off) {
  129. mlog(ML_ERROR, "reservation %d has bad start off!\n",
  130. i);
  131. goto bad;
  132. }
  133. if (resv->r_len == 0) {
  134. mlog(ML_ERROR, "reservation %d has no length!\n",
  135. i);
  136. goto bad;
  137. }
  138. if (resv->r_start > ocfs2_resv_end(resv)) {
  139. mlog(ML_ERROR, "reservation %d has invalid range!\n",
  140. i);
  141. goto bad;
  142. }
  143. if (ocfs2_resv_end(resv) >= resmap->m_bitmap_len) {
  144. mlog(ML_ERROR, "reservation %d extends past bitmap!\n",
  145. i);
  146. goto bad;
  147. }
  148. if (ocfs2_validate_resmap_bits(resmap, i, resv))
  149. goto bad;
  150. off = ocfs2_resv_end(resv);
  151. node = rb_next(node);
  152. i++;
  153. }
  154. return;
  155. bad:
  156. ocfs2_dump_resv(resmap);
  157. BUG();
  158. }
  159. #else
  160. static inline void ocfs2_check_resmap(struct ocfs2_reservation_map *resmap)
  161. {
  162. }
  163. #endif
  164. void ocfs2_resv_init_once(struct ocfs2_alloc_reservation *resv)
  165. {
  166. memset(resv, 0, sizeof(*resv));
  167. INIT_LIST_HEAD(&resv->r_lru);
  168. }
  169. void ocfs2_resv_set_type(struct ocfs2_alloc_reservation *resv,
  170. unsigned int flags)
  171. {
  172. BUG_ON(flags & ~OCFS2_RESV_TYPES);
  173. resv->r_flags |= flags;
  174. }
  175. int ocfs2_resmap_init(struct ocfs2_super *osb,
  176. struct ocfs2_reservation_map *resmap)
  177. {
  178. memset(resmap, 0, sizeof(*resmap));
  179. resmap->m_osb = osb;
  180. resmap->m_reservations = RB_ROOT;
  181. /* m_bitmap_len is initialized to zero by the above memset. */
  182. INIT_LIST_HEAD(&resmap->m_lru);
  183. return 0;
  184. }
  185. static void ocfs2_resv_mark_lru(struct ocfs2_reservation_map *resmap,
  186. struct ocfs2_alloc_reservation *resv)
  187. {
  188. assert_spin_locked(&resv_lock);
  189. if (!list_empty(&resv->r_lru))
  190. list_del_init(&resv->r_lru);
  191. list_add_tail(&resv->r_lru, &resmap->m_lru);
  192. }
  193. static void __ocfs2_resv_trunc(struct ocfs2_alloc_reservation *resv)
  194. {
  195. resv->r_len = 0;
  196. resv->r_start = 0;
  197. }
  198. static void ocfs2_resv_remove(struct ocfs2_reservation_map *resmap,
  199. struct ocfs2_alloc_reservation *resv)
  200. {
  201. if (resv->r_flags & OCFS2_RESV_FLAG_INUSE) {
  202. list_del_init(&resv->r_lru);
  203. rb_erase(&resv->r_node, &resmap->m_reservations);
  204. resv->r_flags &= ~OCFS2_RESV_FLAG_INUSE;
  205. }
  206. }
  207. static void __ocfs2_resv_discard(struct ocfs2_reservation_map *resmap,
  208. struct ocfs2_alloc_reservation *resv)
  209. {
  210. assert_spin_locked(&resv_lock);
  211. __ocfs2_resv_trunc(resv);
  212. /*
  213. * last_len and last_start no longer make sense if
  214. * we're changing the range of our allocations.
  215. */
  216. resv->r_last_len = resv->r_last_start = 0;
  217. ocfs2_resv_remove(resmap, resv);
  218. }
  219. /* does nothing if 'resv' is null */
  220. void ocfs2_resv_discard(struct ocfs2_reservation_map *resmap,
  221. struct ocfs2_alloc_reservation *resv)
  222. {
  223. if (resv) {
  224. spin_lock(&resv_lock);
  225. __ocfs2_resv_discard(resmap, resv);
  226. spin_unlock(&resv_lock);
  227. }
  228. }
  229. static void ocfs2_resmap_clear_all_resv(struct ocfs2_reservation_map *resmap)
  230. {
  231. struct rb_node *node;
  232. struct ocfs2_alloc_reservation *resv;
  233. assert_spin_locked(&resv_lock);
  234. while ((node = rb_last(&resmap->m_reservations)) != NULL) {
  235. resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
  236. __ocfs2_resv_discard(resmap, resv);
  237. }
  238. }
  239. void ocfs2_resmap_restart(struct ocfs2_reservation_map *resmap,
  240. unsigned int clen, char *disk_bitmap)
  241. {
  242. if (ocfs2_resmap_disabled(resmap))
  243. return;
  244. spin_lock(&resv_lock);
  245. ocfs2_resmap_clear_all_resv(resmap);
  246. resmap->m_bitmap_len = clen;
  247. resmap->m_disk_bitmap = disk_bitmap;
  248. spin_unlock(&resv_lock);
  249. }
  250. void ocfs2_resmap_uninit(struct ocfs2_reservation_map *resmap)
  251. {
  252. /* Does nothing for now. Keep this around for API symmetry */
  253. }
  254. static void ocfs2_resv_insert(struct ocfs2_reservation_map *resmap,
  255. struct ocfs2_alloc_reservation *new)
  256. {
  257. struct rb_root *root = &resmap->m_reservations;
  258. struct rb_node *parent = NULL;
  259. struct rb_node **p = &root->rb_node;
  260. struct ocfs2_alloc_reservation *tmp;
  261. assert_spin_locked(&resv_lock);
  262. mlog(0, "Insert reservation start: %u len: %u\n", new->r_start,
  263. new->r_len);
  264. while (*p) {
  265. parent = *p;
  266. tmp = rb_entry(parent, struct ocfs2_alloc_reservation, r_node);
  267. if (new->r_start < tmp->r_start) {
  268. p = &(*p)->rb_left;
  269. /*
  270. * This is a good place to check for
  271. * overlapping reservations.
  272. */
  273. BUG_ON(ocfs2_resv_end(new) >= tmp->r_start);
  274. } else if (new->r_start > ocfs2_resv_end(tmp)) {
  275. p = &(*p)->rb_right;
  276. } else {
  277. /* This should never happen! */
  278. mlog(ML_ERROR, "Duplicate reservation window!\n");
  279. BUG();
  280. }
  281. }
  282. rb_link_node(&new->r_node, parent, p);
  283. rb_insert_color(&new->r_node, root);
  284. new->r_flags |= OCFS2_RESV_FLAG_INUSE;
  285. ocfs2_resv_mark_lru(resmap, new);
  286. ocfs2_check_resmap(resmap);
  287. }
  288. /**
  289. * ocfs2_find_resv_lhs() - find the window which contains goal
  290. * @resmap: reservation map to search
  291. * @goal: which bit to search for
  292. *
  293. * If a window containing that goal is not found, we return the window
  294. * which comes before goal. Returns NULL on empty rbtree or no window
  295. * before goal.
  296. */
  297. static struct ocfs2_alloc_reservation *
  298. ocfs2_find_resv_lhs(struct ocfs2_reservation_map *resmap, unsigned int goal)
  299. {
  300. struct ocfs2_alloc_reservation *resv = NULL;
  301. struct ocfs2_alloc_reservation *prev_resv = NULL;
  302. struct rb_node *node = resmap->m_reservations.rb_node;
  303. struct rb_node *prev = NULL;
  304. assert_spin_locked(&resv_lock);
  305. if (!node)
  306. return NULL;
  307. node = rb_first(&resmap->m_reservations);
  308. while (node) {
  309. resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
  310. if (resv->r_start <= goal && ocfs2_resv_end(resv) >= goal)
  311. break;
  312. /* Check if we overshot the reservation just before goal? */
  313. if (resv->r_start > goal) {
  314. resv = prev_resv;
  315. break;
  316. }
  317. prev_resv = resv;
  318. prev = node;
  319. node = rb_next(node);
  320. }
  321. return resv;
  322. }
  323. /*
  324. * We are given a range within the bitmap, which corresponds to a gap
  325. * inside the reservations tree (search_start, search_len). The range
  326. * can be anything from the whole bitmap, to a gap between
  327. * reservations.
  328. *
  329. * The start value of *rstart is insignificant.
  330. *
  331. * This function searches the bitmap range starting at search_start
  332. * with length csearch_len for a set of contiguous free bits. We try
  333. * to find up to 'wanted' bits, but can sometimes return less.
  334. *
  335. * Returns the length of allocation, 0 if no free bits are found.
  336. *
  337. * *cstart and *clen will also be populated with the result.
  338. */
  339. static int ocfs2_resmap_find_free_bits(struct ocfs2_reservation_map *resmap,
  340. unsigned int wanted,
  341. unsigned int search_start,
  342. unsigned int search_len,
  343. unsigned int *rstart,
  344. unsigned int *rlen)
  345. {
  346. void *bitmap = resmap->m_disk_bitmap;
  347. unsigned int best_start, best_len = 0;
  348. int offset, start, found;
  349. mlog(0, "Find %u bits within range (%u, len %u) resmap len: %u\n",
  350. wanted, search_start, search_len, resmap->m_bitmap_len);
  351. found = best_start = best_len = 0;
  352. start = search_start;
  353. while ((offset = ocfs2_find_next_zero_bit(bitmap, resmap->m_bitmap_len,
  354. start)) != -1) {
  355. /* Search reached end of the region */
  356. if (offset >= (search_start + search_len))
  357. break;
  358. if (offset == start) {
  359. /* we found a zero */
  360. found++;
  361. /* move start to the next bit to test */
  362. start++;
  363. } else {
  364. /* got a zero after some ones */
  365. found = 1;
  366. start = offset + 1;
  367. }
  368. if (found > best_len) {
  369. best_len = found;
  370. best_start = start - found;
  371. }
  372. if (found >= wanted)
  373. break;
  374. }
  375. if (best_len == 0)
  376. return 0;
  377. if (best_len >= wanted)
  378. best_len = wanted;
  379. *rlen = best_len;
  380. *rstart = best_start;
  381. mlog(0, "Found start: %u len: %u\n", best_start, best_len);
  382. return *rlen;
  383. }
  384. static void __ocfs2_resv_find_window(struct ocfs2_reservation_map *resmap,
  385. struct ocfs2_alloc_reservation *resv,
  386. unsigned int goal, unsigned int wanted)
  387. {
  388. struct rb_root *root = &resmap->m_reservations;
  389. unsigned int gap_start, gap_end, gap_len;
  390. struct ocfs2_alloc_reservation *prev_resv, *next_resv;
  391. struct rb_node *prev, *next;
  392. unsigned int cstart, clen;
  393. unsigned int best_start = 0, best_len = 0;
  394. /*
  395. * Nasty cases to consider:
  396. *
  397. * - rbtree is empty
  398. * - our window should be first in all reservations
  399. * - our window should be last in all reservations
  400. * - need to make sure we don't go past end of bitmap
  401. */
  402. mlog(0, "resv start: %u resv end: %u goal: %u wanted: %u\n",
  403. resv->r_start, ocfs2_resv_end(resv), goal, wanted);
  404. assert_spin_locked(&resv_lock);
  405. if (RB_EMPTY_ROOT(root)) {
  406. /*
  407. * Easiest case - empty tree. We can just take
  408. * whatever window of free bits we want.
  409. */
  410. mlog(0, "Empty root\n");
  411. clen = ocfs2_resmap_find_free_bits(resmap, wanted, goal,
  412. resmap->m_bitmap_len - goal,
  413. &cstart, &clen);
  414. /*
  415. * This should never happen - the local alloc window
  416. * will always have free bits when we're called.
  417. */
  418. BUG_ON(goal == 0 && clen == 0);
  419. if (clen == 0)
  420. return;
  421. resv->r_start = cstart;
  422. resv->r_len = clen;
  423. ocfs2_resv_insert(resmap, resv);
  424. return;
  425. }
  426. prev_resv = ocfs2_find_resv_lhs(resmap, goal);
  427. if (prev_resv == NULL) {
  428. mlog(0, "Goal on LHS of leftmost window\n");
  429. /*
  430. * A NULL here means that the search code couldn't
  431. * find a window that starts before goal.
  432. *
  433. * However, we can take the first window after goal,
  434. * which is also by definition, the leftmost window in
  435. * the entire tree. If we can find free bits in the
  436. * gap between goal and the LHS window, then the
  437. * reservation can safely be placed there.
  438. *
  439. * Otherwise we fall back to a linear search, checking
  440. * the gaps in between windows for a place to
  441. * allocate.
  442. */
  443. next = rb_first(root);
  444. next_resv = rb_entry(next, struct ocfs2_alloc_reservation,
  445. r_node);
  446. /*
  447. * The search should never return such a window. (see
  448. * comment above
  449. */
  450. if (next_resv->r_start <= goal) {
  451. mlog(ML_ERROR, "goal: %u next_resv: start %u len %u\n",
  452. goal, next_resv->r_start, next_resv->r_len);
  453. ocfs2_dump_resv(resmap);
  454. BUG();
  455. }
  456. clen = ocfs2_resmap_find_free_bits(resmap, wanted, goal,
  457. next_resv->r_start - goal,
  458. &cstart, &clen);
  459. if (clen) {
  460. best_len = clen;
  461. best_start = cstart;
  462. if (best_len == wanted)
  463. goto out_insert;
  464. }
  465. prev_resv = next_resv;
  466. next_resv = NULL;
  467. }
  468. prev = &prev_resv->r_node;
  469. /* Now we do a linear search for a window, starting at 'prev_rsv' */
  470. while (1) {
  471. next = rb_next(prev);
  472. if (next) {
  473. mlog(0, "One more resv found in linear search\n");
  474. next_resv = rb_entry(next,
  475. struct ocfs2_alloc_reservation,
  476. r_node);
  477. gap_start = ocfs2_resv_end(prev_resv) + 1;
  478. gap_end = next_resv->r_start - 1;
  479. gap_len = gap_end - gap_start + 1;
  480. } else {
  481. mlog(0, "No next node\n");
  482. /*
  483. * We're at the rightmost edge of the
  484. * tree. See if a reservation between this
  485. * window and the end of the bitmap will work.
  486. */
  487. gap_start = ocfs2_resv_end(prev_resv) + 1;
  488. gap_len = resmap->m_bitmap_len - gap_start;
  489. gap_end = resmap->m_bitmap_len - 1;
  490. }
  491. /*
  492. * No need to check this gap if we have already found
  493. * a larger region of free bits.
  494. */
  495. if (gap_len <= best_len)
  496. goto next_resv;
  497. clen = ocfs2_resmap_find_free_bits(resmap, wanted, gap_start,
  498. gap_len, &cstart, &clen);
  499. if (clen == wanted) {
  500. best_len = clen;
  501. best_start = cstart;
  502. goto out_insert;
  503. } else if (clen > best_len) {
  504. best_len = clen;
  505. best_start = cstart;
  506. }
  507. next_resv:
  508. if (!next)
  509. break;
  510. prev = next;
  511. prev_resv = rb_entry(prev, struct ocfs2_alloc_reservation,
  512. r_node);
  513. }
  514. out_insert:
  515. if (best_len) {
  516. resv->r_start = best_start;
  517. resv->r_len = best_len;
  518. ocfs2_resv_insert(resmap, resv);
  519. }
  520. }
  521. static void ocfs2_cannibalize_resv(struct ocfs2_reservation_map *resmap,
  522. struct ocfs2_alloc_reservation *resv,
  523. unsigned int wanted)
  524. {
  525. struct ocfs2_alloc_reservation *lru_resv;
  526. int tmpwindow = !!(resv->r_flags & OCFS2_RESV_FLAG_TMP);
  527. unsigned int min_bits;
  528. if (!tmpwindow)
  529. min_bits = ocfs2_resv_window_bits(resmap, resv) >> 1;
  530. else
  531. min_bits = wanted; /* We at know the temp window will use all
  532. * of these bits */
  533. /*
  534. * Take the first reservation off the LRU as our 'target'. We
  535. * don't try to be smart about it. There might be a case for
  536. * searching based on size but I don't have enough data to be
  537. * sure. --Mark (3/16/2010)
  538. */
  539. lru_resv = list_first_entry(&resmap->m_lru,
  540. struct ocfs2_alloc_reservation, r_lru);
  541. mlog(0, "lru resv: start: %u len: %u end: %u\n", lru_resv->r_start,
  542. lru_resv->r_len, ocfs2_resv_end(lru_resv));
  543. /*
  544. * Cannibalize (some or all) of the target reservation and
  545. * feed it to the current window.
  546. */
  547. if (lru_resv->r_len <= min_bits) {
  548. /*
  549. * Discard completely if size is less than or equal to a
  550. * reasonable threshold - 50% of window bits for non temporary
  551. * windows.
  552. */
  553. resv->r_start = lru_resv->r_start;
  554. resv->r_len = lru_resv->r_len;
  555. __ocfs2_resv_discard(resmap, lru_resv);
  556. } else {
  557. unsigned int shrink;
  558. if (tmpwindow)
  559. shrink = min_bits;
  560. else
  561. shrink = lru_resv->r_len / 2;
  562. lru_resv->r_len -= shrink;
  563. resv->r_start = ocfs2_resv_end(lru_resv) + 1;
  564. resv->r_len = shrink;
  565. }
  566. mlog(0, "Reservation now looks like: r_start: %u r_end: %u "
  567. "r_len: %u r_last_start: %u r_last_len: %u\n",
  568. resv->r_start, ocfs2_resv_end(resv), resv->r_len,
  569. resv->r_last_start, resv->r_last_len);
  570. ocfs2_resv_insert(resmap, resv);
  571. }
  572. static void ocfs2_resv_find_window(struct ocfs2_reservation_map *resmap,
  573. struct ocfs2_alloc_reservation *resv,
  574. unsigned int wanted)
  575. {
  576. unsigned int goal = 0;
  577. BUG_ON(!ocfs2_resv_empty(resv));
  578. /*
  579. * Begin by trying to get a window as close to the previous
  580. * one as possible. Using the most recent allocation as a
  581. * start goal makes sense.
  582. */
  583. if (resv->r_last_len) {
  584. goal = resv->r_last_start + resv->r_last_len;
  585. if (goal >= resmap->m_bitmap_len)
  586. goal = 0;
  587. }
  588. __ocfs2_resv_find_window(resmap, resv, goal, wanted);
  589. /* Search from last alloc didn't work, try once more from beginning. */
  590. if (ocfs2_resv_empty(resv) && goal != 0)
  591. __ocfs2_resv_find_window(resmap, resv, 0, wanted);
  592. if (ocfs2_resv_empty(resv)) {
  593. /*
  594. * Still empty? Pull oldest one off the LRU, remove it from
  595. * tree, put this one in it's place.
  596. */
  597. ocfs2_cannibalize_resv(resmap, resv, wanted);
  598. }
  599. BUG_ON(ocfs2_resv_empty(resv));
  600. }
  601. int ocfs2_resmap_resv_bits(struct ocfs2_reservation_map *resmap,
  602. struct ocfs2_alloc_reservation *resv,
  603. int *cstart, int *clen)
  604. {
  605. unsigned int wanted = *clen;
  606. if (resv == NULL || ocfs2_resmap_disabled(resmap))
  607. return -ENOSPC;
  608. spin_lock(&resv_lock);
  609. /*
  610. * We don't want to over-allocate for temporary
  611. * windows. Otherwise, we run the risk of fragmenting the
  612. * allocation space.
  613. */
  614. wanted = ocfs2_resv_window_bits(resmap, resv);
  615. if ((resv->r_flags & OCFS2_RESV_FLAG_TMP) || wanted < *clen)
  616. wanted = *clen;
  617. if (ocfs2_resv_empty(resv)) {
  618. mlog(0, "empty reservation, find new window\n");
  619. /*
  620. * Try to get a window here. If it works, we must fall
  621. * through and test the bitmap . This avoids some
  622. * ping-ponging of windows due to non-reserved space
  623. * being allocation before we initialize a window for
  624. * that inode.
  625. */
  626. ocfs2_resv_find_window(resmap, resv, wanted);
  627. }
  628. BUG_ON(ocfs2_resv_empty(resv));
  629. *cstart = resv->r_start;
  630. *clen = resv->r_len;
  631. spin_unlock(&resv_lock);
  632. return 0;
  633. }
  634. static void
  635. ocfs2_adjust_resv_from_alloc(struct ocfs2_reservation_map *resmap,
  636. struct ocfs2_alloc_reservation *resv,
  637. unsigned int start, unsigned int end)
  638. {
  639. unsigned int lhs = 0, rhs = 0;
  640. BUG_ON(start < resv->r_start);
  641. /*
  642. * Completely used? We can remove it then.
  643. */
  644. if (ocfs2_resv_end(resv) <= end && resv->r_start >= start) {
  645. __ocfs2_resv_discard(resmap, resv);
  646. return;
  647. }
  648. if (end < ocfs2_resv_end(resv))
  649. rhs = end - ocfs2_resv_end(resv);
  650. if (start > resv->r_start)
  651. lhs = start - resv->r_start;
  652. /*
  653. * This should have been trapped above. At the very least, rhs
  654. * should be non zero.
  655. */
  656. BUG_ON(rhs == 0 && lhs == 0);
  657. if (rhs >= lhs) {
  658. unsigned int old_end = ocfs2_resv_end(resv);
  659. resv->r_start = end + 1;
  660. resv->r_len = old_end - resv->r_start + 1;
  661. } else {
  662. resv->r_len = start - resv->r_start;
  663. }
  664. }
  665. void ocfs2_resmap_claimed_bits(struct ocfs2_reservation_map *resmap,
  666. struct ocfs2_alloc_reservation *resv,
  667. u32 cstart, u32 clen)
  668. {
  669. unsigned int cend = cstart + clen - 1;
  670. if (resmap == NULL || ocfs2_resmap_disabled(resmap))
  671. return;
  672. if (resv == NULL)
  673. return;
  674. spin_lock(&resv_lock);
  675. mlog(0, "claim bits: cstart: %u cend: %u clen: %u r_start: %u "
  676. "r_end: %u r_len: %u, r_last_start: %u r_last_len: %u\n",
  677. cstart, cend, clen, resv->r_start, ocfs2_resv_end(resv),
  678. resv->r_len, resv->r_last_start, resv->r_last_len);
  679. BUG_ON(cstart < resv->r_start);
  680. BUG_ON(cstart > ocfs2_resv_end(resv));
  681. BUG_ON(cend > ocfs2_resv_end(resv));
  682. ocfs2_adjust_resv_from_alloc(resmap, resv, cstart, cend);
  683. resv->r_last_start = cstart;
  684. resv->r_last_len = clen;
  685. /*
  686. * May have been discarded above from
  687. * ocfs2_adjust_resv_from_alloc().
  688. */
  689. if (!ocfs2_resv_empty(resv))
  690. ocfs2_resv_mark_lru(resmap, resv);
  691. mlog(0, "Reservation now looks like: r_start: %u r_end: %u "
  692. "r_len: %u r_last_start: %u r_last_len: %u\n",
  693. resv->r_start, ocfs2_resv_end(resv), resv->r_len,
  694. resv->r_last_start, resv->r_last_len);
  695. ocfs2_check_resmap(resmap);
  696. spin_unlock(&resv_lock);
  697. }