Blame view

fs/gfs2/meta_io.c 12.6 KB
7336d0e65   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
b3b94faa5   David Teigland   [GFS2] The core o...
2
3
  /*
   * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
091806edd   Bob Peterson   [GFS2] filesystem...
4
   * Copyright (C) 2004-2008 Red Hat, Inc.  All rights reserved.
b3b94faa5   David Teigland   [GFS2] The core o...
5
6
7
8
9
10
11
12
13
14
15
16
   */
  
  #include <linux/sched.h>
  #include <linux/slab.h>
  #include <linux/spinlock.h>
  #include <linux/completion.h>
  #include <linux/buffer_head.h>
  #include <linux/mm.h>
  #include <linux/pagemap.h>
  #include <linux/writeback.h>
  #include <linux/swap.h>
  #include <linux/delay.h>
2e565bb69   Steven Whitehouse   [GFS2] Mark metad...
17
  #include <linux/bio.h>
5c676f6d3   Steven Whitehouse   [GFS2] Macros rem...
18
  #include <linux/gfs2_ondisk.h>
b3b94faa5   David Teigland   [GFS2] The core o...
19
20
  
  #include "gfs2.h"
5c676f6d3   Steven Whitehouse   [GFS2] Macros rem...
21
  #include "incore.h"
b3b94faa5   David Teigland   [GFS2] The core o...
22
23
24
25
26
27
28
29
  #include "glock.h"
  #include "glops.h"
  #include "inode.h"
  #include "log.h"
  #include "lops.h"
  #include "meta_io.h"
  #include "rgrp.h"
  #include "trans.h"
5c676f6d3   Steven Whitehouse   [GFS2] Macros rem...
30
  #include "util.h"
627c10b7e   Steven Whitehouse   GFS2: Improve tra...
31
  #include "trace_gfs2.h"
b3b94faa5   David Teigland   [GFS2] The core o...
32

4a0f9a321   Steven Whitehouse   GFS2: Optimise wr...
33
  static int gfs2_aspace_writepage(struct page *page, struct writeback_control *wbc)
b3b94faa5   David Teigland   [GFS2] The core o...
34
  {
4a0f9a321   Steven Whitehouse   GFS2: Optimise wr...
35
36
  	struct buffer_head *bh, *head;
  	int nr_underway = 0;
7637241e6   Jens Axboe   writeback: add wb...
37
  	int write_flags = REQ_META | REQ_PRIO | wbc_to_write_flags(wbc);
b3b94faa5   David Teigland   [GFS2] The core o...
38

4a0f9a321   Steven Whitehouse   GFS2: Optimise wr...
39
40
41
42
43
44
45
46
47
48
49
50
  	BUG_ON(!PageLocked(page));
  	BUG_ON(!page_has_buffers(page));
  
  	head = page_buffers(page);
  	bh = head;
  
  	do {
  		if (!buffer_mapped(bh))
  			continue;
  		/*
  		 * If it's a fully non-blocking write attempt and we cannot
  		 * lock the buffer then redirty the page.  Note that this can
e76e0ec98   Artem Bityutskiy   gfs2: nuke pdflus...
51
  		 * potentially cause a busy-wait loop from flusher thread and kswapd
4a0f9a321   Steven Whitehouse   GFS2: Optimise wr...
52
53
54
  		 * activity, but those code paths have their own higher-level
  		 * throttling.
  		 */
1b430beee   Wu Fengguang   writeback: remove...
55
  		if (wbc->sync_mode != WB_SYNC_NONE) {
4a0f9a321   Steven Whitehouse   GFS2: Optimise wr...
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  			lock_buffer(bh);
  		} else if (!trylock_buffer(bh)) {
  			redirty_page_for_writepage(wbc, page);
  			continue;
  		}
  		if (test_clear_buffer_dirty(bh)) {
  			mark_buffer_async_write(bh);
  		} else {
  			unlock_buffer(bh);
  		}
  	} while ((bh = bh->b_this_page) != head);
  
  	/*
  	 * The page and its buffers are protected by PageWriteback(), so we can
  	 * drop the bh refcounts early.
  	 */
  	BUG_ON(PageWriteback(page));
  	set_page_writeback(page);
  
  	do {
  		struct buffer_head *next = bh->b_this_page;
  		if (buffer_async_write(bh)) {
2a222ca99   Mike Christie   fs: have submit_b...
78
  			submit_bh(REQ_OP_WRITE, write_flags, bh);
4a0f9a321   Steven Whitehouse   GFS2: Optimise wr...
79
80
81
82
83
  			nr_underway++;
  		}
  		bh = next;
  	} while (bh != head);
  	unlock_page(page);
4a0f9a321   Steven Whitehouse   GFS2: Optimise wr...
84
85
  	if (nr_underway == 0)
  		end_page_writeback(page);
eaefbf968   Bob Peterson   GFS2: Eliminate u...
86
  	return 0;
b3b94faa5   David Teigland   [GFS2] The core o...
87
  }
