alloc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. * Originally written by Koji Sato.
  17. * Two allocators were unified by Ryusuke Konishi and Amagai Yoshiji.
  18. */
  19. #include <linux/types.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/fs.h>
  22. #include <linux/bitops.h>
  23. #include <linux/slab.h>
  24. #include "mdt.h"
  25. #include "alloc.h"
  26. /**
  27. * nilfs_palloc_groups_per_desc_block - get the number of groups that a group
  28. * descriptor block can maintain
  29. * @inode: inode of metadata file using this allocator
  30. */
  31. static inline unsigned long
  32. nilfs_palloc_groups_per_desc_block(const struct inode *inode)
  33. {
  34. return (1UL << inode->i_blkbits) /
  35. sizeof(struct nilfs_palloc_group_desc);
  36. }
  37. /**
  38. * nilfs_palloc_groups_count - get maximum number of groups
  39. * @inode: inode of metadata file using this allocator
  40. */
  41. static inline unsigned long
  42. nilfs_palloc_groups_count(const struct inode *inode)
  43. {
  44. return 1UL << (BITS_PER_LONG - (inode->i_blkbits + 3 /* log2(8) */));
  45. }
  46. /**
  47. * nilfs_palloc_init_blockgroup - initialize private variables for allocator
  48. * @inode: inode of metadata file using this allocator
  49. * @entry_size: size of the persistent object
  50. */
  51. int nilfs_palloc_init_blockgroup(struct inode *inode, unsigned int entry_size)
  52. {
  53. struct nilfs_mdt_info *mi = NILFS_MDT(inode);
  54. mi->mi_bgl = kmalloc(sizeof(*mi->mi_bgl), GFP_NOFS);
  55. if (!mi->mi_bgl)
  56. return -ENOMEM;
  57. bgl_lock_init(mi->mi_bgl);
  58. nilfs_mdt_set_entry_size(inode, entry_size, 0);
  59. mi->mi_blocks_per_group =
  60. DIV_ROUND_UP(nilfs_palloc_entries_per_group(inode),
  61. mi->mi_entries_per_block) + 1;
  62. /*
  63. * Number of blocks in a group including entry blocks
  64. * and a bitmap block
  65. */
  66. mi->mi_blocks_per_desc_block =
  67. nilfs_palloc_groups_per_desc_block(inode) *
  68. mi->mi_blocks_per_group + 1;
  69. /*
  70. * Number of blocks per descriptor including the
  71. * descriptor block
  72. */
  73. return 0;
  74. }
  75. /**
  76. * nilfs_palloc_group - get group number and offset from an entry number
  77. * @inode: inode of metadata file using this allocator
  78. * @nr: serial number of the entry (e.g. inode number)
  79. * @offset: pointer to store offset number in the group
  80. */
  81. static unsigned long nilfs_palloc_group(const struct inode *inode, __u64 nr,
  82. unsigned long *offset)
  83. {
  84. __u64 group = nr;
  85. *offset = do_div(group, nilfs_palloc_entries_per_group(inode));
  86. return group;
  87. }
  88. /**
  89. * nilfs_palloc_desc_blkoff - get block offset of a group descriptor block
  90. * @inode: inode of metadata file using this allocator
  91. * @group: group number
  92. *
  93. * nilfs_palloc_desc_blkoff() returns block offset of the descriptor
  94. * block which contains a descriptor of the specified group.
  95. */
  96. static unsigned long
  97. nilfs_palloc_desc_blkoff(const struct inode *inode, unsigned long group)
  98. {
  99. unsigned long desc_block =
  100. group / nilfs_palloc_groups_per_desc_block(inode);
  101. return desc_block * NILFS_MDT(inode)->mi_blocks_per_desc_block;
  102. }
  103. /**
  104. * nilfs_palloc_bitmap_blkoff - get block offset of a bitmap block
  105. * @inode: inode of metadata file using this allocator
  106. * @group: group number
  107. *
  108. * nilfs_palloc_bitmap_blkoff() returns block offset of the bitmap
  109. * block used to allocate/deallocate entries in the specified group.
  110. */
  111. static unsigned long
  112. nilfs_palloc_bitmap_blkoff(const struct inode *inode, unsigned long group)
  113. {
  114. unsigned long desc_offset =
  115. group % nilfs_palloc_groups_per_desc_block(inode);
  116. return nilfs_palloc_desc_blkoff(inode, group) + 1 +
  117. desc_offset * NILFS_MDT(inode)->mi_blocks_per_group;
  118. }
  119. /**
  120. * nilfs_palloc_group_desc_nfrees - get the number of free entries in a group
  121. * @desc: pointer to descriptor structure for the group
  122. * @lock: spin lock protecting @desc
  123. */
  124. static unsigned long
  125. nilfs_palloc_group_desc_nfrees(const struct nilfs_palloc_group_desc *desc,
  126. spinlock_t *lock)
  127. {
  128. unsigned long nfree;
  129. spin_lock(lock);
  130. nfree = le32_to_cpu(desc->pg_nfrees);
  131. spin_unlock(lock);
  132. return nfree;
  133. }
  134. /**
  135. * nilfs_palloc_group_desc_add_entries - adjust count of free entries
  136. * @desc: pointer to descriptor structure for the group
  137. * @lock: spin lock protecting @desc
  138. * @n: delta to be added
  139. */
  140. static u32
  141. nilfs_palloc_group_desc_add_entries(struct nilfs_palloc_group_desc *desc,
  142. spinlock_t *lock, u32 n)
  143. {
  144. u32 nfree;
  145. spin_lock(lock);
  146. le32_add_cpu(&desc->pg_nfrees, n);
  147. nfree = le32_to_cpu(desc->pg_nfrees);
  148. spin_unlock(lock);
  149. return nfree;
  150. }
  151. /**
  152. * nilfs_palloc_entry_blkoff - get block offset of an entry block
  153. * @inode: inode of metadata file using this allocator
  154. * @nr: serial number of the entry (e.g. inode number)
  155. */
  156. static unsigned long
  157. nilfs_palloc_entry_blkoff(const struct inode *inode, __u64 nr)
  158. {
  159. unsigned long group, group_offset;
  160. group = nilfs_palloc_group(inode, nr, &group_offset);
  161. return nilfs_palloc_bitmap_blkoff(inode, group) + 1 +
  162. group_offset / NILFS_MDT(inode)->mi_entries_per_block;
  163. }
  164. /**
  165. * nilfs_palloc_desc_block_init - initialize buffer of a group descriptor block
  166. * @inode: inode of metadata file
  167. * @bh: buffer head of the buffer to be initialized
  168. * @kaddr: kernel address mapped for the page including the buffer
  169. */
  170. static void nilfs_palloc_desc_block_init(struct inode *inode,
  171. struct buffer_head *bh, void *kaddr)
  172. {
  173. struct nilfs_palloc_group_desc *desc = kaddr + bh_offset(bh);
  174. unsigned long n = nilfs_palloc_groups_per_desc_block(inode);
  175. __le32 nfrees;
  176. nfrees = cpu_to_le32(nilfs_palloc_entries_per_group(inode));
  177. while (n-- > 0) {
  178. desc->pg_nfrees = nfrees;
  179. desc++;
  180. }
  181. }
  182. static int nilfs_palloc_get_block(struct inode *inode, unsigned long blkoff,
  183. int create,
  184. void (*init_block)(struct inode *,
  185. struct buffer_head *,
  186. void *),
  187. struct buffer_head **bhp,
  188. struct nilfs_bh_assoc *prev,
  189. spinlock_t *lock)
  190. {
  191. int ret;
  192. spin_lock(lock);
  193. if (prev->bh && blkoff == prev->blkoff) {
  194. get_bh(prev->bh);
  195. *bhp = prev->bh;
  196. spin_unlock(lock);
  197. return 0;
  198. }
  199. spin_unlock(lock);
  200. ret = nilfs_mdt_get_block(inode, blkoff, create, init_block, bhp);
  201. if (!ret) {
  202. spin_lock(lock);
  203. /*
  204. * The following code must be safe for change of the
  205. * cache contents during the get block call.
  206. */
  207. brelse(prev->bh);
  208. get_bh(*bhp);
  209. prev->bh = *bhp;
  210. prev->blkoff = blkoff;
  211. spin_unlock(lock);
  212. }
  213. return ret;
  214. }
  215. /**
  216. * nilfs_palloc_delete_block - delete a block on the persistent allocator file
  217. * @inode: inode of metadata file using this allocator
  218. * @blkoff: block offset
  219. * @prev: nilfs_bh_assoc struct of the last used buffer
  220. * @lock: spin lock protecting @prev
  221. */
  222. static int nilfs_palloc_delete_block(struct inode *inode, unsigned long blkoff,
  223. struct nilfs_bh_assoc *prev,
  224. spinlock_t *lock)
  225. {
  226. spin_lock(lock);
  227. if (prev->bh && blkoff == prev->blkoff) {
  228. brelse(prev->bh);
  229. prev->bh = NULL;
  230. }
  231. spin_unlock(lock);
  232. return nilfs_mdt_delete_block(inode, blkoff);
  233. }
  234. /**
  235. * nilfs_palloc_get_desc_block - get buffer head of a group descriptor 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_desc_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_desc_blkoff(inode, group),
  248. create, nilfs_palloc_desc_block_init,
  249. bhp, &cache->prev_desc, &cache->lock);
  250. }
  251. /**
  252. * nilfs_palloc_get_bitmap_block - get buffer head of a bitmap block
  253. * @inode: inode of metadata file using this allocator
  254. * @group: group number
  255. * @create: create flag
  256. * @bhp: pointer to store the resultant buffer head
  257. */
  258. static int nilfs_palloc_get_bitmap_block(struct inode *inode,
  259. unsigned long group,
  260. int create, struct buffer_head **bhp)
  261. {
  262. struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
  263. return nilfs_palloc_get_block(inode,
  264. nilfs_palloc_bitmap_blkoff(inode, group),
  265. create, NULL, bhp,
  266. &cache->prev_bitmap, &cache->lock);
  267. }
  268. /**
  269. * nilfs_palloc_delete_bitmap_block - delete a bitmap block
  270. * @inode: inode of metadata file using this allocator
  271. * @group: group number
  272. */
  273. static int nilfs_palloc_delete_bitmap_block(struct inode *inode,
  274. unsigned long group)
  275. {
  276. struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
  277. return nilfs_palloc_delete_block(inode,
  278. nilfs_palloc_bitmap_blkoff(inode,
  279. group),
  280. &cache->prev_bitmap, &cache->lock);
  281. }
  282. /**
  283. * nilfs_palloc_get_entry_block - get buffer head of an entry block
  284. * @inode: inode of metadata file using this allocator
  285. * @nr: serial number of the entry (e.g. inode number)
  286. * @create: create flag
  287. * @bhp: pointer to store the resultant buffer head
  288. */
  289. int nilfs_palloc_get_entry_block(struct inode *inode, __u64 nr,
  290. int create, struct buffer_head **bhp)
  291. {
  292. struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
  293. return nilfs_palloc_get_block(inode,
  294. nilfs_palloc_entry_blkoff(inode, nr),
  295. create, NULL, bhp,
  296. &cache->prev_entry, &cache->lock);
  297. }
  298. /**
  299. * nilfs_palloc_delete_entry_block - delete an entry block
  300. * @inode: inode of metadata file using this allocator
  301. * @nr: serial number of the entry
  302. */
  303. static int nilfs_palloc_delete_entry_block(struct inode *inode, __u64 nr)
  304. {
  305. struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
  306. return nilfs_palloc_delete_block(inode,
  307. nilfs_palloc_entry_blkoff(inode, nr),
  308. &cache->prev_entry, &cache->lock);
  309. }
  310. /**
  311. * nilfs_palloc_block_get_group_desc - get kernel address of a group descriptor
  312. * @inode: inode of metadata file using this allocator
  313. * @group: group number
  314. * @bh: buffer head of the buffer storing the group descriptor block
  315. * @kaddr: kernel address mapped for the page including the buffer
  316. */
  317. static struct nilfs_palloc_group_desc *
  318. nilfs_palloc_block_get_group_desc(const struct inode *inode,
  319. unsigned long group,
  320. const struct buffer_head *bh, void *kaddr)
  321. {
  322. return (struct nilfs_palloc_group_desc *)(kaddr + bh_offset(bh)) +
  323. group % nilfs_palloc_groups_per_desc_block(inode);
  324. }
  325. /**
  326. * nilfs_palloc_block_get_entry - get kernel address of an entry
  327. * @inode: inode of metadata file using this allocator
  328. * @nr: serial number of the entry (e.g. inode number)
  329. * @bh: buffer head of the buffer storing the entry block
  330. * @kaddr: kernel address mapped for the page including the buffer
  331. */
  332. void *nilfs_palloc_block_get_entry(const struct inode *inode, __u64 nr,
  333. const struct buffer_head *bh, void *kaddr)
  334. {
  335. unsigned long entry_offset, group_offset;
  336. nilfs_palloc_group(inode, nr, &group_offset);
  337. entry_offset = group_offset % NILFS_MDT(inode)->mi_entries_per_block;
  338. return kaddr + bh_offset(bh) +
  339. entry_offset * NILFS_MDT(inode)->mi_entry_size;
  340. }
  341. /**
  342. * nilfs_palloc_find_available_slot - find available slot in a group
  343. * @bitmap: bitmap of the group
  344. * @target: offset number of an entry in the group (start point)
  345. * @bsize: size in bits
  346. * @lock: spin lock protecting @bitmap
  347. */
  348. static int nilfs_palloc_find_available_slot(unsigned char *bitmap,
  349. unsigned long target,
  350. unsigned int bsize,
  351. spinlock_t *lock)
  352. {
  353. int pos, end = bsize;
  354. if (likely(target < bsize)) {
  355. pos = target;
  356. do {
  357. pos = nilfs_find_next_zero_bit(bitmap, end, pos);
  358. if (pos >= end)
  359. break;
  360. if (!nilfs_set_bit_atomic(lock, pos, bitmap))
  361. return pos;
  362. } while (++pos < end);
  363. end = target;
  364. }
  365. /* wrap around */
  366. for (pos = 0; pos < end; pos++) {
  367. pos = nilfs_find_next_zero_bit(bitmap, end, pos);
  368. if (pos >= end)
  369. break;
  370. if (!nilfs_set_bit_atomic(lock, pos, bitmap))
  371. return pos;
  372. }
  373. return -ENOSPC;
  374. }
  375. /**
  376. * nilfs_palloc_rest_groups_in_desc_block - get the remaining number of groups
  377. * in a group descriptor block
  378. * @inode: inode of metadata file using this allocator
  379. * @curr: current group number
  380. * @max: maximum number of groups
  381. */
  382. static unsigned long
  383. nilfs_palloc_rest_groups_in_desc_block(const struct inode *inode,
  384. unsigned long curr, unsigned long max)
  385. {
  386. return min_t(unsigned long,
  387. nilfs_palloc_groups_per_desc_block(inode) -
  388. curr % nilfs_palloc_groups_per_desc_block(inode),
  389. max - curr + 1);
  390. }
  391. /**
  392. * nilfs_palloc_count_desc_blocks - count descriptor blocks number
  393. * @inode: inode of metadata file using this allocator
  394. * @desc_blocks: descriptor blocks number [out]
  395. */
  396. static int nilfs_palloc_count_desc_blocks(struct inode *inode,
  397. unsigned long *desc_blocks)
  398. {
  399. __u64 blknum;
  400. int ret;
  401. ret = nilfs_bmap_last_key(NILFS_I(inode)->i_bmap, &blknum);
  402. if (likely(!ret))
  403. *desc_blocks = DIV_ROUND_UP(
  404. (unsigned long)blknum,
  405. NILFS_MDT(inode)->mi_blocks_per_desc_block);
  406. return ret;
  407. }
  408. /**
  409. * nilfs_palloc_mdt_file_can_grow - check potential opportunity for
  410. * MDT file growing
  411. * @inode: inode of metadata file using this allocator
  412. * @desc_blocks: known current descriptor blocks count
  413. */
  414. static inline bool nilfs_palloc_mdt_file_can_grow(struct inode *inode,
  415. unsigned long desc_blocks)
  416. {
  417. return (nilfs_palloc_groups_per_desc_block(inode) * desc_blocks) <
  418. nilfs_palloc_groups_count(inode);
  419. }
  420. /**
  421. * nilfs_palloc_count_max_entries - count max number of entries that can be
  422. * described by descriptor blocks count
  423. * @inode: inode of metadata file using this allocator
  424. * @nused: current number of used entries
  425. * @nmaxp: max number of entries [out]
  426. */
  427. int nilfs_palloc_count_max_entries(struct inode *inode, u64 nused, u64 *nmaxp)
  428. {
  429. unsigned long desc_blocks = 0;
  430. u64 entries_per_desc_block, nmax;
  431. int err;
  432. err = nilfs_palloc_count_desc_blocks(inode, &desc_blocks);
  433. if (unlikely(err))
  434. return err;
  435. entries_per_desc_block = (u64)nilfs_palloc_entries_per_group(inode) *
  436. nilfs_palloc_groups_per_desc_block(inode);
  437. nmax = entries_per_desc_block * desc_blocks;
  438. if (nused == nmax &&
  439. nilfs_palloc_mdt_file_can_grow(inode, desc_blocks))
  440. nmax += entries_per_desc_block;
  441. if (nused > nmax)
  442. return -ERANGE;
  443. *nmaxp = nmax;
  444. return 0;
  445. }
  446. /**
  447. * nilfs_palloc_prepare_alloc_entry - prepare to allocate a persistent object
  448. * @inode: inode of metadata file using this allocator
  449. * @req: nilfs_palloc_req structure exchanged for the allocation
  450. */
  451. int nilfs_palloc_prepare_alloc_entry(struct inode *inode,
  452. struct nilfs_palloc_req *req)
  453. {
  454. struct buffer_head *desc_bh, *bitmap_bh;
  455. struct nilfs_palloc_group_desc *desc;
  456. unsigned char *bitmap;
  457. void *desc_kaddr, *bitmap_kaddr;
  458. unsigned long group, maxgroup, ngroups;
  459. unsigned long group_offset, maxgroup_offset;
  460. unsigned long n, entries_per_group;
  461. unsigned long i, j;
  462. spinlock_t *lock;
  463. int pos, ret;
  464. ngroups = nilfs_palloc_groups_count(inode);
  465. maxgroup = ngroups - 1;
  466. group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
  467. entries_per_group = nilfs_palloc_entries_per_group(inode);
  468. for (i = 0; i < ngroups; i += n) {
  469. if (group >= ngroups) {
  470. /* wrap around */
  471. group = 0;
  472. maxgroup = nilfs_palloc_group(inode, req->pr_entry_nr,
  473. &maxgroup_offset) - 1;
  474. }
  475. ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh);
  476. if (ret < 0)
  477. return ret;
  478. desc_kaddr = kmap(desc_bh->b_page);
  479. desc = nilfs_palloc_block_get_group_desc(
  480. inode, group, desc_bh, desc_kaddr);
  481. n = nilfs_palloc_rest_groups_in_desc_block(inode, group,
  482. maxgroup);
  483. for (j = 0; j < n; j++, desc++, group++) {
  484. lock = nilfs_mdt_bgl_lock(inode, group);
  485. if (nilfs_palloc_group_desc_nfrees(desc, lock) > 0) {
  486. ret = nilfs_palloc_get_bitmap_block(
  487. inode, group, 1, &bitmap_bh);
  488. if (ret < 0)
  489. goto out_desc;
  490. bitmap_kaddr = kmap(bitmap_bh->b_page);
  491. bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
  492. pos = nilfs_palloc_find_available_slot(
  493. bitmap, group_offset,
  494. entries_per_group, lock);
  495. if (pos >= 0) {
  496. /* found a free entry */
  497. nilfs_palloc_group_desc_add_entries(
  498. desc, lock, -1);
  499. req->pr_entry_nr =
  500. entries_per_group * group + pos;
  501. kunmap(desc_bh->b_page);
  502. kunmap(bitmap_bh->b_page);
  503. req->pr_desc_bh = desc_bh;
  504. req->pr_bitmap_bh = bitmap_bh;
  505. return 0;
  506. }
  507. kunmap(bitmap_bh->b_page);
  508. brelse(bitmap_bh);
  509. }
  510. group_offset = 0;
  511. }
  512. kunmap(desc_bh->b_page);
  513. brelse(desc_bh);
  514. }
  515. /* no entries left */
  516. return -ENOSPC;
  517. out_desc:
  518. kunmap(desc_bh->b_page);
  519. brelse(desc_bh);
  520. return ret;
  521. }
  522. /**
  523. * nilfs_palloc_commit_alloc_entry - finish allocation of a persistent object
  524. * @inode: inode of metadata file using this allocator
  525. * @req: nilfs_palloc_req structure exchanged for the allocation
  526. */
  527. void nilfs_palloc_commit_alloc_entry(struct inode *inode,
  528. struct nilfs_palloc_req *req)
  529. {
  530. mark_buffer_dirty(req->pr_bitmap_bh);
  531. mark_buffer_dirty(req->pr_desc_bh);
  532. nilfs_mdt_mark_dirty(inode);
  533. brelse(req->pr_bitmap_bh);
  534. brelse(req->pr_desc_bh);
  535. }
  536. /**
  537. * nilfs_palloc_commit_free_entry - finish deallocating a persistent object
  538. * @inode: inode of metadata file using this allocator
  539. * @req: nilfs_palloc_req structure exchanged for the removal
  540. */
  541. void nilfs_palloc_commit_free_entry(struct inode *inode,
  542. struct nilfs_palloc_req *req)
  543. {
  544. struct nilfs_palloc_group_desc *desc;
  545. unsigned long group, group_offset;
  546. unsigned char *bitmap;
  547. void *desc_kaddr, *bitmap_kaddr;
  548. spinlock_t *lock;
  549. group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
  550. desc_kaddr = kmap(req->pr_desc_bh->b_page);
  551. desc = nilfs_palloc_block_get_group_desc(inode, group,
  552. req->pr_desc_bh, desc_kaddr);
  553. bitmap_kaddr = kmap(req->pr_bitmap_bh->b_page);
  554. bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh);
  555. lock = nilfs_mdt_bgl_lock(inode, group);
  556. if (!nilfs_clear_bit_atomic(lock, group_offset, bitmap))
  557. nilfs_msg(inode->i_sb, KERN_WARNING,
  558. "%s (ino=%lu): entry number %llu already freed",
  559. __func__, inode->i_ino,
  560. (unsigned long long)req->pr_entry_nr);
  561. else
  562. nilfs_palloc_group_desc_add_entries(desc, lock, 1);
  563. kunmap(req->pr_bitmap_bh->b_page);
  564. kunmap(req->pr_desc_bh->b_page);
  565. mark_buffer_dirty(req->pr_desc_bh);
  566. mark_buffer_dirty(req->pr_bitmap_bh);
  567. nilfs_mdt_mark_dirty(inode);
  568. brelse(req->pr_bitmap_bh);
  569. brelse(req->pr_desc_bh);
  570. }
  571. /**
  572. * nilfs_palloc_abort_alloc_entry - cancel allocation of a persistent object
  573. * @inode: inode of metadata file using this allocator
  574. * @req: nilfs_palloc_req structure exchanged for the allocation
  575. */
  576. void nilfs_palloc_abort_alloc_entry(struct inode *inode,
  577. struct nilfs_palloc_req *req)
  578. {
  579. struct nilfs_palloc_group_desc *desc;
  580. void *desc_kaddr, *bitmap_kaddr;
  581. unsigned char *bitmap;
  582. unsigned long group, group_offset;
  583. spinlock_t *lock;
  584. group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
  585. desc_kaddr = kmap(req->pr_desc_bh->b_page);
  586. desc = nilfs_palloc_block_get_group_desc(inode, group,
  587. req->pr_desc_bh, desc_kaddr);
  588. bitmap_kaddr = kmap(req->pr_bitmap_bh->b_page);
  589. bitmap = bitmap_kaddr + bh_offset(req->pr_bitmap_bh);
  590. lock = nilfs_mdt_bgl_lock(inode, group);
  591. if (!nilfs_clear_bit_atomic(lock, group_offset, bitmap))
  592. nilfs_msg(inode->i_sb, KERN_WARNING,
  593. "%s (ino=%lu): entry number %llu already freed",
  594. __func__, inode->i_ino,
  595. (unsigned long long)req->pr_entry_nr);
  596. else
  597. nilfs_palloc_group_desc_add_entries(desc, lock, 1);
  598. kunmap(req->pr_bitmap_bh->b_page);
  599. kunmap(req->pr_desc_bh->b_page);
  600. brelse(req->pr_bitmap_bh);
  601. brelse(req->pr_desc_bh);
  602. req->pr_entry_nr = 0;
  603. req->pr_bitmap_bh = NULL;
  604. req->pr_desc_bh = NULL;
  605. }
  606. /**
  607. * nilfs_palloc_prepare_free_entry - prepare to deallocate a persistent object
  608. * @inode: inode of metadata file using this allocator
  609. * @req: nilfs_palloc_req structure exchanged for the removal
  610. */
  611. int nilfs_palloc_prepare_free_entry(struct inode *inode,
  612. struct nilfs_palloc_req *req)
  613. {
  614. struct buffer_head *desc_bh, *bitmap_bh;
  615. unsigned long group, group_offset;
  616. int ret;
  617. group = nilfs_palloc_group(inode, req->pr_entry_nr, &group_offset);
  618. ret = nilfs_palloc_get_desc_block(inode, group, 1, &desc_bh);
  619. if (ret < 0)
  620. return ret;
  621. ret = nilfs_palloc_get_bitmap_block(inode, group, 1, &bitmap_bh);
  622. if (ret < 0) {
  623. brelse(desc_bh);
  624. return ret;
  625. }
  626. req->pr_desc_bh = desc_bh;
  627. req->pr_bitmap_bh = bitmap_bh;
  628. return 0;
  629. }
  630. /**
  631. * nilfs_palloc_abort_free_entry - cancel deallocating a persistent object
  632. * @inode: inode of metadata file using this allocator
  633. * @req: nilfs_palloc_req structure exchanged for the removal
  634. */
  635. void nilfs_palloc_abort_free_entry(struct inode *inode,
  636. struct nilfs_palloc_req *req)
  637. {
  638. brelse(req->pr_bitmap_bh);
  639. brelse(req->pr_desc_bh);
  640. req->pr_entry_nr = 0;
  641. req->pr_bitmap_bh = NULL;
  642. req->pr_desc_bh = NULL;
  643. }
  644. /**
  645. * nilfs_palloc_freev - deallocate a set of persistent objects
  646. * @inode: inode of metadata file using this allocator
  647. * @entry_nrs: array of entry numbers to be deallocated
  648. * @nitems: number of entries stored in @entry_nrs
  649. */
  650. int nilfs_palloc_freev(struct inode *inode, __u64 *entry_nrs, size_t nitems)
  651. {
  652. struct buffer_head *desc_bh, *bitmap_bh;
  653. struct nilfs_palloc_group_desc *desc;
  654. unsigned char *bitmap;
  655. void *desc_kaddr, *bitmap_kaddr;
  656. unsigned long group, group_offset;
  657. __u64 group_min_nr, last_nrs[8];
  658. const unsigned long epg = nilfs_palloc_entries_per_group(inode);
  659. const unsigned int epb = NILFS_MDT(inode)->mi_entries_per_block;
  660. unsigned int entry_start, end, pos;
  661. spinlock_t *lock;
  662. int i, j, k, ret;
  663. u32 nfree;
  664. for (i = 0; i < nitems; i = j) {
  665. int change_group = false;
  666. int nempties = 0, n = 0;
  667. group = nilfs_palloc_group(inode, entry_nrs[i], &group_offset);
  668. ret = nilfs_palloc_get_desc_block(inode, group, 0, &desc_bh);
  669. if (ret < 0)
  670. return ret;
  671. ret = nilfs_palloc_get_bitmap_block(inode, group, 0,
  672. &bitmap_bh);
  673. if (ret < 0) {
  674. brelse(desc_bh);
  675. return ret;
  676. }
  677. /* Get the first entry number of the group */
  678. group_min_nr = (__u64)group * epg;
  679. bitmap_kaddr = kmap(bitmap_bh->b_page);
  680. bitmap = bitmap_kaddr + bh_offset(bitmap_bh);
  681. lock = nilfs_mdt_bgl_lock(inode, group);
  682. j = i;
  683. entry_start = rounddown(group_offset, epb);
  684. do {
  685. if (!nilfs_clear_bit_atomic(lock, group_offset,
  686. bitmap)) {
  687. nilfs_msg(inode->i_sb, KERN_WARNING,
  688. "%s (ino=%lu): entry number %llu already freed",
  689. __func__, inode->i_ino,
  690. (unsigned long long)entry_nrs[j]);
  691. } else {
  692. n++;
  693. }
  694. j++;
  695. if (j >= nitems || entry_nrs[j] < group_min_nr ||
  696. entry_nrs[j] >= group_min_nr + epg) {
  697. change_group = true;
  698. } else {
  699. group_offset = entry_nrs[j] - group_min_nr;
  700. if (group_offset >= entry_start &&
  701. group_offset < entry_start + epb) {
  702. /* This entry is in the same block */
  703. continue;
  704. }
  705. }
  706. /* Test if the entry block is empty or not */
  707. end = entry_start + epb;
  708. pos = nilfs_find_next_bit(bitmap, end, entry_start);
  709. if (pos >= end) {
  710. last_nrs[nempties++] = entry_nrs[j - 1];
  711. if (nempties >= ARRAY_SIZE(last_nrs))
  712. break;
  713. }
  714. if (change_group)
  715. break;
  716. /* Go on to the next entry block */
  717. entry_start = rounddown(group_offset, epb);
  718. } while (true);
  719. kunmap(bitmap_bh->b_page);
  720. mark_buffer_dirty(bitmap_bh);
  721. brelse(bitmap_bh);
  722. for (k = 0; k < nempties; k++) {
  723. ret = nilfs_palloc_delete_entry_block(inode,
  724. last_nrs[k]);
  725. if (ret && ret != -ENOENT)
  726. nilfs_msg(inode->i_sb, KERN_WARNING,
  727. "error %d deleting block that object (entry=%llu, ino=%lu) belongs to",
  728. ret, (unsigned long long)last_nrs[k],
  729. inode->i_ino);
  730. }
  731. desc_kaddr = kmap_atomic(desc_bh->b_page);
  732. desc = nilfs_palloc_block_get_group_desc(
  733. inode, group, desc_bh, desc_kaddr);
  734. nfree = nilfs_palloc_group_desc_add_entries(desc, lock, n);
  735. kunmap_atomic(desc_kaddr);
  736. mark_buffer_dirty(desc_bh);
  737. nilfs_mdt_mark_dirty(inode);
  738. brelse(desc_bh);
  739. if (nfree == nilfs_palloc_entries_per_group(inode)) {
  740. ret = nilfs_palloc_delete_bitmap_block(inode, group);
  741. if (ret && ret != -ENOENT)
  742. nilfs_msg(inode->i_sb, KERN_WARNING,
  743. "error %d deleting bitmap block of group=%lu, ino=%lu",
  744. ret, group, inode->i_ino);
  745. }
  746. }
  747. return 0;
  748. }
  749. void nilfs_palloc_setup_cache(struct inode *inode,
  750. struct nilfs_palloc_cache *cache)
  751. {
  752. NILFS_MDT(inode)->mi_palloc_cache = cache;
  753. spin_lock_init(&cache->lock);
  754. }
  755. void nilfs_palloc_clear_cache(struct inode *inode)
  756. {
  757. struct nilfs_palloc_cache *cache = NILFS_MDT(inode)->mi_palloc_cache;
  758. spin_lock(&cache->lock);
  759. brelse(cache->prev_desc.bh);
  760. brelse(cache->prev_bitmap.bh);
  761. brelse(cache->prev_entry.bh);
  762. cache->prev_desc.bh = NULL;
  763. cache->prev_bitmap.bh = NULL;
  764. cache->prev_entry.bh = NULL;
  765. spin_unlock(&cache->lock);
  766. }
  767. void nilfs_palloc_destroy_cache(struct inode *inode)
  768. {
  769. nilfs_palloc_clear_cache(inode);
  770. NILFS_MDT(inode)->mi_palloc_cache = NULL;
  771. }