alloc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /*
  2. * alloc.c - NILFS dat/inode allocator
  3. *
  4. * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * Original code was written by Koji Sato <koji@osrg.net>.
  21. * Two allocators were unified by Ryusuke Konishi <ryusuke@osrg.net>,
  22. * Amagai Yoshiji <amagai@osrg.net>.
  23. */
  24. #include <linux/types.h>
  25. #include <linux/buffer_head.h>
  26. #include <linux/fs.h>
  27. #include <linux/bitops.h>
  28. #include <linux/slab.h>
  29. #include "mdt.h"
  30. #include "alloc.h"
  31. /**
  32. * nilfs_palloc_groups_per_desc_block - get the number of groups that a group
  33. * descriptor block can maintain
  34. * @inode: inode of metadata file using this allocator
  35. */
  36. static inline unsigned long
  37. nilfs_palloc_groups_per_desc_block(const struct inode *inode)
  38. {
  39. return (1UL << inode->i_blkbits) /
  40. sizeof(struct nilfs_palloc_group_desc);
  41. }
  42. /**
  43. * nilfs_palloc_groups_count - get maximum number of groups
  44. * @inode: inode of metadata file using this allocator
  45. */
  46. static inline unsigned long
  47. nilfs_palloc_groups_count(const struct inode *inode)
  48. {
  49. return 1UL << (BITS_PER_LONG - (inode->i_blkbits + 3 /* log2(8) */));
  50. }
  51. /**
  52. * nilfs_palloc_init_blockgroup - initialize private variables for allocator
  53. * @inode: inode of metadata file using this allocator
  54. * @entry_size: size of the persistent object
  55. */
  56. int nilfs_palloc_init_blockgroup(struct inode *inode, unsigned entry_size)
  57. {
  58. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  59. mi->mi_bgl = kmalloc(sizeof(*mi->mi_bgl), GFP_NOFS);
  60. if (!mi->mi_bgl)
  61. return -ENOMEM;
  62. bgl_lock_init(mi->mi_bgl);
  63. nilfs_mdt_set_entry_size(inode, entry_size, 0);
  64. mi->mi_blocks_per_group =
  65. DIV_ROUND_UP(nilfs_palloc_entries_per_group(inode),
  66. mi->mi_entries_per_block) + 1;
  67. /* Number of blocks in a group including entry blocks and
  68. a bitmap block */
  69. mi->mi_blocks_per_desc_block =
  70. nilfs_palloc_groups_per_desc_block(inode) *
  71. mi->mi_blocks_per_group + 1;
  72. /* Number of blocks per descriptor including the
  73. descriptor block */
  74. return 0;
  75. }
  76. /**
  77. * nilfs_palloc_group - get group number and offset from an entry number
  78. * @inode: inode of metadata file using this allocator
  79. * @nr: serial number of the entry (e.g. inode number)
  80. * @offset: pointer to store offset number in the group
  81. */
  82. static unsigned long nilfs_palloc_group(const struct inode *inode, __u64 nr,
  83. unsigned long *offset)
  84. {
  85. __u64 group = nr;
  86. *offset = do_div(group, nilfs_palloc_entries_per_group(inode));
  87. return group;
  88. }
  89. /**
  90. * nilfs_palloc_desc_blkoff - get block offset of a group descriptor block
  91. * @inode: inode of metadata file using this allocator
  92. * @group: group number
  93. *
  94. * nilfs_palloc_desc_blkoff() returns block offset of the descriptor
  95. * block which contains a descriptor of the specified group.
  96. */
  97. static unsigned long
  98. nilfs_palloc_desc_blkoff(const struct inode *inode, unsigned long group)
  99. {
  100. unsigned long desc_block =
  101. group / nilfs_palloc_groups_per_desc_block(inode);
  102. return desc_block * NILFS_MDT(inode)->mi_blocks_per_desc_block;
  103. }
  104. /**
  105. * nilfs_palloc_bitmap_blkoff - get block offset of a bitmap block
  106. * @inode: inode of metadata file using this allocator
  107. * @group: group number
  108. *
  109. * nilfs_palloc_bitmap_blkoff() returns block offset of the bitmap
  110. * block used to allocate/deallocate entries in the specified group.
  111. */
  112. static unsigned long
  113. nilfs_palloc_bitmap_blkoff(const struct inode *inode, unsigned long group)
  114. {
  115. unsigned long desc_offset =
  116. group % nilfs_palloc_groups_per_desc_block(inode);
  117. return nilfs_palloc_desc_blkoff(inode, group) + 1 +
  118. desc_offset * NILFS_MDT(inode)->mi_blocks_per_group;
  119. }
  120. /**
  121. * nilfs_palloc_group_desc_nfrees - get the number of free entries in a group
  122. * @inode: inode of metadata file using this allocator
  123. * @group: group number
  124. * @desc: pointer to descriptor structure for the group
  125. */
  126. static unsigned long
  127. nilfs_palloc_group_desc_nfrees(struct inode *inode, unsigned long group,
  128. const struct nilfs_palloc_group_desc *desc)
  129. {
  130. unsigned long nfree;
  131. spin_lock(nilfs_mdt_bgl_lock(inode, group));
  132. nfree = le32_to_cpu(desc->pg_nfrees);
  133. spin_unlock(nilfs_mdt_bgl_lock(inode, group));
  134. return nfree;
  135. }
  136. /**
  137. * nilfs_palloc_group_desc_add_entries - adjust count of free entries
  138. * @inode: inode of metadata file using this allocator
  139. * @group: group number
  140. * @desc: pointer to descriptor structure for the group
  141. * @n: delta to be added
  142. */
  143. static void
  144. nilfs_palloc_group_desc_add_entries(struct inode *inode,
  145. unsigned long group,
  146. struct nilfs_palloc_group_desc *desc,
  147. u32 n)
  148. {
  149. spin_lock(nilfs_mdt_bgl_lock(inode, group));
  150. le32_add_cpu(&desc->pg_nfrees, n);
  151. spin_unlock(nilfs_mdt_bgl_lock(inode, group));
  152. }
  153. /**
  154. * nilfs_palloc_entry_blkoff - get block offset of an entry block
  155. * @inode: inode of metadata file using this allocator
  156. * @nr: serial number of the entry (e.g. inode number)
  157. */
  158. static unsigned long
  159. nilfs_palloc_entry_blkoff(const struct inode *inode, __u64 nr)
  160. {
  161. unsigned long group, group_offset;
  162. group = nilfs_palloc_group(inode, nr, &group_offset);
  163. return nilfs_palloc_bitmap_blkoff(inode, group) + 1 +
  164. group_offset / NILFS_MDT(inode)->mi_entries_per_block;
  165. }
  166. /**
  167. * nilfs_palloc_desc_block_init - initialize buffer of a group descriptor block
  168. * @inode: inode of metadata file
  169. * @bh: buffer head of the buffer to be initialized
  170. * @kaddr: kernel address mapped for the page including the buffer
  171. */
  172. static void nilfs_palloc_desc_block_init(struct inode *inode,
  173. struct buffer_head *bh, void *kaddr)
  174. {
  175. struct nilfs_palloc_group_desc *desc = kaddr + bh_offset(bh);
  176. unsigned long n = nilfs_palloc_groups_per_desc_block(inode);
  177. __le32 nfrees;
  178. nfrees = cpu_to_le32(nilfs_palloc_entries_per_group(inode));
  179. while (n-- > 0) {
  180. desc->pg_nfrees = nfrees;
  181. desc++;
  182. }
  183. }
  184. static int nilfs_palloc_get_block(struct inode *inode, unsigned long blkoff,
  185. int create,
  186. void (*init_block)(struct inode *,
  187. struct buffer_head *,
  188. void *),
  189. struct buffer_head **bhp,
  190. struct nilfs_bh_assoc *prev,
  191. spinlock_t *lock)
  192. {
  193. int ret;
  194. spin_lock(lock);
  195. if (prev->bh && blkoff == prev->blkoff) {
  196. get_bh(prev->bh);
  197. *bhp = prev->bh;
  198. spin_unlock(lock);
  199. return 0;
  200. }
  201. spin_unlock(lock);
  202. ret = nilfs_mdt_get_block(inode, blkoff, create, init_block, bhp);
  203. if (!ret) {
  204. spin_lock(lock);
  205. /*
  206. * The following code must be safe for change of the
  207. * cache contents during the get block call.
  208. */
  209. brelse(prev->bh);
  210. get_bh(*bhp);
  211. prev->bh = *bhp;
  212. prev->blkoff = blkoff;
  213. spin_unlock(lock);
  214. }
  215. return ret;
  216. }
  217. /**
  218. * nilfs_palloc_get_desc_block - get buffer head of a group descriptor block
  219. * @inode: inode of metadata file using this allocator
  220. * @group: group number
  221. * @create: create flag
  222. * @bhp: pointer to store the resultant buffer head
  223. */
  224. static int nilfs_palloc_get_desc_block(struct inode *inode,
  225. unsigned long group,
  226. int create, struct buffer_head **bhp)
  227. {
  228. struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
  229. return nilfs_palloc_get_block(inode,
  230. nilfs_palloc_desc_blkoff(inode, group),
  231. create, nilfs_palloc_desc_block_init,
  232. bhp, &cache->prev_desc, &cache->lock);
  233. }
  234. /**
  235. * nilfs_palloc_get_bitmap_block - get buffer head of a bitmap block
  236. * @inode: inode of metadata file using this allocator
  237. * @group: group number
  238. * @create: create flag
  239. * @bhp: pointer to store the resultant buffer head
  240. */
  241. static int nilfs_palloc_get_bitmap_block(struct inode *inode,
  242. unsigned long group,
  243. int create, struct buffer_head **bhp)
  244. {
  245. struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
  246. return nilfs_palloc_get_block(inode,
  247. nilfs_palloc_bitmap_blkoff(inode, group),
  248. create, NULL, bhp,
  249. &cache->prev_bitmap, &cache->lock);
  250. }
  251. /**
  252. * nilfs_palloc_get_entry_block - get buffer head of an entry block
  253. * @inode: inode of metadata file using this allocator
  254. * @nr: serial number of the entry (e.g. inode number)
  255. * @create: create flag
  256. * @bhp: pointer to store the resultant buffer head
  257. */
  258. int nilfs_palloc_get_entry_block(struct inode *inode, __u64 nr,
  259. int create, struct buffer_head **bhp)
  260. {
  261. struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
  262. return nilfs_palloc_get_block(inode,
  263. nilfs_palloc_entry_blkoff(inode, nr),
  264. create, NULL, bhp,
  265. &cache->prev_entry, &cache->lock);
  266. }
  267. /**
  268. * nilfs_palloc_block_get_group_desc - get kernel address of a group descriptor
  269. * @inode: inode of metadata file using this allocator
  270. * @group: group number
  271. * @bh: buffer head of the buffer storing the group descriptor block
  272. * @kaddr: kernel address mapped for the page including the buffer
  273. */
  274. static struct nilfs_palloc_group_desc *
  275. nilfs_palloc_block_get_group_desc(const struct inode *inode,
  276. unsigned long group,
  277. const struct buffer_head *bh, void *kaddr)
  278. {
  279. return (struct nilfs_palloc_group_desc *)(kaddr + bh_offset(bh)) +
  280. group % nilfs_palloc_groups_per_desc_block(inode);
  281. }
  282. /**
  283. * nilfs_palloc_block_get_entry - get kernel address of an entry
  284. * @inode: inode of metadata file using this allocator
  285. * @nr: serial number of the entry (e.g. inode number)
  286. * @bh: buffer head of the buffer storing the entry block
  287. * @kaddr: kernel address mapped for the page including the buffer
  288. */
  289. void *nilfs_palloc_block_get_entry(const struct inode *inode, __u64 nr,
  290. const struct buffer_head *bh, void *kaddr)
  291. {
  292. unsigned long entry_offset, group_offset;
  293. nilfs_palloc_group(inode, nr, &group_offset);
  294. entry_offset = group_offset % NILFS_MDT(inode)->mi_entries_per_block;
  295. return kaddr + bh_offset(bh) +
  296. entry_offset * NILFS_MDT(inode)->mi_entry_size;
  297. }
  298. /**
  299. * nilfs_palloc_find_available_slot - find available slot in a group
  300. * @inode: inode of metadata file using this allocator
  301. * @group: group number
  302. * @target: offset number of an entry in the group (start point)
  303. * @bitmap: bitmap of the group
  304. * @bsize: size in bits
  305. */
  306. static int nilfs_palloc_find_available_slot(struct inode *inode,
  307. unsigned long group,
  308. unsigned long target,
  309. unsigned char *bitmap,
  310. int bsize)
  311. {
  312. int curr, pos, end, i;
  313. if (target > 0) {
  314. end = (target + BITS_PER_LONG - 1) & ~(BITS_PER_LONG - 1);
  315. if (end > bsize)
  316. end = bsize;
  317. pos = nilfs_find_next_zero_bit(bitmap, end, target);
  318. if (pos < end &&
  319. !nilfs_set_bit_atomic(
  320. nilfs_mdt_bgl_lock(inode, group), pos, bitmap))
  321. return pos;
  322. } else
  323. end = 0;
  324. for (i = 0, curr = end;
  325. i < bsize;
  326. i += BITS_PER_LONG, curr += BITS_PER_LONG) {
  327. /* wrap around */
  328. if (curr >= bsize)
  329. curr = 0;
  330. while (*((unsigned long *)bitmap + curr / BITS_PER_LONG)
  331. != ~0UL) {
  332. end = curr + BITS_PER_LONG;
  333. if (end > bsize)
  334. end = bsize;
  335. pos = nilfs_find_next_zero_bit(bitmap, end, curr);
  336. if ((pos < end) &&
  337. !nilfs_set_bit_atomic(
  338. nilfs_mdt_bgl_lock(inode, group), pos,
  339. bitmap))
  340. return pos;
  341. }
  342. }
  343. return -ENOSPC;
  344. }
  345. /**
  346. * nilfs_palloc_rest_groups_in_desc_block - get the remaining number of groups
  347. * in a group descriptor block
  348. * @inode: inode of metadata file using this allocator
  349. * @curr: current group number
  350. * @max: maximum number of groups
  351. */
  352. static unsigned long
  353. nilfs_palloc_rest_groups_in_desc_block(const struct inode *inode,
  354. unsigned long curr, unsigned long max)
  355. {
  356. return min_t(unsigned long,
  357. nilfs_palloc_groups_per_desc_block(inode) -
  358. curr % nilfs_palloc_groups_per_desc_block(inode),
  359. max - curr + 1);
  360. }
  361. /**
  362. * nilfs_palloc_prepare_alloc_entry - prepare to allocate a persistent object
  363. * @inode: inode of metadata file using this allocator
  364. * @req: nilfs_palloc_req structure exchanged for the allocation
  365. */
  366. int nilfs_palloc_prepare_alloc_entry(struct inode *inode,
  367. struct nilfs_palloc_req *req)
  368. {
  369. struct buffer_head *desc_bh, *bitmap_bh;
  370. struct nilfs_palloc_group_desc *desc;
  371. unsigned char *bitmap;
  372. void *desc_kaddr, *bitmap_kaddr;
  373. unsigned long group, maxgroup, ngroups;
  374. unsigned long group_offset, maxgroup_offset;
  375. unsigned long n, entries_per_group, groups_per_desc_block;
  376. unsigned long i, j;
  377. int pos, ret;
  378. ngroups = nilfs_palloc_groups_count(inode);
  379. maxgroup = ngroups - 1;
  380. group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
  381. entries_per_group = nilfs_palloc_entries_per_group(inode);
  382. groups_per_desc_block = nilfs_palloc_groups_per_desc_block(inode);
  383. for (i = 0; i < ngroups; i += n) {
  384. if (group >= ngroups) {
  385. /* wrap around */
  386. group = 0;
  387. maxgroup = nilfs_palloc_group(inode, req->pr_entry_nr,
  388. &maxgroup_offset) - 1;
  389. }
  390. ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh);
  391. if (ret < 0)
  392. return ret;
  393. desc_kaddr = kmap(desc_bh->b_page);
  394. desc = nilfs_palloc_block_get_group_desc(
  395. inode, group, desc_bh, desc_kaddr);
  396. n = nilfs_palloc_rest_groups_in_desc_block(inode, group,
  397. maxgroup);
  398. for (j = 0; j < n; j++, desc++, group++) {
  399. if (nilfs_palloc_group_desc_nfrees(inode, group, desc)
  400. > 0) {
  401. ret = nilfs_palloc_get_bitmap_block(
  402. inode, group, 1, &bitmap_bh);
  403. if (ret < 0)
  404. goto out_desc;
  405. bitmap_kaddr = kmap(bitmap_bh->b_page);
  406. bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
  407. pos = nilfs_palloc_find_available_slot(
  408. inode, group, group_offset, bitmap,
  409. entries_per_group);
  410. if (pos >= 0) {
  411. /* found a free entry */
  412. nilfs_palloc_group_desc_add_entries(
  413. inode, group, desc, -1);
  414. req->pr_entry_nr =
  415. entries_per_group * group + pos;
  416. kunmap(desc_bh->b_page);
  417. kunmap(bitmap_bh->b_page);
  418. req->pr_desc_bh = desc_bh;
  419. req->pr_bitmap_bh = bitmap_bh;
  420. return 0;
  421. }
  422. kunmap(bitmap_bh->b_page);
  423. brelse(bitmap_bh);
  424. }
  425. group_offset = 0;
  426. }
  427. kunmap(desc_bh->b_page);
  428. brelse(desc_bh);
  429. }
  430. /* no entries left */
  431. return -ENOSPC;
  432. out_desc:
  433. kunmap(desc_bh->b_page);
  434. brelse(desc_bh);
  435. return ret;
  436. }
  437. /**
  438. * nilfs_palloc_commit_alloc_entry - finish allocation of a persistent object
  439. * @inode: inode of metadata file using this allocator
  440. * @req: nilfs_palloc_req structure exchanged for the allocation
  441. */
  442. void nilfs_palloc_commit_alloc_entry(struct inode *inode,
  443. struct nilfs_palloc_req *req)
  444. {
  445. mark_buffer_dirty(req->pr_bitmap_bh);
  446. mark_buffer_dirty(req->pr_desc_bh);
  447. nilfs_mdt_mark_dirty(inode);
  448. brelse(req->pr_bitmap_bh);
  449. brelse(req->pr_desc_bh);
  450. }
  451. /**
  452. * nilfs_palloc_commit_free_entry - finish deallocating a persistent object
  453. * @inode: inode of metadata file using this allocator
  454. * @req: nilfs_palloc_req structure exchanged for the removal
  455. */
  456. void nilfs_palloc_commit_free_entry(struct inode *inode,
  457. struct nilfs_palloc_req *req)
  458. {
  459. struct nilfs_palloc_group_desc *desc;
  460. unsigned long group, group_offset;
  461. unsigned char *bitmap;
  462. void *desc_kaddr, *bitmap_kaddr;
  463. group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
  464. desc_kaddr = kmap(req->pr_desc_bh->b_page);
  465. desc = nilfs_palloc_block_get_group_desc(inode, group,
  466. req->pr_desc_bh, desc_kaddr);
  467. bitmap_kaddr = kmap(req->pr_bitmap_bh->b_page);
  468. bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh);
  469. if (!nilfs_clear_bit_atomic(nilfs_mdt_bgl_lock(inode, group),
  470. group_offset, bitmap))
  471. printk(KERN_WARNING "%s: entry number %llu already freed\n",
  472. __func__, (unsigned long long)req->pr_entry_nr);
  473. else
  474. nilfs_palloc_group_desc_add_entries(inode, group, desc, 1);
  475. kunmap(req->pr_bitmap_bh->b_page);
  476. kunmap(req->pr_desc_bh->b_page);
  477. mark_buffer_dirty(req->pr_desc_bh);
  478. mark_buffer_dirty(req->pr_bitmap_bh);
  479. nilfs_mdt_mark_dirty(inode);
  480. brelse(req->pr_bitmap_bh);
  481. brelse(req->pr_desc_bh);
  482. }
  483. /**
  484. * nilfs_palloc_abort_alloc_entry - cancel allocation of a persistent object
  485. * @inode: inode of metadata file using this allocator
  486. * @req: nilfs_palloc_req structure exchanged for the allocation
  487. */
  488. void nilfs_palloc_abort_alloc_entry(struct inode *inode,
  489. struct nilfs_palloc_req *req)
  490. {
  491. struct nilfs_palloc_group_desc *desc;
  492. void *desc_kaddr, *bitmap_kaddr;
  493. unsigned char *bitmap;
  494. unsigned long group, group_offset;
  495. group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
  496. desc_kaddr = kmap(req->pr_desc_bh->b_page);
  497. desc = nilfs_palloc_block_get_group_desc(inode, group,
  498. req->pr_desc_bh, desc_kaddr);
  499. bitmap_kaddr = kmap(req->pr_bitmap_bh->b_page);
  500. bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh);
  501. if (!nilfs_clear_bit_atomic(nilfs_mdt_bgl_lock(inode, group),
  502. group_offset, bitmap))
  503. printk(KERN_WARNING "%s: entry number %llu already freed\n",
  504. __func__, (unsigned long long)req->pr_entry_nr);
  505. else
  506. nilfs_palloc_group_desc_add_entries(inode, group, desc, 1);
  507. kunmap(req->pr_bitmap_bh->b_page);
  508. kunmap(req->pr_desc_bh->b_page);
  509. brelse(req->pr_bitmap_bh);
  510. brelse(req->pr_desc_bh);
  511. req->pr_entry_nr = 0;
  512. req->pr_bitmap_bh = NULL;
  513. req->pr_desc_bh = NULL;
  514. }
  515. /**
  516. * nilfs_palloc_prepare_free_entry - prepare to deallocate a persistent object
  517. * @inode: inode of metadata file using this allocator
  518. * @req: nilfs_palloc_req structure exchanged for the removal
  519. */
  520. int nilfs_palloc_prepare_free_entry(struct inode *inode,
  521. struct nilfs_palloc_req *req)
  522. {
  523. struct buffer_head *desc_bh, *bitmap_bh;
  524. unsigned long group, group_offset;
  525. int ret;
  526. group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
  527. ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh);
  528. if (ret < 0)
  529. return ret;
  530. ret = nilfs_palloc_get_bitmap_block(inode, group, 1, &bitmap_bh);
  531. if (ret < 0) {
  532. brelse(desc_bh);
  533. return ret;
  534. }
  535. req->pr_desc_bh = desc_bh;
  536. req->pr_bitmap_bh = bitmap_bh;
  537. return 0;
  538. }
  539. /**
  540. * nilfs_palloc_abort_free_entry - cancel deallocating a persistent object
  541. * @inode: inode of metadata file using this allocator
  542. * @req: nilfs_palloc_req structure exchanged for the removal
  543. */
  544. void nilfs_palloc_abort_free_entry(struct inode *inode,
  545. struct nilfs_palloc_req *req)
  546. {
  547. brelse(req->pr_bitmap_bh);
  548. brelse(req->pr_desc_bh);
  549. req->pr_entry_nr = 0;
  550. req->pr_bitmap_bh = NULL;
  551. req->pr_desc_bh = NULL;
  552. }
  553. /**
  554. * nilfs_palloc_group_is_in - judge if an entry is in a group
  555. * @inode: inode of metadata file using this allocator
  556. * @group: group number
  557. * @nr: serial number of the entry (e.g. inode number)
  558. */
  559. static int
  560. nilfs_palloc_group_is_in(struct inode *inode, unsigned long group, __u64 nr)
  561. {
  562. __u64 first, last;
  563. first = group * nilfs_palloc_entries_per_group(inode);
  564. last = first + nilfs_palloc_entries_per_group(inode) - 1;
  565. return (nr >= first) && (nr <= last);
  566. }
  567. /**
  568. * nilfs_palloc_freev - deallocate a set of persistent objects
  569. * @inode: inode of metadata file using this allocator
  570. * @entry_nrs: array of entry numbers to be deallocated
  571. * @nitems: number of entries stored in @entry_nrs
  572. */
  573. int nilfs_palloc_freev(struct inode *inode, __u64 *entry_nrs, size_t nitems)
  574. {
  575. struct buffer_head *desc_bh, *bitmap_bh;
  576. struct nilfs_palloc_group_desc *desc;
  577. unsigned char *bitmap;
  578. void *desc_kaddr, *bitmap_kaddr;
  579. unsigned long group, group_offset;
  580. int i, j, n, ret;
  581. for (i = 0; i < nitems; i = j) {
  582. group = nilfs_palloc_group(inode, entry_nrs[i], &group_offset);
  583. ret = nilfs_palloc_get_desc_block(inode, group, 0, &desc_bh);
  584. if (ret < 0)
  585. return ret;
  586. ret = nilfs_palloc_get_bitmap_block(inode, group, 0,
  587. &bitmap_bh);
  588. if (ret < 0) {
  589. brelse(desc_bh);
  590. return ret;
  591. }
  592. desc_kaddr = kmap(desc_bh->b_page);
  593. desc = nilfs_palloc_block_get_group_desc(
  594. inode, group, desc_bh, desc_kaddr);
  595. bitmap_kaddr = kmap(bitmap_bh->b_page);
  596. bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
  597. for (j = i, n = 0;
  598. (j < nitems) && nilfs_palloc_group_is_in(inode, group,
  599. entry_nrs[j]);
  600. j++) {
  601. nilfs_palloc_group(inode, entry_nrs[j], &group_offset);
  602. if (!nilfs_clear_bit_atomic(
  603. nilfs_mdt_bgl_lock(inode, group),
  604. group_offset, bitmap)) {
  605. printk(KERN_WARNING
  606. "%s: entry number %llu already freed\n",
  607. __func__,
  608. (unsigned long long)entry_nrs[j]);
  609. } else {
  610. n++;
  611. }
  612. }
  613. nilfs_palloc_group_desc_add_entries(inode, group, desc, n);
  614. kunmap(bitmap_bh->b_page);
  615. kunmap(desc_bh->b_page);
  616. mark_buffer_dirty(desc_bh);
  617. mark_buffer_dirty(bitmap_bh);
  618. nilfs_mdt_mark_dirty(inode);
  619. brelse(bitmap_bh);
  620. brelse(desc_bh);
  621. }
  622. return 0;
  623. }
  624. void nilfs_palloc_setup_cache(struct inode *inode,
  625. struct nilfs_palloc_cache *cache)
  626. {
  627. NILFS_MDT(inode)->mi_palloc_cache = cache;
  628. spin_lock_init(&cache->lock);
  629. }
  630. void nilfs_palloc_clear_cache(struct inode *inode)
  631. {
  632. struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
  633. spin_lock(&cache->lock);
  634. brelse(cache->prev_desc.bh);
  635. brelse(cache->prev_bitmap.bh);
  636. brelse(cache->prev_entry.bh);
  637. cache->prev_desc.bh = NULL;
  638. cache->prev_bitmap.bh = NULL;
  639. cache->prev_entry.bh = NULL;
  640. spin_unlock(&cache->lock);
  641. }
  642. void nilfs_palloc_destroy_cache(struct inode *inode)
  643. {
  644. nilfs_palloc_clear_cache(inode);
  645. NILFS_MDT(inode)->mi_palloc_cache = NULL;
  646. }