009d85183   Steven Whitehouse   GFS2: Metadata ad...
88
  const struct address_space_operations gfs2_meta_aops = {
b3b94faa5   David Teigland   [GFS2] The core o...
89
  	.writepage = gfs2_aspace_writepage,
4340fe625   Steven Whitehouse   [GFS2] Add genera...
90
  	.releasepage = gfs2_releasepage,
b3b94faa5   David Teigland   [GFS2] The core o...
91
  };
1b2ad4121   Steven Whitehouse   GFS2: Fix address...
92
93
94
95
  const struct address_space_operations gfs2_rgrp_aops = {
  	.writepage = gfs2_aspace_writepage,
  	.releasepage = gfs2_releasepage,
  };
b3b94faa5   David Teigland   [GFS2] The core o...
96
  /**
6802e3400   Steven Whitehouse   [GFS2] Clean up t...
97
   * gfs2_getbuf - Get a buffer with a given address space
cb4c03131   Steven Whitehouse   [GFS2] Reduce num...
98
   * @gl: the glock
b3b94faa5   David Teigland   [GFS2] The core o...
99
100
101
102
103
   * @blkno: the block number (filesystem scope)
   * @create: 1 if the buffer should be created
   *
   * Returns: the buffer
   */
6802e3400   Steven Whitehouse   [GFS2] Clean up t...
104
  struct buffer_head *gfs2_getbuf(struct gfs2_glock *gl, u64 blkno, int create)
b3b94faa5   David Teigland   [GFS2] The core o...
105
  {
009d85183   Steven Whitehouse   GFS2: Metadata ad...
106
  	struct address_space *mapping = gfs2_glock2aspace(gl);
15562c439   Bob Peterson   GFS2: Move glock ...
107
  	struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
b3b94faa5   David Teigland   [GFS2] The core o...
108
109
110
111
112
  	struct page *page;
  	struct buffer_head *bh;
  	unsigned int shift;
  	unsigned long index;
  	unsigned int bufnum;
70d4ee94b   Steven Whitehouse   GFS2: Use only a ...
113
114
  	if (mapping == NULL)
  		mapping = &sdp->sd_aspace;
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
115
  	shift = PAGE_SHIFT - sdp->sd_sb.sb_bsize_shift;
b3b94faa5   David Teigland   [GFS2] The core o...
116
117
118
119
120
  	index = blkno >> shift;             /* convert block to page */
  	bufnum = blkno - (index << shift);  /* block buf index within page */
  
  	if (create) {
  		for (;;) {
cb4c03131   Steven Whitehouse   [GFS2] Reduce num...
121
  			page = grab_cache_page(mapping, index);
b3b94faa5   David Teigland   [GFS2] The core o...
122
123
124
125
126
  			if (page)
  				break;
  			yield();
  		}
  	} else {
2457aec63   Mel Gorman   mm: non-atomicall...
127
128
  		page = find_get_page_flags(mapping, index,
  						FGP_LOCK|FGP_ACCESSED);
b3b94faa5   David Teigland   [GFS2] The core o...
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  		if (!page)
  			return NULL;
  	}
  
  	if (!page_has_buffers(page))
  		create_empty_buffers(page, sdp->sd_sb.sb_bsize, 0);
  
  	/* Locate header for our buffer within our page */
  	for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page)
  		/* Do nothing */;
  	get_bh(bh);
  
  	if (!buffer_mapped(bh))
  		map_bh(bh, sdp->sd_vfs, blkno);
  
  	unlock_page(page);
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
145
  	put_page(page);
