2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
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.
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>
18 #include "lm_interface.h"
32 * meta_go_sync - sync out the metadata for this glock
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.
41 static void meta_go_sync(struct gfs2_glock *gl, int flags)
43 if (!(flags & DIO_METADATA))
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);
53 clear_bit(GLF_SYNC, &gl->gl_flags);
57 * meta_go_inval - invalidate the metadata for this glock
63 static void meta_go_inval(struct gfs2_glock *gl, int flags)
65 if (!(flags & DIO_METADATA))
73 * meta_go_demote_ok - Check to see if it's ok to unlock a glock
76 * Returns: 1 if we have no cached data; ok to demote meta glock
79 static int meta_go_demote_ok(struct gfs2_glock *gl)
81 return !gl->gl_aspace->i_mapping->nrpages;
85 * inode_go_xmote_th - promote/demote a glock
87 * @state: the requested state
92 static void inode_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
95 if (gl->gl_state != LM_ST_UNLOCKED)
97 gfs2_glock_xmote_th(gl, state, flags);
101 * inode_go_xmote_bh - After promoting/demoting a glock
106 static void inode_go_xmote_bh(struct gfs2_glock *gl)
108 struct gfs2_holder *gh = gl->gl_req_gh;
109 struct buffer_head *bh;
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,
122 * inode_go_drop_th - unlock a glock
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.
130 static void inode_go_drop_th(struct gfs2_glock *gl)
132 printk(KERN_INFO "drop th %p\n", gl->gl_object);
134 gfs2_glock_drop_th(gl);
138 * inode_go_sync - Sync the dirty data and/or metadata for an inode glock
139 * @gl: the glock protecting the inode
144 static void inode_go_sync(struct gfs2_glock *gl, int flags)
146 int meta = (flags & DIO_METADATA);
147 int data = (flags & DIO_DATA);
149 if (test_bit(GLF_DIRTY, &gl->gl_flags)) {
151 printk(KERN_INFO "sync all\n");
152 gfs2_page_sync(gl, flags | DIO_START);
153 gfs2_log_flush(gl->gl_sbd, gl);
154 gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
155 gfs2_page_sync(gl, flags | DIO_WAIT);
156 clear_bit(GLF_DIRTY, &gl->gl_flags);
158 gfs2_log_flush(gl->gl_sbd, gl);
159 gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
161 gfs2_page_sync(gl, flags | DIO_START | DIO_WAIT);
162 if (flags & DIO_RELEASE)
163 gfs2_ail_empty_gl(gl);
166 clear_bit(GLF_SYNC, &gl->gl_flags);
170 * inode_go_inval - prepare a inode glock to be released
176 static void inode_go_inval(struct gfs2_glock *gl, int flags)
178 int meta = (flags & DIO_METADATA);
179 int data = (flags & DIO_DATA);
190 * inode_go_demote_ok - Check to see if it's ok to unlock an inode glock
193 * Returns: 1 if it's ok
196 static int inode_go_demote_ok(struct gfs2_glock *gl)
198 struct gfs2_sbd *sdp = gl->gl_sbd;
201 if (!gl->gl_object && !gl->gl_aspace->i_mapping->nrpages)
203 else if (!sdp->sd_args.ar_localcaching &&
204 time_after_eq(jiffies, gl->gl_stamp +
205 gfs2_tune_get(sdp, gt_demote_secs) * HZ))
212 * inode_go_lock - operation done after an inode lock is locked by a process
219 static int inode_go_lock(struct gfs2_holder *gh)
221 struct gfs2_glock *gl = gh->gh_gl;
222 struct gfs2_inode *ip = gl->gl_object;
228 if (ip->i_vn != gl->gl_vn) {
229 printk(KERN_INFO "refresh inode %p\n", &ip->i_inode);
230 error = gfs2_inode_refresh(ip);
233 gfs2_inode_attr_in(ip);
236 if ((ip->i_di.di_flags & GFS2_DIF_TRUNC_IN_PROG) &&
237 (gl->gl_state == LM_ST_EXCLUSIVE) &&
238 (gh->gh_flags & GL_LOCAL_EXCL))
239 error = gfs2_truncatei_resume(ip);
245 * inode_go_unlock - operation done before an inode lock is unlocked by a
252 static void inode_go_unlock(struct gfs2_holder *gh)
254 struct gfs2_glock *gl = gh->gh_gl;
255 struct gfs2_inode *ip = gl->gl_object;
257 if (ip && test_bit(GLF_DIRTY, &gl->gl_flags))
258 gfs2_inode_attr_in(ip);
261 gfs2_meta_cache_flush(ip);
270 static void inode_greedy(struct gfs2_glock *gl)
272 struct gfs2_sbd *sdp = gl->gl_sbd;
273 struct gfs2_inode *ip = gl->gl_object;
274 unsigned int quantum = gfs2_tune_get(sdp, gt_greedy_quantum);
275 unsigned int max = gfs2_tune_get(sdp, gt_greedy_max);
276 unsigned int new_time;
278 spin_lock(&ip->i_spin);
280 if (time_after(ip->i_last_pfault + quantum, jiffies)) {
281 new_time = ip->i_greedy + quantum;
285 new_time = ip->i_greedy - quantum;
286 if (!new_time || new_time > max)
290 ip->i_greedy = new_time;
292 spin_unlock(&ip->i_spin);
298 * rgrp_go_demote_ok - Check to see if it's ok to unlock a RG's glock
301 * Returns: 1 if it's ok
304 static int rgrp_go_demote_ok(struct gfs2_glock *gl)
306 return !gl->gl_aspace->i_mapping->nrpages;
310 * rgrp_go_lock - operation done after an rgrp lock is locked by
311 * a first holder on this node.
318 static int rgrp_go_lock(struct gfs2_holder *gh)
320 return gfs2_rgrp_bh_get(gh->gh_gl->gl_object);
324 * rgrp_go_unlock - operation done before an rgrp lock is unlocked by
325 * a last holder on this node.
331 static void rgrp_go_unlock(struct gfs2_holder *gh)
333 gfs2_rgrp_bh_put(gh->gh_gl->gl_object);
337 * trans_go_xmote_th - promote/demote the transaction glock
339 * @state: the requested state
344 static void trans_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
347 struct gfs2_sbd *sdp = gl->gl_sbd;
349 if (gl->gl_state != LM_ST_UNLOCKED &&
350 test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
351 gfs2_meta_syncfs(sdp);
352 gfs2_log_shutdown(sdp);
355 gfs2_glock_xmote_th(gl, state, flags);
359 * trans_go_xmote_bh - After promoting/demoting the transaction glock
364 static void trans_go_xmote_bh(struct gfs2_glock *gl)
366 struct gfs2_sbd *sdp = gl->gl_sbd;
367 struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
368 struct gfs2_glock *j_gl = ip->i_gl;
369 struct gfs2_log_header head;
372 if (gl->gl_state != LM_ST_UNLOCKED &&
373 test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
374 gfs2_meta_cache_flush(GFS2_I(sdp->sd_jdesc->jd_inode));
375 j_gl->gl_ops->go_inval(j_gl, DIO_METADATA | DIO_DATA);
377 error = gfs2_find_jhead(sdp->sd_jdesc, &head);
380 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT))
383 /* Initialize some head of the log stuff */
384 if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) {
385 sdp->sd_log_sequence = head.lh_sequence + 1;
386 gfs2_log_pointers_init(sdp, head.lh_blkno);
392 * trans_go_drop_th - unlock the transaction glock
395 * We want to sync the device even with localcaching. Remember
396 * that localcaching journal replay only marks buffers dirty.
399 static void trans_go_drop_th(struct gfs2_glock *gl)
401 struct gfs2_sbd *sdp = gl->gl_sbd;
403 if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
404 gfs2_meta_syncfs(sdp);
405 gfs2_log_shutdown(sdp);
408 gfs2_glock_drop_th(gl);
412 * quota_go_demote_ok - Check to see if it's ok to unlock a quota glock
415 * Returns: 1 if it's ok
418 static int quota_go_demote_ok(struct gfs2_glock *gl)
420 return !atomic_read(&gl->gl_lvb_count);
423 struct gfs2_glock_operations gfs2_meta_glops = {
424 .go_xmote_th = gfs2_glock_xmote_th,
425 .go_drop_th = gfs2_glock_drop_th,
426 .go_sync = meta_go_sync,
427 .go_inval = meta_go_inval,
428 .go_demote_ok = meta_go_demote_ok,
429 .go_type = LM_TYPE_META
432 struct gfs2_glock_operations gfs2_inode_glops = {
433 .go_xmote_th = inode_go_xmote_th,
434 .go_xmote_bh = inode_go_xmote_bh,
435 .go_drop_th = inode_go_drop_th,
436 .go_sync = inode_go_sync,
437 .go_inval = inode_go_inval,
438 .go_demote_ok = inode_go_demote_ok,
439 .go_lock = inode_go_lock,
440 .go_unlock = inode_go_unlock,
441 .go_greedy = inode_greedy,
442 .go_type = LM_TYPE_INODE
445 struct gfs2_glock_operations gfs2_rgrp_glops = {
446 .go_xmote_th = gfs2_glock_xmote_th,
447 .go_drop_th = gfs2_glock_drop_th,
448 .go_sync = meta_go_sync,
449 .go_inval = meta_go_inval,
450 .go_demote_ok = rgrp_go_demote_ok,
451 .go_lock = rgrp_go_lock,
452 .go_unlock = rgrp_go_unlock,
453 .go_type = LM_TYPE_RGRP
456 struct gfs2_glock_operations gfs2_trans_glops = {
457 .go_xmote_th = trans_go_xmote_th,
458 .go_xmote_bh = trans_go_xmote_bh,
459 .go_drop_th = trans_go_drop_th,
460 .go_type = LM_TYPE_NONDISK
463 struct gfs2_glock_operations gfs2_iopen_glops = {
464 .go_xmote_th = gfs2_glock_xmote_th,
465 .go_drop_th = gfs2_glock_drop_th,
466 .go_callback = gfs2_iopen_go_callback,
467 .go_type = LM_TYPE_IOPEN
470 struct gfs2_glock_operations gfs2_flock_glops = {
471 .go_xmote_th = gfs2_glock_xmote_th,
472 .go_drop_th = gfs2_glock_drop_th,
473 .go_type = LM_TYPE_FLOCK
476 struct gfs2_glock_operations gfs2_nondisk_glops = {
477 .go_xmote_th = gfs2_glock_xmote_th,
478 .go_drop_th = gfs2_glock_drop_th,
479 .go_type = LM_TYPE_NONDISK
482 struct gfs2_glock_operations gfs2_quota_glops = {
483 .go_xmote_th = gfs2_glock_xmote_th,
484 .go_drop_th = gfs2_glock_drop_th,
485 .go_demote_ok = quota_go_demote_ok,
486 .go_type = LM_TYPE_QUOTA
489 struct gfs2_glock_operations gfs2_journal_glops = {
490 .go_xmote_th = gfs2_glock_xmote_th,
491 .go_drop_th = gfs2_glock_drop_th,
492 .go_type = LM_TYPE_JOURNAL