[GFS2] Readpages support
[pandora-kernel.git] / fs / gfs2 / unlinked.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2005 Red Hat, Inc.  All rights reserved.
4  *
5  * This copyrighted material is made available to anyone wishing to use,
6  * modify, copy, or redistribute it subject to the terms and conditions
7  * of the GNU General Public License v.2.
8  */
9
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/kthread.h>
16 #include <linux/gfs2_ondisk.h>
17 #include <asm/semaphore.h>
18
19 #include "gfs2.h"
20 #include "lm_interface.h"
21 #include "incore.h"
22 #include "bmap.h"
23 #include "inode.h"
24 #include "meta_io.h"
25 #include "trans.h"
26 #include "unlinked.h"
27 #include "util.h"
28
29 static int munge_ondisk(struct gfs2_sbd *sdp, unsigned int slot,
30                         struct gfs2_unlinked_tag *ut)
31 {
32         struct gfs2_inode *ip = sdp->sd_ut_inode->u.generic_ip;
33         unsigned int block, offset;
34         uint64_t dblock;
35         int new = 0;
36         struct buffer_head *bh;
37         int error;
38         int boundary;
39
40         block = slot / sdp->sd_ut_per_block;
41         offset = slot % sdp->sd_ut_per_block;
42
43         error = gfs2_block_map(ip->i_vnode, block, &new, &dblock, &boundary);
44         if (error)
45                 return error;
46         error = gfs2_meta_read(ip->i_gl, dblock, DIO_START | DIO_WAIT, &bh);
47         if (error)
48                 return error;
49         if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_UT)) {
50                 error = -EIO;
51                 goto out;
52         }
53
54         mutex_lock(&sdp->sd_unlinked_mutex);
55         gfs2_trans_add_bh(ip->i_gl, bh, 1);
56         gfs2_unlinked_tag_out(ut, bh->b_data +
57                                   sizeof(struct gfs2_meta_header) +
58                                   offset * sizeof(struct gfs2_unlinked_tag));
59         mutex_unlock(&sdp->sd_unlinked_mutex);
60
61  out:
62         brelse(bh);
63
64         return error;
65 }
66
67 static void ul_hash(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
68 {
69         spin_lock(&sdp->sd_unlinked_spin);
70         list_add(&ul->ul_list, &sdp->sd_unlinked_list);
71         gfs2_assert(sdp, ul->ul_count);
72         ul->ul_count++;
73         atomic_inc(&sdp->sd_unlinked_count);
74         spin_unlock(&sdp->sd_unlinked_spin);
75 }
76
77 static void ul_unhash(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
78 {
79         spin_lock(&sdp->sd_unlinked_spin);
80         list_del_init(&ul->ul_list);
81         gfs2_assert(sdp, ul->ul_count > 1);
82         ul->ul_count--;
83         gfs2_assert_warn(sdp, atomic_read(&sdp->sd_unlinked_count) > 0);
84         atomic_dec(&sdp->sd_unlinked_count);
85         spin_unlock(&sdp->sd_unlinked_spin);
86 }
87
88 static struct gfs2_unlinked *ul_fish(struct gfs2_sbd *sdp)
89 {
90         struct list_head *head;
91         struct gfs2_unlinked *ul;
92         int found = 0;
93
94         if (sdp->sd_vfs->s_flags & MS_RDONLY)
95                 return NULL;
96
97         spin_lock(&sdp->sd_unlinked_spin);
98
99         head = &sdp->sd_unlinked_list;
100
101         list_for_each_entry(ul, head, ul_list) {
102                 if (test_bit(ULF_LOCKED, &ul->ul_flags))
103                         continue;
104
105                 list_move_tail(&ul->ul_list, head);
106                 ul->ul_count++;
107                 set_bit(ULF_LOCKED, &ul->ul_flags);
108                 found = 1;
109
110                 break;
111         }
112
113         if (!found)
114                 ul = NULL;
115
116         spin_unlock(&sdp->sd_unlinked_spin);
117
118         return ul;
119 }
120
121 /**
122  * enforce_limit - limit the number of inodes waiting to be deallocated
123  * @sdp: the filesystem
124  *
125  * Returns: errno
126  */
127
128 static void enforce_limit(struct gfs2_sbd *sdp)
129 {
130         unsigned int tries = 0, min = 0;
131         int error;
132
133         if (atomic_read(&sdp->sd_unlinked_count) >=
134             gfs2_tune_get(sdp, gt_ilimit)) {
135                 tries = gfs2_tune_get(sdp, gt_ilimit_tries);
136                 min = gfs2_tune_get(sdp, gt_ilimit_min);
137         }
138
139         while (tries--) {
140                 struct gfs2_unlinked *ul = ul_fish(sdp);
141                 if (!ul)
142                         break;
143                 error = gfs2_inode_dealloc(sdp, ul);
144                 gfs2_unlinked_put(sdp, ul);
145
146                 if (!error) {
147                         if (!--min)
148                                 break;
149                 } else if (error != 1)
150                         break;
151         }
152 }
153
154 static struct gfs2_unlinked *ul_alloc(struct gfs2_sbd *sdp)
155 {
156         struct gfs2_unlinked *ul;
157
158         ul = kzalloc(sizeof(struct gfs2_unlinked), GFP_KERNEL);
159         if (ul) {
160                 INIT_LIST_HEAD(&ul->ul_list);
161                 ul->ul_count = 1;
162                 set_bit(ULF_LOCKED, &ul->ul_flags);
163         }
164
165         return ul;
166 }
167
168 int gfs2_unlinked_get(struct gfs2_sbd *sdp, struct gfs2_unlinked **ul)
169 {
170         unsigned int c, o = 0, b;
171         unsigned char byte = 0;
172
173         enforce_limit(sdp);
174
175         *ul = ul_alloc(sdp);
176         if (!*ul)
177                 return -ENOMEM;
178
179         spin_lock(&sdp->sd_unlinked_spin);
180
181         for (c = 0; c < sdp->sd_unlinked_chunks; c++)
182                 for (o = 0; o < PAGE_SIZE; o++) {
183                         byte = sdp->sd_unlinked_bitmap[c][o];
184                         if (byte != 0xFF)
185                                 goto found;
186                 }
187
188         goto fail;
189
190  found:
191         for (b = 0; b < 8; b++)
192                 if (!(byte & (1 << b)))
193                         break;
194         (*ul)->ul_slot = c * (8 * PAGE_SIZE) + o * 8 + b;
195
196         if ((*ul)->ul_slot >= sdp->sd_unlinked_slots)
197                 goto fail;
198
199         sdp->sd_unlinked_bitmap[c][o] |= 1 << b;
200
201         spin_unlock(&sdp->sd_unlinked_spin);
202
203         return 0;
204
205  fail:
206         spin_unlock(&sdp->sd_unlinked_spin);
207         kfree(*ul);
208         return -ENOSPC;
209 }
210
211 void gfs2_unlinked_put(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
212 {
213         gfs2_assert_warn(sdp, test_and_clear_bit(ULF_LOCKED, &ul->ul_flags));
214
215         spin_lock(&sdp->sd_unlinked_spin);
216         gfs2_assert(sdp, ul->ul_count);
217         ul->ul_count--;
218         if (!ul->ul_count) {
219                 gfs2_icbit_munge(sdp, sdp->sd_unlinked_bitmap, ul->ul_slot, 0);
220                 spin_unlock(&sdp->sd_unlinked_spin);
221                 kfree(ul);
222         } else
223                 spin_unlock(&sdp->sd_unlinked_spin);
224 }
225
226 int gfs2_unlinked_ondisk_add(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
227 {
228         int error;
229
230         gfs2_assert_warn(sdp, test_bit(ULF_LOCKED, &ul->ul_flags));
231         gfs2_assert_warn(sdp, list_empty(&ul->ul_list));
232
233         error = munge_ondisk(sdp, ul->ul_slot, &ul->ul_ut);
234         if (!error)
235                 ul_hash(sdp, ul);
236
237         return error;
238 }
239
240 int gfs2_unlinked_ondisk_munge(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
241 {
242         int error;
243
244         gfs2_assert_warn(sdp, test_bit(ULF_LOCKED, &ul->ul_flags));
245         gfs2_assert_warn(sdp, !list_empty(&ul->ul_list));
246
247         error = munge_ondisk(sdp, ul->ul_slot, &ul->ul_ut);
248
249         return error;
250 }
251
252 int gfs2_unlinked_ondisk_rm(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
253 {
254         struct gfs2_unlinked_tag ut;
255         int error;
256
257         gfs2_assert_warn(sdp, test_bit(ULF_LOCKED, &ul->ul_flags));
258         gfs2_assert_warn(sdp, !list_empty(&ul->ul_list));
259
260         memset(&ut, 0, sizeof(struct gfs2_unlinked_tag));
261
262         error = munge_ondisk(sdp, ul->ul_slot, &ut);
263         if (error)
264                 return error;
265
266         ul_unhash(sdp, ul);
267
268         return 0;
269 }
270
271 /**
272  * gfs2_unlinked_dealloc - Go through the list of inodes to be deallocated
273  * @sdp: the filesystem
274  *
275  * Returns: errno
276  */
277
278 int gfs2_unlinked_dealloc(struct gfs2_sbd *sdp)
279 {
280         unsigned int hits, strikes;
281         int error;
282
283         for (;;) {
284                 hits = 0;
285                 strikes = 0;
286
287                 for (;;) {
288                         struct gfs2_unlinked *ul = ul_fish(sdp);
289                         if (!ul)
290                                 return 0;
291                         error = gfs2_inode_dealloc(sdp, ul);
292                         gfs2_unlinked_put(sdp, ul);
293
294                         if (!error) {
295                                 hits++;
296                                 if (strikes)
297                                         strikes--;
298                         } else if (error == 1) {
299                                 strikes++;
300                                 if (strikes >=
301                                     atomic_read(&sdp->sd_unlinked_count)) {
302                                         error = 0;
303                                         break;
304                                 }
305                         } else
306                                 return error;
307                 }
308
309                 if (!hits || kthread_should_stop())
310                         break;
311
312                 cond_resched();
313         }
314
315         return 0;
316 }
317
318 int gfs2_unlinked_init(struct gfs2_sbd *sdp)
319 {
320         struct gfs2_inode *ip = sdp->sd_ut_inode->u.generic_ip;
321         unsigned int blocks = ip->i_di.di_size >> sdp->sd_sb.sb_bsize_shift;
322         unsigned int x, slot = 0;
323         unsigned int found = 0;
324         uint64_t dblock;
325         uint32_t extlen = 0;
326         int error;
327
328         if (!ip->i_di.di_size ||
329             ip->i_di.di_size > (64 << 20) ||
330             ip->i_di.di_size & (sdp->sd_sb.sb_bsize - 1)) {
331                 gfs2_consist_inode(ip);
332                 return -EIO;            
333         }
334         sdp->sd_unlinked_slots = blocks * sdp->sd_ut_per_block;
335         sdp->sd_unlinked_chunks = DIV_ROUND_UP(sdp->sd_unlinked_slots,
336                                                8 * PAGE_SIZE);
337
338         error = -ENOMEM;
339
340         sdp->sd_unlinked_bitmap = kcalloc(sdp->sd_unlinked_chunks,
341                                           sizeof(unsigned char *),
342                                           GFP_KERNEL);
343         if (!sdp->sd_unlinked_bitmap)
344                 return error;
345
346         for (x = 0; x < sdp->sd_unlinked_chunks; x++) {
347                 sdp->sd_unlinked_bitmap[x] = kzalloc(PAGE_SIZE, GFP_KERNEL);
348                 if (!sdp->sd_unlinked_bitmap[x])
349                         goto fail;
350         }
351
352         for (x = 0; x < blocks; x++) {
353                 struct buffer_head *bh;
354                 unsigned int y;
355
356                 if (!extlen) {
357                         int new = 0;
358                         error = gfs2_extent_map(ip->i_vnode, x, &new, &dblock, &extlen);
359                         if (error)
360                                 goto fail;
361                 }
362                 gfs2_meta_ra(ip->i_gl, dblock, extlen);
363                 error = gfs2_meta_read(ip->i_gl, dblock, DIO_START | DIO_WAIT,
364                                        &bh);
365                 if (error)
366                         goto fail;
367                 error = -EIO;
368                 if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_UT)) {
369                         brelse(bh);
370                         goto fail;
371                 }
372
373                 for (y = 0;
374                      y < sdp->sd_ut_per_block && slot < sdp->sd_unlinked_slots;
375                      y++, slot++) {
376                         struct gfs2_unlinked_tag ut;
377                         struct gfs2_unlinked *ul;
378
379                         gfs2_unlinked_tag_in(&ut, bh->b_data +
380                                           sizeof(struct gfs2_meta_header) +
381                                           y * sizeof(struct gfs2_unlinked_tag));
382                         if (!ut.ut_inum.no_addr)
383                                 continue;
384
385                         error = -ENOMEM;
386                         ul = ul_alloc(sdp);
387                         if (!ul) {
388                                 brelse(bh);
389                                 goto fail;
390                         }
391                         ul->ul_ut = ut;
392                         ul->ul_slot = slot;
393
394                         spin_lock(&sdp->sd_unlinked_spin);
395                         gfs2_icbit_munge(sdp, sdp->sd_unlinked_bitmap, slot, 1);
396                         spin_unlock(&sdp->sd_unlinked_spin);
397                         ul_hash(sdp, ul);
398
399                         gfs2_unlinked_put(sdp, ul);
400                         found++;
401                 }
402
403                 brelse(bh);
404                 dblock++;
405                 extlen--;
406         }
407
408         if (found)
409                 fs_info(sdp, "found %u unlinked inodes\n", found);
410
411         return 0;
412
413  fail:
414         gfs2_unlinked_cleanup(sdp);
415         return error;
416 }
417
418 /**
419  * gfs2_unlinked_cleanup - get rid of any extra struct gfs2_unlinked structures
420  * @sdp: the filesystem
421  *
422  */
423
424 void gfs2_unlinked_cleanup(struct gfs2_sbd *sdp)
425 {
426         struct list_head *head = &sdp->sd_unlinked_list;
427         struct gfs2_unlinked *ul;
428         unsigned int x;
429
430         spin_lock(&sdp->sd_unlinked_spin);
431         while (!list_empty(head)) {
432                 ul = list_entry(head->next, struct gfs2_unlinked, ul_list);
433
434                 if (ul->ul_count > 1) {
435                         list_move_tail(&ul->ul_list, head);
436                         spin_unlock(&sdp->sd_unlinked_spin);
437                         schedule();
438                         spin_lock(&sdp->sd_unlinked_spin);
439                         continue;
440                 }
441
442                 list_del_init(&ul->ul_list);
443                 atomic_dec(&sdp->sd_unlinked_count);
444
445                 gfs2_assert_warn(sdp, ul->ul_count == 1);
446                 gfs2_assert_warn(sdp, !test_bit(ULF_LOCKED, &ul->ul_flags));
447                 kfree(ul);
448         }
449         spin_unlock(&sdp->sd_unlinked_spin);
450
451         gfs2_assert_warn(sdp, !atomic_read(&sdp->sd_unlinked_count));
452
453         if (sdp->sd_unlinked_bitmap) {
454                 for (x = 0; x < sdp->sd_unlinked_chunks; x++)
455                         kfree(sdp->sd_unlinked_bitmap[x]);
456                 kfree(sdp->sd_unlinked_bitmap);
457         }
458 }
459