reservations.c 20 KB

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