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>
16 #include <linux/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_wait - Wait for writeback of data
83 * Syncs data (not metadata) for a regular file.
84 * No-op for all other types.
87 static void gfs2_page_wait(struct gfs2_glock *gl)
89 struct gfs2_inode *ip = gl->gl_object;
90 struct inode *inode = &ip->i_inode;
91 struct address_space *mapping = inode->i_mapping;
94 if (!S_ISREG(ip->i_di.di_mode))
97 error = filemap_fdatawait(mapping);
99 /* Put back any errors cleared by filemap_fdatawait()
100 so they can be caught by someone who can pass them
103 if (error == -ENOSPC)
104 set_bit(AS_ENOSPC, &mapping->flags);
106 set_bit(AS_EIO, &mapping->flags);
110 static void gfs2_page_writeback(struct gfs2_glock *gl)
112 struct gfs2_inode *ip = gl->gl_object;
113 struct inode *inode = &ip->i_inode;
114 struct address_space *mapping = inode->i_mapping;
116 if (!S_ISREG(ip->i_di.di_mode))
119 filemap_fdatawrite(mapping);
123 * meta_go_sync - sync out the metadata for this glock
127 * Called when demoting or unlocking an EX glock. We must flush
128 * to disk all dirty buffers/pages relating to this glock, and must not
129 * not return to caller to demote/unlock the glock until I/O is complete.
132 static void meta_go_sync(struct gfs2_glock *gl, int flags)
134 if (!(flags & DIO_METADATA))
137 if (test_and_clear_bit(GLF_DIRTY, &gl->gl_flags)) {
138 gfs2_log_flush(gl->gl_sbd, gl);
140 if (flags & DIO_RELEASE)
141 gfs2_ail_empty_gl(gl);
147 * meta_go_inval - invalidate the metadata for this glock
153 static void meta_go_inval(struct gfs2_glock *gl, int flags)
155 if (!(flags & DIO_METADATA))
163 * inode_go_xmote_th - promote/demote a glock
165 * @state: the requested state
170 static void inode_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
173 if (gl->gl_state != LM_ST_UNLOCKED)
175 gfs2_glock_xmote_th(gl, state, flags);
179 * inode_go_xmote_bh - After promoting/demoting a glock
184 static void inode_go_xmote_bh(struct gfs2_glock *gl)
186 struct gfs2_holder *gh = gl->gl_req_gh;
187 struct buffer_head *bh;
190 if (gl->gl_state != LM_ST_UNLOCKED &&
191 (!gh || !(gh->gh_flags & GL_SKIP))) {
192 error = gfs2_meta_read(gl, gl->gl_name.ln_number, 0, &bh);
199 * inode_go_drop_th - unlock a glock
202 * Invoked from rq_demote().
203 * Another node needs the lock in EXCLUSIVE mode, or lock (unused for too long)
204 * is being purged from our node's glock cache; we're dropping lock.
207 static void inode_go_drop_th(struct gfs2_glock *gl)
210 gfs2_glock_drop_th(gl);
214 * inode_go_sync - Sync the dirty data and/or metadata for an inode glock
215 * @gl: the glock protecting the inode
220 static void inode_go_sync(struct gfs2_glock *gl, int flags)
222 int meta = (flags & DIO_METADATA);
223 int data = (flags & DIO_DATA);
225 if (test_bit(GLF_DIRTY, &gl->gl_flags)) {
227 gfs2_page_writeback(gl);
228 gfs2_log_flush(gl->gl_sbd, gl);
231 clear_bit(GLF_DIRTY, &gl->gl_flags);
233 gfs2_log_flush(gl->gl_sbd, gl);
236 gfs2_page_writeback(gl);
239 if (flags & DIO_RELEASE)
240 gfs2_ail_empty_gl(gl);
245 * inode_go_inval - prepare a inode glock to be released
251 static void inode_go_inval(struct gfs2_glock *gl, int flags)
253 int meta = (flags & DIO_METADATA);
254 int data = (flags & DIO_DATA);
265 * inode_go_demote_ok - Check to see if it's ok to unlock an inode glock
268 * Returns: 1 if it's ok
271 static int inode_go_demote_ok(struct gfs2_glock *gl)
273 struct gfs2_sbd *sdp = gl->gl_sbd;
276 if (!gl->gl_object && !gl->gl_aspace->i_mapping->nrpages)
278 else if (!sdp->sd_args.ar_localcaching &&
279 time_after_eq(jiffies, gl->gl_stamp +
280 gfs2_tune_get(sdp, gt_demote_secs) * HZ))
287 * inode_go_lock - operation done after an inode lock is locked by a process
294 static int inode_go_lock(struct gfs2_holder *gh)
296 struct gfs2_glock *gl = gh->gh_gl;
297 struct gfs2_inode *ip = gl->gl_object;
303 if (ip->i_vn != gl->gl_vn) {
304 error = gfs2_inode_refresh(ip);
307 gfs2_inode_attr_in(ip);
310 if ((ip->i_di.di_flags & GFS2_DIF_TRUNC_IN_PROG) &&
311 (gl->gl_state == LM_ST_EXCLUSIVE) &&
312 (gh->gh_flags & GL_LOCAL_EXCL))
313 error = gfs2_truncatei_resume(ip);
319 * inode_go_unlock - operation done before an inode lock is unlocked by a
326 static void inode_go_unlock(struct gfs2_holder *gh)
328 struct gfs2_glock *gl = gh->gh_gl;
329 struct gfs2_inode *ip = gl->gl_object;
333 if (test_bit(GLF_DIRTY, &gl->gl_flags))
334 gfs2_inode_attr_in(ip);
335 gfs2_meta_cache_flush(ip);
344 static void inode_greedy(struct gfs2_glock *gl)
346 struct gfs2_sbd *sdp = gl->gl_sbd;
347 struct gfs2_inode *ip = gl->gl_object;
348 unsigned int quantum = gfs2_tune_get(sdp, gt_greedy_quantum);
349 unsigned int max = gfs2_tune_get(sdp, gt_greedy_max);
350 unsigned int new_time;
352 spin_lock(&ip->i_spin);
354 if (time_after(ip->i_last_pfault + quantum, jiffies)) {
355 new_time = ip->i_greedy + quantum;
359 new_time = ip->i_greedy - quantum;
360 if (!new_time || new_time > max)
364 ip->i_greedy = new_time;
366 spin_unlock(&ip->i_spin);
372 * rgrp_go_demote_ok - Check to see if it's ok to unlock a RG's glock
375 * Returns: 1 if it's ok
378 static int rgrp_go_demote_ok(struct gfs2_glock *gl)
380 return !gl->gl_aspace->i_mapping->nrpages;
384 * rgrp_go_lock - operation done after an rgrp lock is locked by
385 * a first holder on this node.
392 static int rgrp_go_lock(struct gfs2_holder *gh)
394 return gfs2_rgrp_bh_get(gh->gh_gl->gl_object);
398 * rgrp_go_unlock - operation done before an rgrp lock is unlocked by
399 * a last holder on this node.
405 static void rgrp_go_unlock(struct gfs2_holder *gh)
407 gfs2_rgrp_bh_put(gh->gh_gl->gl_object);
411 * trans_go_xmote_th - promote/demote the transaction glock
413 * @state: the requested state
418 static void trans_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
421 struct gfs2_sbd *sdp = gl->gl_sbd;
423 if (gl->gl_state != LM_ST_UNLOCKED &&
424 test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
425 gfs2_meta_syncfs(sdp);
426 gfs2_log_shutdown(sdp);
429 gfs2_glock_xmote_th(gl, state, flags);
433 * trans_go_xmote_bh - After promoting/demoting the transaction glock
438 static void trans_go_xmote_bh(struct gfs2_glock *gl)
440 struct gfs2_sbd *sdp = gl->gl_sbd;
441 struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
442 struct gfs2_glock *j_gl = ip->i_gl;
443 struct gfs2_log_header head;
446 if (gl->gl_state != LM_ST_UNLOCKED &&
447 test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
448 gfs2_meta_cache_flush(GFS2_I(sdp->sd_jdesc->jd_inode));
449 j_gl->gl_ops->go_inval(j_gl, DIO_METADATA | DIO_DATA);
451 error = gfs2_find_jhead(sdp->sd_jdesc, &head);
454 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT))
457 /* Initialize some head of the log stuff */
458 if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) {
459 sdp->sd_log_sequence = head.lh_sequence + 1;
460 gfs2_log_pointers_init(sdp, head.lh_blkno);
466 * trans_go_drop_th - unlock the transaction glock
469 * We want to sync the device even with localcaching. Remember
470 * that localcaching journal replay only marks buffers dirty.
473 static void trans_go_drop_th(struct gfs2_glock *gl)
475 struct gfs2_sbd *sdp = gl->gl_sbd;
477 if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
478 gfs2_meta_syncfs(sdp);
479 gfs2_log_shutdown(sdp);
482 gfs2_glock_drop_th(gl);
486 * quota_go_demote_ok - Check to see if it's ok to unlock a quota glock
489 * Returns: 1 if it's ok
492 static int quota_go_demote_ok(struct gfs2_glock *gl)
494 return !atomic_read(&gl->gl_lvb_count);
497 const struct gfs2_glock_operations gfs2_meta_glops = {
498 .go_xmote_th = gfs2_glock_xmote_th,
499 .go_drop_th = gfs2_glock_drop_th,
500 .go_type = LM_TYPE_META,
503 const struct gfs2_glock_operations gfs2_inode_glops = {
504 .go_xmote_th = inode_go_xmote_th,
505 .go_xmote_bh = inode_go_xmote_bh,
506 .go_drop_th = inode_go_drop_th,
507 .go_sync = inode_go_sync,
508 .go_inval = inode_go_inval,
509 .go_demote_ok = inode_go_demote_ok,
510 .go_lock = inode_go_lock,
511 .go_unlock = inode_go_unlock,
512 .go_greedy = inode_greedy,
513 .go_type = LM_TYPE_INODE,
516 const struct gfs2_glock_operations gfs2_rgrp_glops = {
517 .go_xmote_th = gfs2_glock_xmote_th,
518 .go_drop_th = gfs2_glock_drop_th,
519 .go_sync = meta_go_sync,
520 .go_inval = meta_go_inval,
521 .go_demote_ok = rgrp_go_demote_ok,
522 .go_lock = rgrp_go_lock,
523 .go_unlock = rgrp_go_unlock,
524 .go_type = LM_TYPE_RGRP,
527 const struct gfs2_glock_operations gfs2_trans_glops = {
528 .go_xmote_th = trans_go_xmote_th,
529 .go_xmote_bh = trans_go_xmote_bh,
530 .go_drop_th = trans_go_drop_th,
531 .go_type = LM_TYPE_NONDISK,
534 const struct gfs2_glock_operations gfs2_iopen_glops = {
535 .go_xmote_th = gfs2_glock_xmote_th,
536 .go_drop_th = gfs2_glock_drop_th,
537 .go_type = LM_TYPE_IOPEN,
540 const 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 const 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 const 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 const 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,