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);
240 * inode_go_inval - prepare a inode glock to be released
246 static void inode_go_inval(struct gfs2_glock *gl, int flags)
248 int meta = (flags & DIO_METADATA);
249 int data = (flags & DIO_DATA);
260 * inode_go_demote_ok - Check to see if it's ok to unlock an inode glock
263 * Returns: 1 if it's ok
266 static int inode_go_demote_ok(struct gfs2_glock *gl)
268 struct gfs2_sbd *sdp = gl->gl_sbd;
271 if (!gl->gl_object && !gl->gl_aspace->i_mapping->nrpages)
273 else if (!sdp->sd_args.ar_localcaching &&
274 time_after_eq(jiffies, gl->gl_stamp +
275 gfs2_tune_get(sdp, gt_demote_secs) * HZ))
282 * inode_go_lock - operation done after an inode lock is locked by a process
289 static int inode_go_lock(struct gfs2_holder *gh)
291 struct gfs2_glock *gl = gh->gh_gl;
292 struct gfs2_inode *ip = gl->gl_object;
298 if (ip->i_vn != gl->gl_vn) {
299 error = gfs2_inode_refresh(ip);
302 gfs2_inode_attr_in(ip);
305 if ((ip->i_di.di_flags & GFS2_DIF_TRUNC_IN_PROG) &&
306 (gl->gl_state == LM_ST_EXCLUSIVE) &&
307 (gh->gh_flags & GL_LOCAL_EXCL))
308 error = gfs2_truncatei_resume(ip);
314 * inode_go_unlock - operation done before an inode lock is unlocked by a
321 static void inode_go_unlock(struct gfs2_holder *gh)
323 struct gfs2_glock *gl = gh->gh_gl;
324 struct gfs2_inode *ip = gl->gl_object;
328 if (test_bit(GLF_DIRTY, &gl->gl_flags))
329 gfs2_inode_attr_in(ip);
330 gfs2_meta_cache_flush(ip);
339 static void inode_greedy(struct gfs2_glock *gl)
341 struct gfs2_sbd *sdp = gl->gl_sbd;
342 struct gfs2_inode *ip = gl->gl_object;
343 unsigned int quantum = gfs2_tune_get(sdp, gt_greedy_quantum);
344 unsigned int max = gfs2_tune_get(sdp, gt_greedy_max);
345 unsigned int new_time;
347 spin_lock(&ip->i_spin);
349 if (time_after(ip->i_last_pfault + quantum, jiffies)) {
350 new_time = ip->i_greedy + quantum;
354 new_time = ip->i_greedy - quantum;
355 if (!new_time || new_time > max)
359 ip->i_greedy = new_time;
361 spin_unlock(&ip->i_spin);
367 * rgrp_go_demote_ok - Check to see if it's ok to unlock a RG's glock
370 * Returns: 1 if it's ok
373 static int rgrp_go_demote_ok(struct gfs2_glock *gl)
375 return !gl->gl_aspace->i_mapping->nrpages;
379 * rgrp_go_lock - operation done after an rgrp lock is locked by
380 * a first holder on this node.
387 static int rgrp_go_lock(struct gfs2_holder *gh)
389 return gfs2_rgrp_bh_get(gh->gh_gl->gl_object);
393 * rgrp_go_unlock - operation done before an rgrp lock is unlocked by
394 * a last holder on this node.
400 static void rgrp_go_unlock(struct gfs2_holder *gh)
402 gfs2_rgrp_bh_put(gh->gh_gl->gl_object);
406 * trans_go_xmote_th - promote/demote the transaction glock
408 * @state: the requested state
413 static void trans_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
416 struct gfs2_sbd *sdp = gl->gl_sbd;
418 if (gl->gl_state != LM_ST_UNLOCKED &&
419 test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
420 gfs2_meta_syncfs(sdp);
421 gfs2_log_shutdown(sdp);
424 gfs2_glock_xmote_th(gl, state, flags);
428 * trans_go_xmote_bh - After promoting/demoting the transaction glock
433 static void trans_go_xmote_bh(struct gfs2_glock *gl)
435 struct gfs2_sbd *sdp = gl->gl_sbd;
436 struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
437 struct gfs2_glock *j_gl = ip->i_gl;
438 struct gfs2_log_header head;
441 if (gl->gl_state != LM_ST_UNLOCKED &&
442 test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
443 gfs2_meta_cache_flush(GFS2_I(sdp->sd_jdesc->jd_inode));
444 j_gl->gl_ops->go_inval(j_gl, DIO_METADATA | DIO_DATA);
446 error = gfs2_find_jhead(sdp->sd_jdesc, &head);
449 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT))
452 /* Initialize some head of the log stuff */
453 if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) {
454 sdp->sd_log_sequence = head.lh_sequence + 1;
455 gfs2_log_pointers_init(sdp, head.lh_blkno);
461 * trans_go_drop_th - unlock the transaction glock
464 * We want to sync the device even with localcaching. Remember
465 * that localcaching journal replay only marks buffers dirty.
468 static void trans_go_drop_th(struct gfs2_glock *gl)
470 struct gfs2_sbd *sdp = gl->gl_sbd;
472 if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
473 gfs2_meta_syncfs(sdp);
474 gfs2_log_shutdown(sdp);
477 gfs2_glock_drop_th(gl);
481 * quota_go_demote_ok - Check to see if it's ok to unlock a quota glock
484 * Returns: 1 if it's ok
487 static int quota_go_demote_ok(struct gfs2_glock *gl)
489 return !atomic_read(&gl->gl_lvb_count);
492 const struct gfs2_glock_operations gfs2_meta_glops = {
493 .go_xmote_th = gfs2_glock_xmote_th,
494 .go_drop_th = gfs2_glock_drop_th,
495 .go_type = LM_TYPE_META,
498 const struct gfs2_glock_operations gfs2_inode_glops = {
499 .go_xmote_th = inode_go_xmote_th,
500 .go_xmote_bh = inode_go_xmote_bh,
501 .go_drop_th = inode_go_drop_th,
502 .go_sync = inode_go_sync,
503 .go_inval = inode_go_inval,
504 .go_demote_ok = inode_go_demote_ok,
505 .go_lock = inode_go_lock,
506 .go_unlock = inode_go_unlock,
507 .go_greedy = inode_greedy,
508 .go_type = LM_TYPE_INODE,
511 const struct gfs2_glock_operations gfs2_rgrp_glops = {
512 .go_xmote_th = gfs2_glock_xmote_th,
513 .go_drop_th = gfs2_glock_drop_th,
514 .go_sync = meta_go_sync,
515 .go_inval = meta_go_inval,
516 .go_demote_ok = rgrp_go_demote_ok,
517 .go_lock = rgrp_go_lock,
518 .go_unlock = rgrp_go_unlock,
519 .go_type = LM_TYPE_RGRP,
522 const struct gfs2_glock_operations gfs2_trans_glops = {
523 .go_xmote_th = trans_go_xmote_th,
524 .go_xmote_bh = trans_go_xmote_bh,
525 .go_drop_th = trans_go_drop_th,
526 .go_type = LM_TYPE_NONDISK,
529 const struct gfs2_glock_operations gfs2_iopen_glops = {
530 .go_xmote_th = gfs2_glock_xmote_th,
531 .go_drop_th = gfs2_glock_drop_th,
532 .go_type = LM_TYPE_IOPEN,
535 const struct gfs2_glock_operations gfs2_flock_glops = {
536 .go_xmote_th = gfs2_glock_xmote_th,
537 .go_drop_th = gfs2_glock_drop_th,
538 .go_type = LM_TYPE_FLOCK,
541 const struct gfs2_glock_operations gfs2_nondisk_glops = {
542 .go_xmote_th = gfs2_glock_xmote_th,
543 .go_drop_th = gfs2_glock_drop_th,
544 .go_type = LM_TYPE_NONDISK,
547 const struct gfs2_glock_operations gfs2_quota_glops = {
548 .go_xmote_th = gfs2_glock_xmote_th,
549 .go_drop_th = gfs2_glock_drop_th,
550 .go_demote_ok = quota_go_demote_ok,
551 .go_type = LM_TYPE_QUOTA,
554 const struct gfs2_glock_operations gfs2_journal_glops = {
555 .go_xmote_th = gfs2_glock_xmote_th,
556 .go_drop_th = gfs2_glock_drop_th,
557 .go_type = LM_TYPE_JOURNAL,