b3b94faa5   David Teigland   [GFS2] The core o...
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  
  	return bh;
  }
  
  static void meta_prep_new(struct buffer_head *bh)
  {
  	struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
  
  	lock_buffer(bh);
  	clear_buffer_dirty(bh);
  	set_buffer_uptodate(bh);
  	unlock_buffer(bh);
  
  	mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
  }
  
  /**
   * gfs2_meta_new - Get a block
   * @gl: The glock associated with this block
   * @blkno: The block number
   *
   * Returns: The buffer
   */
cd915493f   Steven Whitehouse   [GFS2] Change all...
169
  struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, u64 blkno)
b3b94faa5   David Teigland   [GFS2] The core o...
170
171
  {
  	struct buffer_head *bh;
6802e3400   Steven Whitehouse   [GFS2] Clean up t...
172
  	bh = gfs2_getbuf(gl, blkno, CREATE);
b3b94faa5   David Teigland   [GFS2] The core o...
173
174
175
  	meta_prep_new(bh);
  	return bh;
  }
39b0555f7   Andreas Gruenbacher   gfs2: Extended at...
176
  static void gfs2_meta_read_endio(struct bio *bio)
c8d577038   Andreas Gruenbacher   gfs2: Extended at...
177
  {
39b0555f7   Andreas Gruenbacher   gfs2: Extended at...
178
  	struct bio_vec *bvec;
6dc4f100c   Ming Lei   block: allow bio_...
179
  	struct bvec_iter_all iter_all;
39b0555f7   Andreas Gruenbacher   gfs2: Extended at...
180

2b070cfe5   Christoph Hellwig   block: remove the...
181
  	bio_for_each_segment_all(bvec, bio, iter_all) {
39b0555f7   Andreas Gruenbacher   gfs2: Extended at...
182
183
184
185
186
187
188
189
190
  		struct page *page = bvec->bv_page;
  		struct buffer_head *bh = page_buffers(page);
  		unsigned int len = bvec->bv_len;
  
  		while (bh_offset(bh) < bvec->bv_offset)
  			bh = bh->b_this_page;
  		do {
  			struct buffer_head *next = bh->b_this_page;
  			len -= bh->b_size;
4e4cbee93   Christoph Hellwig   block: switch bio...
191
  			bh->b_end_io(bh, !bio->bi_status);
39b0555f7   Andreas Gruenbacher   gfs2: Extended at...
192
193
194
195
196
  			bh = next;
  		} while (bh && len);
  	}
  	bio_put(bio);
  }
c8d577038   Andreas Gruenbacher   gfs2: Extended at...
197

39b0555f7   Andreas Gruenbacher   gfs2: Extended at...
198
199
200
201
  /*
   * Submit several consecutive buffer head I/O requests as a single bio I/O
   * request.  (See submit_bh_wbc.)
   */
e1b1afa6f   Mike Christie   gfs2: use bio op ...
202
203
  static void gfs2_submit_bhs(int op, int op_flags, struct buffer_head *bhs[],
  			    int num)
39b0555f7   Andreas Gruenbacher   gfs2: Extended at...
204
  {
23e5671a7   Andreas Gruenbacher   gfs2: Fix extende...
205
206
207
208
209
210
  	while (num > 0) {
  		struct buffer_head *bh = *bhs;
  		struct bio *bio;
  
  		bio = bio_alloc(GFP_NOIO, num);
  		bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
74d46992e   Christoph Hellwig   block: replace bi...
211
  		bio_set_dev(bio, bh->b_bdev);
23e5671a7   Andreas Gruenbacher   gfs2: Fix extende...
212
213
214
215
216
217
218
219
220
221
222
223
  		while (num > 0) {
  			bh = *bhs;
  			if (!bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh))) {
  				BUG_ON(bio->bi_iter.bi_size == 0);
  				break;
  			}
  			bhs++;
  			num--;
  		}
  		bio->bi_end_io = gfs2_meta_read_endio;
  		bio_set_op_attrs(bio, op, op_flags);
  		submit_bio(bio);
