[GFS2] Use a bio to read the superblock
[safe/jmp/linux-2.6] / fs / gfs2 / glops.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
4  *
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>
15 #include <linux/gfs2_ondisk.h>
16
17 #include "gfs2.h"
18 #include "lm_interface.h"
19 #include "incore.h"
20 #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 "recovery.h"
27 #include "rgrp.h"
28 #include "util.h"
29
30
31 /**
32  * gfs2_pte_inval - Sync and invalidate all PTEs associated with a glock
33  * @gl: the glock
34  *
35  */
36
37 static 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
64 static 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
88 static 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
118 /**
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
128 static 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)) {
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);
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
150 static 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 /**
160  * inode_go_xmote_th - promote/demote a glock
161  * @gl: the glock
162  * @state: the requested state
163  * @flags:
164  *
165  */
166
167 static 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
181 static 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
205 static 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
218 static 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);
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);
230                 } else if (meta) {
231                         gfs2_log_flush(gl->gl_sbd, gl);
232                         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
249 static 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
269 static int inode_go_demote_ok(struct gfs2_glock *gl)
270 {
271         struct gfs2_sbd *sdp = gl->gl_sbd;
272         int demote = 0;
273
274         if (!gl->gl_object && !gl->gl_aspace->i_mapping->nrpages)
275                 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
292 static int inode_go_lock(struct gfs2_holder *gh)
293 {
294         struct gfs2_glock *gl = gh->gh_gl;
295         struct gfs2_inode *ip = gl->gl_object;
296         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
324 static void inode_go_unlock(struct gfs2_holder *gh)
325 {
326         struct gfs2_glock *gl = gh->gh_gl;
327         struct gfs2_inode *ip = gl->gl_object;
328
329         if (ip) {
330                 if (test_bit(GLF_DIRTY, &gl->gl_flags))
331                         gfs2_inode_attr_in(ip);
332
333                 gfs2_meta_cache_flush(ip);
334         }
335 }
336
337 /**
338  * inode_greedy -
339  * @gl: the glock
340  *
341  */
342
343 static void inode_greedy(struct gfs2_glock *gl)
344 {
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;
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
367         iput(&ip->i_inode);
368 }
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
377 static 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
391 static int rgrp_go_lock(struct gfs2_holder *gh)
392 {
393         return gfs2_rgrp_bh_get(gh->gh_gl->gl_object);
394 }
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
404 static void rgrp_go_unlock(struct gfs2_holder *gh)
405 {
406         gfs2_rgrp_bh_put(gh->gh_gl->gl_object);
407 }
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
417 static 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
437 static void trans_go_xmote_bh(struct gfs2_glock *gl)
438 {
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;
443         int error;
444
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);
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
472 static 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
491 static int quota_go_demote_ok(struct gfs2_glock *gl)
492 {
493         return !atomic_read(&gl->gl_lvb_count);
494 }
495
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
500 };
501
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
513 };
514
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
524 };
525
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
531 };
532
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
538 };
539
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
544 };
545
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
550 };
551
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
557 };
558
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
563 };
564