Blame view

fs/xfs/xfs_buf_item.c 26.7 KB
0b61f8a40   Dave Chinner   xfs: convert to S...
1
  // SPDX-License-Identifier: GPL-2.0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
  /*
7b7187698   Nathan Scott   [XFS] Update lice...
3
4
   * Copyright (c) 2000-2005 Silicon Graphics, Inc.
   * All Rights Reserved.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
5
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
6
  #include "xfs.h"
a844f4510   Nathan Scott   [XFS] Remove xfs_...
7
  #include "xfs_fs.h"
5467b34bd   Darrick J. Wong   xfs: move xfs_ino...
8
  #include "xfs_shared.h"
4fb6e8ade   Christoph Hellwig   xfs: merge xfs_ag...
9
  #include "xfs_format.h"
239880ef6   Dave Chinner   xfs: decouple log...
10
11
  #include "xfs_log_format.h"
  #include "xfs_trans_resv.h"
a844f4510   Nathan Scott   [XFS] Remove xfs_...
12
  #include "xfs_bit.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
  #include "xfs_mount.h"
239880ef6   Dave Chinner   xfs: decouple log...
14
  #include "xfs_trans.h"
3536b61e7   Dave Chinner   xfs: unwind log i...
15
  #include "xfs_trans_priv.h"
a844f4510   Nathan Scott   [XFS] Remove xfs_...
16
  #include "xfs_buf_item.h"
aac855ab1   Dave Chinner   xfs: make inode I...
17
18
  #include "xfs_inode.h"
  #include "xfs_inode_item.h"
6f5de1808   Dave Chinner   xfs: use direct c...
19
20
21
  #include "xfs_quota.h"
  #include "xfs_dquot_item.h"
  #include "xfs_dquot.h"
0b1b213fc   Christoph Hellwig   xfs: event tracin...
22
  #include "xfs_trace.h"
239880ef6   Dave Chinner   xfs: decouple log...
23
  #include "xfs_log.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24
25
26
  
  
  kmem_zone_t	*xfs_buf_item_zone;
7bfa31d8e   Christoph Hellwig   xfs: give xfs_ite...
27
28
29
30
  static inline struct xfs_buf_log_item *BUF_ITEM(struct xfs_log_item *lip)
  {
  	return container_of(lip, struct xfs_buf_log_item, bli_item);
  }
8a6453a89   Darrick J. Wong   xfs: check log io...
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  /* Is this log iovec plausibly large enough to contain the buffer log format? */
  bool
  xfs_buf_log_check_iovec(
  	struct xfs_log_iovec		*iovec)
  {
  	struct xfs_buf_log_format	*blfp = iovec->i_addr;
  	char				*bmp_end;
  	char				*item_end;
  
  	if (offsetof(struct xfs_buf_log_format, blf_data_map) > iovec->i_len)
  		return false;
  
  	item_end = (char *)iovec->i_addr + iovec->i_len;
  	bmp_end = (char *)&blfp->blf_data_map[blfp->blf_map_size];
  	return bmp_end <= item_end;
  }