c8d577038   Andreas Gruenbacher   gfs2: Extended at...
224
  	}
c8d577038   Andreas Gruenbacher   gfs2: Extended at...
225
  }
b3b94faa5   David Teigland   [GFS2] The core o...
226
227
228
229
  /**
   * gfs2_meta_read - Read a block from disk
   * @gl: The glock covering the block
   * @blkno: The block number
7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
230
   * @flags: flags
b3b94faa5   David Teigland   [GFS2] The core o...
231
232
233
234
   * @bhp: the place where the buffer is returned (NULL on failure)
   *
   * Returns: errno
   */
cd915493f   Steven Whitehouse   [GFS2] Change all...
235
  int gfs2_meta_read(struct gfs2_glock *gl, u64 blkno, int flags,
c8d577038   Andreas Gruenbacher   gfs2: Extended at...
236
  		   int rahead, struct buffer_head **bhp)
b3b94faa5   David Teigland   [GFS2] The core o...
237
  {
15562c439   Bob Peterson   GFS2: Move glock ...
238
  	struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
39b0555f7   Andreas Gruenbacher   gfs2: Extended at...
239
240
  	struct buffer_head *bh, *bhs[2];
  	int num = 0;
c969f58ca   Steven Whitehouse   GFS2: Update the ...
241

601ef0d52   Bob Peterson   gfs2: Force withd...
242
  	if (unlikely(gfs2_withdrawn(sdp)) &&
ac9155842   Bob Peterson   gfs2: fix withdra...
243
  	    (!sdp->sd_jdesc || gl != sdp->sd_jinode_gl)) {
44b8db138   Masatake YAMATO   GFS2: Fixing doub...
244
  		*bhp = NULL;
c969f58ca   Steven Whitehouse   GFS2: Update the ...
245
  		return -EIO;
44b8db138   Masatake YAMATO   GFS2: Fixing doub...
246
  	}
c969f58ca   Steven Whitehouse   GFS2: Update the ...
247
248
249
250
251
252
  
  	*bhp = bh = gfs2_getbuf(gl, blkno, CREATE);
  
  	lock_buffer(bh);
  	if (buffer_uptodate(bh)) {
  		unlock_buffer(bh);
39b0555f7   Andreas Gruenbacher   gfs2: Extended at...
253
254
255
256
257
  		flags &= ~DIO_WAIT;
  	} else {
  		bh->b_end_io = end_buffer_read_sync;
  		get_bh(bh);
  		bhs[num++] = bh;
c969f58ca   Steven Whitehouse   GFS2: Update the ...
258
  	}
39b0555f7   Andreas Gruenbacher   gfs2: Extended at...
259
260
261
262
263
264
265
266
267
268
269
270
271
  
  	if (rahead) {
  		bh = gfs2_getbuf(gl, blkno + 1, CREATE);
  
  		lock_buffer(bh);
  		if (buffer_uptodate(bh)) {
  			unlock_buffer(bh);
  			brelse(bh);
  		} else {
  			bh->b_end_io = end_buffer_read_sync;
  			bhs[num++] = bh;
  		}
  	}
70fd76140   Christoph Hellwig   block,fs: use REQ...
272
  	gfs2_submit_bhs(REQ_OP_READ, REQ_META | REQ_PRIO, bhs, num);
c969f58ca   Steven Whitehouse   GFS2: Update the ...
273
274
  	if (!(flags & DIO_WAIT))
  		return 0;
39b0555f7   Andreas Gruenbacher   gfs2: Extended at...
275
  	bh = *bhp;
c969f58ca   Steven Whitehouse   GFS2: Update the ...
276
277
278
  	wait_on_buffer(bh);
  	if (unlikely(!buffer_uptodate(bh))) {
  		struct gfs2_trans *tr = current->journal_info;
9862ca056   Bob Peterson   GFS2: Switch tr_t...
279
  		if (tr && test_bit(TR_TOUCHED, &tr->tr_flags))
9e1a9ecd1   Andreas Gruenbacher   gfs2: Don't withd...
280
  			gfs2_io_error_bh_wd(sdp, bh);
c969f58ca   Steven Whitehouse   GFS2: Update the ...
281
  		brelse(bh);
44b8db138   Masatake YAMATO   GFS2: Fixing doub...
282
  		*bhp = NULL;
c969f58ca   Steven Whitehouse   GFS2: Update the ...
283
  		return -EIO;
7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
284
  	}
b3b94faa5   David Teigland   [GFS2] The core o...
285

7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
286
  	return 0;
b3b94faa5   David Teigland   [GFS2] The core o...
287
288
289
  }
  
  /**
7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
290
   * gfs2_meta_wait - Reread a block from disk
b3b94faa5   David Teigland   [GFS2] The core o...
291
   * @sdp: the filesystem
7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
292
   * @bh: The block to wait for
b3b94faa5   David Teigland   [GFS2] The core o...
293
294
295
   *
   * Returns: errno
   */
