]> err.no Git - linux-2.6/blob - fs/gfs2/glops.c
[GFS2] Remove page.[ch]
[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  * meta_go_demote_ok - Check to see if it's ok to unlock a glock
161  * @gl: the glock
162  *
163  * Returns: 1 if we have no cached data; ok to demote meta glock
164  */
165
166 static int meta_go_demote_ok(struct gfs2_glock *gl)
167 {
168         return !gl->gl_aspace->i_mapping->nrpages;
169 }
170
171 /**
172  * inode_go_xmote_th - promote/demote a glock
173  * @gl: the glock
174  * @state: the requested state
175  * @flags:
176  *
177  */
178
179 static void inode_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
180                               int flags)
181 {
182         if (gl->gl_state != LM_ST_UNLOCKED)
183                 gfs2_pte_inval(gl);
184         gfs2_glock_xmote_th(gl, state, flags);
185 }
186
187 /**
188  * inode_go_xmote_bh - After promoting/demoting a glock
189  * @gl: the glock
190  *
191  */
192
193 static void inode_go_xmote_bh(struct gfs2_glock *gl)
194 {
195         struct gfs2_holder *gh = gl->gl_req_gh;
196         struct buffer_head *bh;
197         int error;
198
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,
202                                        &bh);
203                 if (!error)
204                         brelse(bh);
205         }
206 }
207
208 /**
209  * inode_go_drop_th - unlock a glock
210  * @gl: the glock
211  *
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.
215  */
216
217 static void inode_go_drop_th(struct gfs2_glock *gl)
218 {
219         gfs2_pte_inval(gl);
220         gfs2_glock_drop_th(gl);
221 }
222
223 /**
224  * inode_go_sync - Sync the dirty data and/or metadata for an inode glock
225  * @gl: the glock protecting the inode
226  * @flags:
227  *
228  */
229
230 static void inode_go_sync(struct gfs2_glock *gl, int flags)
231 {
232         int meta = (flags & DIO_METADATA);
233         int data = (flags & DIO_DATA);
234
235         if (test_bit(GLF_DIRTY, &gl->gl_flags)) {
236                 if (meta && data) {
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);
242                 } else if (meta) {
243                         gfs2_log_flush(gl->gl_sbd, gl);
244                         gfs2_meta_sync(gl, flags | DIO_START | DIO_WAIT);
245                 } else if (data)
246                         gfs2_page_sync(gl, flags | DIO_START | DIO_WAIT);
247                 if (flags & DIO_RELEASE)
248                         gfs2_ail_empty_gl(gl);
249         }
250
251         clear_bit(GLF_SYNC, &gl->gl_flags);
252 }
253
254 /**
255  * inode_go_inval - prepare a inode glock to be released
256  * @gl: the glock
257  * @flags:
258  *
259  */
260
261 static void inode_go_inval(struct gfs2_glock *gl, int flags)
262 {
263         int meta = (flags & DIO_METADATA);
264         int data = (flags & DIO_DATA);
265
266         if (meta) {
267                 gfs2_meta_inval(gl);
268                 gl->gl_vn++;
269         }
270         if (data)
271                 gfs2_page_inval(gl);
272 }
273
274 /**
275  * inode_go_demote_ok - Check to see if it's ok to unlock an inode glock
276  * @gl: the glock
277  *
278  * Returns: 1 if it's ok
279  */
280
281 static int inode_go_demote_ok(struct gfs2_glock *gl)
282 {
283         struct gfs2_sbd *sdp = gl->gl_sbd;
284         int demote = 0;
285
286         if (!gl->gl_object && !gl->gl_aspace->i_mapping->nrpages)
287                 demote = 1;
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))
291                 demote = 1;
292
293         return demote;
294 }
295
296 /**
297  * inode_go_lock - operation done after an inode lock is locked by a process
298  * @gl: the glock
299  * @flags:
300  *
301  * Returns: errno
302  */
303
304 static int inode_go_lock(struct gfs2_holder *gh)
305 {
306         struct gfs2_glock *gl = gh->gh_gl;
307         struct gfs2_inode *ip = gl->gl_object;
308         int error = 0;
309
310         if (!ip)
311                 return 0;
312
313         if (ip->i_vn != gl->gl_vn) {
314                 error = gfs2_inode_refresh(ip);
315                 if (error)
316                         return error;
317                 gfs2_inode_attr_in(ip);
318         }
319
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);
324
325         return error;
326 }
327
328 /**
329  * inode_go_unlock - operation done before an inode lock is unlocked by a
330  *                   process
331  * @gl: the glock
332  * @flags:
333  *
334  */
335
336 static void inode_go_unlock(struct gfs2_holder *gh)
337 {
338         struct gfs2_glock *gl = gh->gh_gl;
339         struct gfs2_inode *ip = gl->gl_object;
340
341         if (ip && test_bit(GLF_DIRTY, &gl->gl_flags))
342                 gfs2_inode_attr_in(ip);
343
344         if (ip)
345                 gfs2_meta_cache_flush(ip);
346 }
347
348 /**
349  * inode_greedy -
350  * @gl: the glock
351  *
352  */
353
354 static void inode_greedy(struct gfs2_glock *gl)
355 {
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;
361
362         spin_lock(&ip->i_spin);
363
364         if (time_after(ip->i_last_pfault + quantum, jiffies)) {
365                 new_time = ip->i_greedy + quantum;
366                 if (new_time > max)
367                         new_time = max;
368         } else {
369                 new_time = ip->i_greedy - quantum;
370                 if (!new_time || new_time > max)
371                         new_time = 1;
372         }
373
374         ip->i_greedy = new_time;
375
376         spin_unlock(&ip->i_spin);
377
378         iput(&ip->i_inode);
379 }
380
381 /**
382  * rgrp_go_demote_ok - Check to see if it's ok to unlock a RG's glock
383  * @gl: the glock
384  *
385  * Returns: 1 if it's ok
386  */
387
388 static int rgrp_go_demote_ok(struct gfs2_glock *gl)
389 {
390         return !gl->gl_aspace->i_mapping->nrpages;
391 }
392
393 /**
394  * rgrp_go_lock - operation done after an rgrp lock is locked by
395  *    a first holder on this node.
396  * @gl: the glock
397  * @flags:
398  *
399  * Returns: errno
400  */
401
402 static int rgrp_go_lock(struct gfs2_holder *gh)
403 {
404         return gfs2_rgrp_bh_get(gh->gh_gl->gl_object);
405 }
406
407 /**
408  * rgrp_go_unlock - operation done before an rgrp lock is unlocked by
409  *    a last holder on this node.
410  * @gl: the glock
411  * @flags:
412  *
413  */
414
415 static void rgrp_go_unlock(struct gfs2_holder *gh)
416 {
417         gfs2_rgrp_bh_put(gh->gh_gl->gl_object);
418 }
419
420 /**
421  * trans_go_xmote_th - promote/demote the transaction glock
422  * @gl: the glock
423  * @state: the requested state
424  * @flags:
425  *
426  */
427
428 static void trans_go_xmote_th(struct gfs2_glock *gl, unsigned int state,
429                               int flags)
430 {
431         struct gfs2_sbd *sdp = gl->gl_sbd;
432
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);
437         }
438
439         gfs2_glock_xmote_th(gl, state, flags);
440 }
441
442 /**
443  * trans_go_xmote_bh - After promoting/demoting the transaction glock
444  * @gl: the glock
445  *
446  */
447
448 static void trans_go_xmote_bh(struct gfs2_glock *gl)
449 {
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;
454         int error;
455
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);
460
461                 error = gfs2_find_jhead(sdp->sd_jdesc, &head);
462                 if (error)
463                         gfs2_consist(sdp);
464                 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT))
465                         gfs2_consist(sdp);
466
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);
471                 }
472         }
473 }
474
475 /**
476  * trans_go_drop_th - unlock the transaction glock
477  * @gl: the glock
478  *
479  * We want to sync the device even with localcaching.  Remember
480  * that localcaching journal replay only marks buffers dirty.
481  */
482
483 static void trans_go_drop_th(struct gfs2_glock *gl)
484 {
485         struct gfs2_sbd *sdp = gl->gl_sbd;
486
487         if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
488                 gfs2_meta_syncfs(sdp);
489                 gfs2_log_shutdown(sdp);
490         }
491
492         gfs2_glock_drop_th(gl);
493 }
494
495 /**
496  * quota_go_demote_ok - Check to see if it's ok to unlock a quota glock
497  * @gl: the glock
498  *
499  * Returns: 1 if it's ok
500  */
501
502 static int quota_go_demote_ok(struct gfs2_glock *gl)
503 {
504         return !atomic_read(&gl->gl_lvb_count);
505 }
506
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
514 };
515
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
527 };
528
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
538 };
539
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
545 };
546
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
552 };
553
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
558 };
559
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
564 };
565
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
571 };
572
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
577 };
578