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 * meta_go_demote_ok - Check to see if it's ok to unlock a glock
163 * Returns: 1 if we have no cached data; ok to demote meta glock
166 static int meta_go_demote_ok(struct gfs2_glock *gl)
168 return !gl->gl_aspace->i_mapping->nrpages;
172 * inode_go_xmote_th - promote/demote a glock
174 * @state: the requested state
179 static void inode_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
182 if (gl->gl_state != LM_ST_UNLOCKED)
184 gfs2_glock_xmote_th(gl, state, flags);
188 * inode_go_xmote_bh - After promoting/demoting a glock
193 static void inode_go_xmote_bh(struct gfs2_glock *gl)
195 struct gfs2_holder *gh = gl->gl_req_gh;
196 struct buffer_head *bh;
199 if (gl->gl_state != LM_ST_UNLOCKED &&
200 (!gh || !(gh->gh_flags & GL_SKIP))) {
201 error = gfs2_meta_read(gl, gl->gl_name.ln_number, DIO_START,
209 * inode_go_drop_th - unlock a glock
212 * Invoked from rq_demote().
213 * Another node needs the lock in EXCLUSIVE mode, or lock (unused for too long)
214 * is being purged from our node's glock cache; we're dropping lock.
217 static void inode_go_drop_th(struct gfs2_glock *gl)
220 gfs2_glock_drop_th(gl);
224 * inode_go_sync - Sync the dirty data and/or metadata for an inode glock
225 * @gl: the glock protecting the inode
230 static void inode_go_sync(struct gfs2_glock *gl, int flags)
232 int meta = (flags & DIO_METADATA);
233 int data = (flags & DIO_DATA);
235 if (test_bit(GLF_DIRTY, &gl->gl_flags)) {
237 gfs2_page_sync(gl, flags | DIO_START);
238 gfs2_log_flush(gl->gl_sbd, gl);
239 gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
240 gfs2_page_sync(gl, flags | DIO_WAIT);
241 clear_bit(GLF_DIRTY, &gl->gl_flags);
243 gfs2_log_flush(gl->gl_sbd, gl);
244 gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
246 gfs2_page_sync(gl, flags | DIO_START | DIO_WAIT);
247 if (flags & DIO_RELEASE)
248 gfs2_ail_empty_gl(gl);
251 clear_bit(GLF_SYNC, &gl->gl_flags);
255 * inode_go_inval - prepare a inode glock to be released
261 static void inode_go_inval(struct gfs2_glock *gl, int flags)
263 int meta = (flags & DIO_METADATA);
264 int data = (flags & DIO_DATA);
275 * inode_go_demote_ok - Check to see if it's ok to unlock an inode glock
278 * Returns: 1 if it's ok
281 static int inode_go_demote_ok(struct gfs2_glock *gl)
283 struct gfs2_sbd *sdp = gl->gl_sbd;
286 if (!gl->gl_object && !gl->gl_aspace->i_mapping->nrpages)
288 else if (!sdp->sd_args.ar_localcaching &&
289 time_after_eq(jiffies, gl->gl_stamp +
290 gfs2_tune_get(sdp, gt_demote_secs) * HZ))
297 * inode_go_lock - operation done after an inode lock is locked by a process
304 static int inode_go_lock(struct gfs2_holder *gh)
306 struct gfs2_glock *gl = gh->gh_gl;
307 struct gfs2_inode *ip = gl->gl_object;
313 if (ip->i_vn != gl->gl_vn) {
314 error = gfs2_inode_refresh(ip);
317 gfs2_inode_attr_in(ip);
320 if ((ip->i_di.di_flags & GFS2_DIF_TRUNC_IN_PROG) &&
321 (gl->gl_state == LM_ST_EXCLUSIVE) &&
322 (gh->gh_flags & GL_LOCAL_EXCL))
323 error = gfs2_truncatei_resume(ip);
329 * inode_go_unlock - operation done before an inode lock is unlocked by a
336 static void inode_go_unlock(struct gfs2_holder *gh)
338 struct gfs2_glock *gl = gh->gh_gl;
339 struct gfs2_inode *ip = gl->gl_object;
341 if (ip && test_bit(GLF_DIRTY, &gl->gl_flags))
342 gfs2_inode_attr_in(ip);
345 gfs2_meta_cache_flush(ip);
354 static void inode_greedy(struct gfs2_glock *gl)
356 struct gfs2_sbd *sdp = gl->gl_sbd;
357 struct gfs2_inode *ip = gl->gl_object;
358 unsigned int quantum = gfs2_tune_get(sdp, gt_greedy_quantum);
359 unsigned int max = gfs2_tune_get(sdp, gt_greedy_max);
360 unsigned int new_time;
362 spin_lock(&ip->i_spin);
364 if (time_after(ip->i_last_pfault + quantum, jiffies)) {
365 new_time = ip->i_greedy + quantum;
369 new_time = ip->i_greedy - quantum;
370 if (!new_time || new_time > max)
374 ip->i_greedy = new_time;
376 spin_unlock(&ip->i_spin);
382 * rgrp_go_demote_ok - Check to see if it's ok to unlock a RG's glock
385 * Returns: 1 if it's ok
388 static int rgrp_go_demote_ok(struct gfs2_glock *gl)
390 return !gl->gl_aspace->i_mapping->nrpages;
394 * rgrp_go_lock - operation done after an rgrp lock is locked by
395 * a first holder on this node.
402 static int rgrp_go_lock(struct gfs2_holder *gh)
404 return gfs2_rgrp_bh_get(gh->gh_gl->gl_object);
408 * rgrp_go_unlock - operation done before an rgrp lock is unlocked by
409 * a last holder on this node.
415 static void rgrp_go_unlock(struct gfs2_holder *gh)
417 gfs2_rgrp_bh_put(gh->gh_gl->gl_object);
421 * trans_go_xmote_th - promote/demote the transaction glock
423 * @state: the requested state
428 static void trans_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
431 struct gfs2_sbd *sdp = gl->gl_sbd;
433 if (gl->gl_state != LM_ST_UNLOCKED &&
434 test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
435 gfs2_meta_syncfs(sdp);
436 gfs2_log_shutdown(sdp);
439 gfs2_glock_xmote_th(gl, state, flags);
443 * trans_go_xmote_bh - After promoting/demoting the transaction glock
448 static void trans_go_xmote_bh(struct gfs2_glock *gl)
450 struct gfs2_sbd *sdp = gl->gl_sbd;
451 struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
452 struct gfs2_glock *j_gl = ip->i_gl;
453 struct gfs2_log_header head;
456 if (gl->gl_state != LM_ST_UNLOCKED &&
457 test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
458 gfs2_meta_cache_flush(GFS2_I(sdp->sd_jdesc->jd_inode));
459 j_gl->gl_ops->go_inval(j_gl, DIO_METADATA | DIO_DATA);
461 error = gfs2_find_jhead(sdp->sd_jdesc, &head);
464 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT))
467 /* Initialize some head of the log stuff */
468 if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) {
469 sdp->sd_log_sequence = head.lh_sequence + 1;
470 gfs2_log_pointers_init(sdp, head.lh_blkno);
476 * trans_go_drop_th - unlock the transaction glock
479 * We want to sync the device even with localcaching. Remember
480 * that localcaching journal replay only marks buffers dirty.
483 static void trans_go_drop_th(struct gfs2_glock *gl)
485 struct gfs2_sbd *sdp = gl->gl_sbd;
487 if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
488 gfs2_meta_syncfs(sdp);
489 gfs2_log_shutdown(sdp);
492 gfs2_glock_drop_th(gl);
496 * quota_go_demote_ok - Check to see if it's ok to unlock a quota glock
499 * Returns: 1 if it's ok
502 static int quota_go_demote_ok(struct gfs2_glock *gl)
504 return !atomic_read(&gl->gl_lvb_count);
507 struct gfs2_glock_operations gfs2_meta_glops = {
508 .go_xmote_th = gfs2_glock_xmote_th,
509 .go_drop_th = gfs2_glock_drop_th,
510 .go_sync = meta_go_sync,
511 .go_inval = meta_go_inval,
512 .go_demote_ok = meta_go_demote_ok,
513 .go_type = LM_TYPE_META
516 struct gfs2_glock_operations gfs2_inode_glops = {
517 .go_xmote_th = inode_go_xmote_th,
518 .go_xmote_bh = inode_go_xmote_bh,
519 .go_drop_th = inode_go_drop_th,
520 .go_sync = inode_go_sync,
521 .go_inval = inode_go_inval,
522 .go_demote_ok = inode_go_demote_ok,
523 .go_lock = inode_go_lock,
524 .go_unlock = inode_go_unlock,
525 .go_greedy = inode_greedy,
526 .go_type = LM_TYPE_INODE
529 struct gfs2_glock_operations gfs2_rgrp_glops = {
530 .go_xmote_th = gfs2_glock_xmote_th,
531 .go_drop_th = gfs2_glock_drop_th,
532 .go_sync = meta_go_sync,
533 .go_inval = meta_go_inval,
534 .go_demote_ok = rgrp_go_demote_ok,
535 .go_lock = rgrp_go_lock,
536 .go_unlock = rgrp_go_unlock,
537 .go_type = LM_TYPE_RGRP
540 struct gfs2_glock_operations gfs2_trans_glops = {
541 .go_xmote_th = trans_go_xmote_th,
542 .go_xmote_bh = trans_go_xmote_bh,
543 .go_drop_th = trans_go_drop_th,
544 .go_type = LM_TYPE_NONDISK
547 struct gfs2_glock_operations gfs2_iopen_glops = {
548 .go_xmote_th = gfs2_glock_xmote_th,
549 .go_drop_th = gfs2_glock_drop_th,
550 .go_callback = gfs2_iopen_go_callback,
551 .go_type = LM_TYPE_IOPEN
554 struct gfs2_glock_operations gfs2_flock_glops = {
555 .go_xmote_th = gfs2_glock_xmote_th,
556 .go_drop_th = gfs2_glock_drop_th,
557 .go_type = LM_TYPE_FLOCK
560 struct gfs2_glock_operations gfs2_nondisk_glops = {
561 .go_xmote_th = gfs2_glock_xmote_th,
562 .go_drop_th = gfs2_glock_drop_th,
563 .go_type = LM_TYPE_NONDISK
566 struct gfs2_glock_operations gfs2_quota_glops = {
567 .go_xmote_th = gfs2_glock_xmote_th,
568 .go_drop_th = gfs2_glock_drop_th,
569 .go_demote_ok = quota_go_demote_ok,
570 .go_type = LM_TYPE_QUOTA
573 struct gfs2_glock_operations gfs2_journal_glops = {
574 .go_xmote_th = gfs2_glock_xmote_th,
575 .go_drop_th = gfs2_glock_drop_th,
576 .go_type = LM_TYPE_JOURNAL