7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
296
  int gfs2_meta_wait(struct gfs2_sbd *sdp, struct buffer_head *bh)
b3b94faa5   David Teigland   [GFS2] The core o...
297
  {
eb43e660c   Bob Peterson   gfs2: Introduce f...
298
  	if (unlikely(gfs2_withdrawn(sdp)))
b3b94faa5   David Teigland   [GFS2] The core o...
299
  		return -EIO;
7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
300
  	wait_on_buffer(bh);
b3b94faa5   David Teigland   [GFS2] The core o...
301

7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
302
303
  	if (!buffer_uptodate(bh)) {
  		struct gfs2_trans *tr = current->journal_info;
9862ca056   Bob Peterson   GFS2: Switch tr_t...
304
  		if (tr && test_bit(TR_TOUCHED, &tr->tr_flags))
9e1a9ecd1   Andreas Gruenbacher   gfs2: Don't withd...
305
  			gfs2_io_error_bh_wd(sdp, bh);
7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
306
  		return -EIO;
b3b94faa5   David Teigland   [GFS2] The core o...
307
  	}
eb43e660c   Bob Peterson   gfs2: Introduce f...
308
  	if (unlikely(gfs2_withdrawn(sdp)))
7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
309
  		return -EIO;
b3b94faa5   David Teigland   [GFS2] The core o...
310
311
312
  
  	return 0;
  }
68cd4ce2c   Bob Peterson   GFS2: Refactor gf...
313
  void gfs2_remove_from_journal(struct buffer_head *bh, int meta)
