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 <linux/gfs2_ondisk.h>
16 #include <asm/semaphore.h>
19 #include "lm_interface.h"
33 * meta_go_sync - sync out the metadata for this glock
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.
42 static void meta_go_sync(struct gfs2_glock *gl, int flags)
44 if (!(flags & DIO_METADATA))
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);
54 clear_bit(GLF_SYNC, &gl->gl_flags);
58 * meta_go_inval - invalidate the metadata for this glock
64 static void meta_go_inval(struct gfs2_glock *gl, int flags)
66 if (!(flags & DIO_METADATA))
74 * meta_go_demote_ok - Check to see if it's ok to unlock a glock
77 * Returns: 1 if we have no cached data; ok to demote meta glock
80 static int meta_go_demote_ok(struct gfs2_glock *gl)
82 return !gl->gl_aspace->i_mapping->nrpages;
86 * inode_go_xmote_th - promote/demote a glock
88 * @state: the requested state
93 static void inode_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
96 if (gl->gl_state != LM_ST_UNLOCKED)
98 gfs2_glock_xmote_th(gl, state, flags);
102 * inode_go_xmote_bh - After promoting/demoting a glock
107 static void inode_go_xmote_bh(struct gfs2_glock *gl)
109 struct gfs2_holder *gh = gl->gl_req_gh;
110 struct buffer_head *bh;
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,
123 * inode_go_drop_th - unlock a glock
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.
131 static void inode_go_drop_th(struct gfs2_glock *gl)
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 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);
157 gfs2_log_flush(gl->gl_sbd, gl);
158 gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
160 gfs2_page_sync(gl, flags | DIO_START | DIO_WAIT);
161 if (flags & DIO_RELEASE)
162 gfs2_ail_empty_gl(gl);
165 clear_bit(GLF_SYNC, &gl->gl_flags);
169 * inode_go_inval - prepare a inode glock to be released
175 static void inode_go_inval(struct gfs2_glock *gl, int flags)
177 int meta = (flags & DIO_METADATA);
178 int data = (flags & DIO_DATA);
189 * inode_go_demote_ok - Check to see if it's ok to unlock an inode glock
192 * Returns: 1 if it's ok
195 static int inode_go_demote_ok(struct gfs2_glock *gl)
197 struct gfs2_sbd *sdp = gl->gl_sbd;
200 if (!gl->gl_object && !gl->gl_aspace->i_mapping->nrpages)
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))
211 * inode_go_lock - operation done after an inode lock is locked by a process
218 static int inode_go_lock(struct gfs2_holder *gh)
220 struct gfs2_glock *gl = gh->gh_gl;
221 struct gfs2_inode *ip = gl->gl_object;
227 if (ip->i_vn != gl->gl_vn) {
228 error = gfs2_inode_refresh(ip);
231 gfs2_inode_attr_in(ip);
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);
243 * inode_go_unlock - operation done before an inode lock is unlocked by a
250 static void inode_go_unlock(struct gfs2_holder *gh)
252 struct gfs2_glock *gl = gh->gh_gl;
253 struct gfs2_inode *ip = gl->gl_object;
255 if (ip && test_bit(GLF_DIRTY, &gl->gl_flags))
256 gfs2_inode_attr_in(ip);
259 gfs2_meta_cache_flush(ip);
268 static void inode_greedy(struct gfs2_glock *gl)
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;
276 spin_lock(&ip->i_spin);
278 if (time_after(ip->i_last_pfault + quantum, jiffies)) {
279 new_time = ip->i_greedy + quantum;
283 new_time = ip->i_greedy - quantum;
284 if (!new_time || new_time > max)
288 ip->i_greedy = new_time;
290 spin_unlock(&ip->i_spin);
296 * rgrp_go_demote_ok - Check to see if it's ok to unlock a RG's glock
299 * Returns: 1 if it's ok
302 static int rgrp_go_demote_ok(struct gfs2_glock *gl)
304 return !gl->gl_aspace->i_mapping->nrpages;
308 * rgrp_go_lock - operation done after an rgrp lock is locked by
309 * a first holder on this node.
316 static int rgrp_go_lock(struct gfs2_holder *gh)
318 return gfs2_rgrp_bh_get(gh->gh_gl->gl_object);
322 * rgrp_go_unlock - operation done before an rgrp lock is unlocked by
323 * a last holder on this node.
329 static void rgrp_go_unlock(struct gfs2_holder *gh)
331 gfs2_rgrp_bh_put(gh->gh_gl->gl_object);
335 * trans_go_xmote_th - promote/demote the transaction glock
337 * @state: the requested state
342 static void trans_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
345 struct gfs2_sbd *sdp = gl->gl_sbd;
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);
353 gfs2_glock_xmote_th(gl, state, flags);
357 * trans_go_xmote_bh - After promoting/demoting the transaction glock
362 static void trans_go_xmote_bh(struct gfs2_glock *gl)
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;
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);
375 error = gfs2_find_jhead(sdp->sd_jdesc, &head);
378 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT))
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);
390 * trans_go_drop_th - unlock the transaction glock
393 * We want to sync the device even with localcaching. Remember
394 * that localcaching journal replay only marks buffers dirty.
397 static void trans_go_drop_th(struct gfs2_glock *gl)
399 struct gfs2_sbd *sdp = gl->gl_sbd;
401 if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
402 gfs2_meta_syncfs(sdp);
403 gfs2_log_shutdown(sdp);
406 gfs2_glock_drop_th(gl);
410 * quota_go_demote_ok - Check to see if it's ok to unlock a quota glock
413 * Returns: 1 if it's ok
416 static int quota_go_demote_ok(struct gfs2_glock *gl)
418 return !atomic_read(&gl->gl_lvb_count);
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
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
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
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
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
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
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
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
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