reservations.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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. int ocfs2_dir_resv_allowed(struct ocfs2_super *osb)
  42. {
  43. return (osb->osb_resv_level && osb->osb_dir_resv_level);
  44. }
  45. static unsigned int ocfs2_resv_window_bits(struct ocfs2_reservation_map *resmap,
  46. struct ocfs2_alloc_reservation *resv)
  47. {
  48. struct ocfs2_super *osb = resmap->m_osb;
  49. unsigned int bits;
  50. if (!(resv->r_flags & OCFS2_RESV_FLAG_DIR)) {
  51. /* 8, 16, 32, 64, 128, 256, 512, 1024 */
  52. bits = 4 << osb->osb_resv_level;
  53. } else {
  54. bits = 4 << osb->osb_dir_resv_level;
  55. }
  56. return bits;
  57. }
  58. static inline unsigned int ocfs2_resv_end(struct ocfs2_alloc_reservation *resv)
  59. {
  60. if (resv->r_len)
  61. return resv->r_start + resv->r_len - 1;
  62. return resv->r_start;
  63. }
  64. static inline int ocfs2_resv_empty(struct ocfs2_alloc_reservation *resv)
  65. {
  66. return !!(resv->r_len == 0);
  67. }
  68. static inline int ocfs2_resmap_disabled(struct ocfs2_reservation_map *resmap)
  69. {
  70. if (resmap->m_osb->osb_resv_level == 0)
  71. return 1;
  72. return 0;
  73. }
  74. static void ocfs2_dump_resv(struct ocfs2_reservation_map *resmap)
  75. {
  76. struct ocfs2_super *osb = resmap->m_osb;
  77. struct rb_node *node;
  78. struct ocfs2_alloc_reservation *resv;
  79. int i = 0;
  80. mlog(ML_NOTICE, "Dumping resmap for device %s. Bitmap length: %u\n",
  81. osb->dev_str, resmap->m_bitmap_len);
  82. node = rb_first(&resmap->m_reservations);
  83. while (node) {
  84. resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
  85. mlog(ML_NOTICE, "start: %u\tend: %u\tlen: %u\tlast_start: %u"
  86. "\tlast_len: %u\n", resv->r_start,
  87. ocfs2_resv_end(resv), resv->r_len, resv->r_last_start,
  88. resv->r_last_len);
  89. node = rb_next(node);
  90. i++;
  91. }
  92. mlog(ML_NOTICE, "%d reservations found. LRU follows\n", i);
  93. i = 0;
  94. list_for_each_entry(resv, &resmap->m_lru, r_lru) {
  95. mlog(ML_NOTICE, "LRU(%d) start: %u\tend: %u\tlen: %u\t"
  96. "last_start: %u\tlast_len: %u\n", i, resv->r_start,
  97. ocfs2_resv_end(resv), resv->r_len, resv->r_last_start,
  98. resv->r_last_len);
  99. i++;
  100. }
  101. }
  102. #ifdef OCFS2_CHECK_RESERVATIONS
  103. static int ocfs2_validate_resmap_bits(struct ocfs2_reservation_map *resmap,
  104. int i,
  105. struct ocfs2_alloc_reservation *resv)
  106. {
  107. char *disk_bitmap = resmap->m_disk_bitmap;
  108. unsigned int start = resv->r_start;
  109. unsigned int end = ocfs2_resv_end(resv);
  110. while (start <= end) {
  111. if (ocfs2_test_bit(start, disk_bitmap)) {
  112. mlog(ML_ERROR,
  113. "reservation %d covers an allocated area "
  114. "starting at bit %u!\n", i, start);
  115. return 1;
  116. }
  117. start++;
  118. }
  119. return 0;
  120. }
  121. static void ocfs2_check_resmap(struct ocfs2_reservation_map *resmap)
  122. {
  123. unsigned int off = 0;
  124. int i = 0;
  125. struct rb_node *node;
  126. struct ocfs2_alloc_reservation *resv;
  127. node = rb_first(&resmap->m_reservations);
  128. while (node) {
  129. resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
  130. if (i > 0 && resv->r_start <= off) {
  131. mlog(ML_ERROR, "reservation %d has bad start off!\n",
  132. i);
  133. goto bad;
  134. }
  135. if (resv->r_len == 0) {
  136. mlog(ML_ERROR, "reservation %d has no length!\n",
  137. i);
  138. goto bad;
  139. }
  140. if (resv->r_start > ocfs2_resv_end(resv)) {
  141. mlog(ML_ERROR, "reservation %d has invalid range!\n",
  142. i);
  143. goto bad;
  144. }
  145. if (ocfs2_resv_end(resv) >= resmap->m_bitmap_len) {
  146. mlog(ML_ERROR, "reservation %d extends past bitmap!\n",
  147. i);
  148. goto bad;
  149. }
  150. if (ocfs2_validate_resmap_bits(resmap, i, resv))
  151. goto bad;
  152. off = ocfs2_resv_end(resv);
  153. node = rb_next(node);
  154. i++;
  155. }
  156. return;
  157. bad:
  158. ocfs2_dump_resv(resmap);
  159. BUG();
  160. }
  161. #else
  162. static inline void ocfs2_check_resmap(struct ocfs2_reservation_map *resmap)
  163. {
  164. }
  165. #endif
  166. void ocfs2_resv_init_once(struct ocfs2_alloc_reservation *resv)
  167. {
  168. memset(resv, 0, sizeof(*resv));
  169. INIT_LIST_HEAD(&resv->r_lru);
  170. }
  171. void ocfs2_resv_set_type(struct ocfs2_alloc_reservation *resv,
  172. unsigned int flags)
  173. {
  174. BUG_ON(flags & ~OCFS2_RESV_TYPES);
  175. resv->r_flags |= flags;
  176. }
  177. int ocfs2_resmap_init(struct ocfs2_super *osb,
  178. struct ocfs2_reservation_map *resmap)
  179. {
  180. memset(resmap, 0, sizeof(*resmap));
  181. resmap->m_osb = osb;
  182. resmap->m_reservations = RB_ROOT;
  183. /* m_bitmap_len is initialized to zero by the above memset. */
  184. INIT_LIST_HEAD(&resmap->m_lru);
  185. return 0;
  186. }
  187. static void ocfs2_resv_mark_lru(struct ocfs2_reservation_map *resmap,
  188. struct ocfs2_alloc_reservation *resv)
  189. {
  190. assert_spin_locked(&resv_lock);
  191. if (!list_empty(&resv->r_lru))
  192. list_del_init(&resv->r_lru);
  193. list_add_tail(&resv->r_lru, &resmap->m_lru);
  194. }
  195. static void __ocfs2_resv_trunc(struct ocfs2_alloc_reservation *resv)
  196. {
  197. resv->r_len = 0;
  198. resv->r_start = 0;
  199. }
  200. static void ocfs2_resv_remove(struct ocfs2_reservation_map *resmap,
  201. struct ocfs2_alloc_reservation *resv)
  202. {
  203. if (resv->r_flags & OCFS2_RESV_FLAG_INUSE) {
  204. list_del_init(&resv->r_lru);
  205. rb_erase(&resv->r_node, &resmap->m_reservations);
  206. resv->r_flags &= ~OCFS2_RESV_FLAG_INUSE;
  207. }
  208. }
  209. static void __ocfs2_resv_discard(struct ocfs2_reservation_map *resmap,
  210. struct ocfs2_alloc_reservation *resv)
  211. {
  212. assert_spin_locked(&resv_lock);
  213. __ocfs2_resv_trunc(resv);
  214. /*
  215. * last_len and last_start no longer make sense if
  216. * we're changing the range of our allocations.
  217. */
  218. resv->r_last_len = resv->r_last_start = 0;
  219. ocfs2_resv_remove(resmap, resv);
  220. }
  221. /* does nothing if 'resv' is null */
  222. void ocfs2_resv_discard(struct ocfs2_reservation_map *resmap,
  223. struct ocfs2_alloc_reservation *resv)
  224. {
  225. if (resv) {
  226. spin_lock(&resv_lock);
  227. __ocfs2_resv_discard(resmap, resv);
  228. spin_unlock(&resv_lock);
  229. }
  230. }
  231. static void ocfs2_resmap_clear_all_resv(struct ocfs2_reservation_map *resmap)
  232. {
  233. struct rb_node *node;
  234. struct ocfs2_alloc_reservation *resv;
  235. assert_spin_locked(&resv_lock);
  236. while ((node = rb_last(&resmap->m_reservations)) != NULL) {
  237. resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
  238. __ocfs2_resv_discard(resmap, resv);
  239. }
  240. }
  241. void ocfs2_resmap_restart(struct ocfs2_reservation_map *resmap,
  242. unsigned int clen, char *disk_bitmap)
  243. {
  244. if (ocfs2_resmap_disabled(resmap))
  245. return;
  246. spin_lock(&resv_lock);
  247. ocfs2_resmap_clear_all_resv(resmap);
  248. resmap->m_bitmap_len = clen;
  249. resmap->m_disk_bitmap = disk_bitmap;
  250. spin_unlock(&resv_lock);
  251. }
  252. void ocfs2_resmap_uninit(struct ocfs2_reservation_map *resmap)
  253. {
  254. /* Does nothing for now. Keep this around for API symmetry */
  255. }
  256. static void ocfs2_resv_insert(struct ocfs2_reservation_map *resmap,
  257. struct ocfs2_alloc_reservation *new)
  258. {
  259. struct rb_root *root = &resmap->m_reservations;
  260. struct rb_node *parent = NULL;
  261. struct rb_node **p = &root->rb_node;
  262. struct ocfs2_alloc_reservation *tmp;
  263. assert_spin_locked(&resv_lock);
  264. mlog(0, "Insert reservation start: %u len: %u\n", new->r_start,
  265. new->r_len);
  266. while (*p) {
  267. parent = *p;
  268. tmp = rb_entry(parent, struct ocfs2_alloc_reservation, r_node);
  269. if (new->r_start < tmp->r_start) {
  270. p = &(*p)->rb_left;
  271. /*
  272. * This is a good place to check for
  273. * overlapping reservations.
  274. */
  275. BUG_ON(ocfs2_resv_end(new) >= tmp->r_start);
  276. } else if (new->r_start > ocfs2_resv_end(tmp)) {
  277. p = &(*p)->rb_right;
  278. } else {
  279. /* This should never happen! */
  280. mlog(ML_ERROR, "Duplicate reservation window!\n");
  281. BUG();
  282. }
  283. }
  284. rb_link_node(&new->r_node, parent, p);
  285. rb_insert_color(&new->r_node, root);
  286. new->r_flags |= OCFS2_RESV_FLAG_INUSE;
  287. ocfs2_resv_mark_lru(resmap, new);
  288. ocfs2_check_resmap(resmap);
  289. }
  290. /**
  291. * ocfs2_find_resv_lhs() - find the window which contains goal
  292. * @resmap: reservation map to search
  293. * @goal: which bit to search for
  294. *
  295. * If a window containing that goal is not found, we return the window
  296. * which comes before goal. Returns NULL on empty rbtree or no window
  297. * before goal.
  298. */
  299. static struct ocfs2_alloc_reservation *
  300. ocfs2_find_resv_lhs(struct ocfs2_reservation_map *resmap, unsigned int goal)
  301. {
  302. struct ocfs2_alloc_reservation *resv = NULL;
  303. struct ocfs2_alloc_reservation *prev_resv = NULL;
  304. struct rb_node *node = resmap->m_reservations.rb_node;
  305. assert_spin_locked(&resv_lock);
  306. if (!node)
  307. return NULL;
  308. node = rb_first(&resmap->m_reservations);
  309. while (node) {
  310. resv = rb_entry(node, struct ocfs2_alloc_reservation, r_node);
  311. if (resv->r_start <= goal && ocfs2_resv_end(resv) >= goal)
  312. break;
  313. /* Check if we overshot the reservation just before goal? */
  314. if (resv->r_start > goal) {
  315. resv = prev_resv;
  316. break;
  317. }
  318. prev_resv = resv;
  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 search_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 rhs = 0;
  640. unsigned int old_end = ocfs2_resv_end(resv);
  641. BUG_ON(start != resv->r_start || old_end < end);
  642. /*
  643. * Completely used? We can remove it then.
  644. */
  645. if (old_end == end) {
  646. __ocfs2_resv_discard(resmap, resv);
  647. return;
  648. }
  649. rhs = old_end - end;
  650. /*
  651. * This should have been trapped above.
  652. */
  653. BUG_ON(rhs == 0);
  654. resv->r_start = end + 1;
  655. resv->r_len = old_end - resv->r_start + 1;
  656. }
  657. void ocfs2_resmap_claimed_bits(struct ocfs2_reservation_map *resmap,
  658. struct ocfs2_alloc_reservation *resv,
  659. u32 cstart, u32 clen)
  660. {
  661. unsigned int cend = cstart + clen - 1;
  662. if (resmap == NULL || ocfs2_resmap_disabled(resmap))
  663. return;
  664. if (resv == NULL)
  665. return;
  666. BUG_ON(cstart != resv->r_start);
  667. spin_lock(&resv_lock);
  668. mlog(0, "claim bits: cstart: %u cend: %u clen: %u r_start: %u "
  669. "r_end: %u r_len: %u, r_last_start: %u r_last_len: %u\n",
  670. cstart, cend, clen, resv->r_start, ocfs2_resv_end(resv),
  671. resv->r_len, resv->r_last_start, resv->r_last_len);
  672. BUG_ON(cstart < resv->r_start);
  673. BUG_ON(cstart > ocfs2_resv_end(resv));
  674. BUG_ON(cend > ocfs2_resv_end(resv));
  675. ocfs2_adjust_resv_from_alloc(resmap, resv, cstart, cend);
  676. resv->r_last_start = cstart;
  677. resv->r_last_len = clen;
  678. /*
  679. * May have been discarded above from
  680. * ocfs2_adjust_resv_from_alloc().
  681. */
  682. if (!ocfs2_resv_empty(resv))
  683. ocfs2_resv_mark_lru(resmap, resv);
  684. mlog(0, "Reservation now looks like: r_start: %u r_end: %u "
  685. "r_len: %u r_last_start: %u r_last_len: %u\n",
  686. resv->r_start, ocfs2_resv_end(resv), resv->r_len,
  687. resv->r_last_start, resv->r_last_len);
  688. ocfs2_check_resmap(resmap);
  689. spin_unlock(&resv_lock);
  690. }