16615be18   Steven Whitehouse   [GFS2] Clean up j...
314
  {
009d85183   Steven Whitehouse   GFS2: Metadata ad...
315
316
  	struct address_space *mapping = bh->b_page->mapping;
  	struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
16615be18   Steven Whitehouse   [GFS2] Clean up j...
317
  	struct gfs2_bufdata *bd = bh->b_private;
68cd4ce2c   Bob Peterson   GFS2: Refactor gf...
318
  	struct gfs2_trans *tr = current->journal_info;
502be2a32   Bob Peterson   GFS2: Fix slab me...
319
  	int was_pinned = 0;
009d85183   Steven Whitehouse   GFS2: Metadata ad...
320

16615be18   Steven Whitehouse   [GFS2] Clean up j...
321
  	if (test_clear_buffer_pinned(bh)) {
627c10b7e   Steven Whitehouse   GFS2: Improve tra...
322
  		trace_gfs2_pin(bd, 0);
5e687eac1   Benjamin Marzinski   GFS2: Various gfs...
323
  		atomic_dec(&sdp->sd_log_pinned);
c0752aa7e   Bob Peterson   GFS2: eliminate l...
324
  		list_del_init(&bd->bd_list);
68cd4ce2c   Bob Peterson   GFS2: Refactor gf...
325
  		if (meta == REMOVE_META)
16615be18   Steven Whitehouse   [GFS2] Clean up j...
326
  			tr->tr_num_buf_rm++;
022ef4fee   Steven Whitehouse   GFS2: Move log bu...
327
  		else
16615be18   Steven Whitehouse   [GFS2] Clean up j...
328
  			tr->tr_num_databuf_rm++;
9862ca056   Bob Peterson   GFS2: Switch tr_t...
329
  		set_bit(TR_TOUCHED, &tr->tr_flags);
502be2a32   Bob Peterson   GFS2: Fix slab me...
330
  		was_pinned = 1;
16615be18   Steven Whitehouse   [GFS2] Clean up j...
331
332
333
  		brelse(bh);
  	}
  	if (bd) {
16ca9412d   Benjamin Marzinski   GFS2: replace gfs...
334
  		if (bd->bd_tr) {
16615be18   Steven Whitehouse   [GFS2] Clean up j...
335
  			gfs2_trans_add_revoke(sdp, bd);
502be2a32   Bob Peterson   GFS2: Fix slab me...
336
337
338
  		} else if (was_pinned) {
  			bh->b_private = NULL;
  			kmem_cache_free(gfs2_bufdata_cachep, bd);
68942870c   Bob Peterson   gfs2: Wipe jdata ...
339
340
341
  		} else if (!list_empty(&bd->bd_ail_st_list) &&
  					!list_empty(&bd->bd_ail_gl_list)) {
  			gfs2_remove_from_ail(bd);
16615be18   Steven Whitehouse   [GFS2] Clean up j...
342
343
344
345
346
  		}
  	}
  	clear_buffer_dirty(bh);
  	clear_buffer_uptodate(bh);
  }
b3b94faa5   David Teigland   [GFS2] The core o...
347
  /**
68942870c   Bob Peterson   gfs2: Wipe jdata ...
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
   * gfs2_ail1_wipe - remove deleted/freed buffers from the ail1 list
   * @sdp: superblock
   * @bstart: starting block address of buffers to remove
   * @blen: length of buffers to be removed
   *
   * This function is called from gfs2_journal wipe, whose job is to remove
   * buffers, corresponding to deleted blocks, from the journal. If we find any
   * bufdata elements on the system ail1 list, they haven't been written to
   * the journal yet. So we remove them.
   */
  static void gfs2_ail1_wipe(struct gfs2_sbd *sdp, u64 bstart, u32 blen)
  {
  	struct gfs2_trans *tr, *s;
  	struct gfs2_bufdata *bd, *bs;
  	struct buffer_head *bh;
  	u64 end = bstart + blen;
  
  	gfs2_log_lock(sdp);
  	spin_lock(&sdp->sd_ail_lock);
  	list_for_each_entry_safe(tr, s, &sdp->sd_ail1_list, tr_list) {
  		list_for_each_entry_safe(bd, bs, &tr->tr_ail1_list,
  					 bd_ail_st_list) {
  			bh = bd->bd_bh;
  			if (bh->b_blocknr < bstart || bh->b_blocknr >= end)
  				continue;
  
  			gfs2_remove_from_journal(bh, REMOVE_JDATA);
  		}
  	}
  	spin_unlock(&sdp->sd_ail_lock);
  	gfs2_log_unlock(sdp);
  }
  
  static struct buffer_head *gfs2_getjdatabuf(struct gfs2_inode *ip, u64 blkno)
  {
  	struct address_space *mapping = ip->i_inode.i_mapping;
  	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  	struct page *page;
  	struct buffer_head *bh;
  	unsigned int shift = PAGE_SHIFT - sdp->sd_sb.sb_bsize_shift;
  	unsigned long index = blkno >> shift; /* convert block to page */
  	unsigned int bufnum = blkno - (index << shift);
  
  	page = find_get_page_flags(mapping, index, FGP_LOCK|FGP_ACCESSED);
  	if (!page)
  		return NULL;
  	if (!page_has_buffers(page)) {
  		unlock_page(page);
  		put_page(page);
  		return NULL;
  	}
  	/* Locate header for our buffer within our page */
  	for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page)
  		/* Do nothing */;
  	get_bh(bh);
  	unlock_page(page);
  	put_page(page);
  	return bh;
  }
  
  /**
   * gfs2_journal_wipe - make inode's buffers so they aren't dirty/pinned anymore
b3b94faa5   David Teigland   [GFS2] The core o...
410
411
412
413
414
   * @ip: the inode who owns the buffers
   * @bstart: the first buffer in the run
   * @blen: the number of buffers in the run
   *
   */
