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 * 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);
140 clear_bit(GLF_SYNC, &gl->gl_flags);
144 * meta_go_inval - invalidate the metadata for this glock
150 static void meta_go_inval(struct gfs2_glock *gl, int flags)
152 if (!(flags & DIO_METADATA))
160 * inode_go_xmote_th - promote/demote a glock
162 * @state: the requested state
167 static void inode_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
170 if (gl->gl_state != LM_ST_UNLOCKED)
172 gfs2_glock_xmote_th(gl, state, flags);
176 * inode_go_xmote_bh - After promoting/demoting a glock
181 static void inode_go_xmote_bh(struct gfs2_glock *gl)
183 struct gfs2_holder *gh = gl->gl_req_gh;
184 struct buffer_head *bh;
187 if (gl->gl_state != LM_ST_UNLOCKED &&
188 (!gh || !(gh->gh_flags & GL_SKIP))) {
189 error = gfs2_meta_read(gl, gl->gl_name.ln_number, DIO_START,
197 * inode_go_drop_th - unlock a glock
200 * Invoked from rq_demote().
201 * Another node needs the lock in EXCLUSIVE mode, or lock (unused for too long)
202 * is being purged from our node's glock cache; we're dropping lock.
205 static void inode_go_drop_th(struct gfs2_glock *gl)
208 gfs2_glock_drop_th(gl);
212 * inode_go_sync - Sync the dirty data and/or metadata for an inode glock
213 * @gl: the glock protecting the inode
218 static void inode_go_sync(struct gfs2_glock *gl, int flags)
220 int meta = (flags & DIO_METADATA);
221 int data = (flags & DIO_DATA);
223 if (test_bit(GLF_DIRTY, &gl->gl_flags)) {
225 gfs2_page_sync(gl, flags | DIO_START);
226 gfs2_log_flush(gl->gl_sbd, gl);
227 gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
228 gfs2_page_sync(gl, flags | DIO_WAIT);
229 clear_bit(GLF_DIRTY, &gl->gl_flags);
231 gfs2_log_flush(gl->gl_sbd, gl);
232 gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
234 gfs2_page_sync(gl, flags | DIO_START | DIO_WAIT);
235 if (flags & DIO_RELEASE)
236 gfs2_ail_empty_gl(gl);
239 clear_bit(GLF_SYNC, &gl->gl_flags);
243 * inode_go_inval - prepare a inode glock to be released
249 static void inode_go_inval(struct gfs2_glock *gl, int flags)
251 int meta = (flags & DIO_METADATA);
252 int data = (flags & DIO_DATA);
263 * inode_go_demote_ok - Check to see if it's ok to unlock an inode glock
266 * Returns: 1 if it's ok
269 static int inode_go_demote_ok(struct gfs2_glock *gl)
271 struct gfs2_sbd *sdp = gl->gl_sbd;
274 if (!gl->gl_object && !gl->gl_aspace->i_mapping->nrpages)
276 else if (!sdp->sd_args.ar_localcaching &&
277 time_after_eq(jiffies, gl->gl_stamp +
278 gfs2_tune_get(sdp, gt_demote_secs) * HZ))
285 * inode_go_lock - operation done after an inode lock is locked by a process
292 static int inode_go_lock(struct gfs2_holder *gh)
294 struct gfs2_glock *gl = gh->gh_gl;
295 struct gfs2_inode *ip = gl->gl_object;
301 if (ip->i_vn != gl->gl_vn) {
302 error = gfs2_inode_refresh(ip);
305 gfs2_inode_attr_in(ip);
308 if ((ip->i_di.di_flags & GFS2_DIF_TRUNC_IN_PROG) &&
309 (gl->gl_state == LM_ST_EXCLUSIVE) &&
310 (gh->gh_flags & GL_LOCAL_EXCL))
311 error = gfs2_truncatei_resume(ip);
317 * inode_go_unlock - operation done before an inode lock is unlocked by a
324 static void inode_go_unlock(struct gfs2_holder *gh)
326 struct gfs2_glock *gl = gh->gh_gl;
327 struct gfs2_inode *ip = gl->gl_object;
330 if (test_bit(GLF_DIRTY, &gl->gl_flags))
331 gfs2_inode_attr_in(ip);
333 gfs2_meta_cache_flush(ip);
343 static void inode_greedy(struct gfs2_glock *gl)
345 struct gfs2_sbd *sdp = gl->gl_sbd;
346 struct gfs2_inode *ip = gl->gl_object;
347 unsigned int quantum = gfs2_tune_get(sdp, gt_greedy_quantum);
348 unsigned int max = gfs2_tune_get(sdp, gt_greedy_max);
349 unsigned int new_time;
351 spin_lock(&ip->i_spin);
353 if (time_after(ip->i_last_pfault + quantum, jiffies)) {
354 new_time = ip->i_greedy + quantum;
358 new_time = ip->i_greedy - quantum;
359 if (!new_time || new_time > max)
363 ip->i_greedy = new_time;
365 spin_unlock(&ip->i_spin);
371 * rgrp_go_demote_ok - Check to see if it's ok to unlock a RG's glock
374 * Returns: 1 if it's ok
377 static int rgrp_go_demote_ok(struct gfs2_glock *gl)
379 return !gl->gl_aspace->i_mapping->nrpages;
383 * rgrp_go_lock - operation done after an rgrp lock is locked by
384 * a first holder on this node.
391 static int rgrp_go_lock(struct gfs2_holder *gh)
393 return gfs2_rgrp_bh_get(gh->gh_gl->gl_object);
397 * rgrp_go_unlock - operation done before an rgrp lock is unlocked by
398 * a last holder on this node.
404 static void rgrp_go_unlock(struct gfs2_holder *gh)
406 gfs2_rgrp_bh_put(gh->gh_gl->gl_object);
410 * trans_go_xmote_th - promote/demote the transaction glock
412 * @state: the requested state
417 static void trans_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
420 struct gfs2_sbd *sdp = gl->gl_sbd;
422 if (gl->gl_state != LM_ST_UNLOCKED &&
423 test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
424 gfs2_meta_syncfs(sdp);
425 gfs2_log_shutdown(sdp);
428 gfs2_glock_xmote_th(gl, state, flags);
432 * trans_go_xmote_bh - After promoting/demoting the transaction glock
437 static void trans_go_xmote_bh(struct gfs2_glock *gl)
439 struct gfs2_sbd *sdp = gl->gl_sbd;
440 struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
441 struct gfs2_glock *j_gl = ip->i_gl;
442 struct gfs2_log_header head;
445 if (gl->gl_state != LM_ST_UNLOCKED &&
446 test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
447 gfs2_meta_cache_flush(GFS2_I(sdp->sd_jdesc->jd_inode));
448 j_gl->gl_ops->go_inval(j_gl, DIO_METADATA | DIO_DATA);
450 error = gfs2_find_jhead(sdp->sd_jdesc, &head);
453 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT))
456 /* Initialize some head of the log stuff */
457 if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) {
458 sdp->sd_log_sequence = head.lh_sequence + 1;
459 gfs2_log_pointers_init(sdp, head.lh_blkno);
465 * trans_go_drop_th - unlock the transaction glock
468 * We want to sync the device even with localcaching. Remember
469 * that localcaching journal replay only marks buffers dirty.
472 static void trans_go_drop_th(struct gfs2_glock *gl)
474 struct gfs2_sbd *sdp = gl->gl_sbd;
476 if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
477 gfs2_meta_syncfs(sdp);
478 gfs2_log_shutdown(sdp);
481 gfs2_glock_drop_th(gl);
485 * quota_go_demote_ok - Check to see if it's ok to unlock a quota glock
488 * Returns: 1 if it's ok
491 static int quota_go_demote_ok(struct gfs2_glock *gl)
493 return !atomic_read(&gl->gl_lvb_count);
496 struct gfs2_glock_operations gfs2_meta_glops = {
497 .go_xmote_th = gfs2_glock_xmote_th,
498 .go_drop_th = gfs2_glock_drop_th,
499 .go_type = LM_TYPE_META
502 struct gfs2_glock_operations gfs2_inode_glops = {
503 .go_xmote_th = inode_go_xmote_th,
504 .go_xmote_bh = inode_go_xmote_bh,
505 .go_drop_th = inode_go_drop_th,
506 .go_sync = inode_go_sync,
507 .go_inval = inode_go_inval,
508 .go_demote_ok = inode_go_demote_ok,
509 .go_lock = inode_go_lock,
510 .go_unlock = inode_go_unlock,
511 .go_greedy = inode_greedy,
512 .go_type = LM_TYPE_INODE
515 struct gfs2_glock_operations gfs2_rgrp_glops = {
516 .go_xmote_th = gfs2_glock_xmote_th,
517 .go_drop_th = gfs2_glock_drop_th,
518 .go_sync = meta_go_sync,
519 .go_inval = meta_go_inval,
520 .go_demote_ok = rgrp_go_demote_ok,
521 .go_lock = rgrp_go_lock,
522 .go_unlock = rgrp_go_unlock,
523 .go_type = LM_TYPE_RGRP
526 struct gfs2_glock_operations gfs2_trans_glops = {
527 .go_xmote_th = trans_go_xmote_th,
528 .go_xmote_bh = trans_go_xmote_bh,
529 .go_drop_th = trans_go_drop_th,
530 .go_type = LM_TYPE_NONDISK
533 struct gfs2_glock_operations gfs2_iopen_glops = {
534 .go_xmote_th = gfs2_glock_xmote_th,
535 .go_drop_th = gfs2_glock_drop_th,
536 .go_callback = gfs2_iopen_go_callback,
537 .go_type = LM_TYPE_IOPEN
540 struct gfs2_glock_operations gfs2_flock_glops = {
541 .go_xmote_th = gfs2_glock_xmote_th,
542 .go_drop_th = gfs2_glock_drop_th,
543 .go_type = LM_TYPE_FLOCK
546 struct gfs2_glock_operations gfs2_nondisk_glops = {
547 .go_xmote_th = gfs2_glock_xmote_th,
548 .go_drop_th = gfs2_glock_drop_th,
549 .go_type = LM_TYPE_NONDISK
552 struct gfs2_glock_operations gfs2_quota_glops = {
553 .go_xmote_th = gfs2_glock_xmote_th,
554 .go_drop_th = gfs2_glock_drop_th,
555 .go_demote_ok = quota_go_demote_ok,
556 .go_type = LM_TYPE_QUOTA
559 struct gfs2_glock_operations gfs2_journal_glops = {
560 .go_xmote_th = gfs2_glock_xmote_th,
561 .go_drop_th = gfs2_glock_drop_th,
562 .go_type = LM_TYPE_JOURNAL