blob: e262f22f744e0ae534b5657ce4ceb9653bfdce0a [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
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.
8 */
9
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>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050015#include <linux/gfs2_ondisk.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000016
17#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050018#include "lm_interface.h"
19#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000020#include "bmap.h"
21#include "glock.h"
22#include "glops.h"
23#include "inode.h"
24#include "log.h"
25#include "meta_io.h"
26#include "page.h"
27#include "recovery.h"
28#include "rgrp.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050029#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000030
31/**
32 * meta_go_sync - sync out the metadata for this glock
33 * @gl: the glock
34 * @flags: DIO_*
35 *
36 * Called when demoting or unlocking an EX glock. We must flush
37 * to disk all dirty buffers/pages relating to this glock, and must not
38 * not return to caller to demote/unlock the glock until I/O is complete.
39 */
40
41static void meta_go_sync(struct gfs2_glock *gl, int flags)
42{
43 if (!(flags & DIO_METADATA))
44 return;
45
46 if (test_and_clear_bit(GLF_DIRTY, &gl->gl_flags)) {
Steven Whitehouseb09e5932006-04-07 11:17:32 -040047 gfs2_log_flush(gl->gl_sbd, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +000048 gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
49 if (flags & DIO_RELEASE)
50 gfs2_ail_empty_gl(gl);
51 }
52
53 clear_bit(GLF_SYNC, &gl->gl_flags);
54}
55
56/**
57 * meta_go_inval - invalidate the metadata for this glock
58 * @gl: the glock
59 * @flags:
60 *
61 */
62
63static void meta_go_inval(struct gfs2_glock *gl, int flags)
64{
65 if (!(flags & DIO_METADATA))
66 return;
67
68 gfs2_meta_inval(gl);
69 gl->gl_vn++;
70}
71
72/**
73 * meta_go_demote_ok - Check to see if it's ok to unlock a glock
74 * @gl: the glock
75 *
76 * Returns: 1 if we have no cached data; ok to demote meta glock
77 */
78
79static int meta_go_demote_ok(struct gfs2_glock *gl)
80{
81 return !gl->gl_aspace->i_mapping->nrpages;
82}
83
84/**
85 * inode_go_xmote_th - promote/demote a glock
86 * @gl: the glock
87 * @state: the requested state
88 * @flags:
89 *
90 */
91
92static void inode_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
93 int flags)
94{
95 if (gl->gl_state != LM_ST_UNLOCKED)
96 gfs2_pte_inval(gl);
97 gfs2_glock_xmote_th(gl, state, flags);
98}
99
100/**
101 * inode_go_xmote_bh - After promoting/demoting a glock
102 * @gl: the glock
103 *
104 */
105
106static void inode_go_xmote_bh(struct gfs2_glock *gl)
107{
108 struct gfs2_holder *gh = gl->gl_req_gh;
109 struct buffer_head *bh;
110 int error;
111
112 if (gl->gl_state != LM_ST_UNLOCKED &&
113 (!gh || !(gh->gh_flags & GL_SKIP))) {
114 error = gfs2_meta_read(gl, gl->gl_name.ln_number, DIO_START,
115 &bh);
116 if (!error)
117 brelse(bh);
118 }
119}
120
121/**
122 * inode_go_drop_th - unlock a glock
123 * @gl: the glock
124 *
125 * Invoked from rq_demote().
126 * Another node needs the lock in EXCLUSIVE mode, or lock (unused for too long)
127 * is being purged from our node's glock cache; we're dropping lock.
128 */
129
130static void inode_go_drop_th(struct gfs2_glock *gl)
131{
132 gfs2_pte_inval(gl);
133 gfs2_glock_drop_th(gl);
134}
135
136/**
137 * inode_go_sync - Sync the dirty data and/or metadata for an inode glock
138 * @gl: the glock protecting the inode
139 * @flags:
140 *
141 */
142
143static void inode_go_sync(struct gfs2_glock *gl, int flags)
144{
145 int meta = (flags & DIO_METADATA);
146 int data = (flags & DIO_DATA);
147
148 if (test_bit(GLF_DIRTY, &gl->gl_flags)) {
149 if (meta && data) {
150 gfs2_page_sync(gl, flags | DIO_START);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400151 gfs2_log_flush(gl->gl_sbd, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000152 gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
153 gfs2_page_sync(gl, flags | DIO_WAIT);
154 clear_bit(GLF_DIRTY, &gl->gl_flags);
155 } else if (meta) {
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400156 gfs2_log_flush(gl->gl_sbd, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000157 gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
158 } else if (data)
159 gfs2_page_sync(gl, flags | DIO_START | DIO_WAIT);
160 if (flags & DIO_RELEASE)
161 gfs2_ail_empty_gl(gl);
162 }
163
164 clear_bit(GLF_SYNC, &gl->gl_flags);
165}
166
167/**
168 * inode_go_inval - prepare a inode glock to be released
169 * @gl: the glock
170 * @flags:
171 *
172 */
173
174static void inode_go_inval(struct gfs2_glock *gl, int flags)
175{
176 int meta = (flags & DIO_METADATA);
177 int data = (flags & DIO_DATA);
178
179 if (meta) {
180 gfs2_meta_inval(gl);
181 gl->gl_vn++;
182 }
183 if (data)
184 gfs2_page_inval(gl);
185}
186
187/**
188 * inode_go_demote_ok - Check to see if it's ok to unlock an inode glock
189 * @gl: the glock
190 *
191 * Returns: 1 if it's ok
192 */
193
194static int inode_go_demote_ok(struct gfs2_glock *gl)
195{
196 struct gfs2_sbd *sdp = gl->gl_sbd;
197 int demote = 0;
198
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500199 if (!gl->gl_object && !gl->gl_aspace->i_mapping->nrpages)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000200 demote = 1;
201 else if (!sdp->sd_args.ar_localcaching &&
202 time_after_eq(jiffies, gl->gl_stamp +
203 gfs2_tune_get(sdp, gt_demote_secs) * HZ))
204 demote = 1;
205
206 return demote;
207}
208
209/**
210 * inode_go_lock - operation done after an inode lock is locked by a process
211 * @gl: the glock
212 * @flags:
213 *
214 * Returns: errno
215 */
216
217static int inode_go_lock(struct gfs2_holder *gh)
218{
219 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500220 struct gfs2_inode *ip = gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000221 int error = 0;
222
223 if (!ip)
224 return 0;
225
226 if (ip->i_vn != gl->gl_vn) {
227 error = gfs2_inode_refresh(ip);
228 if (error)
229 return error;
230 gfs2_inode_attr_in(ip);
231 }
232
233 if ((ip->i_di.di_flags & GFS2_DIF_TRUNC_IN_PROG) &&
234 (gl->gl_state == LM_ST_EXCLUSIVE) &&
235 (gh->gh_flags & GL_LOCAL_EXCL))
236 error = gfs2_truncatei_resume(ip);
237
238 return error;
239}
240
241/**
242 * inode_go_unlock - operation done before an inode lock is unlocked by a
243 * process
244 * @gl: the glock
245 * @flags:
246 *
247 */
248
249static void inode_go_unlock(struct gfs2_holder *gh)
250{
251 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500252 struct gfs2_inode *ip = gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000253
254 if (ip && test_bit(GLF_DIRTY, &gl->gl_flags))
255 gfs2_inode_attr_in(ip);
256
257 if (ip)
258 gfs2_meta_cache_flush(ip);
259}
260
261/**
262 * inode_greedy -
263 * @gl: the glock
264 *
265 */
266
267static void inode_greedy(struct gfs2_glock *gl)
268{
269 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500270 struct gfs2_inode *ip = gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000271 unsigned int quantum = gfs2_tune_get(sdp, gt_greedy_quantum);
272 unsigned int max = gfs2_tune_get(sdp, gt_greedy_max);
273 unsigned int new_time;
274
275 spin_lock(&ip->i_spin);
276
277 if (time_after(ip->i_last_pfault + quantum, jiffies)) {
278 new_time = ip->i_greedy + quantum;
279 if (new_time > max)
280 new_time = max;
281 } else {
282 new_time = ip->i_greedy - quantum;
283 if (!new_time || new_time > max)
284 new_time = 1;
285 }
286
287 ip->i_greedy = new_time;
288
289 spin_unlock(&ip->i_spin);
290
291 gfs2_inode_put(ip);
292}
293
294/**
295 * rgrp_go_demote_ok - Check to see if it's ok to unlock a RG's glock
296 * @gl: the glock
297 *
298 * Returns: 1 if it's ok
299 */
300
301static int rgrp_go_demote_ok(struct gfs2_glock *gl)
302{
303 return !gl->gl_aspace->i_mapping->nrpages;
304}
305
306/**
307 * rgrp_go_lock - operation done after an rgrp lock is locked by
308 * a first holder on this node.
309 * @gl: the glock
310 * @flags:
311 *
312 * Returns: errno
313 */
314
315static int rgrp_go_lock(struct gfs2_holder *gh)
316{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500317 return gfs2_rgrp_bh_get(gh->gh_gl->gl_object);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000318}
319
320/**
321 * rgrp_go_unlock - operation done before an rgrp lock is unlocked by
322 * a last holder on this node.
323 * @gl: the glock
324 * @flags:
325 *
326 */
327
328static void rgrp_go_unlock(struct gfs2_holder *gh)
329{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500330 gfs2_rgrp_bh_put(gh->gh_gl->gl_object);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000331}
332
333/**
334 * trans_go_xmote_th - promote/demote the transaction glock
335 * @gl: the glock
336 * @state: the requested state
337 * @flags:
338 *
339 */
340
341static void trans_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
342 int flags)
343{
344 struct gfs2_sbd *sdp = gl->gl_sbd;
345
346 if (gl->gl_state != LM_ST_UNLOCKED &&
347 test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
348 gfs2_meta_syncfs(sdp);
349 gfs2_log_shutdown(sdp);
350 }
351
352 gfs2_glock_xmote_th(gl, state, flags);
353}
354
355/**
356 * trans_go_xmote_bh - After promoting/demoting the transaction glock
357 * @gl: the glock
358 *
359 */
360
361static void trans_go_xmote_bh(struct gfs2_glock *gl)
362{
363 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500364 struct gfs2_inode *ip = sdp->sd_jdesc->jd_inode->u.generic_ip;
365 struct gfs2_glock *j_gl = ip->i_gl;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000366 struct gfs2_log_header head;
367 int error;
368
369 if (gl->gl_state != LM_ST_UNLOCKED &&
370 test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500371 gfs2_meta_cache_flush(sdp->sd_jdesc->jd_inode->u.generic_ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000372 j_gl->gl_ops->go_inval(j_gl, DIO_METADATA | DIO_DATA);
373
374 error = gfs2_find_jhead(sdp->sd_jdesc, &head);
375 if (error)
376 gfs2_consist(sdp);
377 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT))
378 gfs2_consist(sdp);
379
380 /* Initialize some head of the log stuff */
381 if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) {
382 sdp->sd_log_sequence = head.lh_sequence + 1;
383 gfs2_log_pointers_init(sdp, head.lh_blkno);
384 }
385 }
386}
387
388/**
389 * trans_go_drop_th - unlock the transaction glock
390 * @gl: the glock
391 *
392 * We want to sync the device even with localcaching. Remember
393 * that localcaching journal replay only marks buffers dirty.
394 */
395
396static void trans_go_drop_th(struct gfs2_glock *gl)
397{
398 struct gfs2_sbd *sdp = gl->gl_sbd;
399
400 if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
401 gfs2_meta_syncfs(sdp);
402 gfs2_log_shutdown(sdp);
403 }
404
405 gfs2_glock_drop_th(gl);
406}
407
408/**
409 * quota_go_demote_ok - Check to see if it's ok to unlock a quota glock
410 * @gl: the glock
411 *
412 * Returns: 1 if it's ok
413 */
414
415static int quota_go_demote_ok(struct gfs2_glock *gl)
416{
417 return !atomic_read(&gl->gl_lvb_count);
418}
419
420struct gfs2_glock_operations gfs2_meta_glops = {
421 .go_xmote_th = gfs2_glock_xmote_th,
422 .go_drop_th = gfs2_glock_drop_th,
423 .go_sync = meta_go_sync,
424 .go_inval = meta_go_inval,
425 .go_demote_ok = meta_go_demote_ok,
426 .go_type = LM_TYPE_META
427};
428
429struct gfs2_glock_operations gfs2_inode_glops = {
430 .go_xmote_th = inode_go_xmote_th,
431 .go_xmote_bh = inode_go_xmote_bh,
432 .go_drop_th = inode_go_drop_th,
433 .go_sync = inode_go_sync,
434 .go_inval = inode_go_inval,
435 .go_demote_ok = inode_go_demote_ok,
436 .go_lock = inode_go_lock,
437 .go_unlock = inode_go_unlock,
438 .go_greedy = inode_greedy,
439 .go_type = LM_TYPE_INODE
440};
441
442struct gfs2_glock_operations gfs2_rgrp_glops = {
443 .go_xmote_th = gfs2_glock_xmote_th,
444 .go_drop_th = gfs2_glock_drop_th,
445 .go_sync = meta_go_sync,
446 .go_inval = meta_go_inval,
447 .go_demote_ok = rgrp_go_demote_ok,
448 .go_lock = rgrp_go_lock,
449 .go_unlock = rgrp_go_unlock,
450 .go_type = LM_TYPE_RGRP
451};
452
453struct gfs2_glock_operations gfs2_trans_glops = {
454 .go_xmote_th = trans_go_xmote_th,
455 .go_xmote_bh = trans_go_xmote_bh,
456 .go_drop_th = trans_go_drop_th,
457 .go_type = LM_TYPE_NONDISK
458};
459
460struct gfs2_glock_operations gfs2_iopen_glops = {
461 .go_xmote_th = gfs2_glock_xmote_th,
462 .go_drop_th = gfs2_glock_drop_th,
463 .go_callback = gfs2_iopen_go_callback,
464 .go_type = LM_TYPE_IOPEN
465};
466
467struct gfs2_glock_operations gfs2_flock_glops = {
468 .go_xmote_th = gfs2_glock_xmote_th,
469 .go_drop_th = gfs2_glock_drop_th,
470 .go_type = LM_TYPE_FLOCK
471};
472
473struct gfs2_glock_operations gfs2_nondisk_glops = {
474 .go_xmote_th = gfs2_glock_xmote_th,
475 .go_drop_th = gfs2_glock_drop_th,
476 .go_type = LM_TYPE_NONDISK
477};
478
479struct gfs2_glock_operations gfs2_quota_glops = {
480 .go_xmote_th = gfs2_glock_xmote_th,
481 .go_drop_th = gfs2_glock_drop_th,
482 .go_demote_ok = quota_go_demote_ok,
483 .go_type = LM_TYPE_QUOTA
484};
485
486struct gfs2_glock_operations gfs2_journal_glops = {
487 .go_xmote_th = gfs2_glock_xmote_th,
488 .go_drop_th = gfs2_glock_drop_th,
489 .go_type = LM_TYPE_JOURNAL
490};
491