68942870c   Bob Peterson   gfs2: Wipe jdata ...
415
  void gfs2_journal_wipe(struct gfs2_inode *ip, u64 bstart, u32 blen)
b3b94faa5   David Teigland   [GFS2] The core o...
416
  {
feaa7bba0   Steven Whitehouse   [GFS2] Fix unlink...
417
  	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
b3b94faa5   David Teigland   [GFS2] The core o...
418
  	struct buffer_head *bh;
68942870c   Bob Peterson   gfs2: Wipe jdata ...
419
  	int ty;
b3b94faa5   David Teigland   [GFS2] The core o...
420

68942870c   Bob Peterson   gfs2: Wipe jdata ...
421
  	gfs2_ail1_wipe(sdp, bstart, blen);
b3b94faa5   David Teigland   [GFS2] The core o...
422
  	while (blen) {
68942870c   Bob Peterson   gfs2: Wipe jdata ...
423
  		ty = REMOVE_META;
6802e3400   Steven Whitehouse   [GFS2] Clean up t...
424
  		bh = gfs2_getbuf(ip->i_gl, bstart, NO_CREATE);
68942870c   Bob Peterson   gfs2: Wipe jdata ...
425
426
427
428
  		if (!bh && gfs2_is_jdata(ip)) {
  			bh = gfs2_getjdatabuf(ip, bstart);
  			ty = REMOVE_JDATA;
  		}
b3b94faa5   David Teigland   [GFS2] The core o...
429
  		if (bh) {
1ad38c437   Steven Whitehouse   [GFS2] Clean up g...
430
431
  			lock_buffer(bh);
  			gfs2_log_lock(sdp);
68942870c   Bob Peterson   gfs2: Wipe jdata ...
432
433
434
  			spin_lock(&sdp->sd_ail_lock);
  			gfs2_remove_from_journal(bh, ty);
  			spin_unlock(&sdp->sd_ail_lock);
1ad38c437   Steven Whitehouse   [GFS2] Clean up g...
435
  			gfs2_log_unlock(sdp);
b3b94faa5   David Teigland   [GFS2] The core o...
436
  			unlock_buffer(bh);
b3b94faa5   David Teigland   [GFS2] The core o...
437
438
439
440
441
442
443
444
445
  			brelse(bh);
  		}
  
  		bstart++;
  		blen--;
  	}
  }
  
  /**
b3b94faa5   David Teigland   [GFS2] The core o...
446
447
448
449
   * gfs2_meta_indirect_buffer - Get a metadata buffer
   * @ip: The GFS2 inode
   * @height: The level of this buf in the metadata (indir addr) tree (if any)
   * @num: The block number (device relative) of the buffer
b3b94faa5   David Teigland   [GFS2] The core o...
450
451
   * @bhp: the buffer is returned here
   *
b3b94faa5   David Teigland   [GFS2] The core o...
452
453
   * Returns: errno
   */
cd915493f   Steven Whitehouse   [GFS2] Change all...
454
  int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, u64 num,
f2f9c8124   Bob Peterson   GFS2: Eliminate u...
455
  			      struct buffer_head **bhp)
