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 version 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 * gfs2_pte_inval - Sync and invalidate all PTEs associated with a glock
37 static void gfs2_pte_inval(struct gfs2_glock *gl)
39 struct gfs2_inode *ip;
44 if (!ip || !S_ISREG(ip->i_di.di_mode))
47 if (!test_bit(GIF_PAGED, &ip->i_flags))
50 unmap_shared_mapping_range(inode->i_mapping, 0, 0);
52 if (test_bit(GIF_SW_PAGED, &ip->i_flags))
53 set_bit(GLF_DIRTY, &gl->gl_flags);
55 clear_bit(GIF_SW_PAGED, &ip->i_flags);
59 * gfs2_page_inval - Invalidate all pages associated with a glock
64 static void gfs2_page_inval(struct gfs2_glock *gl)
66 struct gfs2_inode *ip;
71 if (!ip || !S_ISREG(ip->i_di.di_mode))
74 truncate_inode_pages(inode->i_mapping, 0);
75 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), !inode->i_mapping->nrpages);
76 clear_bit(GIF_PAGED, &ip->i_flags);
80 * gfs2_page_sync - Sync the data pages (not metadata) associated with a glock
82 * @flags: DIO_START | DIO_WAIT
84 * Syncs data (not metadata) for a regular file.
85 * No-op for all other types.
88 static void gfs2_page_sync(struct gfs2_glock *gl, int flags)
90 struct gfs2_inode *ip;
92 struct address_space *mapping;
97 if (!ip || !S_ISREG(ip->i_di.di_mode))
100 mapping = inode->i_mapping;
102 if (flags & DIO_START)
103 filemap_fdatawrite(mapping);
104 if (!error && (flags & DIO_WAIT))
105 error = filemap_fdatawait(mapping);
107 /* Put back any errors cleared by filemap_fdatawait()
108 so they can be caught by someone who can pass them
111 if (error == -ENOSPC)
112 set_bit(AS_ENOSPC, &mapping->flags);
114 set_bit(AS_EIO, &mapping->flags);
119 * meta_go_sync - sync out the metadata for this glock
123 * Called when demoting or unlocking an EX glock. We must flush
124 * to disk all dirty buffers/pages relating to this glock, and must not
125 * not return to caller to demote/unlock the glock until I/O is complete.
128 static void meta_go_sync(struct gfs2_glock *gl, int flags)
130 if (!(flags & DIO_METADATA))
133 if (test_and_clear_bit(GLF_DIRTY, &gl->gl_flags)) {
134 gfs2_log_flush(gl->gl_sbd, gl);
135 gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
136 if (flags & DIO_RELEASE)
137 gfs2_ail_empty_gl(gl);
143 * meta_go_inval - invalidate the metadata for this glock
149 static void meta_go_inval(struct gfs2_glock *gl, int flags)
151 if (!(flags & DIO_METADATA))
159 * inode_go_xmote_th - promote/demote a glock
161 * @state: the requested state
166 static void inode_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
169 if (gl->gl_state != LM_ST_UNLOCKED)
171 gfs2_glock_xmote_th(gl, state, flags);
175 * inode_go_xmote_bh - After promoting/demoting a glock
180 static void inode_go_xmote_bh(struct gfs2_glock *gl)
182 struct gfs2_holder *gh = gl->gl_req_gh;
183 struct buffer_head *bh;
186 if (gl->gl_state != LM_ST_UNLOCKED &&
187 (!gh || !(gh->gh_flags & GL_SKIP))) {
188 error = gfs2_meta_read(gl, gl->gl_name.ln_number, DIO_START,
196 * inode_go_drop_th - unlock a glock
199 * Invoked from rq_demote().
200 * Another node needs the lock in EXCLUSIVE mode, or lock (unused for too long)
201 * is being purged from our node's glock cache; we're dropping lock.
204 static void inode_go_drop_th(struct gfs2_glock *gl)
207 gfs2_glock_drop_th(gl);
211 * inode_go_sync - Sync the dirty data and/or metadata for an inode glock
212 * @gl: the glock protecting the inode
217 static void inode_go_sync(struct gfs2_glock *gl, int flags)
219 int meta = (flags & DIO_METADATA);
220 int data = (flags & DIO_DATA);
222 if (test_bit(GLF_DIRTY, &gl->gl_flags)) {
224 gfs2_page_sync(gl, flags | DIO_START);
225 gfs2_log_flush(gl->gl_sbd, gl);
226 gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
227 gfs2_page_sync(gl, flags | DIO_WAIT);
228 clear_bit(GLF_DIRTY, &gl->gl_flags);
230 gfs2_log_flush(gl->gl_sbd, gl);
231 gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
233 gfs2_page_sync(gl, flags | DIO_START | DIO_WAIT);
234 if (flags & DIO_RELEASE)
235 gfs2_ail_empty_gl(gl);
241 * inode_go_inval - prepare a inode glock to be released
247 static void inode_go_inval(struct gfs2_glock *gl, int flags)
249 int meta = (flags & DIO_METADATA);
250 int data = (flags & DIO_DATA);
261 * inode_go_demote_ok - Check to see if it's ok to unlock an inode glock
264 * Returns: 1 if it's ok
267 static int inode_go_demote_ok(struct gfs2_glock *gl)
269 struct gfs2_sbd *sdp = gl->gl_sbd;
272 if (!gl->gl_object && !gl->gl_aspace->i_mapping->nrpages)
274 else if (!sdp->sd_args.ar_localcaching &&
275 time_after_eq(jiffies, gl->gl_stamp +
276 gfs2_tune_get(sdp, gt_demote_secs) * HZ))
283 * inode_go_lock - operation done after an inode lock is locked by a process
290 static int inode_go_lock(struct gfs2_holder *gh)
292 struct gfs2_glock *gl = gh->gh_gl;
293 struct gfs2_inode *ip = gl->gl_object;
299 if (ip->i_vn != gl->gl_vn) {
300 error = gfs2_inode_refresh(ip);
303 gfs2_inode_attr_in(ip);
306 if ((ip->i_di.di_flags & GFS2_DIF_TRUNC_IN_PROG) &&
307 (gl->gl_state == LM_ST_EXCLUSIVE) &&
308 (gh->gh_flags & GL_LOCAL_EXCL))
309 error = gfs2_truncatei_resume(ip);
315 * inode_go_unlock - operation done before an inode lock is unlocked by a
322 static void inode_go_unlock(struct gfs2_holder *gh)
324 struct gfs2_glock *gl = gh->gh_gl;
325 struct gfs2_inode *ip = gl->gl_object;
328 if (test_bit(GLF_DIRTY, &gl->gl_flags))
329 gfs2_inode_attr_in(ip);
331 gfs2_meta_cache_flush(ip);
341 static void inode_greedy(struct gfs2_glock *gl)
343 struct gfs2_sbd *sdp = gl->gl_sbd;
344 struct gfs2_inode *ip = gl->gl_object;
345 unsigned int quantum = gfs2_tune_get(sdp, gt_greedy_quantum);
346 unsigned int max = gfs2_tune_get(sdp, gt_greedy_max);
347 unsigned int new_time;
349 spin_lock(&ip->i_spin);
351 if (time_after(ip->i_last_pfault + quantum, jiffies)) {
352 new_time = ip->i_greedy + quantum;
356 new_time = ip->i_greedy - quantum;
357 if (!new_time || new_time > max)
361 ip->i_greedy = new_time;
363 spin_unlock(&ip->i_spin);
369 * rgrp_go_demote_ok - Check to see if it's ok to unlock a RG's glock
372 * Returns: 1 if it's ok
375 static int rgrp_go_demote_ok(struct gfs2_glock *gl)
377 return !gl->gl_aspace->i_mapping->nrpages;
381 * rgrp_go_lock - operation done after an rgrp lock is locked by
382 * a first holder on this node.
389 static int rgrp_go_lock(struct gfs2_holder *gh)
391 return gfs2_rgrp_bh_get(gh->gh_gl->gl_object);
395 * rgrp_go_unlock - operation done before an rgrp lock is unlocked by
396 * a last holder on this node.
402 static void rgrp_go_unlock(struct gfs2_holder *gh)
404 gfs2_rgrp_bh_put(gh->gh_gl->gl_object);
408 * trans_go_xmote_th - promote/demote the transaction glock
410 * @state: the requested state
415 static void trans_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
418 struct gfs2_sbd *sdp = gl->gl_sbd;
420 if (gl->gl_state != LM_ST_UNLOCKED &&
421 test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
422 gfs2_meta_syncfs(sdp);
423 gfs2_log_shutdown(sdp);
426 gfs2_glock_xmote_th(gl, state, flags);
430 * trans_go_xmote_bh - After promoting/demoting the transaction glock
435 static void trans_go_xmote_bh(struct gfs2_glock *gl)
437 struct gfs2_sbd *sdp = gl->gl_sbd;
438 struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
439 struct gfs2_glock *j_gl = ip->i_gl;
440 struct gfs2_log_header head;
443 if (gl->gl_state != LM_ST_UNLOCKED &&
444 test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
445 gfs2_meta_cache_flush(GFS2_I(sdp->sd_jdesc->jd_inode));
446 j_gl->gl_ops->go_inval(j_gl, DIO_METADATA | DIO_DATA);
448 error = gfs2_find_jhead(sdp->sd_jdesc, &head);
451 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT))
454 /* Initialize some head of the log stuff */
455 if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) {
456 sdp->sd_log_sequence = head.lh_sequence + 1;
457 gfs2_log_pointers_init(sdp, head.lh_blkno);
463 * trans_go_drop_th - unlock the transaction glock
466 * We want to sync the device even with localcaching. Remember
467 * that localcaching journal replay only marks buffers dirty.
470 static void trans_go_drop_th(struct gfs2_glock *gl)
472 struct gfs2_sbd *sdp = gl->gl_sbd;
474 if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
475 gfs2_meta_syncfs(sdp);
476 gfs2_log_shutdown(sdp);
479 gfs2_glock_drop_th(gl);
483 * quota_go_demote_ok - Check to see if it's ok to unlock a quota glock
486 * Returns: 1 if it's ok
489 static int quota_go_demote_ok(struct gfs2_glock *gl)
491 return !atomic_read(&gl->gl_lvb_count);
494 const struct gfs2_glock_operations gfs2_meta_glops = {
495 .go_xmote_th = gfs2_glock_xmote_th,
496 .go_drop_th = gfs2_glock_drop_th,
497 .go_type = LM_TYPE_META
500 const struct gfs2_glock_operations gfs2_inode_glops = {
501 .go_xmote_th = inode_go_xmote_th,
502 .go_xmote_bh = inode_go_xmote_bh,
503 .go_drop_th = inode_go_drop_th,
504 .go_sync = inode_go_sync,
505 .go_inval = inode_go_inval,
506 .go_demote_ok = inode_go_demote_ok,
507 .go_lock = inode_go_lock,
508 .go_unlock = inode_go_unlock,
509 .go_greedy = inode_greedy,
510 .go_type = LM_TYPE_INODE
513 const struct gfs2_glock_operations gfs2_rgrp_glops = {
514 .go_xmote_th = gfs2_glock_xmote_th,
515 .go_drop_th = gfs2_glock_drop_th,
516 .go_sync = meta_go_sync,
517 .go_inval = meta_go_inval,
518 .go_demote_ok = rgrp_go_demote_ok,
519 .go_lock = rgrp_go_lock,
520 .go_unlock = rgrp_go_unlock,
521 .go_type = LM_TYPE_RGRP
524 const struct gfs2_glock_operations gfs2_trans_glops = {
525 .go_xmote_th = trans_go_xmote_th,
526 .go_xmote_bh = trans_go_xmote_bh,
527 .go_drop_th = trans_go_drop_th,
528 .go_type = LM_TYPE_NONDISK
531 const struct gfs2_glock_operations gfs2_iopen_glops = {
532 .go_xmote_th = gfs2_glock_xmote_th,
533 .go_drop_th = gfs2_glock_drop_th,
534 .go_callback = gfs2_iopen_go_callback,
535 .go_type = LM_TYPE_IOPEN
538 const struct gfs2_glock_operations gfs2_flock_glops = {
539 .go_xmote_th = gfs2_glock_xmote_th,
540 .go_drop_th = gfs2_glock_drop_th,
541 .go_type = LM_TYPE_FLOCK
544 const struct gfs2_glock_operations gfs2_nondisk_glops = {
545 .go_xmote_th = gfs2_glock_xmote_th,
546 .go_drop_th = gfs2_glock_drop_th,
547 .go_type = LM_TYPE_NONDISK
550 const struct gfs2_glock_operations gfs2_quota_glops = {
551 .go_xmote_th = gfs2_glock_xmote_th,
552 .go_drop_th = gfs2_glock_drop_th,
553 .go_demote_ok = quota_go_demote_ok,
554 .go_type = LM_TYPE_QUOTA
557 const struct gfs2_glock_operations gfs2_journal_glops = {
558 .go_xmote_th = gfs2_glock_xmote_th,
559 .go_drop_th = gfs2_glock_drop_th,
560 .go_type = LM_TYPE_JOURNAL