blob: 0c92c52fc92aee0459873865f73ca27273c50363 [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"
David Teiglandb3b94fa2006-01-16 16:50:04 +000026#include "recovery.h"
27#include "rgrp.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050028#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000029
Steven Whitehouseba7f7292006-07-26 11:27:10 -040030
31/**
32 * gfs2_pte_inval - Sync and invalidate all PTEs associated with a glock
33 * @gl: the glock
34 *
35 */
36
37static void gfs2_pte_inval(struct gfs2_glock *gl)
38{
39 struct gfs2_inode *ip;
40 struct inode *inode;
41
42 ip = gl->gl_object;
43 inode = &ip->i_inode;
44 if (!ip || !S_ISREG(ip->i_di.di_mode))
45 return;
46
47 if (!test_bit(GIF_PAGED, &ip->i_flags))
48 return;
49
50 unmap_shared_mapping_range(inode->i_mapping, 0, 0);
51
52 if (test_bit(GIF_SW_PAGED, &ip->i_flags))
53 set_bit(GLF_DIRTY, &gl->gl_flags);
54
55 clear_bit(GIF_SW_PAGED, &ip->i_flags);
56}
57
58/**
59 * gfs2_page_inval - Invalidate all pages associated with a glock
60 * @gl: the glock
61 *
62 */
63
64static void gfs2_page_inval(struct gfs2_glock *gl)
65{
66 struct gfs2_inode *ip;
67 struct inode *inode;
68
69 ip = gl->gl_object;
70 inode = &ip->i_inode;
71 if (!ip || !S_ISREG(ip->i_di.di_mode))
72 return;
73
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);
77}
78
79/**
80 * gfs2_page_sync - Sync the data pages (not metadata) associated with a glock
81 * @gl: the glock
82 * @flags: DIO_START | DIO_WAIT
83 *
84 * Syncs data (not metadata) for a regular file.
85 * No-op for all other types.
86 */
87
88static void gfs2_page_sync(struct gfs2_glock *gl, int flags)
89{
90 struct gfs2_inode *ip;
91 struct inode *inode;
92 struct address_space *mapping;
93 int error = 0;
94
95 ip = gl->gl_object;
96 inode = &ip->i_inode;
97 if (!ip || !S_ISREG(ip->i_di.di_mode))
98 return;
99
100 mapping = inode->i_mapping;
101
102 if (flags & DIO_START)
103 filemap_fdatawrite(mapping);
104 if (!error && (flags & DIO_WAIT))
105 error = filemap_fdatawait(mapping);
106
107 /* Put back any errors cleared by filemap_fdatawait()
108 so they can be caught by someone who can pass them
109 up to user space. */
110
111 if (error == -ENOSPC)
112 set_bit(AS_ENOSPC, &mapping->flags);
113 else if (error)
114 set_bit(AS_EIO, &mapping->flags);
115
116}
117
David Teiglandb3b94fa2006-01-16 16:50:04 +0000118/**
119 * meta_go_sync - sync out the metadata for this glock
120 * @gl: the glock
121 * @flags: DIO_*
122 *
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.
126 */
127
128static void meta_go_sync(struct gfs2_glock *gl, int flags)
129{
130 if (!(flags & DIO_METADATA))
131 return;
132
133 if (test_and_clear_bit(GLF_DIRTY, &gl->gl_flags)) {
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400134 gfs2_log_flush(gl->gl_sbd, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000135 gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
136 if (flags & DIO_RELEASE)
137 gfs2_ail_empty_gl(gl);
138 }
139
140 clear_bit(GLF_SYNC, &gl->gl_flags);
141}
142
143/**
144 * meta_go_inval - invalidate the metadata for this glock
145 * @gl: the glock
146 * @flags:
147 *
148 */
149
150static void meta_go_inval(struct gfs2_glock *gl, int flags)
151{
152 if (!(flags & DIO_METADATA))
153 return;
154
155 gfs2_meta_inval(gl);
156 gl->gl_vn++;
157}
158
159/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000160 * inode_go_xmote_th - promote/demote a glock
161 * @gl: the glock
162 * @state: the requested state
163 * @flags:
164 *
165 */
166
167static void inode_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
168 int flags)
169{
170 if (gl->gl_state != LM_ST_UNLOCKED)
171 gfs2_pte_inval(gl);
172 gfs2_glock_xmote_th(gl, state, flags);
173}
174
175/**
176 * inode_go_xmote_bh - After promoting/demoting a glock
177 * @gl: the glock
178 *
179 */
180
181static void inode_go_xmote_bh(struct gfs2_glock *gl)
182{
183 struct gfs2_holder *gh = gl->gl_req_gh;
184 struct buffer_head *bh;
185 int error;
186
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,
190 &bh);
191 if (!error)
192 brelse(bh);
193 }
194}
195
196/**
197 * inode_go_drop_th - unlock a glock
198 * @gl: the glock
199 *
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.
203 */
204
205static void inode_go_drop_th(struct gfs2_glock *gl)
206{
207 gfs2_pte_inval(gl);
208 gfs2_glock_drop_th(gl);
209}
210
211/**
212 * inode_go_sync - Sync the dirty data and/or metadata for an inode glock
213 * @gl: the glock protecting the inode
214 * @flags:
215 *
216 */
217
218static void inode_go_sync(struct gfs2_glock *gl, int flags)
219{
220 int meta = (flags & DIO_METADATA);
221 int data = (flags & DIO_DATA);
222
223 if (test_bit(GLF_DIRTY, &gl->gl_flags)) {
224 if (meta && data) {
225 gfs2_page_sync(gl, flags | DIO_START);
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400226 gfs2_log_flush(gl->gl_sbd, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000227 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);
230 } else if (meta) {
Steven Whitehouseb09e5932006-04-07 11:17:32 -0400231 gfs2_log_flush(gl->gl_sbd, gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000232 gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
233 } else if (data)
234 gfs2_page_sync(gl, flags | DIO_START | DIO_WAIT);
235 if (flags & DIO_RELEASE)
236 gfs2_ail_empty_gl(gl);
237 }
238
239 clear_bit(GLF_SYNC, &gl->gl_flags);
240}
241
242/**
243 * inode_go_inval - prepare a inode glock to be released
244 * @gl: the glock
245 * @flags:
246 *
247 */
248
249static void inode_go_inval(struct gfs2_glock *gl, int flags)
250{
251 int meta = (flags & DIO_METADATA);
252 int data = (flags & DIO_DATA);
253
254 if (meta) {
255 gfs2_meta_inval(gl);
256 gl->gl_vn++;
257 }
258 if (data)
259 gfs2_page_inval(gl);
260}
261
262/**
263 * inode_go_demote_ok - Check to see if it's ok to unlock an inode glock
264 * @gl: the glock
265 *
266 * Returns: 1 if it's ok
267 */
268
269static int inode_go_demote_ok(struct gfs2_glock *gl)
270{
271 struct gfs2_sbd *sdp = gl->gl_sbd;
272 int demote = 0;
273
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500274 if (!gl->gl_object && !gl->gl_aspace->i_mapping->nrpages)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000275 demote = 1;
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))
279 demote = 1;
280
281 return demote;
282}
283
284/**
285 * inode_go_lock - operation done after an inode lock is locked by a process
286 * @gl: the glock
287 * @flags:
288 *
289 * Returns: errno
290 */
291
292static int inode_go_lock(struct gfs2_holder *gh)
293{
294 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500295 struct gfs2_inode *ip = gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000296 int error = 0;
297
298 if (!ip)
299 return 0;
300
301 if (ip->i_vn != gl->gl_vn) {
302 error = gfs2_inode_refresh(ip);
303 if (error)
304 return error;
305 gfs2_inode_attr_in(ip);
306 }
307
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);
312
313 return error;
314}
315
316/**
317 * inode_go_unlock - operation done before an inode lock is unlocked by a
318 * process
319 * @gl: the glock
320 * @flags:
321 *
322 */
323
324static void inode_go_unlock(struct gfs2_holder *gh)
325{
326 struct gfs2_glock *gl = gh->gh_gl;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500327 struct gfs2_inode *ip = gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000328
Steven Whitehousef45b7dd2006-07-27 13:53:53 -0400329 if (ip) {
330 if (test_bit(GLF_DIRTY, &gl->gl_flags))
331 gfs2_inode_attr_in(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000332
David Teiglandb3b94fa2006-01-16 16:50:04 +0000333 gfs2_meta_cache_flush(ip);
Steven Whitehousef45b7dd2006-07-27 13:53:53 -0400334 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000335}
336
337/**
338 * inode_greedy -
339 * @gl: the glock
340 *
341 */
342
343static void inode_greedy(struct gfs2_glock *gl)
344{
345 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500346 struct gfs2_inode *ip = gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000347 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;
350
351 spin_lock(&ip->i_spin);
352
353 if (time_after(ip->i_last_pfault + quantum, jiffies)) {
354 new_time = ip->i_greedy + quantum;
355 if (new_time > max)
356 new_time = max;
357 } else {
358 new_time = ip->i_greedy - quantum;
359 if (!new_time || new_time > max)
360 new_time = 1;
361 }
362
363 ip->i_greedy = new_time;
364
365 spin_unlock(&ip->i_spin);
366
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400367 iput(&ip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000368}
369
370/**
371 * rgrp_go_demote_ok - Check to see if it's ok to unlock a RG's glock
372 * @gl: the glock
373 *
374 * Returns: 1 if it's ok
375 */
376
377static int rgrp_go_demote_ok(struct gfs2_glock *gl)
378{
379 return !gl->gl_aspace->i_mapping->nrpages;
380}
381
382/**
383 * rgrp_go_lock - operation done after an rgrp lock is locked by
384 * a first holder on this node.
385 * @gl: the glock
386 * @flags:
387 *
388 * Returns: errno
389 */
390
391static int rgrp_go_lock(struct gfs2_holder *gh)
392{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500393 return gfs2_rgrp_bh_get(gh->gh_gl->gl_object);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000394}
395
396/**
397 * rgrp_go_unlock - operation done before an rgrp lock is unlocked by
398 * a last holder on this node.
399 * @gl: the glock
400 * @flags:
401 *
402 */
403
404static void rgrp_go_unlock(struct gfs2_holder *gh)
405{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500406 gfs2_rgrp_bh_put(gh->gh_gl->gl_object);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000407}
408
409/**
410 * trans_go_xmote_th - promote/demote the transaction glock
411 * @gl: the glock
412 * @state: the requested state
413 * @flags:
414 *
415 */
416
417static void trans_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
418 int flags)
419{
420 struct gfs2_sbd *sdp = gl->gl_sbd;
421
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);
426 }
427
428 gfs2_glock_xmote_th(gl, state, flags);
429}
430
431/**
432 * trans_go_xmote_bh - After promoting/demoting the transaction glock
433 * @gl: the glock
434 *
435 */
436
437static void trans_go_xmote_bh(struct gfs2_glock *gl)
438{
439 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400440 struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500441 struct gfs2_glock *j_gl = ip->i_gl;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000442 struct gfs2_log_header head;
443 int error;
444
445 if (gl->gl_state != LM_ST_UNLOCKED &&
446 test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400447 gfs2_meta_cache_flush(GFS2_I(sdp->sd_jdesc->jd_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000448 j_gl->gl_ops->go_inval(j_gl, DIO_METADATA | DIO_DATA);
449
450 error = gfs2_find_jhead(sdp->sd_jdesc, &head);
451 if (error)
452 gfs2_consist(sdp);
453 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT))
454 gfs2_consist(sdp);
455
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);
460 }
461 }
462}
463
464/**
465 * trans_go_drop_th - unlock the transaction glock
466 * @gl: the glock
467 *
468 * We want to sync the device even with localcaching. Remember
469 * that localcaching journal replay only marks buffers dirty.
470 */
471
472static void trans_go_drop_th(struct gfs2_glock *gl)
473{
474 struct gfs2_sbd *sdp = gl->gl_sbd;
475
476 if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
477 gfs2_meta_syncfs(sdp);
478 gfs2_log_shutdown(sdp);
479 }
480
481 gfs2_glock_drop_th(gl);
482}
483
484/**
485 * quota_go_demote_ok - Check to see if it's ok to unlock a quota glock
486 * @gl: the glock
487 *
488 * Returns: 1 if it's ok
489 */
490
491static int quota_go_demote_ok(struct gfs2_glock *gl)
492{
493 return !atomic_read(&gl->gl_lvb_count);
494}
495
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400496const struct gfs2_glock_operations gfs2_meta_glops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000497 .go_xmote_th = gfs2_glock_xmote_th,
498 .go_drop_th = gfs2_glock_drop_th,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000499 .go_type = LM_TYPE_META
500};
501
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400502const struct gfs2_glock_operations gfs2_inode_glops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000503 .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
513};
514
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400515const struct gfs2_glock_operations gfs2_rgrp_glops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000516 .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
524};
525
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400526const struct gfs2_glock_operations gfs2_trans_glops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000527 .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
531};
532
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400533const struct gfs2_glock_operations gfs2_iopen_glops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000534 .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
538};
539
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400540const struct gfs2_glock_operations gfs2_flock_glops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000541 .go_xmote_th = gfs2_glock_xmote_th,
542 .go_drop_th = gfs2_glock_drop_th,
543 .go_type = LM_TYPE_FLOCK
544};
545
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400546const struct gfs2_glock_operations gfs2_nondisk_glops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000547 .go_xmote_th = gfs2_glock_xmote_th,
548 .go_drop_th = gfs2_glock_drop_th,
549 .go_type = LM_TYPE_NONDISK
550};
551
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400552const struct gfs2_glock_operations gfs2_quota_glops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000553 .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
557};
558
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400559const struct gfs2_glock_operations gfs2_journal_glops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000560 .go_xmote_th = gfs2_glock_xmote_th,
561 .go_drop_th = gfs2_glock_drop_th,
562 .go_type = LM_TYPE_JOURNAL
563};
564