b3b94faa5   David Teigland   [GFS2] The core o...
456
  {
7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
457
458
  	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  	struct gfs2_glock *gl = ip->i_gl;
f91a0d3e2   Steven Whitehouse   [GFS2] Remove use...
459
460
  	struct buffer_head *bh;
  	int ret = 0;
f2f9c8124   Bob Peterson   GFS2: Eliminate u...
461
  	u32 mtype = height ? GFS2_METATYPE_IN : GFS2_METATYPE_DI;
c8d577038   Andreas Gruenbacher   gfs2: Extended at...
462
463
464
465
  	int rahead = 0;
  
  	if (num == ip->i_no_addr)
  		rahead = ip->i_rahead;
b3b94faa5   David Teigland   [GFS2] The core o...
466

c8d577038   Andreas Gruenbacher   gfs2: Extended at...
467
  	ret = gfs2_meta_read(gl, num, DIO_WAIT, rahead, &bh);
f2f9c8124   Bob Peterson   GFS2: Eliminate u...
468
469
470
  	if (ret == 0 && gfs2_metatype_check(sdp, bh, mtype)) {
  		brelse(bh);
  		ret = -EIO;
61eaadcd5   Bob Peterson   GFS2: Prevent dou...
471
472
  	} else {
  		*bhp = bh;
b3b94faa5   David Teigland   [GFS2] The core o...
473
  	}
f91a0d3e2   Steven Whitehouse   [GFS2] Remove use...
474
  	return ret;
b3b94faa5   David Teigland   [GFS2] The core o...
475
476
477
478
479
480
481
482
  }
  
  /**
   * gfs2_meta_ra - start readahead on an extent of a file
   * @gl: the glock the blocks belong to
   * @dblock: the starting disk block
   * @extlen: the number of blocks in the extent
   *
7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
483
   * returns: the first buffer in the extent
b3b94faa5   David Teigland   [GFS2] The core o...
484
   */
7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
485
  struct buffer_head *gfs2_meta_ra(struct gfs2_glock *gl, u64 dblock, u32 extlen)
b3b94faa5   David Teigland   [GFS2] The core o...
486
  {
15562c439   Bob Peterson   GFS2: Move glock ...
487
  	struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
b3b94faa5   David Teigland   [GFS2] The core o...
488
  	struct buffer_head *first_bh, *bh;
cd915493f   Steven Whitehouse   [GFS2] Change all...
489
  	u32 max_ra = gfs2_tune_get(sdp, gt_max_readahead) >>
568f4c965   Steven Whitehouse   [GFS2] 80 Column ...
490
  			  sdp->sd_sb.sb_bsize_shift;
b3b94faa5   David Teigland   [GFS2] The core o...
491

7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
492
493
494
495
  	BUG_ON(!extlen);
  
  	if (max_ra < 1)
  		max_ra = 1;
b3b94faa5   David Teigland   [GFS2] The core o...
496
497
  	if (extlen > max_ra)
  		extlen = max_ra;
6802e3400   Steven Whitehouse   [GFS2] Clean up t...
498
  	first_bh = gfs2_getbuf(gl, dblock, CREATE);
b3b94faa5   David Teigland   [GFS2] The core o...
499
500
501
  
  	if (buffer_uptodate(first_bh))
  		goto out;
7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
502
  	if (!buffer_locked(first_bh))
e477b24b5   Coly Li   gfs2: add flag RE...
503
  		ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &first_bh);
b3b94faa5   David Teigland   [GFS2] The core o...
504
505
506
507
508
  
  	dblock++;
  	extlen--;
  
  	while (extlen) {
6802e3400   Steven Whitehouse   [GFS2] Clean up t...
509
  		bh = gfs2_getbuf(gl, dblock, CREATE);
b3b94faa5   David Teigland   [GFS2] The core o...
510

7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
511
  		if (!buffer_uptodate(bh) && !buffer_locked(bh))
e477b24b5   Coly Li   gfs2: add flag RE...
512
513
514
  			ll_rw_block(REQ_OP_READ,
  				    REQ_RAHEAD | REQ_META | REQ_PRIO,
  				    1, &bh);
7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
515
  		brelse(bh);
b3b94faa5   David Teigland   [GFS2] The core o...
516
517
  		dblock++;
  		extlen--;
7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
518
519
  		if (!buffer_locked(first_bh) && buffer_uptodate(first_bh))
  			goto out;
b3b94faa5   David Teigland   [GFS2] The core o...
520
  	}
7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
521
  	wait_on_buffer(first_bh);
a91ea69ff   Steven Whitehouse   [GFS2] Align all ...
522
  out:
7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
523
  	return first_bh;
b3b94faa5   David Teigland   [GFS2] The core o...
524
  }