2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 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 <asm/semaphore.h>
29 * meta_go_sync - sync out the metadata for this glock
33 * Called when demoting or unlocking an EX glock. We must flush
34 * to disk all dirty buffers/pages relating to this glock, and must not
35 * not return to caller to demote/unlock the glock until I/O is complete.
38 static void meta_go_sync(struct gfs2_glock *gl, int flags)
40 if (!(flags & DIO_METADATA))
43 if (test_and_clear_bit(GLF_DIRTY, &gl->gl_flags)) {
44 gfs2_log_flush_glock(gl);
45 gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
46 if (flags & DIO_RELEASE)
47 gfs2_ail_empty_gl(gl);
50 clear_bit(GLF_SYNC, &gl->gl_flags);
54 * meta_go_inval - invalidate the metadata for this glock
60 static void meta_go_inval(struct gfs2_glock *gl, int flags)
62 if (!(flags & DIO_METADATA))
70 * meta_go_demote_ok - Check to see if it's ok to unlock a glock
73 * Returns: 1 if we have no cached data; ok to demote meta glock
76 static int meta_go_demote_ok(struct gfs2_glock *gl)
78 return !gl->gl_aspace->i_mapping->nrpages;
82 * inode_go_xmote_th - promote/demote a glock
84 * @state: the requested state
89 static void inode_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
92 if (gl->gl_state != LM_ST_UNLOCKED)
94 gfs2_glock_xmote_th(gl, state, flags);
98 * inode_go_xmote_bh - After promoting/demoting a glock
103 static void inode_go_xmote_bh(struct gfs2_glock *gl)
105 struct gfs2_holder *gh = gl->gl_req_gh;
106 struct buffer_head *bh;
109 if (gl->gl_state != LM_ST_UNLOCKED &&
110 (!gh || !(gh->gh_flags & GL_SKIP))) {
111 error = gfs2_meta_read(gl, gl->gl_name.ln_number, DIO_START,
119 * inode_go_drop_th - unlock a glock
122 * Invoked from rq_demote().
123 * Another node needs the lock in EXCLUSIVE mode, or lock (unused for too long)
124 * is being purged from our node's glock cache; we're dropping lock.
127 static void inode_go_drop_th(struct gfs2_glock *gl)
130 gfs2_glock_drop_th(gl);
134 * inode_go_sync - Sync the dirty data and/or metadata for an inode glock
135 * @gl: the glock protecting the inode
140 static void inode_go_sync(struct gfs2_glock *gl, int flags)
142 int meta = (flags & DIO_METADATA);
143 int data = (flags & DIO_DATA);
145 if (test_bit(GLF_DIRTY, &gl->gl_flags)) {
147 gfs2_page_sync(gl, flags | DIO_START);
148 gfs2_log_flush_glock(gl);
149 gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
150 gfs2_page_sync(gl, flags | DIO_WAIT);
151 clear_bit(GLF_DIRTY, &gl->gl_flags);
153 gfs2_log_flush_glock(gl);
154 gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
156 gfs2_page_sync(gl, flags | DIO_START | DIO_WAIT);
157 if (flags & DIO_RELEASE)
158 gfs2_ail_empty_gl(gl);
161 clear_bit(GLF_SYNC, &gl->gl_flags);
165 * inode_go_inval - prepare a inode glock to be released
171 static void inode_go_inval(struct gfs2_glock *gl, int flags)
173 int meta = (flags & DIO_METADATA);
174 int data = (flags & DIO_DATA);
185 * inode_go_demote_ok - Check to see if it's ok to unlock an inode glock
188 * Returns: 1 if it's ok
191 static int inode_go_demote_ok(struct gfs2_glock *gl)
193 struct gfs2_sbd *sdp = gl->gl_sbd;
196 if (!get_gl2ip(gl) && !gl->gl_aspace->i_mapping->nrpages)
198 else if (!sdp->sd_args.ar_localcaching &&
199 time_after_eq(jiffies, gl->gl_stamp +
200 gfs2_tune_get(sdp, gt_demote_secs) * HZ))
207 * inode_go_lock - operation done after an inode lock is locked by a process
214 static int inode_go_lock(struct gfs2_holder *gh)
216 struct gfs2_glock *gl = gh->gh_gl;
217 struct gfs2_inode *ip = get_gl2ip(gl);
223 if (ip->i_vn != gl->gl_vn) {
224 error = gfs2_inode_refresh(ip);
227 gfs2_inode_attr_in(ip);
230 if ((ip->i_di.di_flags & GFS2_DIF_TRUNC_IN_PROG) &&
231 (gl->gl_state == LM_ST_EXCLUSIVE) &&
232 (gh->gh_flags & GL_LOCAL_EXCL))
233 error = gfs2_truncatei_resume(ip);
239 * inode_go_unlock - operation done before an inode lock is unlocked by a
246 static void inode_go_unlock(struct gfs2_holder *gh)
248 struct gfs2_glock *gl = gh->gh_gl;
249 struct gfs2_inode *ip = get_gl2ip(gl);
251 if (ip && test_bit(GLF_DIRTY, &gl->gl_flags))
252 gfs2_inode_attr_in(ip);
255 gfs2_meta_cache_flush(ip);
264 static void inode_greedy(struct gfs2_glock *gl)
266 struct gfs2_sbd *sdp = gl->gl_sbd;
267 struct gfs2_inode *ip = get_gl2ip(gl);
268 unsigned int quantum = gfs2_tune_get(sdp, gt_greedy_quantum);
269 unsigned int max = gfs2_tune_get(sdp, gt_greedy_max);
270 unsigned int new_time;
272 spin_lock(&ip->i_spin);
274 if (time_after(ip->i_last_pfault + quantum, jiffies)) {
275 new_time = ip->i_greedy + quantum;
279 new_time = ip->i_greedy - quantum;
280 if (!new_time || new_time > max)
284 ip->i_greedy = new_time;
286 spin_unlock(&ip->i_spin);
292 * rgrp_go_demote_ok - Check to see if it's ok to unlock a RG's glock
295 * Returns: 1 if it's ok
298 static int rgrp_go_demote_ok(struct gfs2_glock *gl)
300 return !gl->gl_aspace->i_mapping->nrpages;
304 * rgrp_go_lock - operation done after an rgrp lock is locked by
305 * a first holder on this node.
312 static int rgrp_go_lock(struct gfs2_holder *gh)
314 return gfs2_rgrp_bh_get(get_gl2rgd(gh->gh_gl));
318 * rgrp_go_unlock - operation done before an rgrp lock is unlocked by
319 * a last holder on this node.
325 static void rgrp_go_unlock(struct gfs2_holder *gh)
327 gfs2_rgrp_bh_put(get_gl2rgd(gh->gh_gl));
331 * trans_go_xmote_th - promote/demote the transaction glock
333 * @state: the requested state
338 static void trans_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
341 struct gfs2_sbd *sdp = gl->gl_sbd;
343 if (gl->gl_state != LM_ST_UNLOCKED &&
344 test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
345 gfs2_meta_syncfs(sdp);
346 gfs2_log_shutdown(sdp);
349 gfs2_glock_xmote_th(gl, state, flags);
353 * trans_go_xmote_bh - After promoting/demoting the transaction glock
358 static void trans_go_xmote_bh(struct gfs2_glock *gl)
360 struct gfs2_sbd *sdp = gl->gl_sbd;
361 struct gfs2_glock *j_gl = get_v2ip(sdp->sd_jdesc->jd_inode)->i_gl;
362 struct gfs2_log_header head;
365 if (gl->gl_state != LM_ST_UNLOCKED &&
366 test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
367 gfs2_meta_cache_flush(get_v2ip(sdp->sd_jdesc->jd_inode));
368 j_gl->gl_ops->go_inval(j_gl, DIO_METADATA | DIO_DATA);
370 error = gfs2_find_jhead(sdp->sd_jdesc, &head);
373 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT))
376 /* Initialize some head of the log stuff */
377 if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) {
378 sdp->sd_log_sequence = head.lh_sequence + 1;
379 gfs2_log_pointers_init(sdp, head.lh_blkno);
385 * trans_go_drop_th - unlock the transaction glock
388 * We want to sync the device even with localcaching. Remember
389 * that localcaching journal replay only marks buffers dirty.
392 static void trans_go_drop_th(struct gfs2_glock *gl)
394 struct gfs2_sbd *sdp = gl->gl_sbd;
396 if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
397 gfs2_meta_syncfs(sdp);
398 gfs2_log_shutdown(sdp);
401 gfs2_glock_drop_th(gl);
405 * quota_go_demote_ok - Check to see if it's ok to unlock a quota glock
408 * Returns: 1 if it's ok
411 static int quota_go_demote_ok(struct gfs2_glock *gl)
413 return !atomic_read(&gl->gl_lvb_count);
416 struct gfs2_glock_operations gfs2_meta_glops = {
417 .go_xmote_th = gfs2_glock_xmote_th,
418 .go_drop_th = gfs2_glock_drop_th,
419 .go_sync = meta_go_sync,
420 .go_inval = meta_go_inval,
421 .go_demote_ok = meta_go_demote_ok,
422 .go_type = LM_TYPE_META
425 struct gfs2_glock_operations gfs2_inode_glops = {
426 .go_xmote_th = inode_go_xmote_th,
427 .go_xmote_bh = inode_go_xmote_bh,
428 .go_drop_th = inode_go_drop_th,
429 .go_sync = inode_go_sync,
430 .go_inval = inode_go_inval,
431 .go_demote_ok = inode_go_demote_ok,
432 .go_lock = inode_go_lock,
433 .go_unlock = inode_go_unlock,
434 .go_greedy = inode_greedy,
435 .go_type = LM_TYPE_INODE
438 struct gfs2_glock_operations gfs2_rgrp_glops = {
439 .go_xmote_th = gfs2_glock_xmote_th,
440 .go_drop_th = gfs2_glock_drop_th,
441 .go_sync = meta_go_sync,
442 .go_inval = meta_go_inval,
443 .go_demote_ok = rgrp_go_demote_ok,
444 .go_lock = rgrp_go_lock,
445 .go_unlock = rgrp_go_unlock,
446 .go_type = LM_TYPE_RGRP
449 struct gfs2_glock_operations gfs2_trans_glops = {
450 .go_xmote_th = trans_go_xmote_th,
451 .go_xmote_bh = trans_go_xmote_bh,
452 .go_drop_th = trans_go_drop_th,
453 .go_type = LM_TYPE_NONDISK
456 struct gfs2_glock_operations gfs2_iopen_glops = {
457 .go_xmote_th = gfs2_glock_xmote_th,
458 .go_drop_th = gfs2_glock_drop_th,
459 .go_callback = gfs2_iopen_go_callback,
460 .go_type = LM_TYPE_IOPEN
463 struct gfs2_glock_operations gfs2_flock_glops = {
464 .go_xmote_th = gfs2_glock_xmote_th,
465 .go_drop_th = gfs2_glock_drop_th,
466 .go_type = LM_TYPE_FLOCK
469 struct gfs2_glock_operations gfs2_nondisk_glops = {
470 .go_xmote_th = gfs2_glock_xmote_th,
471 .go_drop_th = gfs2_glock_drop_th,
472 .go_type = LM_TYPE_NONDISK
475 struct gfs2_glock_operations gfs2_quota_glops = {
476 .go_xmote_th = gfs2_glock_xmote_th,
477 .go_drop_th = gfs2_glock_drop_th,
478 .go_demote_ok = quota_go_demote_ok,
479 .go_type = LM_TYPE_QUOTA
482 struct gfs2_glock_operations gfs2_journal_glops = {
483 .go_xmote_th = gfs2_glock_xmote_th,
484 .go_drop_th = gfs2_glock_drop_th,
485 .go_type = LM_TYPE_JOURNAL