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