166d13688   Dave Chinner   xfs: return log i...
47
48
49
50
51
52
53
  static inline int
  xfs_buf_log_format_size(
  	struct xfs_buf_log_format *blfp)
  {
  	return offsetof(struct xfs_buf_log_format, blf_data_map) +
  			(blfp->blf_map_size * sizeof(blfp->blf_data_map[0]));
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
55
56
57
58
59
60
61
62
63
  /*
   * This returns the number of log iovecs needed to log the
   * given buf log item.
   *
   * It calculates this as 1 iovec for the buf log format structure
   * and 1 for each stretch of non-contiguous chunks to be logged.
   * Contiguous chunks are logged in a single iovec.
   *
   * If the XFS_BLI_STALE flag has been set, then log nothing.
   */
166d13688   Dave Chinner   xfs: return log i...
64
  STATIC void
372cc85ec   Dave Chinner   xfs: support disc...
65
  xfs_buf_item_size_segment(
70a206553   Carlos Maiolino   Get rid of xfs_bu...
66
67
68
69
  	struct xfs_buf_log_item		*bip,
  	struct xfs_buf_log_format	*blfp,
  	int				*nvecs,
  	int				*nbytes)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
70
  {
70a206553   Carlos Maiolino   Get rid of xfs_bu...
71
72
73
  	struct xfs_buf			*bp = bip->bli_buf;
  	int				next_bit;
  	int				last_bit;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
74

372cc85ec   Dave Chinner   xfs: support disc...
75
76
  	last_bit = xfs_next_bit(blfp->blf_data_map, blfp->blf_map_size, 0);
  	if (last_bit == -1)
166d13688   Dave Chinner   xfs: return log i...
77
  		return;
372cc85ec   Dave Chinner   xfs: support disc...
78
79
80
81
82
  
  	/*
  	 * initial count for a dirty buffer is 2 vectors - the format structure
  	 * and the first dirty region.
  	 */
166d13688   Dave Chinner   xfs: return log i...
83
84
  	*nvecs += 2;
  	*nbytes += xfs_buf_log_format_size(blfp) + XFS_BLF_CHUNK;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
85

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
87
88
89
90
91
92
  	while (last_bit != -1) {
  		/*
  		 * This takes the bit number to start looking from and
  		 * returns the next set bit from there.  It returns -1
  		 * if there are no more bits set or the start bit is
  		 * beyond the end of the bitmap.
  		 */
372cc85ec   Dave Chinner   xfs: support disc...
93
94
  		next_bit = xfs_next_bit(blfp->blf_data_map, blfp->blf_map_size,
  					last_bit + 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
95
96
97
98
99
100
  		/*
  		 * If we run out of bits, leave the loop,
  		 * else if we find a new set of bits bump the number of vecs,
  		 * else keep scanning the current set of bits.
  		 */
  		if (next_bit == -1) {
372cc85ec   Dave Chinner   xfs: support disc...
101
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
102
103
  		} else if (next_bit != last_bit + 1) {
  			last_bit = next_bit;
166d13688   Dave Chinner   xfs: return log i...
104
  			(*nvecs)++;
c11554104   Dave Chinner   xfs: Clean up XFS...
105
106
107
  		} else if (xfs_buf_offset(bp, next_bit * XFS_BLF_CHUNK) !=
  			   (xfs_buf_offset(bp, last_bit * XFS_BLF_CHUNK) +
  			    XFS_BLF_CHUNK)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
108
  			last_bit = next_bit;
166d13688   Dave Chinner   xfs: return log i...
109
  			(*nvecs)++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
110
111
112
  		} else {
  			last_bit++;
  		}
166d13688   Dave Chinner   xfs: return log i...
113
  		*nbytes += XFS_BLF_CHUNK;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
115
116
117
  }
  
  /*
372cc85ec   Dave Chinner   xfs: support disc...
118
119
120
121
122
123
   * This returns the number of log iovecs needed to log the given buf log item.
   *
   * It calculates this as 1 iovec for the buf log format structure and 1 for each
   * stretch of non-contiguous chunks to be logged.  Contiguous chunks are logged
   * in a single iovec.
   *
b63da6c8d   Randy Dunlap   xfs: delete dupli...
124
   * Discontiguous buffers need a format structure per region that is being
372cc85ec   Dave Chinner   xfs: support disc...
125
126
127
128
129
130
131
132
   * logged. This makes the changes in the buffer appear to log recovery as though
   * they came from separate buffers, just like would occur if multiple buffers
   * were used instead of a single discontiguous buffer. This enables
   * discontiguous buffers to be in-memory constructs, completely transparent to
   * what ends up on disk.
   *
   * If the XFS_BLI_STALE flag has been set, then log nothing but the buf log
   * format structures.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
133
   */
166d13688   Dave Chinner   xfs: return log i...
134
  STATIC void
372cc85ec   Dave Chinner   xfs: support disc...
135
  xfs_buf_item_size(
166d13688   Dave Chinner   xfs: return log i...
136
137
138
  	struct xfs_log_item	*lip,
  	int			*nvecs,
  	int			*nbytes)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
  {
7bfa31d8e   Christoph Hellwig   xfs: give xfs_ite...
140
  	struct xfs_buf_log_item	*bip = BUF_ITEM(lip);
372cc85ec   Dave Chinner   xfs: support disc...
141
142
143
144
145
146
147
148
149
150
  	int			i;
  
  	ASSERT(atomic_read(&bip->bli_refcount) > 0);
  	if (bip->bli_flags & XFS_BLI_STALE) {
  		/*
  		 * The buffer is stale, so all we need to log
  		 * is the buf log format structure with the
  		 * cancel flag in it.
  		 */
  		trace_xfs_buf_item_size_stale(bip);
b94381737   Mark Tinguely   xfs: rename bli_f...
151
  		ASSERT(bip->__bli_format.blf_flags & XFS_BLF_CANCEL);
166d13688   Dave Chinner   xfs: return log i...
152
153
154
155
156
  		*nvecs += bip->bli_format_count;
  		for (i = 0; i < bip->bli_format_count; i++) {
  			*nbytes += xfs_buf_log_format_size(&bip->bli_formats[i]);
  		}
  		return;
372cc85ec   Dave Chinner   xfs: support disc...
157
158
159
  	}
  
  	ASSERT(bip->bli_flags & XFS_BLI_LOGGED);
5f6bed76c   Dave Chinner   xfs: Introduce an...
160
161
162
163
164
165
166
  	if (bip->bli_flags & XFS_BLI_ORDERED) {
  		/*
  		 * The buffer has been logged just to order it.
  		 * It is not being included in the transaction
  		 * commit, so no vectors are used at all.
  		 */
  		trace_xfs_buf_item_size_ordered(bip);
166d13688   Dave Chinner   xfs: return log i...
167
168
  		*nvecs = XFS_LOG_VEC_ORDERED;
  		return;
5f6bed76c   Dave Chinner   xfs: Introduce an...
169
  	}
372cc85ec   Dave Chinner   xfs: support disc...
170
171
172
173
174
175
176
177
178
  	/*
  	 * the vector count is based on the number of buffer vectors we have
  	 * dirty bits in. This will only be greater than one when we have a
  	 * compound buffer with more than one segment dirty. Hence for compound
  	 * buffers we need to track which segment the dirty bits correspond to,
  	 * and when we move from one segment to the next increment the vector
  	 * count for the extra buf log format structure that will need to be
  	 * written.
  	 */
372cc85ec   Dave Chinner   xfs: support disc...
179
  	for (i = 0; i < bip->bli_format_count; i++) {
166d13688   Dave Chinner   xfs: return log i...
180
181
  		xfs_buf_item_size_segment(bip, &bip->bli_formats[i],
  					  nvecs, nbytes);
372cc85ec   Dave Chinner   xfs: support disc...
182
  	}
372cc85ec   Dave Chinner   xfs: support disc...
183
  	trace_xfs_buf_item_size(bip);
372cc85ec   Dave Chinner   xfs: support disc...
184
  }
1234351cb   Christoph Hellwig   xfs: introduce xl...
185
  static inline void
7aeb72224   Christoph Hellwig   xfs: refactor xfs...
186
  xfs_buf_item_copy_iovec(
bde7cff67   Christoph Hellwig   xfs: format log i...
187
  	struct xfs_log_vec	*lv,
1234351cb   Christoph Hellwig   xfs: introduce xl...
188
  	struct xfs_log_iovec	**vecp,
7aeb72224   Christoph Hellwig   xfs: refactor xfs...
189
190
191
192
193
194
  	struct xfs_buf		*bp,
  	uint			offset,
  	int			first_bit,
  	uint			nbits)
  {
  	offset += first_bit * XFS_BLF_CHUNK;
bde7cff67   Christoph Hellwig   xfs: format log i...
195
  	xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_BCHUNK,
1234351cb   Christoph Hellwig   xfs: introduce xl...
196
197
  			xfs_buf_offset(bp, offset),
  			nbits * XFS_BLF_CHUNK);
7aeb72224   Christoph Hellwig   xfs: refactor xfs...
198
199
200
201
202
203
204
205
206
207
208
209
210
  }
  
  static inline bool
  xfs_buf_item_straddle(
  	struct xfs_buf		*bp,
  	uint			offset,
  	int			next_bit,
  	int			last_bit)
  {
  	return xfs_buf_offset(bp, offset + (next_bit << XFS_BLF_SHIFT)) !=
  		(xfs_buf_offset(bp, offset + (last_bit << XFS_BLF_SHIFT)) +
  		 XFS_BLF_CHUNK);
  }
1234351cb   Christoph Hellwig   xfs: introduce xl...
211
  static void
372cc85ec   Dave Chinner   xfs: support disc...
212
213
  xfs_buf_item_format_segment(
  	struct xfs_buf_log_item	*bip,
bde7cff67   Christoph Hellwig   xfs: format log i...
214
  	struct xfs_log_vec	*lv,
1234351cb   Christoph Hellwig   xfs: introduce xl...
215
  	struct xfs_log_iovec	**vecp,
372cc85ec   Dave Chinner   xfs: support disc...
216
217
218
  	uint			offset,
  	struct xfs_buf_log_format *blfp)
  {
70a206553   Carlos Maiolino   Get rid of xfs_bu...
219
220
221
222
223
224
  	struct xfs_buf		*bp = bip->bli_buf;
  	uint			base_size;
  	int			first_bit;
  	int			last_bit;
  	int			next_bit;
  	uint			nbits;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
225

372cc85ec   Dave Chinner   xfs: support disc...
226
  	/* copy the flags across from the base format item */
b94381737   Mark Tinguely   xfs: rename bli_f...
227
  	blfp->blf_flags = bip->__bli_format.blf_flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
228
229
  
  	/*
77c1a08fc   Dave Chinner   xfs: struct xfs_b...
230
231
232
  	 * Base size is the actual size of the ondisk structure - it reflects
  	 * the actual size of the dirty bitmap rather than the size of the in
  	 * memory structure.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
233
  	 */
166d13688   Dave Chinner   xfs: return log i...
234
  	base_size = xfs_buf_log_format_size(blfp);
820a554f2   Mark Tinguely   xfs: fix segment ...
235

820a554f2   Mark Tinguely   xfs: fix segment ...
236
237
238
239
240
241
  	first_bit = xfs_next_bit(blfp->blf_data_map, blfp->blf_map_size, 0);
  	if (!(bip->bli_flags & XFS_BLI_STALE) && first_bit == -1) {
  		/*
  		 * If the map is not be dirty in the transaction, mark
  		 * the size as zero and do not advance the vector pointer.
  		 */
bde7cff67   Christoph Hellwig   xfs: format log i...
242
  		return;
820a554f2   Mark Tinguely   xfs: fix segment ...
243
  	}
bde7cff67   Christoph Hellwig   xfs: format log i...
244
245
  	blfp = xlog_copy_iovec(lv, vecp, XLOG_REG_TYPE_BFORMAT, blfp, base_size);
  	blfp->blf_size = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
246
247
248
249
250
251
252
  
  	if (bip->bli_flags & XFS_BLI_STALE) {
  		/*
  		 * The buffer is stale, so all we need to log
  		 * is the buf log format structure with the
  		 * cancel flag in it.
  		 */
0b1b213fc   Christoph Hellwig   xfs: event tracin...
253
  		trace_xfs_buf_item_format_stale(bip);
372cc85ec   Dave Chinner   xfs: support disc...
254
  		ASSERT(blfp->blf_flags & XFS_BLF_CANCEL);
bde7cff67   Christoph Hellwig   xfs: format log i...
255
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
256
  	}
5f6bed76c   Dave Chinner   xfs: Introduce an...
257

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
258
259
260
  	/*
  	 * Fill in an iovec for each set of contiguous chunks.
  	 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
261
262
263
264
265
266
267
268
269
  	last_bit = first_bit;
  	nbits = 1;
  	for (;;) {
  		/*
  		 * This takes the bit number to start looking from and
  		 * returns the next set bit from there.  It returns -1
  		 * if there are no more bits set or the start bit is
  		 * beyond the end of the bitmap.
  		 */
372cc85ec   Dave Chinner   xfs: support disc...
270
271
  		next_bit = xfs_next_bit(blfp->blf_data_map, blfp->blf_map_size,
  					(uint)last_bit + 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
272
  		/*
7aeb72224   Christoph Hellwig   xfs: refactor xfs...
273
274
275
276
277
  		 * If we run out of bits fill in the last iovec and get out of
  		 * the loop.  Else if we start a new set of bits then fill in
  		 * the iovec for the series we were looking at and start
  		 * counting the bits in the new one.  Else we're still in the
  		 * same set of bits so just keep counting and scanning.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
278
279
  		 */
  		if (next_bit == -1) {
bde7cff67   Christoph Hellwig   xfs: format log i...
280
  			xfs_buf_item_copy_iovec(lv, vecp, bp, offset,
7aeb72224   Christoph Hellwig   xfs: refactor xfs...
281
  						first_bit, nbits);
bde7cff67   Christoph Hellwig   xfs: format log i...
282
  			blfp->blf_size++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
283
  			break;
7aeb72224   Christoph Hellwig   xfs: refactor xfs...
284
285
  		} else if (next_bit != last_bit + 1 ||
  		           xfs_buf_item_straddle(bp, offset, next_bit, last_bit)) {
bde7cff67   Christoph Hellwig   xfs: format log i...
286
  			xfs_buf_item_copy_iovec(lv, vecp, bp, offset,
1234351cb   Christoph Hellwig   xfs: introduce xl...
287
  						first_bit, nbits);
bde7cff67   Christoph Hellwig   xfs: format log i...
288
  			blfp->blf_size++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
289
290
291
292
293
294
295
296
  			first_bit = next_bit;
  			last_bit = next_bit;
  			nbits = 1;
  		} else {
  			last_bit++;
  			nbits++;
  		}
  	}
372cc85ec   Dave Chinner   xfs: support disc...
297
298
299
300
301
302
303
304
305
306
307
  }
  
  /*
   * This is called to fill in the vector of log iovecs for the
   * given log buf item.  It fills the first entry with a buf log
   * format structure, and the rest point to contiguous chunks
   * within the buffer.
   */
  STATIC void
  xfs_buf_item_format(
  	struct xfs_log_item	*lip,
bde7cff67   Christoph Hellwig   xfs: format log i...
308
  	struct xfs_log_vec	*lv)
372cc85ec   Dave Chinner   xfs: support disc...
309
310
311
  {
  	struct xfs_buf_log_item	*bip = BUF_ITEM(lip);
  	struct xfs_buf		*bp = bip->bli_buf;
bde7cff67   Christoph Hellwig   xfs: format log i...
312
  	struct xfs_log_iovec	*vecp = NULL;
372cc85ec   Dave Chinner   xfs: support disc...
313
314
315
316
317
318
  	uint			offset = 0;
  	int			i;
  
  	ASSERT(atomic_read(&bip->bli_refcount) > 0);
  	ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
  	       (bip->bli_flags & XFS_BLI_STALE));
0d612fb57   Dave Chinner   xfs: ensure buffe...
319
320
321
  	ASSERT((bip->bli_flags & XFS_BLI_STALE) ||
  	       (xfs_blft_from_flags(&bip->__bli_format) > XFS_BLFT_UNKNOWN_BUF
  	        && xfs_blft_from_flags(&bip->__bli_format) < XFS_BLFT_MAX_BUF));
e9385cc6f   Brian Foster   xfs: ordered buff...
322
323
  	ASSERT(!(bip->bli_flags & XFS_BLI_ORDERED) ||
  	       (bip->bli_flags & XFS_BLI_STALE));
0d612fb57   Dave Chinner   xfs: ensure buffe...
324

372cc85ec   Dave Chinner   xfs: support disc...
325
326
327
  
  	/*
  	 * If it is an inode buffer, transfer the in-memory state to the
ddf6ad014   Dave Chinner   xfs: Use inode cr...
328
329
330
  	 * format flags and clear the in-memory state.
  	 *
  	 * For buffer based inode allocation, we do not transfer
372cc85ec   Dave Chinner   xfs: support disc...
331
332
333
  	 * this state if the inode buffer allocation has not yet been committed
  	 * to the log as setting the XFS_BLI_INODE_BUF flag will prevent
  	 * correct replay of the inode allocation.
ddf6ad014   Dave Chinner   xfs: Use inode cr...
334
335
336
337
338
  	 *
  	 * For icreate item based inode allocation, the buffers aren't written
  	 * to the journal during allocation, and hence we should always tag the
  	 * buffer as an inode buffer so that the correct unlinked list replay
  	 * occurs during recovery.
372cc85ec   Dave Chinner   xfs: support disc...
339
340
  	 */
  	if (bip->bli_flags & XFS_BLI_INODE_BUF) {
b81b79f4e   Christoph Hellwig   xfs: add a new xf...
341
  		if (xfs_sb_version_has_v3inode(&lip->li_mountp->m_sb) ||
ddf6ad014   Dave Chinner   xfs: Use inode cr...
342
  		    !((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) &&
372cc85ec   Dave Chinner   xfs: support disc...
343
  		      xfs_log_item_in_current_chkpt(lip)))
b94381737   Mark Tinguely   xfs: rename bli_f...
344
  			bip->__bli_format.blf_flags |= XFS_BLF_INODE_BUF;
372cc85ec   Dave Chinner   xfs: support disc...
345
346
347
348
  		bip->bli_flags &= ~XFS_BLI_INODE_BUF;
  	}
  
  	for (i = 0; i < bip->bli_format_count; i++) {
bde7cff67   Christoph Hellwig   xfs: format log i...
349
  		xfs_buf_item_format_segment(bip, lv, &vecp, offset,
1234351cb   Christoph Hellwig   xfs: introduce xl...
350
  					    &bip->bli_formats[i]);
a3916e528   Brian Foster   xfs: fix broken m...
351
  		offset += BBTOB(bp->b_maps[i].bm_len);
372cc85ec   Dave Chinner   xfs: support disc...
352
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
353
354
355
356
  
  	/*
  	 * Check to make sure everything is consistent.
  	 */
0b1b213fc   Christoph Hellwig   xfs: event tracin...
357
  	trace_xfs_buf_item_format(bip);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
358
359
360
  }
  
  /*
64fc35de6   Dave Chinner   xfs: modify buffe...
361
   * This is called to pin the buffer associated with the buf log item in memory
4d16e9246   Christoph Hellwig   xfs: simplify buf...
362
   * so it cannot be written out.
64fc35de6   Dave Chinner   xfs: modify buffe...
363
364
365
366
367
   *
   * We also always take a reference to the buffer log item here so that the bli
   * is held while the item is pinned in memory. This means that we can
   * unconditionally drop the reference count a transaction holds when the
   * transaction is completed.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
368
   */
ba0f32d46   Christoph Hellwig   [XFS] mark variou...
369
  STATIC void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
370
  xfs_buf_item_pin(
7bfa31d8e   Christoph Hellwig   xfs: give xfs_ite...
371
  	struct xfs_log_item	*lip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
372
  {
7bfa31d8e   Christoph Hellwig   xfs: give xfs_ite...
373
  	struct xfs_buf_log_item	*bip = BUF_ITEM(lip);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
374

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
375
376
  	ASSERT(atomic_read(&bip->bli_refcount) > 0);
  	ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
5f6bed76c   Dave Chinner   xfs: Introduce an...
377
  	       (bip->bli_flags & XFS_BLI_ORDERED) ||
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
378
  	       (bip->bli_flags & XFS_BLI_STALE));
7bfa31d8e   Christoph Hellwig   xfs: give xfs_ite...
379

0b1b213fc   Christoph Hellwig   xfs: event tracin...
380
  	trace_xfs_buf_item_pin(bip);
4d16e9246   Christoph Hellwig   xfs: simplify buf...
381
382
383
  
  	atomic_inc(&bip->bli_refcount);
  	atomic_inc(&bip->bli_buf->b_pin_count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
384
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
385
386
387
  /*
   * This is called to unpin the buffer associated with the buf log
   * item which was previously pinned with a call to xfs_buf_item_pin().
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
388
389
390
391
   *
   * Also drop the reference to the buf item for the current transaction.
   * If the XFS_BLI_STALE flag is set and we are the last reference,
   * then free up the buf log item and unlock the buffer.
9412e3181   Christoph Hellwig   xfs: merge iop_un...
392
393
394
395
396
   *
   * If the remove flag is set we are called from uncommit in the
   * forced-shutdown path.  If that is true and the reference count on
   * the log item is going to drop to zero we need to free the item's
   * descriptor in the transaction.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
397
   */
ba0f32d46   Christoph Hellwig   [XFS] mark variou...
398
  STATIC void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
399
  xfs_buf_item_unpin(
7bfa31d8e   Christoph Hellwig   xfs: give xfs_ite...
400
  	struct xfs_log_item	*lip,
9412e3181   Christoph Hellwig   xfs: merge iop_un...
401
  	int			remove)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
402
  {
7bfa31d8e   Christoph Hellwig   xfs: give xfs_ite...
403
  	struct xfs_buf_log_item	*bip = BUF_ITEM(lip);
70a206553   Carlos Maiolino   Get rid of xfs_bu...
404
  	xfs_buf_t		*bp = bip->bli_buf;
70a206553   Carlos Maiolino   Get rid of xfs_bu...
405
406
  	int			stale = bip->bli_flags & XFS_BLI_STALE;
  	int			freed;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
407

fb1755a64   Carlos Maiolino   Split buffer's b_...
408
  	ASSERT(bp->b_log_item == bip);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
409
  	ASSERT(atomic_read(&bip->bli_refcount) > 0);
9412e3181   Christoph Hellwig   xfs: merge iop_un...
410

0b1b213fc   Christoph Hellwig   xfs: event tracin...
411
  	trace_xfs_buf_item_unpin(bip);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
412
413
  
  	freed = atomic_dec_and_test(&bip->bli_refcount);
4d16e9246   Christoph Hellwig   xfs: simplify buf...
414
415
416
  
  	if (atomic_dec_and_test(&bp->b_pin_count))
  		wake_up_all(&bp->b_waiters);
7bfa31d8e   Christoph Hellwig   xfs: give xfs_ite...
417

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
418
419
  	if (freed && stale) {
  		ASSERT(bip->bli_flags & XFS_BLI_STALE);
0c842ad46   Christoph Hellwig   xfs: clean up buf...
420
  		ASSERT(xfs_buf_islocked(bp));
5cfd28b6a   Dave Chinner   xfs: remove XBF_S...
421
  		ASSERT(bp->b_flags & XBF_STALE);
b94381737   Mark Tinguely   xfs: rename bli_f...
422
  		ASSERT(bip->__bli_format.blf_flags & XFS_BLF_CANCEL);
9412e3181   Christoph Hellwig   xfs: merge iop_un...
423

0b1b213fc   Christoph Hellwig   xfs: event tracin...
424
  		trace_xfs_buf_item_unpin_stale(bip);
9412e3181   Christoph Hellwig   xfs: merge iop_un...
425
426
  		if (remove) {
  			/*
e34a314c5   Dave Chinner   xfs: fix efi item...
427
428
429
430
431
  			 * If we are in a transaction context, we have to
  			 * remove the log item from the transaction as we are
  			 * about to release our reference to the buffer.  If we
  			 * don't, the unlock that occurs later in
  			 * xfs_trans_uncommit() will try to reference the
9412e3181   Christoph Hellwig   xfs: merge iop_un...
432
433
  			 * buffer which we no longer have a hold on.
  			 */
e6631f855   Dave Chinner   xfs: get rid of t...
434
  			if (!list_empty(&lip->li_trans))
e34a314c5   Dave Chinner   xfs: fix efi item...
435
  				xfs_trans_del_item(lip);
9412e3181   Christoph Hellwig   xfs: merge iop_un...
436
437
438
439
440
  
  			/*
  			 * Since the transaction no longer refers to the buffer,
  			 * the buffer should no longer refer to the transaction.
  			 */
bf9d9013a   Christoph Hellwig   xfs: add a proper...
441
  			bp->b_transp = NULL;
9412e3181   Christoph Hellwig   xfs: merge iop_un...
442
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
443
  		/*
849274c10   Brian Foster   xfs: acquire ->ai...
444
445
446
447
  		 * If we get called here because of an IO error, we may or may
  		 * not have the item on the AIL. xfs_trans_ail_delete() will
  		 * take care of that situation. xfs_trans_ail_delete() drops
  		 * the AIL lock.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
448
449
  		 */
  		if (bip->bli_flags & XFS_BLI_STALE_INODE) {
fec671cd3   Dave Chinner   xfs: clean up the...
450
  			xfs_buf_item_done(bp);
664ffb8a4   Christoph Hellwig   xfs: move the buf...
451
  			xfs_buf_inode_iodone(bp);
48d55e2ae   Dave Chinner   xfs: attach inode...
452
  			ASSERT(list_empty(&bp->b_li_list));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
453
  		} else {
849274c10   Brian Foster   xfs: acquire ->ai...
454
  			xfs_trans_ail_delete(lip, SHUTDOWN_LOG_IO_ERROR);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
455
  			xfs_buf_item_relse(bp);
fb1755a64   Carlos Maiolino   Split buffer's b_...
456
  			ASSERT(bp->b_log_item == NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
457
458
  		}
  		xfs_buf_relse(bp);
960c60af8   Christoph Hellwig   xfs: do not add b...
459
  	} else if (freed && remove) {
137fff09b   Dave Chinner   xfs: fix buffer s...
460
  		/*
54b3b1f61   Brian Foster   xfs: factor out b...
461
462
  		 * The buffer must be locked and held by the caller to simulate
  		 * an async I/O failure.
137fff09b   Dave Chinner   xfs: fix buffer s...
463
  		 */
960c60af8   Christoph Hellwig   xfs: do not add b...
464
  		xfs_buf_lock(bp);
137fff09b   Dave Chinner   xfs: fix buffer s...
465
466
  		xfs_buf_hold(bp);
  		bp->b_flags |= XBF_ASYNC;
54b3b1f61   Brian Foster   xfs: factor out b...
467
  		xfs_buf_ioend_fail(bp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
468
469
  	}
  }
ba0f32d46   Christoph Hellwig   [XFS] mark variou...
470
  STATIC uint
43ff2122e   Christoph Hellwig   xfs: on-stack del...
471
472
473
  xfs_buf_item_push(
  	struct xfs_log_item	*lip,
  	struct list_head	*buffer_list)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
474
  {
7bfa31d8e   Christoph Hellwig   xfs: give xfs_ite...
475
476
  	struct xfs_buf_log_item	*bip = BUF_ITEM(lip);
  	struct xfs_buf		*bp = bip->bli_buf;
43ff2122e   Christoph Hellwig   xfs: on-stack del...
477
  	uint			rval = XFS_ITEM_SUCCESS;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
478

811e64c71   Chandra Seetharaman   Replace the macro...
479
  	if (xfs_buf_ispinned(bp))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
480
  		return XFS_ITEM_PINNED;
5337fe9b1   Brian Foster   xfs: recheck buff...
481
482
483
484
485
486
487
488
489
490
  	if (!xfs_buf_trylock(bp)) {
  		/*
  		 * If we have just raced with a buffer being pinned and it has
  		 * been marked stale, we could end up stalling until someone else
  		 * issues a log force to unpin the stale buffer. Check for the
  		 * race condition here so xfsaild recognizes the buffer is pinned
  		 * and queues a log force to move it along.
  		 */
  		if (xfs_buf_ispinned(bp))
  			return XFS_ITEM_PINNED;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
491
  		return XFS_ITEM_LOCKED;
5337fe9b1   Brian Foster   xfs: recheck buff...
492
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
493

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
494
  	ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
43ff2122e   Christoph Hellwig   xfs: on-stack del...
495
496
  
  	trace_xfs_buf_item_push(bip);
ac8809f9a   Dave Chinner   xfs: abort metada...
497
  	/* has a previous flush failed due to IO errors? */
f9bccfcc3   Brian Foster   xfs: refactor rat...
498
499
500
501
  	if (bp->b_flags & XBF_WRITE_FAIL) {
  		xfs_buf_alert_ratelimited(bp, "XFS: Failing async write",
  	    "Failing async write on buffer block 0x%llx. Retrying async write.",
  					  (long long)bp->b_bn);
ac8809f9a   Dave Chinner   xfs: abort metada...
502
  	}
43ff2122e   Christoph Hellwig   xfs: on-stack del...
503
504
505
506
  	if (!xfs_buf_delwri_queue(bp, buffer_list))
  		rval = XFS_ITEM_FLUSHING;
  	xfs_buf_unlock(bp);
  	return rval;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
507
508
509
  }
  
  /*
95808459b   Brian Foster   xfs: refactor xfs...
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
   * Drop the buffer log item refcount and take appropriate action. This helper
   * determines whether the bli must be freed or not, since a decrement to zero
   * does not necessarily mean the bli is unused.
   *
   * Return true if the bli is freed, false otherwise.
   */
  bool
  xfs_buf_item_put(
  	struct xfs_buf_log_item	*bip)
  {
  	struct xfs_log_item	*lip = &bip->bli_item;
  	bool			aborted;
  	bool			dirty;
  
  	/* drop the bli ref and return if it wasn't the last one */
  	if (!atomic_dec_and_test(&bip->bli_refcount))
  		return false;
  
  	/*
  	 * We dropped the last ref and must free the item if clean or aborted.
  	 * If the bli is dirty and non-aborted, the buffer was clean in the
  	 * transaction but still awaiting writeback from previous changes. In
  	 * that case, the bli is freed on buffer writeback completion.
  	 */
  	aborted = test_bit(XFS_LI_ABORTED, &lip->li_flags) ||
  		  XFS_FORCED_SHUTDOWN(lip->li_mountp);
  	dirty = bip->bli_flags & XFS_BLI_DIRTY;
  	if (dirty && !aborted)
  		return false;
  
  	/*
  	 * The bli is aborted or clean. An aborted item may be in the AIL
  	 * regardless of dirty state.  For example, consider an aborted
  	 * transaction that invalidated a dirty bli and cleared the dirty
  	 * state.
  	 */
  	if (aborted)
2b3cf0935   Brian Foster   xfs: combine xfs_...
547
  		xfs_trans_ail_delete(lip, 0);
95808459b   Brian Foster   xfs: refactor xfs...
548
549
550
551
552
  	xfs_buf_item_relse(bip->bli_buf);
  	return true;
  }
  
  /*
64fc35de6   Dave Chinner   xfs: modify buffe...
553
554
555
   * Release the buffer associated with the buf log item.  If there is no dirty
   * logged data associated with the buffer recorded in the buf log item, then
   * free the buf log item and remove the reference to it in the buffer.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
556
   *
64fc35de6   Dave Chinner   xfs: modify buffe...
557
558
   * This call ignores the recursion count.  It is only called when the buffer
   * should REALLY be unlocked, regardless of the recursion count.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
559
   *
64fc35de6   Dave Chinner   xfs: modify buffe...
560
561
562
563
564
565
566
567
568
569
   * We unconditionally drop the transaction's reference to the log item. If the
   * item was logged, then another reference was taken when it was pinned, so we
   * can safely drop the transaction reference now.  This also allows us to avoid
   * potential races with the unpin code freeing the bli by not referencing the
   * bli after we've dropped the reference count.
   *
   * If the XFS_BLI_HOLD flag is set in the buf log item, then free the log item
   * if necessary but do not unlock the buffer.  This is for support of
   * xfs_trans_bhold(). Make sure the XFS_BLI_HOLD field is cleared if we don't
   * free the item.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
570
   */
ba0f32d46   Christoph Hellwig   [XFS] mark variou...
571
  STATIC void
ddf92053e   Christoph Hellwig   xfs: split iop_un...
572
  xfs_buf_item_release(
7bfa31d8e   Christoph Hellwig   xfs: give xfs_ite...
573
  	struct xfs_log_item	*lip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
574
  {
7bfa31d8e   Christoph Hellwig   xfs: give xfs_ite...
575
576
  	struct xfs_buf_log_item	*bip = BUF_ITEM(lip);
  	struct xfs_buf		*bp = bip->bli_buf;
95808459b   Brian Foster   xfs: refactor xfs...
577
  	bool			released;
d9183105c   Brian Foster   xfs: don't unlock...
578
  	bool			hold = bip->bli_flags & XFS_BLI_HOLD;
d9183105c   Brian Foster   xfs: don't unlock...
579
  	bool			stale = bip->bli_flags & XFS_BLI_STALE;
7bf7a193a   Darrick J. Wong   xfs: fix compiler...
580
  #if defined(DEBUG) || defined(XFS_WARN)
d9183105c   Brian Foster   xfs: don't unlock...
581
  	bool			ordered = bip->bli_flags & XFS_BLI_ORDERED;
95808459b   Brian Foster   xfs: refactor xfs...
582
  	bool			dirty = bip->bli_flags & XFS_BLI_DIRTY;
4d09807f2   Brian Foster   xfs: fix use afte...
583
584
  	bool			aborted = test_bit(XFS_LI_ABORTED,
  						   &lip->li_flags);
7bf7a193a   Darrick J. Wong   xfs: fix compiler...
585
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
586

ddf92053e   Christoph Hellwig   xfs: split iop_un...
587
  	trace_xfs_buf_item_release(bip);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
588
589
  
  	/*
6453c65d3   Brian Foster   xfs: remove unnec...
590
591
  	 * The bli dirty state should match whether the blf has logged segments
  	 * except for ordered buffers, where only the bli should be dirty.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
592
  	 */
6453c65d3   Brian Foster   xfs: remove unnec...
593
594
  	ASSERT((!ordered && dirty == xfs_buf_item_dirty_format(bip)) ||
  	       (ordered && dirty && !xfs_buf_item_dirty_format(bip)));
d9183105c   Brian Foster   xfs: don't unlock...
595
  	ASSERT(!stale || (bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
46f9d2eb3   Dave Chinner   xfs: aborted buf ...
596
  	/*
d9183105c   Brian Foster   xfs: don't unlock...
597
598
599
600
601
602
603
  	 * Clear the buffer's association with this transaction and
  	 * per-transaction state from the bli, which has been copied above.
  	 */
  	bp->b_transp = NULL;
  	bip->bli_flags &= ~(XFS_BLI_LOGGED | XFS_BLI_HOLD | XFS_BLI_ORDERED);
  
  	/*
95808459b   Brian Foster   xfs: refactor xfs...
604
605
606
607
  	 * Unref the item and unlock the buffer unless held or stale. Stale
  	 * buffers remain locked until final unpin unless the bli is freed by
  	 * the unref call. The latter implies shutdown because buffer
  	 * invalidation dirties the bli and transaction.
46f9d2eb3   Dave Chinner   xfs: aborted buf ...
608
  	 */
95808459b   Brian Foster   xfs: refactor xfs...
609
610
  	released = xfs_buf_item_put(bip);
  	if (hold || (stale && !released))
d9183105c   Brian Foster   xfs: don't unlock...
611
  		return;
4d09807f2   Brian Foster   xfs: fix use afte...
612
  	ASSERT(!stale || aborted);
95808459b   Brian Foster   xfs: refactor xfs...
613
  	xfs_buf_relse(bp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
614
  }
ddf92053e   Christoph Hellwig   xfs: split iop_un...
615
616
617
618
619
620
621
  STATIC void
  xfs_buf_item_committing(
  	struct xfs_log_item	*lip,
  	xfs_lsn_t		commit_lsn)
  {
  	return xfs_buf_item_release(lip);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
  /*
   * This is called to find out where the oldest active copy of the
   * buf log item in the on disk log resides now that the last log
   * write of it completed at the given lsn.
   * We always re-log all the dirty data in a buffer, so usually the
   * latest copy in the on disk log is the only one that matters.  For
   * those cases we simply return the given lsn.
   *
   * The one exception to this is for buffers full of newly allocated
   * inodes.  These buffers are only relogged with the XFS_BLI_INODE_BUF
   * flag set, indicating that only the di_next_unlinked fields from the
   * inodes in the buffers will be replayed during recovery.  If the
   * original newly allocated inode images have not yet been flushed
   * when the buffer is so relogged, then we need to make sure that we
   * keep the old images in the 'active' portion of the log.  We do this
   * by returning the original lsn of that transaction here rather than
   * the current one.
   */
ba0f32d46   Christoph Hellwig   [XFS] mark variou...
640
  STATIC xfs_lsn_t
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
641
  xfs_buf_item_committed(
7bfa31d8e   Christoph Hellwig   xfs: give xfs_ite...
642
  	struct xfs_log_item	*lip,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
643
644
  	xfs_lsn_t		lsn)
  {
7bfa31d8e   Christoph Hellwig   xfs: give xfs_ite...
645
  	struct xfs_buf_log_item	*bip = BUF_ITEM(lip);
0b1b213fc   Christoph Hellwig   xfs: event tracin...
646
  	trace_xfs_buf_item_committed(bip);
7bfa31d8e   Christoph Hellwig   xfs: give xfs_ite...
647
648
649
  	if ((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) && lip->li_lsn != 0)
  		return lip->li_lsn;
  	return lsn;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
650
  }
272e42b21   Christoph Hellwig   xfs: constify xfs...
651
  static const struct xfs_item_ops xfs_buf_item_ops = {
7bfa31d8e   Christoph Hellwig   xfs: give xfs_ite...
652
653
654
655
  	.iop_size	= xfs_buf_item_size,
  	.iop_format	= xfs_buf_item_format,
  	.iop_pin	= xfs_buf_item_pin,
  	.iop_unpin	= xfs_buf_item_unpin,
ddf92053e   Christoph Hellwig   xfs: split iop_un...
656
657
  	.iop_release	= xfs_buf_item_release,
  	.iop_committing	= xfs_buf_item_committing,
7bfa31d8e   Christoph Hellwig   xfs: give xfs_ite...
658
659
  	.iop_committed	= xfs_buf_item_committed,
  	.iop_push	= xfs_buf_item_push,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
660
  };
c64dd49b5   Darrick J. Wong   xfs: clean up xfs...
661
  STATIC void
372cc85ec   Dave Chinner   xfs: support disc...
662
663
664
665
666
667
668
669
  xfs_buf_item_get_format(
  	struct xfs_buf_log_item	*bip,
  	int			count)
  {
  	ASSERT(bip->bli_formats == NULL);
  	bip->bli_format_count = count;
  
  	if (count == 1) {
b94381737   Mark Tinguely   xfs: rename bli_f...
670
  		bip->bli_formats = &bip->__bli_format;
c64dd49b5   Darrick J. Wong   xfs: clean up xfs...
671
  		return;
372cc85ec   Dave Chinner   xfs: support disc...
672
673
674
  	}
  
  	bip->bli_formats = kmem_zalloc(count * sizeof(struct xfs_buf_log_format),
707e0ddaf   Tetsuo Handa   fs: xfs: Remove K...
675
  				0);
372cc85ec   Dave Chinner   xfs: support disc...
676
677
678
679
680
681
  }
  
  STATIC void
  xfs_buf_item_free_format(
  	struct xfs_buf_log_item	*bip)
  {
b94381737   Mark Tinguely   xfs: rename bli_f...
682
  	if (bip->bli_formats != &bip->__bli_format) {
372cc85ec   Dave Chinner   xfs: support disc...
683
684
685
686
  		kmem_free(bip->bli_formats);
  		bip->bli_formats = NULL;
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
687
688
689
  
  /*
   * Allocate a new buf log item to go with the given buffer.
fb1755a64   Carlos Maiolino   Split buffer's b_...
690
691
   * Set the buffer's b_log_item field to point to the new
   * buf log item.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
692
   */
f79af0b90   Dave Chinner   xfs: fix non-debu...
693
  int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
694
  xfs_buf_item_init(
f79af0b90   Dave Chinner   xfs: fix non-debu...
695
696
  	struct xfs_buf	*bp,
  	struct xfs_mount *mp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
697
  {
fb1755a64   Carlos Maiolino   Split buffer's b_...
698
  	struct xfs_buf_log_item	*bip = bp->b_log_item;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
699
700
  	int			chunks;
  	int			map_size;
372cc85ec   Dave Chinner   xfs: support disc...
701
  	int			i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
702
703
704
  
  	/*
  	 * Check to see if there is already a buf log item for
fb1755a64   Carlos Maiolino   Split buffer's b_...
705
  	 * this buffer. If we do already have one, there is
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
706
707
  	 * nothing to do here so return.
  	 */
dbd329f1e   Christoph Hellwig   xfs: add struct x...
708
  	ASSERT(bp->b_mount == mp);
1a2ebf835   Dave Chinner   xfs: add some mor...
709
  	if (bip) {
fb1755a64   Carlos Maiolino   Split buffer's b_...
710
  		ASSERT(bip->bli_item.li_type == XFS_LI_BUF);
1a2ebf835   Dave Chinner   xfs: add some mor...
711
712
  		ASSERT(!bp->b_transp);
  		ASSERT(bip->bli_buf == bp);
f79af0b90   Dave Chinner   xfs: fix non-debu...
713
  		return 0;
fb1755a64   Carlos Maiolino   Split buffer's b_...
714
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
715

32a2b11f4   Carlos Maiolino   xfs: Remove kmem_...
716
  	bip = kmem_cache_zalloc(xfs_buf_item_zone, GFP_KERNEL | __GFP_NOFAIL);
43f5efc5b   Dave Chinner   xfs: factor log i...
717
  	xfs_log_item_init(mp, &bip->bli_item, XFS_LI_BUF, &xfs_buf_item_ops);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
718
  	bip->bli_buf = bp;
372cc85ec   Dave Chinner   xfs: support disc...
719
720
721
722
723
724
725
726
727
728
  
  	/*
  	 * chunks is the number of XFS_BLF_CHUNK size pieces the buffer
  	 * can be divided into. Make sure not to truncate any pieces.
  	 * map_size is the size of the bitmap needed to describe the
  	 * chunks of the buffer.
  	 *
  	 * Discontiguous buffer support follows the layout of the underlying
  	 * buffer. This makes the implementation as simple as possible.
  	 */
c64dd49b5   Darrick J. Wong   xfs: clean up xfs...
729
  	xfs_buf_item_get_format(bip, bp->b_map_count);
372cc85ec   Dave Chinner   xfs: support disc...
730
731
732
733
734
  
  	for (i = 0; i < bip->bli_format_count; i++) {
  		chunks = DIV_ROUND_UP(BBTOB(bp->b_maps[i].bm_len),
  				      XFS_BLF_CHUNK);
  		map_size = DIV_ROUND_UP(chunks, NBWORD);
c3d5f0c2f   Darrick J. Wong   xfs: complain if ...
735
736
737
738
739
740
741
742
  		if (map_size > XFS_BLF_DATAMAP_SIZE) {
  			kmem_cache_free(xfs_buf_item_zone, bip);
  			xfs_err(mp,
  	"buffer item dirty bitmap (%u uints) too small to reflect %u bytes!",
  					map_size,
  					BBTOB(bp->b_maps[i].bm_len));
  			return -EFSCORRUPTED;
  		}
372cc85ec   Dave Chinner   xfs: support disc...
743
744
745
746
747
  		bip->bli_formats[i].blf_type = XFS_LI_BUF;
  		bip->bli_formats[i].blf_blkno = bp->b_maps[i].bm_bn;
  		bip->bli_formats[i].blf_len = bp->b_maps[i].bm_len;
  		bip->bli_formats[i].blf_map_size = map_size;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
748

fb1755a64   Carlos Maiolino   Split buffer's b_...
749
  	bp->b_log_item = bip;
f79af0b90   Dave Chinner   xfs: fix non-debu...
750
751
  	xfs_buf_hold(bp);
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
752
753
754
755
756
757
758
  }
  
  
  /*
   * Mark bytes first through last inclusive as dirty in the buf
   * item's bitmap.
   */
632b89e82   Dave Chinner   xfs: fix static a...
759
  static void
372cc85ec   Dave Chinner   xfs: support disc...
760
  xfs_buf_item_log_segment(
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
761
  	uint			first,
372cc85ec   Dave Chinner   xfs: support disc...
762
763
  	uint			last,
  	uint			*map)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
764
765
766
767
768
769
770
771
772
773
  {
  	uint		first_bit;
  	uint		last_bit;
  	uint		bits_to_set;
  	uint		bits_set;
  	uint		word_num;
  	uint		*wordp;
  	uint		bit;
  	uint		end_bit;
  	uint		mask;
c3d5f0c2f   Darrick J. Wong   xfs: complain if ...
774
775
  	ASSERT(first < XFS_BLF_DATAMAP_SIZE * XFS_BLF_CHUNK * NBWORD);
  	ASSERT(last < XFS_BLF_DATAMAP_SIZE * XFS_BLF_CHUNK * NBWORD);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
776
  	/*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
777
778
  	 * Convert byte offsets to bit numbers.
  	 */
c11554104   Dave Chinner   xfs: Clean up XFS...
779
780
  	first_bit = first >> XFS_BLF_SHIFT;
  	last_bit = last >> XFS_BLF_SHIFT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
781
782
783
784
785
786
787
788
789
790
791
  
  	/*
  	 * Calculate the total number of bits to be set.
  	 */
  	bits_to_set = last_bit - first_bit + 1;
  
  	/*
  	 * Get a pointer to the first word in the bitmap
  	 * to set a bit in.
  	 */
  	word_num = first_bit >> BIT_TO_WORD_SHIFT;
372cc85ec   Dave Chinner   xfs: support disc...
792
  	wordp = &map[word_num];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
  
  	/*
  	 * Calculate the starting bit in the first word.
  	 */
  	bit = first_bit & (uint)(NBWORD - 1);
  
  	/*
  	 * First set any bits in the first word of our range.
  	 * If it starts at bit 0 of the word, it will be
  	 * set below rather than here.  That is what the variable
  	 * bit tells us. The variable bits_set tracks the number
  	 * of bits that have been set so far.  End_bit is the number
  	 * of the last bit to be set in this word plus one.
  	 */
  	if (bit) {
9bb54cb56   Dave Chinner   xfs: clean up MIN...
808
  		end_bit = min(bit + bits_to_set, (uint)NBWORD);
79c350e45   Xie XiuQi   xfs: fix signed i...
809
  		mask = ((1U << (end_bit - bit)) - 1) << bit;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
810
811
812
813
814
815
816
817
818
819
820
821
  		*wordp |= mask;
  		wordp++;
  		bits_set = end_bit - bit;
  	} else {
  		bits_set = 0;
  	}
  
  	/*
  	 * Now set bits a whole word at a time that are between
  	 * first_bit and last_bit.
  	 */
  	while ((bits_to_set - bits_set) >= NBWORD) {
120254608   Darrick J. Wong   xfs: "optimize" b...
822
  		*wordp = 0xffffffff;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
823
824
825
826
827
828
829
830
831
  		bits_set += NBWORD;
  		wordp++;
  	}
  
  	/*
  	 * Finally, set any bits left to be set in one last partial word.
  	 */
  	end_bit = bits_to_set - bits_set;
  	if (end_bit) {
79c350e45   Xie XiuQi   xfs: fix signed i...
832
  		mask = (1U << end_bit) - 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
833
834
  		*wordp |= mask;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
835
  }
372cc85ec   Dave Chinner   xfs: support disc...
836
837
838
839
840
841
  /*
   * Mark bytes first through last inclusive as dirty in the buf
   * item's bitmap.
   */
  void
  xfs_buf_item_log(
70a206553   Carlos Maiolino   Get rid of xfs_bu...
842
  	struct xfs_buf_log_item	*bip,
372cc85ec   Dave Chinner   xfs: support disc...
843
844
845
846
847
848
849
850
851
  	uint			first,
  	uint			last)
  {
  	int			i;
  	uint			start;
  	uint			end;
  	struct xfs_buf		*bp = bip->bli_buf;
  
  	/*
372cc85ec   Dave Chinner   xfs: support disc...
852
853
854
855
856
857
  	 * walk each buffer segment and mark them dirty appropriately.
  	 */
  	start = 0;
  	for (i = 0; i < bip->bli_format_count; i++) {
  		if (start > last)
  			break;
a3916e528   Brian Foster   xfs: fix broken m...
858
859
860
  		end = start + BBTOB(bp->b_maps[i].bm_len) - 1;
  
  		/* skip to the map that includes the first byte to log */
372cc85ec   Dave Chinner   xfs: support disc...
861
862
863
864
  		if (first > end) {
  			start += BBTOB(bp->b_maps[i].bm_len);
  			continue;
  		}
a3916e528   Brian Foster   xfs: fix broken m...
865
866
867
868
869
870
871
  
  		/*
  		 * Trim the range to this segment and mark it in the bitmap.
  		 * Note that we must convert buffer offsets to segment relative
  		 * offsets (e.g., the first byte of each segment is byte 0 of
  		 * that segment).
  		 */
372cc85ec   Dave Chinner   xfs: support disc...
872
873
874
875
  		if (first < start)
  			first = start;
  		if (end > last)
  			end = last;
a3916e528   Brian Foster   xfs: fix broken m...
876
  		xfs_buf_item_log_segment(first - start, end - start,
372cc85ec   Dave Chinner   xfs: support disc...
877
  					 &bip->bli_formats[i].blf_data_map[0]);
a3916e528   Brian Foster   xfs: fix broken m...
878
  		start += BBTOB(bp->b_maps[i].bm_len);
372cc85ec   Dave Chinner   xfs: support disc...
879
880
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
881

6453c65d3   Brian Foster   xfs: remove unnec...
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
  /*
   * Return true if the buffer has any ranges logged/dirtied by a transaction,
   * false otherwise.
   */
  bool
  xfs_buf_item_dirty_format(
  	struct xfs_buf_log_item	*bip)
  {
  	int			i;
  
  	for (i = 0; i < bip->bli_format_count; i++) {
  		if (!xfs_bitmap_empty(bip->bli_formats[i].blf_data_map,
  			     bip->bli_formats[i].blf_map_size))
  			return true;
  	}
  
  	return false;
  }
e1f5dbd70   Lachlan McIlroy   [XFS] Fix use-aft...
900
901
  STATIC void
  xfs_buf_item_free(
70a206553   Carlos Maiolino   Get rid of xfs_bu...
902
  	struct xfs_buf_log_item	*bip)
e1f5dbd70   Lachlan McIlroy   [XFS] Fix use-aft...
903
  {
372cc85ec   Dave Chinner   xfs: support disc...
904
  	xfs_buf_item_free_format(bip);
b1c5ebb21   Dave Chinner   xfs: allocate log...
905
  	kmem_free(bip->bli_item.li_lv_shadow);
377bcd5f3   Carlos Maiolino   xfs: Remove kmem_...
906
  	kmem_cache_free(xfs_buf_item_zone, bip);
e1f5dbd70   Lachlan McIlroy   [XFS] Fix use-aft...
907
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
908
  /*
b01d1461a   Dave Chinner   xfs: call xfs_buf...
909
   * xfs_buf_item_relse() is called when the buf log item is no longer needed.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
910
911
912
913
914
   */
  void
  xfs_buf_item_relse(
  	xfs_buf_t	*bp)
  {
fb1755a64   Carlos Maiolino   Split buffer's b_...
915
  	struct xfs_buf_log_item	*bip = bp->b_log_item;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
916

0b1b213fc   Christoph Hellwig   xfs: event tracin...
917
  	trace_xfs_buf_item_relse(bp, _RET_IP_);
826f7e341   Brian Foster   xfs: use bitops i...
918
  	ASSERT(!test_bit(XFS_LI_IN_AIL, &bip->bli_item.li_flags));
0b1b213fc   Christoph Hellwig   xfs: event tracin...
919

fb1755a64   Carlos Maiolino   Split buffer's b_...
920
  	bp->b_log_item = NULL;
e1f5dbd70   Lachlan McIlroy   [XFS] Fix use-aft...
921
922
  	xfs_buf_rele(bp);
  	xfs_buf_item_free(bip);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
923
  }
664ffb8a4   Christoph Hellwig   xfs: move the buf...
924
  void
fec671cd3   Dave Chinner   xfs: clean up the...
925
  xfs_buf_item_done(
aac855ab1   Dave Chinner   xfs: make inode I...
926
927
  	struct xfs_buf		*bp)
  {
fec671cd3   Dave Chinner   xfs: clean up the...
928
929
930
931
932
933
934
935
  	/*
  	 * If we are forcibly shutting down, this may well be off the AIL
  	 * already. That's because we simulate the log-committed callbacks to
  	 * unpin these buffers. Or we may never have put this item on AIL
  	 * because of the transaction was aborted forcibly.
  	 * xfs_trans_ail_delete() takes care of these.
  	 *
  	 * Either way, AIL is useless if we're forcing a shutdown.
22c10589a   Christoph Hellwig   xfs: remove xlog_...
936
937
938
  	 *
  	 * Note that log recovery writes might have buffer items that are not on
  	 * the AIL even when the file system is not shut down.
fec671cd3   Dave Chinner   xfs: clean up the...
939
  	 */
b840e2ada   Christoph Hellwig   xfs: use xfs_buf_...
940
  	xfs_trans_ail_delete(&bp->b_log_item->bli_item,
22c10589a   Christoph Hellwig   xfs: remove xlog_...
941
  			     (bp->b_flags & _XBF_LOGRECOVERY) ? 0 :
b840e2ada   Christoph Hellwig   xfs: use xfs_buf_...
942
943
  			     SHUTDOWN_CORRUPT_INCORE);
  	xfs_buf_item_relse(bp);
f593bf144   Dave Chinner   xfs: mark inode b...
944
  }