[GFS2] Fix a ref count bug and other clean ups
[safe/jmp/linux-2.6] / fs / gfs2 / glops.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/gfs2_ondisk.h>
16 #include <asm/semaphore.h>
17
18 #include "gfs2.h"
19 #include "lm_interface.h"
20 #include "incore.h"
21 #include "bmap.h"
22 #include "glock.h"
23 #include "glops.h"
24 #include "inode.h"
25 #include "log.h"
26 #include "meta_io.h"
27 #include "page.h"
28 #include "recovery.h"
29 #include "rgrp.h"
30 #include "util.h"
31
32 /**
33  * meta_go_sync - sync out the metadata for this glock
34  * @gl: the glock
35  * @flags: DIO_*
36  *
37  * Called when demoting or unlocking an EX glock.  We must flush
38  * to disk all dirty buffers/pages relating to this glock, and must not
39  * not return to caller to demote/unlock the glock until I/O is complete.
40  */
41
42 static void meta_go_sync(struct gfs2_glock *gl, int flags)
43 {
44         if (!(flags & DIO_METADATA))
45                 return;
46
47         if (test_and_clear_bit(GLF_DIRTY, &gl->gl_flags)) {
48                 gfs2_log_flush(gl->gl_sbd, gl);
49                 gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
50                 if (flags & DIO_RELEASE)
51                         gfs2_ail_empty_gl(gl);
52         }
53
54         clear_bit(GLF_SYNC, &gl->gl_flags);
55 }
56
57 /**
58  * meta_go_inval - invalidate the metadata for this glock
59  * @gl: the glock
60  * @flags:
61  *
62  */
63
64 static void meta_go_inval(struct gfs2_glock *gl, int flags)
65 {
66         if (!(flags & DIO_METADATA))
67                 return;
68
69         gfs2_meta_inval(gl);
70         gl->gl_vn++;
71 }
72
73 /**
74  * meta_go_demote_ok - Check to see if it's ok to unlock a glock
75  * @gl: the glock
76  *
77  * Returns: 1 if we have no cached data; ok to demote meta glock
78  */
79
80 static int meta_go_demote_ok(struct gfs2_glock *gl)
81 {
82         return !gl->gl_aspace->i_mapping->nrpages;
83 }
84
85 /**
86  * inode_go_xmote_th - promote/demote a glock
87  * @gl: the glock
88  * @state: the requested state
89  * @flags:
90  *
91  */
92
93 static void inode_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
94                               int flags)
95 {
96         if (gl->gl_state != LM_ST_UNLOCKED)
97                 gfs2_pte_inval(gl);
98         gfs2_glock_xmote_th(gl, state, flags);
99 }
100
101 /**
102  * inode_go_xmote_bh - After promoting/demoting a glock
103  * @gl: the glock
104  *
105  */
106
107 static void inode_go_xmote_bh(struct gfs2_glock *gl)
108 {
109         struct gfs2_holder *gh = gl->gl_req_gh;
110         struct buffer_head *bh;
111         int error;
112
113         if (gl->gl_state != LM_ST_UNLOCKED &&
114             (!gh || !(gh->gh_flags & GL_SKIP))) {
115                 error = gfs2_meta_read(gl, gl->gl_name.ln_number, DIO_START,
116                                        &bh);
117                 if (!error)
118                         brelse(bh);
119         }
120 }
121
122 /**
123  * inode_go_drop_th - unlock a glock
124  * @gl: the glock
125  *
126  * Invoked from rq_demote().
127  * Another node needs the lock in EXCLUSIVE mode, or lock (unused for too long)
128  * is being purged from our node's glock cache; we're dropping lock.
129  */
130
131 static void inode_go_drop_th(struct gfs2_glock *gl)
132 {
133         gfs2_pte_inval(gl);
134         gfs2_glock_drop_th(gl);
135 }
136
137 /**
138  * inode_go_sync - Sync the dirty data and/or metadata for an inode glock
139  * @gl: the glock protecting the inode
140  * @flags:
141  *
142  */
143
144 static void inode_go_sync(struct gfs2_glock *gl, int flags)
145 {
146         int meta = (flags & DIO_METADATA);
147         int data = (flags & DIO_DATA);
148
149         if (test_bit(GLF_DIRTY, &gl->gl_flags)) {
150                 if (meta && data) {
151                         gfs2_page_sync(gl, flags | DIO_START);
152                         gfs2_log_flush(gl->gl_sbd, gl);
153                         gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
154                         gfs2_page_sync(gl, flags | DIO_WAIT);
155                         clear_bit(GLF_DIRTY, &gl->gl_flags);
156                 } else if (meta) {
157                         gfs2_log_flush(gl->gl_sbd, gl);
158                         gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
159                 } else if (data)
160                         gfs2_page_sync(gl, flags | DIO_START | DIO_WAIT);
161                 if (flags & DIO_RELEASE)
162                         gfs2_ail_empty_gl(gl);
163         }
164
165         clear_bit(GLF_SYNC, &gl->gl_flags);
166 }
167
168 /**
169  * inode_go_inval - prepare a inode glock to be released
170  * @gl: the glock
171  * @flags:
172  *
173  */
174
175 static void inode_go_inval(struct gfs2_glock *gl, int flags)
176 {
177         int meta = (flags & DIO_METADATA);
178         int data = (flags & DIO_DATA);
179
180         if (meta) {
181                 gfs2_meta_inval(gl);
182                 gl->gl_vn++;
183         }
184         if (data)
185                 gfs2_page_inval(gl);
186 }
187
188 /**
189  * inode_go_demote_ok - Check to see if it's ok to unlock an inode glock
190  * @gl: the glock
191  *
192  * Returns: 1 if it's ok
193  */
194
195 static int inode_go_demote_ok(struct gfs2_glock *gl)
196 {
197         struct gfs2_sbd *sdp = gl->gl_sbd;
198         int demote = 0;
199
200         if (!gl->gl_object && !gl->gl_aspace->i_mapping->nrpages)
201                 demote = 1;
202         else if (!sdp->sd_args.ar_localcaching &&
203                  time_after_eq(jiffies, gl->gl_stamp +
204                                gfs2_tune_get(sdp, gt_demote_secs) * HZ))
205                 demote = 1;
206
207         return demote;
208 }
209
210 /**
211  * inode_go_lock - operation done after an inode lock is locked by a process
212  * @gl: the glock
213  * @flags:
214  *
215  * Returns: errno
216  */
217
218 static int inode_go_lock(struct gfs2_holder *gh)
219 {
220         struct gfs2_glock *gl = gh->gh_gl;
221         struct gfs2_inode *ip = gl->gl_object;
222         int error = 0;
223
224         if (!ip)
225                 return 0;
226
227         if (ip->i_vn != gl->gl_vn) {
228                 error = gfs2_inode_refresh(ip);
229                 if (error)
230                         return error;
231                 gfs2_inode_attr_in(ip);
232         }
233
234         if ((ip->i_di.di_flags & GFS2_DIF_TRUNC_IN_PROG) &&
235             (gl->gl_state == LM_ST_EXCLUSIVE) &&
236             (gh->gh_flags & GL_LOCAL_EXCL))
237                 error = gfs2_truncatei_resume(ip);
238
239         return error;
240 }
241
242 /**
243  * inode_go_unlock - operation done before an inode lock is unlocked by a
244  *                   process
245  * @gl: the glock
246  * @flags:
247  *
248  */
249
250 static void inode_go_unlock(struct gfs2_holder *gh)
251 {
252         struct gfs2_glock *gl = gh->gh_gl;
253         struct gfs2_inode *ip = gl->gl_object;
254
255         if (ip && test_bit(GLF_DIRTY, &gl->gl_flags))
256                 gfs2_inode_attr_in(ip);
257
258         if (ip)
259                 gfs2_meta_cache_flush(ip);
260 }
261
262 /**
263  * inode_greedy -
264  * @gl: the glock
265  *
266  */
267
268 static void inode_greedy(struct gfs2_glock *gl)
269 {
270         struct gfs2_sbd *sdp = gl->gl_sbd;
271         struct gfs2_inode *ip = gl->gl_object;
272         unsigned int quantum = gfs2_tune_get(sdp, gt_greedy_quantum);
273         unsigned int max = gfs2_tune_get(sdp, gt_greedy_max);
274         unsigned int new_time;
275
276         spin_lock(&ip->i_spin);
277
278         if (time_after(ip->i_last_pfault + quantum, jiffies)) {
279                 new_time = ip->i_greedy + quantum;
280                 if (new_time > max)
281                         new_time = max;
282         } else {
283                 new_time = ip->i_greedy - quantum;
284                 if (!new_time || new_time > max)
285                         new_time = 1;
286         }
287
288         ip->i_greedy = new_time;
289
290         spin_unlock(&ip->i_spin);
291
292         gfs2_inode_put(ip);
293 }
294
295 /**
296  * rgrp_go_demote_ok - Check to see if it's ok to unlock a RG's glock
297  * @gl: the glock
298  *
299  * Returns: 1 if it's ok
300  */
301
302 static int rgrp_go_demote_ok(struct gfs2_glock *gl)
303 {
304         return !gl->gl_aspace->i_mapping->nrpages;
305 }
306
307 /**
308  * rgrp_go_lock - operation done after an rgrp lock is locked by
309  *    a first holder on this node.
310  * @gl: the glock
311  * @flags:
312  *
313  * Returns: errno
314  */
315
316 static int rgrp_go_lock(struct gfs2_holder *gh)
317 {
318         return gfs2_rgrp_bh_get(gh->gh_gl->gl_object);
319 }
320
321 /**
322  * rgrp_go_unlock - operation done before an rgrp lock is unlocked by
323  *    a last holder on this node.
324  * @gl: the glock
325  * @flags:
326  *
327  */
328
329 static void rgrp_go_unlock(struct gfs2_holder *gh)
330 {
331         gfs2_rgrp_bh_put(gh->gh_gl->gl_object);
332 }
333
334 /**
335  * trans_go_xmote_th - promote/demote the transaction glock
336  * @gl: the glock
337  * @state: the requested state
338  * @flags:
339  *
340  */
341
342 static void trans_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
343                               int flags)
344 {
345         struct gfs2_sbd *sdp = gl->gl_sbd;
346
347         if (gl->gl_state != LM_ST_UNLOCKED &&
348             test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
349                 gfs2_meta_syncfs(sdp);
350                 gfs2_log_shutdown(sdp);
351         }
352
353         gfs2_glock_xmote_th(gl, state, flags);
354 }
355
356 /**
357  * trans_go_xmote_bh - After promoting/demoting the transaction glock
358  * @gl: the glock
359  *
360  */
361
362 static void trans_go_xmote_bh(struct gfs2_glock *gl)
363 {
364         struct gfs2_sbd *sdp = gl->gl_sbd;
365         struct gfs2_inode *ip = sdp->sd_jdesc->jd_inode->u.generic_ip;
366         struct gfs2_glock *j_gl = ip->i_gl;
367         struct gfs2_log_header head;
368         int error;
369
370         if (gl->gl_state != LM_ST_UNLOCKED &&
371             test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
372                 gfs2_meta_cache_flush(sdp->sd_jdesc->jd_inode->u.generic_ip);
373                 j_gl->gl_ops->go_inval(j_gl, DIO_METADATA | DIO_DATA);
374
375                 error = gfs2_find_jhead(sdp->sd_jdesc, &head);
376                 if (error)
377                         gfs2_consist(sdp);
378                 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT))
379                         gfs2_consist(sdp);
380
381                 /*  Initialize some head of the log stuff  */
382                 if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) {
383                         sdp->sd_log_sequence = head.lh_sequence + 1;
384                         gfs2_log_pointers_init(sdp, head.lh_blkno);
385                 }
386         }
387 }
388
389 /**
390  * trans_go_drop_th - unlock the transaction glock
391  * @gl: the glock
392  *
393  * We want to sync the device even with localcaching.  Remember
394  * that localcaching journal replay only marks buffers dirty.
395  */
396
397 static void trans_go_drop_th(struct gfs2_glock *gl)
398 {
399         struct gfs2_sbd *sdp = gl->gl_sbd;
400
401         if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
402                 gfs2_meta_syncfs(sdp);
403                 gfs2_log_shutdown(sdp);
404         }
405
406         gfs2_glock_drop_th(gl);
407 }
408
409 /**
410  * quota_go_demote_ok - Check to see if it's ok to unlock a quota glock
411  * @gl: the glock
412  *
413  * Returns: 1 if it's ok
414  */
415
416 static int quota_go_demote_ok(struct gfs2_glock *gl)
417 {
418         return !atomic_read(&gl->gl_lvb_count);
419 }
420
421 struct gfs2_glock_operations gfs2_meta_glops = {
422         .go_xmote_th = gfs2_glock_xmote_th,
423         .go_drop_th = gfs2_glock_drop_th,
424         .go_sync = meta_go_sync,
425         .go_inval = meta_go_inval,
426         .go_demote_ok = meta_go_demote_ok,
427         .go_type = LM_TYPE_META
428 };
429
430 struct gfs2_glock_operations gfs2_inode_glops = {
431         .go_xmote_th = inode_go_xmote_th,
432         .go_xmote_bh = inode_go_xmote_bh,
433         .go_drop_th = inode_go_drop_th,
434         .go_sync = inode_go_sync,
435         .go_inval = inode_go_inval,
436         .go_demote_ok = inode_go_demote_ok,
437         .go_lock = inode_go_lock,
438         .go_unlock = inode_go_unlock,
439         .go_greedy = inode_greedy,
440         .go_type = LM_TYPE_INODE
441 };
442
443 struct gfs2_glock_operations gfs2_rgrp_glops = {
444         .go_xmote_th = gfs2_glock_xmote_th,
445         .go_drop_th = gfs2_glock_drop_th,
446         .go_sync = meta_go_sync,
447         .go_inval = meta_go_inval,
448         .go_demote_ok = rgrp_go_demote_ok,
449         .go_lock = rgrp_go_lock,
450         .go_unlock = rgrp_go_unlock,
451         .go_type = LM_TYPE_RGRP
452 };
453
454 struct gfs2_glock_operations gfs2_trans_glops = {
455         .go_xmote_th = trans_go_xmote_th,
456         .go_xmote_bh = trans_go_xmote_bh,
457         .go_drop_th = trans_go_drop_th,
458         .go_type = LM_TYPE_NONDISK
459 };
460
461 struct gfs2_glock_operations gfs2_iopen_glops = {
462         .go_xmote_th = gfs2_glock_xmote_th,
463         .go_drop_th = gfs2_glock_drop_th,
464         .go_callback = gfs2_iopen_go_callback,
465         .go_type = LM_TYPE_IOPEN
466 };
467
468 struct gfs2_glock_operations gfs2_flock_glops = {
469         .go_xmote_th = gfs2_glock_xmote_th,
470         .go_drop_th = gfs2_glock_drop_th,
471         .go_type = LM_TYPE_FLOCK
472 };
473
474 struct gfs2_glock_operations gfs2_nondisk_glops = {
475         .go_xmote_th = gfs2_glock_xmote_th,
476         .go_drop_th = gfs2_glock_drop_th,
477         .go_type = LM_TYPE_NONDISK
478 };
479
480 struct gfs2_glock_operations gfs2_quota_glops = {
481         .go_xmote_th = gfs2_glock_xmote_th,
482         .go_drop_th = gfs2_glock_drop_th,
483         .go_demote_ok = quota_go_demote_ok,
484         .go_type = LM_TYPE_QUOTA
485 };
486
487 struct gfs2_glock_operations gfs2_journal_glops = {
488         .go_xmote_th = gfs2_glock_xmote_th,
489         .go_drop_th = gfs2_glock_drop_th,
490         .go_type = LM_TYPE_JOURNAL
491 };
492