Blame view

fs/jfs/jfs_txnmgr.c 75.5 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
  /*
   *   Copyright (C) International Business Machines Corp., 2000-2005
   *   Portions Copyright (C) Christoph Hellwig, 2001-2002
   *
   *   This program is free software;  you can redistribute it and/or modify
   *   it under the terms of the GNU General Public License as published by
63f83c9fc   Dave Kleikamp   JFS: White space ...
7
   *   the Free Software Foundation; either version 2 of the License, or
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8
   *   (at your option) any later version.
63f83c9fc   Dave Kleikamp   JFS: White space ...
9
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
10
11
12
13
14
15
   *   This program is distributed in the hope that it will be useful,
   *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
   *   the GNU General Public License for more details.
   *
   *   You should have received a copy of the GNU General Public License
63f83c9fc   Dave Kleikamp   JFS: White space ...
16
   *   along with this program;  if not, write to the Free Software
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
18
19
20
   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
   */
  
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
21
   *	jfs_txnmgr.c: transaction manager
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
   *
   * notes:
   * transaction starts with txBegin() and ends with txCommit()
   * or txAbort().
   *
   * tlock is acquired at the time of update;
   * (obviate scan at commit time for xtree and dtree)
   * tlock and mp points to each other;
   * (no hashlist for mp -> tlock).
   *
   * special cases:
   * tlock on in-memory inode:
   * in-place tlock in the in-memory inode itself;
   * converted to page lock by iWrite() at commit time.
   *
   * tlock during write()/mmap() under anonymous transaction (tid = 0):
   * transferred (?) to transaction at commit time.
   *
   * use the page itself to update allocation maps
   * (obviate intermediate replication of allocation/deallocation data)
   * hold on to mp+lock thru update of maps
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
44
45
  #include <linux/fs.h>
  #include <linux/vmalloc.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46
  #include <linux/completion.h>
7dfb71030   Nigel Cunningham   [PATCH] Add inclu...
47
  #include <linux/freezer.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
48
49
  #include <linux/module.h>
  #include <linux/moduleparam.h>
91dbb4deb   Christoph Hellwig   JFS: Use the kthr...
50
  #include <linux/kthread.h>
b2e03ca74   Alexey Dobriyan   JFS: switch to se...
51
  #include <linux/seq_file.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
52
  #include "jfs_incore.h"
1868f4aa5   Dave Kleikamp   JFS: fix sparse w...
53
  #include "jfs_inode.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
55
56
57
58
59
60
61
62
  #include "jfs_filsys.h"
  #include "jfs_metapage.h"
  #include "jfs_dinode.h"
  #include "jfs_imap.h"
  #include "jfs_dmap.h"
  #include "jfs_superblock.h"
  #include "jfs_debug.h"
  
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
63
   *	transaction management structures
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
   */
  static struct {
  	int freetid;		/* index of a free tid structure */
  	int freelock;		/* index first free lock word */
  	wait_queue_head_t freewait;	/* eventlist of free tblock */
  	wait_queue_head_t freelockwait;	/* eventlist of free tlock */
  	wait_queue_head_t lowlockwait;	/* eventlist of ample tlocks */
  	int tlocksInUse;	/* Number of tlocks in use */
  	spinlock_t LazyLock;	/* synchronize sync_queue & unlock_queue */
  /*	struct tblock *sync_queue; * Transactions waiting for data sync */
  	struct list_head unlock_queue;	/* Txns waiting to be released */
  	struct list_head anon_list;	/* inodes having anonymous txns */
  	struct list_head anon_list2;	/* inodes having anonymous txns
  					   that couldn't be sync'ed */
  } TxAnchor;
  
  int jfs_tlocks_low;		/* Indicates low number of available tlocks */
  
  #ifdef CONFIG_JFS_STATISTICS
  static struct {
  	uint txBegin;
  	uint txBegin_barrier;
  	uint txBegin_lockslow;
  	uint txBegin_freetid;
  	uint txBeginAnon;
  	uint txBeginAnon_barrier;
  	uint txBeginAnon_lockslow;
  	uint txLockAlloc;
  	uint txLockAlloc_freelock;
  } TxStat;
  #endif
  
  static int nTxBlock = -1;	/* number of transaction blocks */
  module_param(nTxBlock, int, 0);
  MODULE_PARM_DESC(nTxBlock,
  		 "Number of transaction blocks (max:65536)");
  
  static int nTxLock = -1;	/* number of transaction locks */
  module_param(nTxLock, int, 0);
  MODULE_PARM_DESC(nTxLock,
  		 "Number of transaction locks (max:65536)");
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
105
106
107
108
109
  struct tblock *TxBlock;	/* transaction block table */
  static int TxLockLWM;	/* Low water mark for number of txLocks used */
  static int TxLockHWM;	/* High water mark for number of txLocks used */
  static int TxLockVHWM;	/* Very High water mark */
  struct tlock *TxLock;	/* transaction lock table */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
110

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
111
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
112
   *	transaction management lock
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
113
114
   */
  static DEFINE_SPINLOCK(jfsTxnLock);
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
115
116
  #define TXN_LOCK()		spin_lock(&jfsTxnLock)
  #define TXN_UNLOCK()		spin_unlock(&jfsTxnLock)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
117
118
119
120
  
  #define LAZY_LOCK_INIT()	spin_lock_init(&TxAnchor.LazyLock);
  #define LAZY_LOCK(flags)	spin_lock_irqsave(&TxAnchor.LazyLock, flags)
  #define LAZY_UNLOCK(flags) spin_unlock_irqrestore(&TxAnchor.LazyLock, flags)
91dbb4deb   Christoph Hellwig   JFS: Use the kthr...
121
  static DECLARE_WAIT_QUEUE_HEAD(jfs_commit_thread_wait);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
123
124
125
126
127
128
129
130
131
132
133
  static int jfs_commit_thread_waking;
  
  /*
   * Retry logic exist outside these macros to protect from spurrious wakeups.
   */
  static inline void TXN_SLEEP_DROP_LOCK(wait_queue_head_t * event)
  {
  	DECLARE_WAITQUEUE(wait, current);
  
  	add_wait_queue(event, &wait);
  	set_current_state(TASK_UNINTERRUPTIBLE);
  	TXN_UNLOCK();
4aa0d230c   Dave Kleikamp   JFS: call io_sche...
134
  	io_schedule();
3cbb1c8e1   Milind Arun Choudhary   JFS: use __set_cu...
135
  	__set_current_state(TASK_RUNNING);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136
137
138
139
140
141
142
143
144
145
  	remove_wait_queue(event, &wait);
  }
  
  #define TXN_SLEEP(event)\
  {\
  	TXN_SLEEP_DROP_LOCK(event);\
  	TXN_LOCK();\
  }
  
  #define TXN_WAKEUP(event) wake_up_all(event)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
146
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
147
   *	statistics
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148
149
150
151
152
153
154
155
   */
  static struct {
  	tid_t maxtid;		/* 4: biggest tid ever used */
  	lid_t maxlid;		/* 4: biggest lid ever used */
  	int ntid;		/* 4: # of transactions performed */
  	int nlid;		/* 4: # of tlocks acquired */
  	int waitlock;		/* 4: # of tlock wait */
  } stattx;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
  /*
   * forward references
   */
  static int diLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
  		struct tlock * tlck, struct commit * cd);
  static int dataLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
  		struct tlock * tlck);
  static void dtLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
  		struct tlock * tlck);
  static void mapLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
  		struct tlock * tlck);
  static void txAllocPMap(struct inode *ip, struct maplock * maplock,
  		struct tblock * tblk);
  static void txForce(struct tblock * tblk);
  static int txLog(struct jfs_log * log, struct tblock * tblk,
  		struct commit * cd);
  static void txUpdateMap(struct tblock * tblk);
  static void txRelease(struct tblock * tblk);
  static void xtLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
  	   struct tlock * tlck);
  static void LogSyncRelease(struct metapage * mp);
  
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
179
180
   *		transaction block/lock management
   *		---------------------------------
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
   */
  
  /*
   * Get a transaction lock from the free list.  If the number in use is
   * greater than the high water mark, wake up the sync daemon.  This should
   * free some anonymous transaction locks.  (TXN_LOCK must be held.)
   */
  static lid_t txLockAlloc(void)
  {
  	lid_t lid;
  
  	INCREMENT(TxStat.txLockAlloc);
  	if (!TxAnchor.freelock) {
  		INCREMENT(TxStat.txLockAlloc_freelock);
  	}
  
  	while (!(lid = TxAnchor.freelock))
  		TXN_SLEEP(&TxAnchor.freelockwait);
  	TxAnchor.freelock = TxLock[lid].next;
  	HIGHWATERMARK(stattx.maxlid, lid);
  	if ((++TxAnchor.tlocksInUse > TxLockHWM) && (jfs_tlocks_low == 0)) {
  		jfs_info("txLockAlloc tlocks low");
  		jfs_tlocks_low = 1;
91dbb4deb   Christoph Hellwig   JFS: Use the kthr...
204
  		wake_up_process(jfsSyncThread);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
205
206
207
208
209
210
211
  	}
  
  	return lid;
  }
  
  static void txLockFree(lid_t lid)
  {
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
212
  	TxLock[lid].tid = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
213
214
215
216
217
218
219
220
221
222
223
224
  	TxLock[lid].next = TxAnchor.freelock;
  	TxAnchor.freelock = lid;
  	TxAnchor.tlocksInUse--;
  	if (jfs_tlocks_low && (TxAnchor.tlocksInUse < TxLockLWM)) {
  		jfs_info("txLockFree jfs_tlocks_low no more");
  		jfs_tlocks_low = 0;
  		TXN_WAKEUP(&TxAnchor.lowlockwait);
  	}
  	TXN_WAKEUP(&TxAnchor.freelockwait);
  }
  
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
225
   * NAME:	txInit()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
226
   *
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
227
   * FUNCTION:	initialize transaction management structures
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
   *
   * RETURN:
   *
   * serialization: single thread at jfs_init()
   */
  int txInit(void)
  {
  	int k, size;
  	struct sysinfo si;
  
  	/* Set defaults for nTxLock and nTxBlock if unset */
  
  	if (nTxLock == -1) {
  		if (nTxBlock == -1) {
  			/* Base default on memory size */
  			si_meminfo(&si);
  			if (si.totalram > (256 * 1024)) /* 1 GB */
  				nTxLock = 64 * 1024;
  			else
  				nTxLock = si.totalram >> 2;
  		} else if (nTxBlock > (8 * 1024))
  			nTxLock = 64 * 1024;
  		else
  			nTxLock = nTxBlock << 3;
  	}
  	if (nTxBlock == -1)
  		nTxBlock = nTxLock >> 3;
  
  	/* Verify tunable parameters */
  	if (nTxBlock < 16)
  		nTxBlock = 16;	/* No one should set it this low */
  	if (nTxBlock > 65536)
  		nTxBlock = 65536;
  	if (nTxLock < 256)
  		nTxLock = 256;	/* No one should set it this low */
  	if (nTxLock > 65536)
  		nTxLock = 65536;
  
  	printk(KERN_INFO "JFS: nTxBlock = %d, nTxLock = %d
  ",
  	       nTxBlock, nTxLock);
  	/*
  	 * initialize transaction block (tblock) table
  	 *
  	 * transaction id (tid) = tblock index
  	 * tid = 0 is reserved.
  	 */
  	TxLockLWM = (nTxLock * 4) / 10;
  	TxLockHWM = (nTxLock * 7) / 10;
  	TxLockVHWM = (nTxLock * 8) / 10;
  
  	size = sizeof(struct tblock) * nTxBlock;
f52720ca5   Panagiotis Issaris   [PATCH] fs: Remov...
280
  	TxBlock = vmalloc(size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
  	if (TxBlock == NULL)
  		return -ENOMEM;
  
  	for (k = 1; k < nTxBlock - 1; k++) {
  		TxBlock[k].next = k + 1;
  		init_waitqueue_head(&TxBlock[k].gcwait);
  		init_waitqueue_head(&TxBlock[k].waitor);
  	}
  	TxBlock[k].next = 0;
  	init_waitqueue_head(&TxBlock[k].gcwait);
  	init_waitqueue_head(&TxBlock[k].waitor);
  
  	TxAnchor.freetid = 1;
  	init_waitqueue_head(&TxAnchor.freewait);
  
  	stattx.maxtid = 1;	/* statistics */
  
  	/*
  	 * initialize transaction lock (tlock) table
  	 *
  	 * transaction lock id = tlock index
  	 * tlock id = 0 is reserved.
  	 */
  	size = sizeof(struct tlock) * nTxLock;
f52720ca5   Panagiotis Issaris   [PATCH] fs: Remov...
305
  	TxLock = vmalloc(size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
  	if (TxLock == NULL) {
  		vfree(TxBlock);
  		return -ENOMEM;
  	}
  
  	/* initialize tlock table */
  	for (k = 1; k < nTxLock - 1; k++)
  		TxLock[k].next = k + 1;
  	TxLock[k].next = 0;
  	init_waitqueue_head(&TxAnchor.freelockwait);
  	init_waitqueue_head(&TxAnchor.lowlockwait);
  
  	TxAnchor.freelock = 1;
  	TxAnchor.tlocksInUse = 0;
  	INIT_LIST_HEAD(&TxAnchor.anon_list);
  	INIT_LIST_HEAD(&TxAnchor.anon_list2);
  
  	LAZY_LOCK_INIT();
  	INIT_LIST_HEAD(&TxAnchor.unlock_queue);
  
  	stattx.maxlid = 1;	/* statistics */
  
  	return 0;
  }
  
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
332
   * NAME:	txExit()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
333
   *
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
334
   * FUNCTION:	clean up when module is unloaded
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
335
336
337
338
339
340
341
342
   */
  void txExit(void)
  {
  	vfree(TxLock);
  	TxLock = NULL;
  	vfree(TxBlock);
  	TxBlock = NULL;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
343
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
344
   * NAME:	txBegin()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
345
   *
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
346
   * FUNCTION:	start a transaction.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
347
   *
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
348
349
   * PARAMETER:	sb	- superblock
   *		flag	- force for nested tx;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
   *
   * RETURN:	tid	- transaction id
   *
   * note: flag force allows to start tx for nested tx
   * to prevent deadlock on logsync barrier;
   */
  tid_t txBegin(struct super_block *sb, int flag)
  {
  	tid_t t;
  	struct tblock *tblk;
  	struct jfs_log *log;
  
  	jfs_info("txBegin: flag = 0x%x", flag);
  	log = JFS_SBI(sb)->log;
  
  	TXN_LOCK();
  
  	INCREMENT(TxStat.txBegin);
  
        retry:
  	if (!(flag & COMMIT_FORCE)) {
  		/*
  		 * synchronize with logsync barrier
  		 */
  		if (test_bit(log_SYNCBARRIER, &log->flag) ||
  		    test_bit(log_QUIESCE, &log->flag)) {
  			INCREMENT(TxStat.txBegin_barrier);
  			TXN_SLEEP(&log->syncwait);
  			goto retry;
  		}
  	}
  	if (flag == 0) {
  		/*
  		 * Don't begin transaction if we're getting starved for tlocks
  		 * unless COMMIT_FORCE or COMMIT_INODE (which may ultimately
  		 * free tlocks)
  		 */
  		if (TxAnchor.tlocksInUse > TxLockVHWM) {
  			INCREMENT(TxStat.txBegin_lockslow);
  			TXN_SLEEP(&TxAnchor.lowlockwait);
  			goto retry;
  		}
  	}
  
  	/*
  	 * allocate transaction id/block
  	 */
  	if ((t = TxAnchor.freetid) == 0) {
  		jfs_info("txBegin: waiting for free tid");
  		INCREMENT(TxStat.txBegin_freetid);
  		TXN_SLEEP(&TxAnchor.freewait);
  		goto retry;
  	}
  
  	tblk = tid_to_tblock(t);
  
  	if ((tblk->next == 0) && !(flag & COMMIT_FORCE)) {
  		/* Don't let a non-forced transaction take the last tblk */
  		jfs_info("txBegin: waiting for free tid");
  		INCREMENT(TxStat.txBegin_freetid);
  		TXN_SLEEP(&TxAnchor.freewait);
  		goto retry;
  	}
  
  	TxAnchor.freetid = tblk->next;
  
  	/*
  	 * initialize transaction
  	 */
  
  	/*
  	 * We can't zero the whole thing or we screw up another thread being
  	 * awakened after sleeping on tblk->waitor
  	 *
  	 * memset(tblk, 0, sizeof(struct tblock));
  	 */
  	tblk->next = tblk->last = tblk->xflag = tblk->flag = tblk->lsn = 0;
  
  	tblk->sb = sb;
  	++log->logtid;
  	tblk->logtid = log->logtid;
  
  	++log->active;
  
  	HIGHWATERMARK(stattx.maxtid, t);	/* statistics */
  	INCREMENT(stattx.ntid);	/* statistics */
  
  	TXN_UNLOCK();
  
  	jfs_info("txBegin: returning tid = %d", t);
  
  	return t;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
443
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
444
   * NAME:	txBeginAnon()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
445
   *
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
446
   * FUNCTION:	start an anonymous transaction.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
447
448
449
   *		Blocks if logsync or available tlocks are low to prevent
   *		anonymous tlocks from depleting supply.
   *
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
450
   * PARAMETER:	sb	- superblock
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
   *
   * RETURN:	none
   */
  void txBeginAnon(struct super_block *sb)
  {
  	struct jfs_log *log;
  
  	log = JFS_SBI(sb)->log;
  
  	TXN_LOCK();
  	INCREMENT(TxStat.txBeginAnon);
  
        retry:
  	/*
  	 * synchronize with logsync barrier
  	 */
  	if (test_bit(log_SYNCBARRIER, &log->flag) ||
  	    test_bit(log_QUIESCE, &log->flag)) {
  		INCREMENT(TxStat.txBeginAnon_barrier);
  		TXN_SLEEP(&log->syncwait);
  		goto retry;
  	}
  
  	/*
  	 * Don't begin transaction if we're getting starved for tlocks
  	 */
  	if (TxAnchor.tlocksInUse > TxLockVHWM) {
  		INCREMENT(TxStat.txBeginAnon_lockslow);
  		TXN_SLEEP(&TxAnchor.lowlockwait);
  		goto retry;
  	}
  	TXN_UNLOCK();
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
484
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
485
   *	txEnd()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
486
487
488
   *
   * function: free specified transaction block.
   *
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
489
   *	logsync barrier processing:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
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
   *
   * serialization:
   */
  void txEnd(tid_t tid)
  {
  	struct tblock *tblk = tid_to_tblock(tid);
  	struct jfs_log *log;
  
  	jfs_info("txEnd: tid = %d", tid);
  	TXN_LOCK();
  
  	/*
  	 * wakeup transactions waiting on the page locked
  	 * by the current transaction
  	 */
  	TXN_WAKEUP(&tblk->waitor);
  
  	log = JFS_SBI(tblk->sb)->log;
  
  	/*
  	 * Lazy commit thread can't free this guy until we mark it UNLOCKED,
  	 * otherwise, we would be left with a transaction that may have been
  	 * reused.
  	 *
  	 * Lazy commit thread will turn off tblkGC_LAZY before calling this
  	 * routine.
  	 */
  	if (tblk->flag & tblkGC_LAZY) {
  		jfs_info("txEnd called w/lazy tid: %d, tblk = 0x%p", tid, tblk);
  		TXN_UNLOCK();
  
  		spin_lock_irq(&log->gclock);	// LOGGC_LOCK
  		tblk->flag |= tblkGC_UNLOCKED;
  		spin_unlock_irq(&log->gclock);	// LOGGC_UNLOCK
  		return;
  	}
  
  	jfs_info("txEnd: tid: %d, tblk = 0x%p", tid, tblk);
  
  	assert(tblk->next == 0);
  
  	/*
  	 * insert tblock back on freelist
  	 */
  	tblk->next = TxAnchor.freetid;
  	TxAnchor.freetid = tid;
  
  	/*
  	 * mark the tblock not active
  	 */
  	if (--log->active == 0) {
  		clear_bit(log_FLUSH, &log->flag);
  
  		/*
  		 * synchronize with logsync barrier
  		 */
  		if (test_bit(log_SYNCBARRIER, &log->flag)) {
cbc3d65eb   Dave Kleikamp   JFS: Improve sync...
547
548
549
550
  			TXN_UNLOCK();
  
  			/* write dirty metadata & forward log syncpt */
  			jfs_syncpt(log, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
551
552
553
554
555
556
557
  			jfs_info("log barrier off: 0x%x", log->lsn);
  
  			/* enable new transactions start */
  			clear_bit(log_SYNCBARRIER, &log->flag);
  
  			/* wakeup all waitors for logsync barrier */
  			TXN_WAKEUP(&log->syncwait);
1c6278295   Dave Kleikamp   [PATCH] JFS: Writ...
558

1c6278295   Dave Kleikamp   [PATCH] JFS: Writ...
559
  			goto wakeup;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
560
561
  		}
  	}
1c6278295   Dave Kleikamp   [PATCH] JFS: Writ...
562
563
  	TXN_UNLOCK();
  wakeup:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
564
565
566
567
  	/*
  	 * wakeup all waitors for a free tblock
  	 */
  	TXN_WAKEUP(&TxAnchor.freewait);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
568
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
569
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
570
   *	txLock()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
571
572
573
574
575
   *
   * function: acquire a transaction lock on the specified <mp>
   *
   * parameter:
   *
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
576
   * return:	transaction lock id
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
   *
   * serialization:
   */
  struct tlock *txLock(tid_t tid, struct inode *ip, struct metapage * mp,
  		     int type)
  {
  	struct jfs_inode_info *jfs_ip = JFS_IP(ip);
  	int dir_xtree = 0;
  	lid_t lid;
  	tid_t xtid;
  	struct tlock *tlck;
  	struct xtlock *xtlck;
  	struct linelock *linelock;
  	xtpage_t *p;
  	struct tblock *tblk;
  
  	TXN_LOCK();
  
  	if (S_ISDIR(ip->i_mode) && (type & tlckXTREE) &&
  	    !(mp->xflag & COMMIT_PAGE)) {
  		/*
  		 * Directory inode is special.  It can have both an xtree tlock
  		 * and a dtree tlock associated with it.
  		 */
  		dir_xtree = 1;
  		lid = jfs_ip->xtlid;
  	} else
  		lid = mp->lid;
  
  	/* is page not locked by a transaction ? */
  	if (lid == 0)
  		goto allocateLock;
  
  	jfs_info("txLock: tid:%d ip:0x%p mp:0x%p lid:%d", tid, ip, mp, lid);
  
  	/* is page locked by the requester transaction ? */
  	tlck = lid_to_tlock(lid);
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
614
615
  	if ((xtid = tlck->tid) == tid) {
  		TXN_UNLOCK();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
616
  		goto grantLock;
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
617
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
618
619
620
621
622
623
624
625
626
627
  
  	/*
  	 * is page locked by anonymous transaction/lock ?
  	 *
  	 * (page update without transaction (i.e., file write) is
  	 * locked under anonymous transaction tid = 0:
  	 * anonymous tlocks maintained on anonymous tlock list of
  	 * the inode of the page and available to all anonymous
  	 * transactions until txCommit() time at which point
  	 * they are transferred to the transaction tlock list of
25985edce   Lucas De Marchi   Fix common misspe...
628
  	 * the committing transaction of the inode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
629
630
631
  	 */
  	if (xtid == 0) {
  		tlck->tid = tid;
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
632
  		TXN_UNLOCK();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
  		tblk = tid_to_tblock(tid);
  		/*
  		 * The order of the tlocks in the transaction is important
  		 * (during truncate, child xtree pages must be freed before
  		 * parent's tlocks change the working map).
  		 * Take tlock off anonymous list and add to tail of
  		 * transaction list
  		 *
  		 * Note:  We really need to get rid of the tid & lid and
  		 * use list_head's.  This code is getting UGLY!
  		 */
  		if (jfs_ip->atlhead == lid) {
  			if (jfs_ip->atltail == lid) {
  				/* only anonymous txn.
  				 * Remove from anon_list
  				 */
8a9cd6d67   Dave Kleikamp   JFS: Fix race in ...
649
  				TXN_LOCK();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
650
  				list_del_init(&jfs_ip->anon_inode_list);
8a9cd6d67   Dave Kleikamp   JFS: Fix race in ...
651
  				TXN_UNLOCK();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
  			}
  			jfs_ip->atlhead = tlck->next;
  		} else {
  			lid_t last;
  			for (last = jfs_ip->atlhead;
  			     lid_to_tlock(last)->next != lid;
  			     last = lid_to_tlock(last)->next) {
  				assert(last);
  			}
  			lid_to_tlock(last)->next = tlck->next;
  			if (jfs_ip->atltail == lid)
  				jfs_ip->atltail = last;
  		}
  
  		/* insert the tlock at tail of transaction tlock list */
  
  		if (tblk->next)
  			lid_to_tlock(tblk->last)->next = lid;
  		else
  			tblk->next = lid;
  		tlck->next = 0;
  		tblk->last = lid;
  
  		goto grantLock;
  	}
  
  	goto waitLock;
  
  	/*
  	 * allocate a tlock
  	 */
        allocateLock:
  	lid = txLockAlloc();
  	tlck = lid_to_tlock(lid);
  
  	/*
  	 * initialize tlock
  	 */
  	tlck->tid = tid;
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
691
  	TXN_UNLOCK();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
692
693
694
695
696
697
  	/* mark tlock for meta-data page */
  	if (mp->xflag & COMMIT_PAGE) {
  
  		tlck->flag = tlckPAGELOCK;
  
  		/* mark the page dirty and nohomeok */
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
698
  		metapage_nohomeok(mp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
699
700
  
  		jfs_info("locking mp = 0x%p, nohomeok = %d tid = %d tlck = 0x%p",
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
701
  			 mp, mp->nohomeok, tid, tlck);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
702
703
704
705
706
707
708
709
710
711
712
713
  
  		/* if anonymous transaction, and buffer is on the group
  		 * commit synclist, mark inode to show this.  This will
  		 * prevent the buffer from being marked nohomeok for too
  		 * long a time.
  		 */
  		if ((tid == 0) && mp->lsn)
  			set_cflag(COMMIT_Synclist, ip);
  	}
  	/* mark tlock for in-memory inode */
  	else
  		tlck->flag = tlckINODELOCK;
438282d85   Dave Kleikamp   JFS: don't derefe...
714
715
  	if (S_ISDIR(ip->i_mode))
  		tlck->flag |= tlckDIRECTORY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
  	tlck->type = 0;
  
  	/* bind the tlock and the page */
  	tlck->ip = ip;
  	tlck->mp = mp;
  	if (dir_xtree)
  		jfs_ip->xtlid = lid;
  	else
  		mp->lid = lid;
  
  	/*
  	 * enqueue transaction lock to transaction/inode
  	 */
  	/* insert the tlock at tail of transaction tlock list */
  	if (tid) {
  		tblk = tid_to_tblock(tid);
  		if (tblk->next)
  			lid_to_tlock(tblk->last)->next = lid;
  		else
  			tblk->next = lid;
  		tlck->next = 0;
  		tblk->last = lid;
  	}
  	/* anonymous transaction:
  	 * insert the tlock at head of inode anonymous tlock list
  	 */
  	else {
  		tlck->next = jfs_ip->atlhead;
  		jfs_ip->atlhead = lid;
  		if (tlck->next == 0) {
  			/* This inode's first anonymous transaction */
  			jfs_ip->atltail = lid;
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
748
  			TXN_LOCK();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
749
750
  			list_add_tail(&jfs_ip->anon_inode_list,
  				      &TxAnchor.anon_list);
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
751
  			TXN_UNLOCK();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
  		}
  	}
  
  	/* initialize type dependent area for linelock */
  	linelock = (struct linelock *) & tlck->lock;
  	linelock->next = 0;
  	linelock->flag = tlckLINELOCK;
  	linelock->maxcnt = TLOCKSHORT;
  	linelock->index = 0;
  
  	switch (type & tlckTYPE) {
  	case tlckDTREE:
  		linelock->l2linesize = L2DTSLOTSIZE;
  		break;
  
  	case tlckXTREE:
  		linelock->l2linesize = L2XTSLOTSIZE;
  
  		xtlck = (struct xtlock *) linelock;
  		xtlck->header.offset = 0;
  		xtlck->header.length = 2;
  
  		if (type & tlckNEW) {
  			xtlck->lwm.offset = XTENTRYSTART;
  		} else {
  			if (mp->xflag & COMMIT_PAGE)
  				p = (xtpage_t *) mp->data;
  			else
  				p = &jfs_ip->i_xtroot;
  			xtlck->lwm.offset =
  			    le16_to_cpu(p->header.nextindex);
  		}
  		xtlck->lwm.length = 0;	/* ! */
  		xtlck->twm.offset = 0;
  		xtlck->hwm.offset = 0;
  
  		xtlck->index = 2;
  		break;
  
  	case tlckINODE:
  		linelock->l2linesize = L2INODESLOTSIZE;
  		break;
  
  	case tlckDATA:
  		linelock->l2linesize = L2DATASLOTSIZE;
  		break;
  
  	default:
  		jfs_err("UFO tlock:0x%p", tlck);
  	}
  
  	/*
  	 * update tlock vector
  	 */
        grantLock:
  	tlck->type |= type;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
808
809
810
811
812
813
814
815
816
  	return tlck;
  
  	/*
  	 * page is being locked by another transaction:
  	 */
        waitLock:
  	/* Only locks on ipimap or ipaimap should reach here */
  	/* assert(jfs_ip->fileset == AGGREGATE_I); */
  	if (jfs_ip->fileset != AGGREGATE_I) {
209e101bf   Dave Kleikamp   JFS: use print_he...
817
  		printk(KERN_ERR "txLock: trying to lock locked page!");
288e4d838   Dave Kleikamp   JFS: Update print...
818
819
820
821
822
823
824
825
826
  		print_hex_dump(KERN_ERR, "ip: ", DUMP_PREFIX_ADDRESS, 16, 4,
  			       ip, sizeof(*ip), 0);
  		print_hex_dump(KERN_ERR, "mp: ", DUMP_PREFIX_ADDRESS, 16, 4,
  			       mp, sizeof(*mp), 0);
  		print_hex_dump(KERN_ERR, "Locker's tblock: ",
  			       DUMP_PREFIX_ADDRESS, 16, 4, tid_to_tblock(tid),
  			       sizeof(struct tblock), 0);
  		print_hex_dump(KERN_ERR, "Tlock: ", DUMP_PREFIX_ADDRESS, 16, 4,
  			       tlck, sizeof(*tlck), 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
827
828
829
  		BUG();
  	}
  	INCREMENT(stattx.waitlock);	/* statistics */
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
830
  	TXN_UNLOCK();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
831
  	release_metapage(mp);
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
832
  	TXN_LOCK();
0418726bb   Adrian Bunk   typo fixes: aquir...
833
  	xtid = tlck->tid;	/* reacquire after dropping TXN_LOCK */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
834
835
836
  
  	jfs_info("txLock: in waitLock, tid = %d, xtid = %d, lid = %d",
  		 tid, xtid, lid);
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
837
838
839
840
841
842
  
  	/* Recheck everything since dropping TXN_LOCK */
  	if (xtid && (tlck->mp == mp) && (mp->lid == lid))
  		TXN_SLEEP_DROP_LOCK(&tid_to_tblock(xtid)->waitor);
  	else
  		TXN_UNLOCK();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
843
844
845
846
  	jfs_info("txLock: awakened     tid = %d, lid = %d", tid, lid);
  
  	return NULL;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
847
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
848
   * NAME:	txRelease()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
849
   *
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
850
   * FUNCTION:	Release buffers associated with transaction locks, but don't
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
851
852
853
854
855
   *		mark homeok yet.  The allows other transactions to modify
   *		buffers, but won't let them go to disk until commit record
   *		actually gets written.
   *
   * PARAMETER:
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
856
   *		tblk	-
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
857
   *
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
858
   * RETURN:	Errors from subroutines.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
   */
  static void txRelease(struct tblock * tblk)
  {
  	struct metapage *mp;
  	lid_t lid;
  	struct tlock *tlck;
  
  	TXN_LOCK();
  
  	for (lid = tblk->next; lid; lid = tlck->next) {
  		tlck = lid_to_tlock(lid);
  		if ((mp = tlck->mp) != NULL &&
  		    (tlck->type & tlckBTROOT) == 0) {
  			assert(mp->xflag & COMMIT_PAGE);
  			mp->lid = 0;
  		}
  	}
  
  	/*
  	 * wakeup transactions waiting on a page locked
  	 * by the current transaction
  	 */
  	TXN_WAKEUP(&tblk->waitor);
  
  	TXN_UNLOCK();
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
885
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
886
   * NAME:	txUnlock()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
887
   *
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
888
889
   * FUNCTION:	Initiates pageout of pages modified by tid in journalled
   *		objects and frees their lockwords.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
890
891
892
893
894
895
896
897
898
   */
  static void txUnlock(struct tblock * tblk)
  {
  	struct tlock *tlck;
  	struct linelock *linelock;
  	lid_t lid, next, llid, k;
  	struct metapage *mp;
  	struct jfs_log *log;
  	int difft, diffp;
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
899
  	unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
  
  	jfs_info("txUnlock: tblk = 0x%p", tblk);
  	log = JFS_SBI(tblk->sb)->log;
  
  	/*
  	 * mark page under tlock homeok (its log has been written):
  	 */
  	for (lid = tblk->next; lid; lid = next) {
  		tlck = lid_to_tlock(lid);
  		next = tlck->next;
  
  		jfs_info("unlocking lid = %d, tlck = 0x%p", lid, tlck);
  
  		/* unbind page from tlock */
  		if ((mp = tlck->mp) != NULL &&
  		    (tlck->type & tlckBTROOT) == 0) {
  			assert(mp->xflag & COMMIT_PAGE);
  
  			/* hold buffer
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
919
  			 */
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
920
  			hold_metapage(mp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
921

7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
922
923
  			assert(mp->nohomeok > 0);
  			_metapage_homeok(mp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
924
925
  
  			/* inherit younger/larger clsn */
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
926
  			LOGSYNC_LOCK(log, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
927
928
929
930
931
932
933
  			if (mp->clsn) {
  				logdiff(difft, tblk->clsn, log);
  				logdiff(diffp, mp->clsn, log);
  				if (difft > diffp)
  					mp->clsn = tblk->clsn;
  			} else
  				mp->clsn = tblk->clsn;
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
934
  			LOGSYNC_UNLOCK(log, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
935
936
  
  			assert(!(tlck->flag & tlckFREEPAGE));
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
937
  			put_metapage(mp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
  		}
  
  		/* insert tlock, and linelock(s) of the tlock if any,
  		 * at head of freelist
  		 */
  		TXN_LOCK();
  
  		llid = ((struct linelock *) & tlck->lock)->next;
  		while (llid) {
  			linelock = (struct linelock *) lid_to_tlock(llid);
  			k = linelock->next;
  			txLockFree(llid);
  			llid = k;
  		}
  		txLockFree(lid);
  
  		TXN_UNLOCK();
  	}
  	tblk->next = tblk->last = 0;
  
  	/*
  	 * remove tblock from logsynclist
  	 * (allocation map pages inherited lsn of tblk and
  	 * has been inserted in logsync list at txUpdateMap())
  	 */
  	if (tblk->lsn) {
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
964
  		LOGSYNC_LOCK(log, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
965
966
  		log->count--;
  		list_del(&tblk->synclist);
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
967
  		LOGSYNC_UNLOCK(log, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
968
969
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
970
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
971
   *	txMaplock()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
972
973
   *
   * function: allocate a transaction lock for freed page/entry;
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
974
   *	for freed page, maplock is used as xtlock/dtlock type;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
   */
  struct tlock *txMaplock(tid_t tid, struct inode *ip, int type)
  {
  	struct jfs_inode_info *jfs_ip = JFS_IP(ip);
  	lid_t lid;
  	struct tblock *tblk;
  	struct tlock *tlck;
  	struct maplock *maplock;
  
  	TXN_LOCK();
  
  	/*
  	 * allocate a tlock
  	 */
  	lid = txLockAlloc();
  	tlck = lid_to_tlock(lid);
  
  	/*
  	 * initialize tlock
  	 */
  	tlck->tid = tid;
  
  	/* bind the tlock and the object */
  	tlck->flag = tlckINODELOCK;
438282d85   Dave Kleikamp   JFS: don't derefe...
999
1000
  	if (S_ISDIR(ip->i_mode))
  		tlck->flag |= tlckDIRECTORY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
  	tlck->ip = ip;
  	tlck->mp = NULL;
  
  	tlck->type = type;
  
  	/*
  	 * enqueue transaction lock to transaction/inode
  	 */
  	/* insert the tlock at tail of transaction tlock list */
  	if (tid) {
  		tblk = tid_to_tblock(tid);
  		if (tblk->next)
  			lid_to_tlock(tblk->last)->next = lid;
  		else
  			tblk->next = lid;
  		tlck->next = 0;
  		tblk->last = lid;
  	}
  	/* anonymous transaction:
  	 * insert the tlock at head of inode anonymous tlock list
  	 */
  	else {
  		tlck->next = jfs_ip->atlhead;
  		jfs_ip->atlhead = lid;
  		if (tlck->next == 0) {
  			/* This inode's first anonymous transaction */
  			jfs_ip->atltail = lid;
  			list_add_tail(&jfs_ip->anon_inode_list,
  				      &TxAnchor.anon_list);
  		}
  	}
  
  	TXN_UNLOCK();
  
  	/* initialize type dependent area for maplock */
  	maplock = (struct maplock *) & tlck->lock;
  	maplock->next = 0;
  	maplock->maxcnt = 0;
  	maplock->index = 0;
  
  	return tlck;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1043
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1044
   *	txLinelock()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
   *
   * function: allocate a transaction lock for log vector list
   */
  struct linelock *txLinelock(struct linelock * tlock)
  {
  	lid_t lid;
  	struct tlock *tlck;
  	struct linelock *linelock;
  
  	TXN_LOCK();
  
  	/* allocate a TxLock structure */
  	lid = txLockAlloc();
  	tlck = lid_to_tlock(lid);
  
  	TXN_UNLOCK();
  
  	/* initialize linelock */
  	linelock = (struct linelock *) tlck;
  	linelock->next = 0;
  	linelock->flag = tlckLINELOCK;
  	linelock->maxcnt = TLOCKLONG;
  	linelock->index = 0;
438282d85   Dave Kleikamp   JFS: don't derefe...
1068
1069
  	if (tlck->flag & tlckDIRECTORY)
  		linelock->flag |= tlckDIRECTORY;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1070
1071
1072
1073
1074
1075
1076
  
  	/* append linelock after tlock */
  	linelock->next = tlock->next;
  	tlock->next = lid;
  
  	return linelock;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1077
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1078
1079
   *		transaction commit management
   *		-----------------------------
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1080
1081
1082
   */
  
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
   * NAME:	txCommit()
   *
   * FUNCTION:	commit the changes to the objects specified in
   *		clist.  For journalled segments only the
   *		changes of the caller are committed, ie by tid.
   *		for non-journalled segments the data are flushed to
   *		disk and then the change to the disk inode and indirect
   *		blocks committed (so blocks newly allocated to the
   *		segment will be made a part of the segment atomically).
   *
   *		all of the segments specified in clist must be in
   *		one file system. no more than 6 segments are needed
   *		to handle all unix svcs.
   *
   *		if the i_nlink field (i.e. disk inode link count)
   *		is zero, and the type of inode is a regular file or
   *		directory, or symbolic link , the inode is truncated
   *		to zero length. the truncation is committed but the
   *		VM resources are unaffected until it is closed (see
   *		iput and iclose).
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1103
1104
1105
1106
1107
1108
   *
   * PARAMETER:
   *
   * RETURN:
   *
   * serialization:
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1109
1110
   *		on entry the inode lock on each segment is assumed
   *		to be held.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
   *
   * i/o error:
   */
  int txCommit(tid_t tid,		/* transaction identifier */
  	     int nip,		/* number of inodes to commit */
  	     struct inode **iplist,	/* list of inode to commit */
  	     int flag)
  {
  	int rc = 0;
  	struct commit cd;
  	struct jfs_log *log;
  	struct tblock *tblk;
  	struct lrd *lrd;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
  	struct inode *ip;
  	struct jfs_inode_info *jfs_ip;
  	int k, n;
  	ino_t top;
  	struct super_block *sb;
  
  	jfs_info("txCommit, tid = %d, flag = %d", tid, flag);
  	/* is read-only file system ? */
  	if (isReadOnly(iplist[0])) {
  		rc = -EROFS;
  		goto TheEnd;
  	}
  
  	sb = cd.sb = iplist[0]->i_sb;
  	cd.tid = tid;
  
  	if (tid == 0)
  		tid = txBegin(sb, 0);
  	tblk = tid_to_tblock(tid);
  
  	/*
  	 * initialize commit structure
  	 */
  	log = JFS_SBI(sb)->log;
  	cd.log = log;
  
  	/* initialize log record descriptor in commit */
  	lrd = &cd.lrd;
  	lrd->logtid = cpu_to_le32(tblk->logtid);
  	lrd->backchain = 0;
  
  	tblk->xflag |= flag;
  
  	if ((flag & (COMMIT_FORCE | COMMIT_SYNC)) == 0)
  		tblk->xflag |= COMMIT_LAZY;
  	/*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1160
  	 *	prepare non-journaled objects for commit
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
  	 *
  	 * flush data pages of non-journaled file
  	 * to prevent the file getting non-initialized disk blocks
  	 * in case of crash.
  	 * (new blocks - )
  	 */
  	cd.iplist = iplist;
  	cd.nip = nip;
  
  	/*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1171
  	 *	acquire transaction lock on (on-disk) inodes
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
  	 *
  	 * update on-disk inode from in-memory inode
  	 * acquiring transaction locks for AFTER records
  	 * on the on-disk inode of file object
  	 *
  	 * sort the inodes array by inode number in descending order
  	 * to prevent deadlock when acquiring transaction lock
  	 * of on-disk inodes on multiple on-disk inode pages by
  	 * multiple concurrent transactions
  	 */
  	for (k = 0; k < cd.nip; k++) {
  		top = (cd.iplist[k])->i_ino;
  		for (n = k + 1; n < cd.nip; n++) {
  			ip = cd.iplist[n];
  			if (ip->i_ino > top) {
  				top = ip->i_ino;
  				cd.iplist[n] = cd.iplist[k];
  				cd.iplist[k] = ip;
  			}
  		}
  
  		ip = cd.iplist[k];
  		jfs_ip = JFS_IP(ip);
  
  		/*
  		 * BUGBUG - This code has temporarily been removed.  The
  		 * intent is to ensure that any file data is written before
  		 * the metadata is committed to the journal.  This prevents
  		 * uninitialized data from appearing in a file after the
  		 * journal has been replayed.  (The uninitialized data
  		 * could be sensitive data removed by another user.)
  		 *
  		 * The problem now is that we are holding the IWRITELOCK
  		 * on the inode, and calling filemap_fdatawrite on an
  		 * unmapped page will cause a deadlock in jfs_get_block.
  		 *
  		 * The long term solution is to pare down the use of
  		 * IWRITELOCK.  We are currently holding it too long.
  		 * We could also be smarter about which data pages need
  		 * to be written before the transaction is committed and
  		 * when we don't need to worry about it at all.
  		 *
  		 * if ((!S_ISDIR(ip->i_mode))
28fd12982   OGAWA Hirofumi   [PATCH] Fix and a...
1215
1216
  		 *    && (tblk->flag & COMMIT_DELETE) == 0)
  		 *	filemap_write_and_wait(ip->i_mapping);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
  		 */
  
  		/*
  		 * Mark inode as not dirty.  It will still be on the dirty
  		 * inode list, but we'll know not to commit it again unless
  		 * it gets marked dirty again
  		 */
  		clear_cflag(COMMIT_Dirty, ip);
  
  		/* inherit anonymous tlock(s) of inode */
  		if (jfs_ip->atlhead) {
  			lid_to_tlock(jfs_ip->atltail)->next = tblk->next;
  			tblk->next = jfs_ip->atlhead;
  			if (!tblk->last)
  				tblk->last = jfs_ip->atltail;
  			jfs_ip->atlhead = jfs_ip->atltail = 0;
  			TXN_LOCK();
  			list_del_init(&jfs_ip->anon_inode_list);
  			TXN_UNLOCK();
  		}
  
  		/*
  		 * acquire transaction lock on on-disk inode page
  		 * (become first tlock of the tblk's tlock list)
  		 */
  		if (((rc = diWrite(tid, ip))))
  			goto out;
  	}
  
  	/*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1247
  	 *	write log records from transaction locks
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
  	 *
  	 * txUpdateMap() resets XAD_NEW in XAD.
  	 */
  	if ((rc = txLog(log, tblk, &cd)))
  		goto TheEnd;
  
  	/*
  	 * Ensure that inode isn't reused before
  	 * lazy commit thread finishes processing
  	 */
  	if (tblk->xflag & COMMIT_DELETE) {
7de9c6ee3   Al Viro   new helper: ihold()
1259
  		ihold(tblk->u.ip);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
  		/*
  		 * Avoid a rare deadlock
  		 *
  		 * If the inode is locked, we may be blocked in
  		 * jfs_commit_inode.  If so, we don't want the
  		 * lazy_commit thread doing the last iput() on the inode
  		 * since that may block on the locked inode.  Instead,
  		 * commit the transaction synchronously, so the last iput
  		 * will be done by the calling thread (or later)
  		 */
1c0eeaf56   Joern Engel   introduce I_SYNC
1270
1271
  		/*
  		 * I believe this code is no longer needed.  Splitting I_LOCK
eaff8079d   Christoph Hellwig   kill I_LOCK
1272
  		 * into two bits, I_NEW and I_SYNC should prevent this
1c0eeaf56   Joern Engel   introduce I_SYNC
1273
1274
1275
1276
1277
  		 * deadlock as well.  But since I don't have a JFS testload
  		 * to verify this, only a trivial s/I_LOCK/I_SYNC/ was done.
  		 * Joern
  		 */
  		if (tblk->u.ip->i_state & I_SYNC)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1278
1279
1280
1281
1282
1283
1284
1285
  			tblk->xflag &= ~COMMIT_LAZY;
  	}
  
  	ASSERT((!(tblk->xflag & COMMIT_DELETE)) ||
  	       ((tblk->u.ip->i_nlink == 0) &&
  		!test_cflag(COMMIT_Nolink, tblk->u.ip)));
  
  	/*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1286
  	 *	write COMMIT log record
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1287
1288
1289
  	 */
  	lrd->type = cpu_to_le16(LOG_COMMIT);
  	lrd->length = 0;
3c2c22628   Dave Kleikamp   jfs: clean up som...
1290
  	lmLog(log, tblk, lrd, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1291
1292
1293
1294
  
  	lmGroupCommit(log, tblk);
  
  	/*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1295
  	 *	- transaction is now committed -
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
  	 */
  
  	/*
  	 * force pages in careful update
  	 * (imap addressing structure update)
  	 */
  	if (flag & COMMIT_FORCE)
  		txForce(tblk);
  
  	/*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1306
  	 *	update allocation map.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1307
1308
1309
  	 *
  	 * update inode allocation map and inode:
  	 * free pager lock on memory object of inode if any.
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1310
  	 * update block allocation map.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1311
1312
1313
1314
1315
1316
1317
  	 *
  	 * txUpdateMap() resets XAD_NEW in XAD.
  	 */
  	if (tblk->xflag & COMMIT_FORCE)
  		txUpdateMap(tblk);
  
  	/*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1318
  	 *	free transaction locks and pageout/free pages
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1319
1320
1321
1322
1323
1324
1325
1326
  	 */
  	txRelease(tblk);
  
  	if ((tblk->flag & tblkGC_LAZY) == 0)
  		txUnlock(tblk);
  
  
  	/*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1327
  	 *	reset in-memory object state
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
  	 */
  	for (k = 0; k < cd.nip; k++) {
  		ip = cd.iplist[k];
  		jfs_ip = JFS_IP(ip);
  
  		/*
  		 * reset in-memory inode state
  		 */
  		jfs_ip->bxflag = 0;
  		jfs_ip->blid = 0;
  	}
  
        out:
  	if (rc != 0)
  		txAbort(tid, 1);
  
        TheEnd:
  	jfs_info("txCommit: tid = %d, returning %d", tid, rc);
  	return rc;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1348
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1349
   * NAME:	txLog()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1350
   *
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1351
1352
1353
   * FUNCTION:	Writes AFTER log records for all lines modified
   *		by tid for segments specified by inodes in comdata.
   *		Code assumes only WRITELOCKS are recorded in lockwords.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
   *
   * PARAMETERS:
   *
   * RETURN :
   */
  static int txLog(struct jfs_log * log, struct tblock * tblk, struct commit * cd)
  {
  	int rc = 0;
  	struct inode *ip;
  	lid_t lid;
  	struct tlock *tlck;
  	struct lrd *lrd = &cd->lrd;
  
  	/*
  	 * write log record(s) for each tlock of transaction,
  	 */
  	for (lid = tblk->next; lid; lid = tlck->next) {
  		tlck = lid_to_tlock(lid);
  
  		tlck->flag |= tlckLOG;
  
  		/* initialize lrd common */
  		ip = tlck->ip;
  		lrd->aggregate = cpu_to_le32(JFS_SBI(ip->i_sb)->aggregate);
  		lrd->log.redopage.fileset = cpu_to_le32(JFS_IP(ip)->fileset);
  		lrd->log.redopage.inode = cpu_to_le32(ip->i_ino);
  
  		/* write log record of page from the tlock */
  		switch (tlck->type & tlckTYPE) {
  		case tlckXTREE:
  			xtLog(log, tblk, lrd, tlck);
  			break;
  
  		case tlckDTREE:
  			dtLog(log, tblk, lrd, tlck);
  			break;
  
  		case tlckINODE:
  			diLog(log, tblk, lrd, tlck, cd);
  			break;
  
  		case tlckMAP:
  			mapLog(log, tblk, lrd, tlck);
  			break;
  
  		case tlckDATA:
  			dataLog(log, tblk, lrd, tlck);
  			break;
  
  		default:
  			jfs_err("UFO tlock:0x%p", tlck);
  		}
  	}
  
  	return rc;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1410
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1411
   *	diLog()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1412
   *
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1413
   * function:	log inode tlock and format maplock to update bmap;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1414
1415
   */
  static int diLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1416
  		 struct tlock * tlck, struct commit * cd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
  {
  	int rc = 0;
  	struct metapage *mp;
  	pxd_t *pxd;
  	struct pxd_lock *pxdlock;
  
  	mp = tlck->mp;
  
  	/* initialize as REDOPAGE record format */
  	lrd->log.redopage.type = cpu_to_le16(LOG_INODE);
  	lrd->log.redopage.l2linesize = cpu_to_le16(L2INODESLOTSIZE);
  
  	pxd = &lrd->log.redopage.pxd;
  
  	/*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1432
  	 *	inode after image
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1433
1434
1435
1436
  	 */
  	if (tlck->type & tlckENTRY) {
  		/* log after-image for logredo(): */
  		lrd->type = cpu_to_le16(LOG_REDOPAGE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1437
1438
1439
1440
1441
1442
1443
1444
1445
  		PXDaddress(pxd, mp->index);
  		PXDlength(pxd,
  			  mp->logical_size >> tblk->sb->s_blocksize_bits);
  		lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  
  		/* mark page as homeward bound */
  		tlck->flag |= tlckWRITEPAGE;
  	} else if (tlck->type & tlckFREE) {
  		/*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1446
  		 *	free inode extent
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
  		 *
  		 * (pages of the freed inode extent have been invalidated and
  		 * a maplock for free of the extent has been formatted at
  		 * txLock() time);
  		 *
  		 * the tlock had been acquired on the inode allocation map page
  		 * (iag) that specifies the freed extent, even though the map
  		 * page is not itself logged, to prevent pageout of the map
  		 * page before the log;
  		 */
  
  		/* log LOG_NOREDOINOEXT of the freed inode extent for
  		 * logredo() to start NoRedoPage filters, and to update
  		 * imap and bmap for free of the extent;
  		 */
  		lrd->type = cpu_to_le16(LOG_NOREDOINOEXT);
  		/*
  		 * For the LOG_NOREDOINOEXT record, we need
  		 * to pass the IAG number and inode extent
  		 * index (within that IAG) from which the
  		 * the extent being released.  These have been
  		 * passed to us in the iplist[1] and iplist[2].
  		 */
  		lrd->log.noredoinoext.iagnum =
  		    cpu_to_le32((u32) (size_t) cd->iplist[1]);
  		lrd->log.noredoinoext.inoext_idx =
  		    cpu_to_le32((u32) (size_t) cd->iplist[2]);
  
  		pxdlock = (struct pxd_lock *) & tlck->lock;
  		*pxd = pxdlock->pxd;
  		lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, NULL));
  
  		/* update bmap */
  		tlck->flag |= tlckUPDATEMAP;
  
  		/* mark page as homeward bound */
  		tlck->flag |= tlckWRITEPAGE;
  	} else
  		jfs_err("diLog: UFO type tlck:0x%p", tlck);
  #ifdef  _JFS_WIP
  	/*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1488
  	 *	alloc/free external EA extent
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
  	 *
  	 * a maplock for txUpdateMap() to update bPWMAP for alloc/free
  	 * of the extent has been formatted at txLock() time;
  	 */
  	else {
  		assert(tlck->type & tlckEA);
  
  		/* log LOG_UPDATEMAP for logredo() to update bmap for
  		 * alloc of new (and free of old) external EA extent;
  		 */
  		lrd->type = cpu_to_le16(LOG_UPDATEMAP);
  		pxdlock = (struct pxd_lock *) & tlck->lock;
  		nlock = pxdlock->index;
  		for (i = 0; i < nlock; i++, pxdlock++) {
  			if (pxdlock->flag & mlckALLOCPXD)
  				lrd->log.updatemap.type =
  				    cpu_to_le16(LOG_ALLOCPXD);
  			else
  				lrd->log.updatemap.type =
  				    cpu_to_le16(LOG_FREEPXD);
  			lrd->log.updatemap.nxd = cpu_to_le16(1);
  			lrd->log.updatemap.pxd = pxdlock->pxd;
  			lrd->backchain =
  			    cpu_to_le32(lmLog(log, tblk, lrd, NULL));
  		}
  
  		/* update bmap */
  		tlck->flag |= tlckUPDATEMAP;
  	}
  #endif				/* _JFS_WIP */
  
  	return rc;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1522
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1523
   *	dataLog()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1524
   *
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1525
   * function:	log data tlock
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
   */
  static int dataLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
  	    struct tlock * tlck)
  {
  	struct metapage *mp;
  	pxd_t *pxd;
  
  	mp = tlck->mp;
  
  	/* initialize as REDOPAGE record format */
  	lrd->log.redopage.type = cpu_to_le16(LOG_DATA);
  	lrd->log.redopage.l2linesize = cpu_to_le16(L2DATASLOTSIZE);
  
  	pxd = &lrd->log.redopage.pxd;
  
  	/* log after-image for logredo(): */
  	lrd->type = cpu_to_le16(LOG_REDOPAGE);
  
  	if (jfs_dirtable_inline(tlck->ip)) {
  		/*
  		 * The table has been truncated, we've must have deleted
  		 * the last entry, so don't bother logging this
  		 */
  		mp->lid = 0;
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
1550
1551
  		grab_metapage(mp);
  		metapage_homeok(mp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
  		discard_metapage(mp);
  		tlck->mp = NULL;
  		return 0;
  	}
  
  	PXDaddress(pxd, mp->index);
  	PXDlength(pxd, mp->logical_size >> tblk->sb->s_blocksize_bits);
  
  	lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  
  	/* mark page as homeward bound */
  	tlck->flag |= tlckWRITEPAGE;
  
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1567
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1568
   *	dtLog()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1569
   *
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1570
   * function:	log dtree tlock and format maplock to update bmap;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
   */
  static void dtLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
  	   struct tlock * tlck)
  {
  	struct metapage *mp;
  	struct pxd_lock *pxdlock;
  	pxd_t *pxd;
  
  	mp = tlck->mp;
  
  	/* initialize as REDOPAGE/NOREDOPAGE record format */
  	lrd->log.redopage.type = cpu_to_le16(LOG_DTREE);
  	lrd->log.redopage.l2linesize = cpu_to_le16(L2DTSLOTSIZE);
  
  	pxd = &lrd->log.redopage.pxd;
  
  	if (tlck->type & tlckBTROOT)
  		lrd->log.redopage.type |= cpu_to_le16(LOG_BTROOT);
  
  	/*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1591
1592
1593
1594
  	 *	page extension via relocation: entry insertion;
  	 *	page extension in-place: entry insertion;
  	 *	new right page from page split, reinitialized in-line
  	 *	root from root page split: entry insertion;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
  	 */
  	if (tlck->type & (tlckNEW | tlckEXTEND)) {
  		/* log after-image of the new page for logredo():
  		 * mark log (LOG_NEW) for logredo() to initialize
  		 * freelist and update bmap for alloc of the new page;
  		 */
  		lrd->type = cpu_to_le16(LOG_REDOPAGE);
  		if (tlck->type & tlckEXTEND)
  			lrd->log.redopage.type |= cpu_to_le16(LOG_EXTEND);
  		else
  			lrd->log.redopage.type |= cpu_to_le16(LOG_NEW);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
  		PXDaddress(pxd, mp->index);
  		PXDlength(pxd,
  			  mp->logical_size >> tblk->sb->s_blocksize_bits);
  		lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  
  		/* format a maplock for txUpdateMap() to update bPMAP for
  		 * alloc of the new page;
  		 */
  		if (tlck->type & tlckBTROOT)
  			return;
  		tlck->flag |= tlckUPDATEMAP;
  		pxdlock = (struct pxd_lock *) & tlck->lock;
  		pxdlock->flag = mlckALLOCPXD;
  		pxdlock->pxd = *pxd;
  
  		pxdlock->index = 1;
  
  		/* mark page as homeward bound */
  		tlck->flag |= tlckWRITEPAGE;
  		return;
  	}
  
  	/*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1629
1630
  	 *	entry insertion/deletion,
  	 *	sibling page link update (old right page before split);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
  	 */
  	if (tlck->type & (tlckENTRY | tlckRELINK)) {
  		/* log after-image for logredo(): */
  		lrd->type = cpu_to_le16(LOG_REDOPAGE);
  		PXDaddress(pxd, mp->index);
  		PXDlength(pxd,
  			  mp->logical_size >> tblk->sb->s_blocksize_bits);
  		lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  
  		/* mark page as homeward bound */
  		tlck->flag |= tlckWRITEPAGE;
  		return;
  	}
  
  	/*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1646
1647
  	 *	page deletion: page has been invalidated
  	 *	page relocation: source extent
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1648
  	 *
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1649
1650
  	 *	a maplock for free of the page has been formatted
  	 *	at txLock() time);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
  	 */
  	if (tlck->type & (tlckFREE | tlckRELOCATE)) {
  		/* log LOG_NOREDOPAGE of the deleted page for logredo()
  		 * to start NoRedoPage filter and to update bmap for free
  		 * of the deletd page
  		 */
  		lrd->type = cpu_to_le16(LOG_NOREDOPAGE);
  		pxdlock = (struct pxd_lock *) & tlck->lock;
  		*pxd = pxdlock->pxd;
  		lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, NULL));
  
  		/* a maplock for txUpdateMap() for free of the page
  		 * has been formatted at txLock() time;
  		 */
  		tlck->flag |= tlckUPDATEMAP;
  	}
  	return;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1669
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1670
   *	xtLog()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1671
   *
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1672
   * function:	log xtree tlock and format maplock to update bmap;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
   */
  static void xtLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
  	   struct tlock * tlck)
  {
  	struct inode *ip;
  	struct metapage *mp;
  	xtpage_t *p;
  	struct xtlock *xtlck;
  	struct maplock *maplock;
  	struct xdlistlock *xadlock;
  	struct pxd_lock *pxdlock;
66f3131f5   Dave Kleikamp   [PATCH] JFS: redu...
1684
  	pxd_t *page_pxd;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1685
1686
1687
1688
1689
1690
1691
1692
  	int next, lwm, hwm;
  
  	ip = tlck->ip;
  	mp = tlck->mp;
  
  	/* initialize as REDOPAGE/NOREDOPAGE record format */
  	lrd->log.redopage.type = cpu_to_le16(LOG_XTREE);
  	lrd->log.redopage.l2linesize = cpu_to_le16(L2XTSLOTSIZE);
66f3131f5   Dave Kleikamp   [PATCH] JFS: redu...
1693
  	page_pxd = &lrd->log.redopage.pxd;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
  
  	if (tlck->type & tlckBTROOT) {
  		lrd->log.redopage.type |= cpu_to_le16(LOG_BTROOT);
  		p = &JFS_IP(ip)->i_xtroot;
  		if (S_ISDIR(ip->i_mode))
  			lrd->log.redopage.type |=
  			    cpu_to_le16(LOG_DIR_XTREE);
  	} else
  		p = (xtpage_t *) mp->data;
  	next = le16_to_cpu(p->header.nextindex);
  
  	xtlck = (struct xtlock *) & tlck->lock;
  
  	maplock = (struct maplock *) & tlck->lock;
  	xadlock = (struct xdlistlock *) maplock;
  
  	/*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1711
1712
  	 *	entry insertion/extension;
  	 *	sibling page link update (old right page before split);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
  	 */
  	if (tlck->type & (tlckNEW | tlckGROW | tlckRELINK)) {
  		/* log after-image for logredo():
  		 * logredo() will update bmap for alloc of new/extended
  		 * extents (XAD_NEW|XAD_EXTEND) of XAD[lwm:next) from
  		 * after-image of XADlist;
  		 * logredo() resets (XAD_NEW|XAD_EXTEND) flag when
  		 * applying the after-image to the meta-data page.
  		 */
  		lrd->type = cpu_to_le16(LOG_REDOPAGE);
66f3131f5   Dave Kleikamp   [PATCH] JFS: redu...
1723
1724
  		PXDaddress(page_pxd, mp->index);
  		PXDlength(page_pxd,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
  			  mp->logical_size >> tblk->sb->s_blocksize_bits);
  		lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  
  		/* format a maplock for txUpdateMap() to update bPMAP
  		 * for alloc of new/extended extents of XAD[lwm:next)
  		 * from the page itself;
  		 * txUpdateMap() resets (XAD_NEW|XAD_EXTEND) flag.
  		 */
  		lwm = xtlck->lwm.offset;
  		if (lwm == 0)
  			lwm = XTPAGEMAXSLOT;
  
  		if (lwm == next)
  			goto out;
  		if (lwm > next) {
  			jfs_err("xtLog: lwm > next
  ");
  			goto out;
  		}
  		tlck->flag |= tlckUPDATEMAP;
  		xadlock->flag = mlckALLOCXADLIST;
  		xadlock->count = next - lwm;
66f3131f5   Dave Kleikamp   [PATCH] JFS: redu...
1747
  		if ((xadlock->count <= 4) && (tblk->xflag & COMMIT_LAZY)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1748
  			int i;
66f3131f5   Dave Kleikamp   [PATCH] JFS: redu...
1749
  			pxd_t *pxd;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1750
1751
1752
1753
  			/*
  			 * Lazy commit may allow xtree to be modified before
  			 * txUpdateMap runs.  Copy xad into linelock to
  			 * preserve correct data.
66f3131f5   Dave Kleikamp   [PATCH] JFS: redu...
1754
1755
  			 *
  			 * We can fit twice as may pxd's as xads in the lock
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1756
  			 */
66f3131f5   Dave Kleikamp   [PATCH] JFS: redu...
1757
1758
1759
1760
1761
  			xadlock->flag = mlckALLOCPXDLIST;
  			pxd = xadlock->xdlist = &xtlck->pxdlock;
  			for (i = 0; i < xadlock->count; i++) {
  				PXDaddress(pxd, addressXAD(&p->xad[lwm + i]));
  				PXDlength(pxd, lengthXAD(&p->xad[lwm + i]));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1762
1763
  				p->xad[lwm + i].flag &=
  				    ~(XAD_NEW | XAD_EXTENDED);
66f3131f5   Dave Kleikamp   [PATCH] JFS: redu...
1764
1765
  				pxd++;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1766
1767
1768
1769
1770
  		} else {
  			/*
  			 * xdlist will point to into inode's xtree, ensure
  			 * that transaction is not committed lazily.
  			 */
66f3131f5   Dave Kleikamp   [PATCH] JFS: redu...
1771
  			xadlock->flag = mlckALLOCXADLIST;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
  			xadlock->xdlist = &p->xad[lwm];
  			tblk->xflag &= ~COMMIT_LAZY;
  		}
  		jfs_info("xtLog: alloc ip:0x%p mp:0x%p tlck:0x%p lwm:%d "
  			 "count:%d", tlck->ip, mp, tlck, lwm, xadlock->count);
  
  		maplock->index = 1;
  
  	      out:
  		/* mark page as homeward bound */
  		tlck->flag |= tlckWRITEPAGE;
  
  		return;
  	}
  
  	/*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1788
  	 *	page deletion: file deletion/truncation (ref. xtTruncate())
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
  	 *
  	 * (page will be invalidated after log is written and bmap
  	 * is updated from the page);
  	 */
  	if (tlck->type & tlckFREE) {
  		/* LOG_NOREDOPAGE log for NoRedoPage filter:
  		 * if page free from file delete, NoRedoFile filter from
  		 * inode image of zero link count will subsume NoRedoPage
  		 * filters for each page;
  		 * if page free from file truncattion, write NoRedoPage
  		 * filter;
  		 *
  		 * upadte of block allocation map for the page itself:
  		 * if page free from deletion and truncation, LOG_UPDATEMAP
  		 * log for the page itself is generated from processing
  		 * its parent page xad entries;
  		 */
  		/* if page free from file truncation, log LOG_NOREDOPAGE
  		 * of the deleted page for logredo() to start NoRedoPage
  		 * filter for the page;
  		 */
  		if (tblk->xflag & COMMIT_TRUNCATE) {
  			/* write NOREDOPAGE for the page */
  			lrd->type = cpu_to_le16(LOG_NOREDOPAGE);
66f3131f5   Dave Kleikamp   [PATCH] JFS: redu...
1813
1814
  			PXDaddress(page_pxd, mp->index);
  			PXDlength(page_pxd,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
  				  mp->logical_size >> tblk->sb->
  				  s_blocksize_bits);
  			lrd->backchain =
  			    cpu_to_le32(lmLog(log, tblk, lrd, NULL));
  
  			if (tlck->type & tlckBTROOT) {
  				/* Empty xtree must be logged */
  				lrd->type = cpu_to_le16(LOG_REDOPAGE);
  				lrd->backchain =
  				    cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  			}
  		}
  
  		/* init LOG_UPDATEMAP of the freed extents
  		 * XAD[XTENTRYSTART:hwm) from the deleted page itself
  		 * for logredo() to update bmap;
  		 */
  		lrd->type = cpu_to_le16(LOG_UPDATEMAP);
  		lrd->log.updatemap.type = cpu_to_le16(LOG_FREEXADLIST);
  		xtlck = (struct xtlock *) & tlck->lock;
  		hwm = xtlck->hwm.offset;
  		lrd->log.updatemap.nxd =
  		    cpu_to_le16(hwm - XTENTRYSTART + 1);
  		/* reformat linelock for lmLog() */
  		xtlck->header.offset = XTENTRYSTART;
  		xtlck->header.length = hwm - XTENTRYSTART + 1;
  		xtlck->index = 1;
  		lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  
  		/* format a maplock for txUpdateMap() to update bmap
  		 * to free extents of XAD[XTENTRYSTART:hwm) from the
  		 * deleted page itself;
  		 */
  		tlck->flag |= tlckUPDATEMAP;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1849
  		xadlock->count = hwm - XTENTRYSTART + 1;
66f3131f5   Dave Kleikamp   [PATCH] JFS: redu...
1850
1851
1852
  		if ((xadlock->count <= 4) && (tblk->xflag & COMMIT_LAZY)) {
  			int i;
  			pxd_t *pxd;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1853
1854
1855
1856
  			/*
  			 * Lazy commit may allow xtree to be modified before
  			 * txUpdateMap runs.  Copy xad into linelock to
  			 * preserve correct data.
66f3131f5   Dave Kleikamp   [PATCH] JFS: redu...
1857
1858
  			 *
  			 * We can fit twice as may pxd's as xads in the lock
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1859
  			 */
66f3131f5   Dave Kleikamp   [PATCH] JFS: redu...
1860
1861
1862
1863
1864
1865
1866
1867
1868
  			xadlock->flag = mlckFREEPXDLIST;
  			pxd = xadlock->xdlist = &xtlck->pxdlock;
  			for (i = 0; i < xadlock->count; i++) {
  				PXDaddress(pxd,
  					addressXAD(&p->xad[XTENTRYSTART + i]));
  				PXDlength(pxd,
  					lengthXAD(&p->xad[XTENTRYSTART + i]));
  				pxd++;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1869
1870
1871
1872
1873
  		} else {
  			/*
  			 * xdlist will point to into inode's xtree, ensure
  			 * that transaction is not committed lazily.
  			 */
66f3131f5   Dave Kleikamp   [PATCH] JFS: redu...
1874
  			xadlock->flag = mlckFREEXADLIST;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
  			xadlock->xdlist = &p->xad[XTENTRYSTART];
  			tblk->xflag &= ~COMMIT_LAZY;
  		}
  		jfs_info("xtLog: free ip:0x%p mp:0x%p count:%d lwm:2",
  			 tlck->ip, mp, xadlock->count);
  
  		maplock->index = 1;
  
  		/* mark page as invalid */
  		if (((tblk->xflag & COMMIT_PWMAP) || S_ISDIR(ip->i_mode))
  		    && !(tlck->type & tlckBTROOT))
  			tlck->flag |= tlckFREEPAGE;
  		/*
  		   else (tblk->xflag & COMMIT_PMAP)
  		   ? release the page;
  		 */
  		return;
  	}
  
  	/*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1895
  	 *	page/entry truncation: file truncation (ref. xtTruncate())
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1896
  	 *
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1897
1898
1899
1900
1901
  	 *	|----------+------+------+---------------|
  	 *		   |      |      |
  	 *		   |      |     hwm - hwm before truncation
  	 *		   |     next - truncation point
  	 *		  lwm - lwm before truncation
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1902
1903
1904
  	 * header ?
  	 */
  	if (tlck->type & tlckTRUNCATE) {
c9e3ad602   Dave Kleikamp   JFS: Get rid of "...
1905
1906
  		/* This odd declaration suppresses a bogus gcc warning */
  		pxd_t pxd = pxd;	/* truncated extent of xad */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
  		int twm;
  
  		/*
  		 * For truncation the entire linelock may be used, so it would
  		 * be difficult to store xad list in linelock itself.
  		 * Therefore, we'll just force transaction to be committed
  		 * synchronously, so that xtree pages won't be changed before
  		 * txUpdateMap runs.
  		 */
  		tblk->xflag &= ~COMMIT_LAZY;
  		lwm = xtlck->lwm.offset;
  		if (lwm == 0)
  			lwm = XTPAGEMAXSLOT;
  		hwm = xtlck->hwm.offset;
  		twm = xtlck->twm.offset;
  
  		/*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1924
  		 *	write log records
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
  		 */
  		/* log after-image for logredo():
  		 *
  		 * logredo() will update bmap for alloc of new/extended
  		 * extents (XAD_NEW|XAD_EXTEND) of XAD[lwm:next) from
  		 * after-image of XADlist;
  		 * logredo() resets (XAD_NEW|XAD_EXTEND) flag when
  		 * applying the after-image to the meta-data page.
  		 */
  		lrd->type = cpu_to_le16(LOG_REDOPAGE);
66f3131f5   Dave Kleikamp   [PATCH] JFS: redu...
1935
1936
1937
  		PXDaddress(page_pxd, mp->index);
  		PXDlength(page_pxd,
  			  mp->logical_size >> tblk->sb->s_blocksize_bits);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
  		lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  
  		/*
  		 * truncate entry XAD[twm == next - 1]:
  		 */
  		if (twm == next - 1) {
  			/* init LOG_UPDATEMAP for logredo() to update bmap for
  			 * free of truncated delta extent of the truncated
  			 * entry XAD[next - 1]:
  			 * (xtlck->pxdlock = truncated delta extent);
  			 */
  			pxdlock = (struct pxd_lock *) & xtlck->pxdlock;
  			/* assert(pxdlock->type & tlckTRUNCATE); */
  			lrd->type = cpu_to_le16(LOG_UPDATEMAP);
  			lrd->log.updatemap.type = cpu_to_le16(LOG_FREEPXD);
  			lrd->log.updatemap.nxd = cpu_to_le16(1);
  			lrd->log.updatemap.pxd = pxdlock->pxd;
66f3131f5   Dave Kleikamp   [PATCH] JFS: redu...
1955
  			pxd = pxdlock->pxd;	/* save to format maplock */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
  			lrd->backchain =
  			    cpu_to_le32(lmLog(log, tblk, lrd, NULL));
  		}
  
  		/*
  		 * free entries XAD[next:hwm]:
  		 */
  		if (hwm >= next) {
  			/* init LOG_UPDATEMAP of the freed extents
  			 * XAD[next:hwm] from the deleted page itself
  			 * for logredo() to update bmap;
  			 */
  			lrd->type = cpu_to_le16(LOG_UPDATEMAP);
  			lrd->log.updatemap.type =
  			    cpu_to_le16(LOG_FREEXADLIST);
  			xtlck = (struct xtlock *) & tlck->lock;
  			hwm = xtlck->hwm.offset;
  			lrd->log.updatemap.nxd =
  			    cpu_to_le16(hwm - next + 1);
  			/* reformat linelock for lmLog() */
  			xtlck->header.offset = next;
  			xtlck->header.length = hwm - next + 1;
  			xtlck->index = 1;
  			lrd->backchain =
  			    cpu_to_le32(lmLog(log, tblk, lrd, tlck));
  		}
  
  		/*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
1984
  		 *	format maplock(s) for txUpdateMap() to update bmap
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
  		 */
  		maplock->index = 0;
  
  		/*
  		 * allocate entries XAD[lwm:next):
  		 */
  		if (lwm < next) {
  			/* format a maplock for txUpdateMap() to update bPMAP
  			 * for alloc of new/extended extents of XAD[lwm:next)
  			 * from the page itself;
  			 * txUpdateMap() resets (XAD_NEW|XAD_EXTEND) flag.
  			 */
  			tlck->flag |= tlckUPDATEMAP;
  			xadlock->flag = mlckALLOCXADLIST;
  			xadlock->count = next - lwm;
  			xadlock->xdlist = &p->xad[lwm];
  
  			jfs_info("xtLog: alloc ip:0x%p mp:0x%p count:%d "
  				 "lwm:%d next:%d",
  				 tlck->ip, mp, xadlock->count, lwm, next);
  			maplock->index++;
  			xadlock++;
  		}
  
  		/*
  		 * truncate entry XAD[twm == next - 1]:
  		 */
  		if (twm == next - 1) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2013
2014
2015
2016
2017
2018
2019
2020
2021
  			/* format a maplock for txUpdateMap() to update bmap
  			 * to free truncated delta extent of the truncated
  			 * entry XAD[next - 1];
  			 * (xtlck->pxdlock = truncated delta extent);
  			 */
  			tlck->flag |= tlckUPDATEMAP;
  			pxdlock = (struct pxd_lock *) xadlock;
  			pxdlock->flag = mlckFREEPXD;
  			pxdlock->count = 1;
66f3131f5   Dave Kleikamp   [PATCH] JFS: redu...
2022
  			pxdlock->pxd = pxd;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
  
  			jfs_info("xtLog: truncate ip:0x%p mp:0x%p count:%d "
  				 "hwm:%d", ip, mp, pxdlock->count, hwm);
  			maplock->index++;
  			xadlock++;
  		}
  
  		/*
  		 * free entries XAD[next:hwm]:
  		 */
  		if (hwm >= next) {
  			/* format a maplock for txUpdateMap() to update bmap
  			 * to free extents of XAD[next:hwm] from thedeleted
  			 * page itself;
  			 */
  			tlck->flag |= tlckUPDATEMAP;
  			xadlock->flag = mlckFREEXADLIST;
  			xadlock->count = hwm - next + 1;
  			xadlock->xdlist = &p->xad[next];
  
  			jfs_info("xtLog: free ip:0x%p mp:0x%p count:%d "
  				 "next:%d hwm:%d",
  				 tlck->ip, mp, xadlock->count, next, hwm);
  			maplock->index++;
  		}
  
  		/* mark page as homeward bound */
  		tlck->flag |= tlckWRITEPAGE;
  	}
  	return;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2054
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
2055
   *	mapLog()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2056
   *
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
2057
   * function:	log from maplock of freed data extents;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2058
   */
6cb1269b9   Dave Kleikamp   JFS: Fix sparse w...
2059
2060
  static void mapLog(struct jfs_log * log, struct tblock * tblk, struct lrd * lrd,
  		   struct tlock * tlck)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2061
2062
2063
2064
2065
2066
  {
  	struct pxd_lock *pxdlock;
  	int i, nlock;
  	pxd_t *pxd;
  
  	/*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
2067
  	 *	page relocation: free the source page extent
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
  	 *
  	 * a maplock for txUpdateMap() for free of the page
  	 * has been formatted at txLock() time saving the src
  	 * relocated page address;
  	 */
  	if (tlck->type & tlckRELOCATE) {
  		/* log LOG_NOREDOPAGE of the old relocated page
  		 * for logredo() to start NoRedoPage filter;
  		 */
  		lrd->type = cpu_to_le16(LOG_NOREDOPAGE);
  		pxdlock = (struct pxd_lock *) & tlck->lock;
  		pxd = &lrd->log.redopage.pxd;
  		*pxd = pxdlock->pxd;
  		lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, NULL));
  
  		/* (N.B. currently, logredo() does NOT update bmap
  		 * for free of the page itself for (LOG_XTREE|LOG_NOREDOPAGE);
  		 * if page free from relocation, LOG_UPDATEMAP log is
  		 * specifically generated now for logredo()
  		 * to update bmap for free of src relocated page;
  		 * (new flag LOG_RELOCATE may be introduced which will
  		 * inform logredo() to start NORedoPage filter and also
  		 * update block allocation map at the same time, thus
  		 * avoiding an extra log write);
  		 */
  		lrd->type = cpu_to_le16(LOG_UPDATEMAP);
  		lrd->log.updatemap.type = cpu_to_le16(LOG_FREEPXD);
  		lrd->log.updatemap.nxd = cpu_to_le16(1);
  		lrd->log.updatemap.pxd = pxdlock->pxd;
  		lrd->backchain = cpu_to_le32(lmLog(log, tblk, lrd, NULL));
  
  		/* a maplock for txUpdateMap() for free of the page
  		 * has been formatted at txLock() time;
  		 */
  		tlck->flag |= tlckUPDATEMAP;
  		return;
  	}
  	/*
  
  	 * Otherwise it's not a relocate request
  	 *
  	 */
  	else {
  		/* log LOG_UPDATEMAP for logredo() to update bmap for
  		 * free of truncated/relocated delta extent of the data;
  		 * e.g.: external EA extent, relocated/truncated extent
  		 * from xtTailgate();
  		 */
  		lrd->type = cpu_to_le16(LOG_UPDATEMAP);
  		pxdlock = (struct pxd_lock *) & tlck->lock;
  		nlock = pxdlock->index;
  		for (i = 0; i < nlock; i++, pxdlock++) {
  			if (pxdlock->flag & mlckALLOCPXD)
  				lrd->log.updatemap.type =
  				    cpu_to_le16(LOG_ALLOCPXD);
  			else
  				lrd->log.updatemap.type =
  				    cpu_to_le16(LOG_FREEPXD);
  			lrd->log.updatemap.nxd = cpu_to_le16(1);
  			lrd->log.updatemap.pxd = pxdlock->pxd;
  			lrd->backchain =
  			    cpu_to_le32(lmLog(log, tblk, lrd, NULL));
  			jfs_info("mapLog: xaddr:0x%lx xlen:0x%x",
  				 (ulong) addressPXD(&pxdlock->pxd),
  				 lengthPXD(&pxdlock->pxd));
  		}
  
  		/* update bmap */
  		tlck->flag |= tlckUPDATEMAP;
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2139
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
2140
   *	txEA()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2141
   *
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
2142
2143
   * function:	acquire maplock for EA/ACL extents or
   *		set COMMIT_INLINE flag;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
   */
  void txEA(tid_t tid, struct inode *ip, dxd_t * oldea, dxd_t * newea)
  {
  	struct tlock *tlck = NULL;
  	struct pxd_lock *maplock = NULL, *pxdlock = NULL;
  
  	/*
  	 * format maplock for alloc of new EA extent
  	 */
  	if (newea) {
  		/* Since the newea could be a completely zeroed entry we need to
  		 * check for the two flags which indicate we should actually
  		 * commit new EA data
  		 */
  		if (newea->flag & DXD_EXTENT) {
  			tlck = txMaplock(tid, ip, tlckMAP);
  			maplock = (struct pxd_lock *) & tlck->lock;
  			pxdlock = (struct pxd_lock *) maplock;
  			pxdlock->flag = mlckALLOCPXD;
  			PXDaddress(&pxdlock->pxd, addressDXD(newea));
  			PXDlength(&pxdlock->pxd, lengthDXD(newea));
  			pxdlock++;
  			maplock->index = 1;
  		} else if (newea->flag & DXD_INLINE) {
  			tlck = NULL;
  
  			set_cflag(COMMIT_Inlineea, ip);
  		}
  	}
  
  	/*
  	 * format maplock for free of old EA extent
  	 */
  	if (!test_cflag(COMMIT_Nolink, ip) && oldea->flag & DXD_EXTENT) {
  		if (tlck == NULL) {
  			tlck = txMaplock(tid, ip, tlckMAP);
  			maplock = (struct pxd_lock *) & tlck->lock;
  			pxdlock = (struct pxd_lock *) maplock;
  			maplock->index = 0;
  		}
  		pxdlock->flag = mlckFREEPXD;
  		PXDaddress(&pxdlock->pxd, addressDXD(oldea));
  		PXDlength(&pxdlock->pxd, lengthDXD(oldea));
  		maplock->index++;
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2190
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
2191
   *	txForce()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2192
2193
   *
   * function: synchronously write pages locked by transaction
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
2194
   *	     after txLog() but before txUpdateMap();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2195
   */
6cb1269b9   Dave Kleikamp   JFS: Fix sparse w...
2196
  static void txForce(struct tblock * tblk)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
  {
  	struct tlock *tlck;
  	lid_t lid, next;
  	struct metapage *mp;
  
  	/*
  	 * reverse the order of transaction tlocks in
  	 * careful update order of address index pages
  	 * (right to left, bottom up)
  	 */
  	tlck = lid_to_tlock(tblk->next);
  	lid = tlck->next;
  	tlck->next = 0;
  	while (lid) {
  		tlck = lid_to_tlock(lid);
  		next = tlck->next;
  		tlck->next = tblk->next;
  		tblk->next = lid;
  		lid = next;
  	}
  
  	/*
  	 * synchronously write the page, and
  	 * hold the page for txUpdateMap();
  	 */
  	for (lid = tblk->next; lid; lid = next) {
  		tlck = lid_to_tlock(lid);
  		next = tlck->next;
  
  		if ((mp = tlck->mp) != NULL &&
  		    (tlck->type & tlckBTROOT) == 0) {
  			assert(mp->xflag & COMMIT_PAGE);
  
  			if (tlck->flag & tlckWRITEPAGE) {
  				tlck->flag &= ~tlckWRITEPAGE;
  
  				/* do not release page to freelist */
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
2234
2235
  				force_metapage(mp);
  #if 0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
  				/*
  				 * The "right" thing to do here is to
  				 * synchronously write the metadata.
  				 * With the current implementation this
  				 * is hard since write_metapage requires
  				 * us to kunmap & remap the page.  If we
  				 * have tlocks pointing into the metadata
  				 * pages, we don't want to do this.  I think
  				 * we can get by with synchronously writing
  				 * the pages when they are released.
  				 */
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
2247
  				assert(mp->nohomeok);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2248
2249
  				set_bit(META_dirty, &mp->flag);
  				set_bit(META_sync, &mp->flag);
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
2250
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2251
2252
2253
2254
  			}
  		}
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2255
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
2256
   *	txUpdateMap()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2257
   *
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
2258
2259
   * function:	update persistent allocation map (and working map
   *		if appropriate);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
   *
   * parameter:
   */
  static void txUpdateMap(struct tblock * tblk)
  {
  	struct inode *ip;
  	struct inode *ipimap;
  	lid_t lid;
  	struct tlock *tlck;
  	struct maplock *maplock;
  	struct pxd_lock pxdlock;
  	int maptype;
  	int k, nlock;
  	struct metapage *mp = NULL;
  
  	ipimap = JFS_SBI(tblk->sb)->ipimap;
  
  	maptype = (tblk->xflag & COMMIT_PMAP) ? COMMIT_PMAP : COMMIT_PWMAP;
  
  
  	/*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
2281
  	 *	update block allocation map
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
  	 *
  	 * update allocation state in pmap (and wmap) and
  	 * update lsn of the pmap page;
  	 */
  	/*
  	 * scan each tlock/page of transaction for block allocation/free:
  	 *
  	 * for each tlock/page of transaction, update map.
  	 *  ? are there tlock for pmap and pwmap at the same time ?
  	 */
  	for (lid = tblk->next; lid; lid = tlck->next) {
  		tlck = lid_to_tlock(lid);
  
  		if ((tlck->flag & tlckUPDATEMAP) == 0)
  			continue;
  
  		if (tlck->flag & tlckFREEPAGE) {
  			/*
  			 * Another thread may attempt to reuse freed space
  			 * immediately, so we want to get rid of the metapage
  			 * before anyone else has a chance to get it.
  			 * Lock metapage, update maps, then invalidate
  			 * the metapage.
  			 */
  			mp = tlck->mp;
  			ASSERT(mp->xflag & COMMIT_PAGE);
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
2308
  			grab_metapage(mp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
  		}
  
  		/*
  		 * extent list:
  		 * . in-line PXD list:
  		 * . out-of-line XAD list:
  		 */
  		maplock = (struct maplock *) & tlck->lock;
  		nlock = maplock->index;
  
  		for (k = 0; k < nlock; k++, maplock++) {
  			/*
  			 * allocate blocks in persistent map:
  			 *
  			 * blocks have been allocated from wmap at alloc time;
  			 */
  			if (maplock->flag & mlckALLOC) {
  				txAllocPMap(ipimap, maplock, tblk);
  			}
  			/*
  			 * free blocks in persistent and working map:
  			 * blocks will be freed in pmap and then in wmap;
  			 *
  			 * ? tblock specifies the PMAP/PWMAP based upon
  			 * transaction
  			 *
  			 * free blocks in persistent map:
  			 * blocks will be freed from wmap at last reference
  			 * release of the object for regular files;
  			 *
  			 * Alway free blocks from both persistent & working
  			 * maps for directories
  			 */
  			else {	/* (maplock->flag & mlckFREE) */
438282d85   Dave Kleikamp   JFS: don't derefe...
2343
  				if (tlck->flag & tlckDIRECTORY)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
  					txFreeMap(ipimap, maplock,
  						  tblk, COMMIT_PWMAP);
  				else
  					txFreeMap(ipimap, maplock,
  						  tblk, maptype);
  			}
  		}
  		if (tlck->flag & tlckFREEPAGE) {
  			if (!(tblk->flag & tblkGC_LAZY)) {
  				/* This is equivalent to txRelease */
  				ASSERT(mp->lid == lid);
  				tlck->mp->lid = 0;
  			}
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
2357
2358
  			assert(mp->nohomeok == 1);
  			metapage_homeok(mp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2359
2360
2361
2362
2363
  			discard_metapage(mp);
  			tlck->mp = NULL;
  		}
  	}
  	/*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
2364
  	 *	update inode allocation map
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2365
2366
2367
2368
2369
2370
2371
2372
  	 *
  	 * update allocation state in pmap and
  	 * update lsn of the pmap page;
  	 * update in-memory inode flag/state
  	 *
  	 * unlock mapper/write lock
  	 */
  	if (tblk->xflag & COMMIT_CREATE) {
4d81715fc   Richard Knutsson   [PATCH] fs/jfs: C...
2373
  		diUpdatePMap(ipimap, tblk->ino, false, tblk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2374
2375
2376
2377
2378
2379
2380
2381
2382
  		/* update persistent block allocation map
  		 * for the allocation of inode extent;
  		 */
  		pxdlock.flag = mlckALLOCPXD;
  		pxdlock.pxd = tblk->u.ixpxd;
  		pxdlock.index = 1;
  		txAllocPMap(ipimap, (struct maplock *) & pxdlock, tblk);
  	} else if (tblk->xflag & COMMIT_DELETE) {
  		ip = tblk->u.ip;
4d81715fc   Richard Knutsson   [PATCH] fs/jfs: C...
2383
  		diUpdatePMap(ipimap, ip->i_ino, true, tblk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2384
2385
2386
  		iput(ip);
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2387
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
2388
   *	txAllocPMap()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2389
2390
2391
2392
   *
   * function: allocate from persistent map;
   *
   * parameter:
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
   *	ipbmap	-
   *	malock	-
   *		xad list:
   *		pxd:
   *
   *	maptype -
   *		allocate from persistent map;
   *		free from persistent map;
   *		(e.g., tmp file - free from working map at releae
   *		 of last reference);
   *		free from persistent and working map;
   *
   *	lsn	- log sequence number;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
   */
  static void txAllocPMap(struct inode *ip, struct maplock * maplock,
  			struct tblock * tblk)
  {
  	struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
  	struct xdlistlock *xadlistlock;
  	xad_t *xad;
  	s64 xaddr;
  	int xlen;
  	struct pxd_lock *pxdlock;
  	struct xdlistlock *pxdlistlock;
  	pxd_t *pxd;
  	int n;
  
  	/*
  	 * allocate from persistent map;
  	 */
  	if (maplock->flag & mlckALLOCXADLIST) {
  		xadlistlock = (struct xdlistlock *) maplock;
  		xad = xadlistlock->xdlist;
  		for (n = 0; n < xadlistlock->count; n++, xad++) {
  			if (xad->flag & (XAD_NEW | XAD_EXTENDED)) {
  				xaddr = addressXAD(xad);
  				xlen = lengthXAD(xad);
4d81715fc   Richard Knutsson   [PATCH] fs/jfs: C...
2430
  				dbUpdatePMap(ipbmap, false, xaddr,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
  					     (s64) xlen, tblk);
  				xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
  				jfs_info("allocPMap: xaddr:0x%lx xlen:%d",
  					 (ulong) xaddr, xlen);
  			}
  		}
  	} else if (maplock->flag & mlckALLOCPXD) {
  		pxdlock = (struct pxd_lock *) maplock;
  		xaddr = addressPXD(&pxdlock->pxd);
  		xlen = lengthPXD(&pxdlock->pxd);
4d81715fc   Richard Knutsson   [PATCH] fs/jfs: C...
2441
  		dbUpdatePMap(ipbmap, false, xaddr, (s64) xlen, tblk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2442
2443
2444
2445
2446
2447
2448
2449
  		jfs_info("allocPMap: xaddr:0x%lx xlen:%d", (ulong) xaddr, xlen);
  	} else {		/* (maplock->flag & mlckALLOCPXDLIST) */
  
  		pxdlistlock = (struct xdlistlock *) maplock;
  		pxd = pxdlistlock->xdlist;
  		for (n = 0; n < pxdlistlock->count; n++, pxd++) {
  			xaddr = addressPXD(pxd);
  			xlen = lengthPXD(pxd);
4d81715fc   Richard Knutsson   [PATCH] fs/jfs: C...
2450
  			dbUpdatePMap(ipbmap, false, xaddr, (s64) xlen,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2451
2452
2453
2454
2455
2456
  				     tblk);
  			jfs_info("allocPMap: xaddr:0x%lx xlen:%d",
  				 (ulong) xaddr, xlen);
  		}
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2457
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
2458
   *	txFreeMap()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2459
   *
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
2460
   * function:	free from persistent and/or working map;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
   *
   * todo: optimization
   */
  void txFreeMap(struct inode *ip,
  	       struct maplock * maplock, struct tblock * tblk, int maptype)
  {
  	struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
  	struct xdlistlock *xadlistlock;
  	xad_t *xad;
  	s64 xaddr;
  	int xlen;
  	struct pxd_lock *pxdlock;
  	struct xdlistlock *pxdlistlock;
  	pxd_t *pxd;
  	int n;
  
  	jfs_info("txFreeMap: tblk:0x%p maplock:0x%p maptype:0x%x",
  		 tblk, maplock, maptype);
  
  	/*
  	 * free from persistent map;
  	 */
  	if (maptype == COMMIT_PMAP || maptype == COMMIT_PWMAP) {
  		if (maplock->flag & mlckFREEXADLIST) {
  			xadlistlock = (struct xdlistlock *) maplock;
  			xad = xadlistlock->xdlist;
  			for (n = 0; n < xadlistlock->count; n++, xad++) {
  				if (!(xad->flag & XAD_NEW)) {
  					xaddr = addressXAD(xad);
  					xlen = lengthXAD(xad);
4d81715fc   Richard Knutsson   [PATCH] fs/jfs: C...
2491
  					dbUpdatePMap(ipbmap, true, xaddr,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
  						     (s64) xlen, tblk);
  					jfs_info("freePMap: xaddr:0x%lx "
  						 "xlen:%d",
  						 (ulong) xaddr, xlen);
  				}
  			}
  		} else if (maplock->flag & mlckFREEPXD) {
  			pxdlock = (struct pxd_lock *) maplock;
  			xaddr = addressPXD(&pxdlock->pxd);
  			xlen = lengthPXD(&pxdlock->pxd);
4d81715fc   Richard Knutsson   [PATCH] fs/jfs: C...
2502
  			dbUpdatePMap(ipbmap, true, xaddr, (s64) xlen,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
  				     tblk);
  			jfs_info("freePMap: xaddr:0x%lx xlen:%d",
  				 (ulong) xaddr, xlen);
  		} else {	/* (maplock->flag & mlckALLOCPXDLIST) */
  
  			pxdlistlock = (struct xdlistlock *) maplock;
  			pxd = pxdlistlock->xdlist;
  			for (n = 0; n < pxdlistlock->count; n++, pxd++) {
  				xaddr = addressPXD(pxd);
  				xlen = lengthPXD(pxd);
4d81715fc   Richard Knutsson   [PATCH] fs/jfs: C...
2513
  				dbUpdatePMap(ipbmap, true, xaddr,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
  					     (s64) xlen, tblk);
  				jfs_info("freePMap: xaddr:0x%lx xlen:%d",
  					 (ulong) xaddr, xlen);
  			}
  		}
  	}
  
  	/*
  	 * free from working map;
  	 */
  	if (maptype == COMMIT_PWMAP || maptype == COMMIT_WMAP) {
  		if (maplock->flag & mlckFREEXADLIST) {
  			xadlistlock = (struct xdlistlock *) maplock;
  			xad = xadlistlock->xdlist;
  			for (n = 0; n < xadlistlock->count; n++, xad++) {
  				xaddr = addressXAD(xad);
  				xlen = lengthXAD(xad);
  				dbFree(ip, xaddr, (s64) xlen);
  				xad->flag = 0;
  				jfs_info("freeWMap: xaddr:0x%lx xlen:%d",
  					 (ulong) xaddr, xlen);
  			}
  		} else if (maplock->flag & mlckFREEPXD) {
  			pxdlock = (struct pxd_lock *) maplock;
  			xaddr = addressPXD(&pxdlock->pxd);
  			xlen = lengthPXD(&pxdlock->pxd);
  			dbFree(ip, xaddr, (s64) xlen);
  			jfs_info("freeWMap: xaddr:0x%lx xlen:%d",
  				 (ulong) xaddr, xlen);
  		} else {	/* (maplock->flag & mlckFREEPXDLIST) */
  
  			pxdlistlock = (struct xdlistlock *) maplock;
  			pxd = pxdlistlock->xdlist;
  			for (n = 0; n < pxdlistlock->count; n++, pxd++) {
  				xaddr = addressPXD(pxd);
  				xlen = lengthPXD(pxd);
  				dbFree(ip, xaddr, (s64) xlen);
  				jfs_info("freeWMap: xaddr:0x%lx xlen:%d",
  					 (ulong) xaddr, xlen);
  			}
  		}
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2557
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
2558
   *	txFreelock()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2559
   *
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
2560
   * function:	remove tlock from inode anonymous locklist
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
   */
  void txFreelock(struct inode *ip)
  {
  	struct jfs_inode_info *jfs_ip = JFS_IP(ip);
  	struct tlock *xtlck, *tlck;
  	lid_t xlid = 0, lid;
  
  	if (!jfs_ip->atlhead)
  		return;
  
  	TXN_LOCK();
  	xtlck = (struct tlock *) &jfs_ip->atlhead;
  
  	while ((lid = xtlck->next) != 0) {
  		tlck = lid_to_tlock(lid);
  		if (tlck->flag & tlckFREELOCK) {
  			xtlck->next = tlck->next;
  			txLockFree(lid);
  		} else {
  			xtlck = tlck;
  			xlid = lid;
  		}
  	}
  
  	if (jfs_ip->atlhead)
  		jfs_ip->atltail = xlid;
  	else {
  		jfs_ip->atltail = 0;
  		/*
  		 * If inode was on anon_list, remove it
  		 */
  		list_del_init(&jfs_ip->anon_inode_list);
  	}
  	TXN_UNLOCK();
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2596
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
2597
   *	txAbort()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
   *
   * function: abort tx before commit;
   *
   * frees line-locks and segment locks for all
   * segments in comdata structure.
   * Optionally sets state of file-system to FM_DIRTY in super-block.
   * log age of page-frames in memory for which caller has
   * are reset to 0 (to avoid logwarap).
   */
  void txAbort(tid_t tid, int dirty)
  {
  	lid_t lid, next;
  	struct metapage *mp;
  	struct tblock *tblk = tid_to_tblock(tid);
  	struct tlock *tlck;
  
  	/*
  	 * free tlocks of the transaction
  	 */
  	for (lid = tblk->next; lid; lid = next) {
  		tlck = lid_to_tlock(lid);
  		next = tlck->next;
  		mp = tlck->mp;
  		JFS_IP(tlck->ip)->xtlid = 0;
  
  		if (mp) {
  			mp->lid = 0;
  
  			/*
  			 * reset lsn of page to avoid logwarap:
  			 *
  			 * (page may have been previously committed by another
  			 * transaction(s) but has not been paged, i.e.,
  			 * it may be on logsync list even though it has not
  			 * been logged for the current tx.)
  			 */
  			if (mp->xflag & COMMIT_PAGE && mp->lsn)
  				LogSyncRelease(mp);
  		}
  		/* insert tlock at head of freelist */
  		TXN_LOCK();
  		txLockFree(lid);
  		TXN_UNLOCK();
  	}
  
  	/* caller will free the transaction block */
  
  	tblk->next = tblk->last = 0;
  
  	/*
  	 * mark filesystem dirty
  	 */
  	if (dirty)
  		jfs_error(tblk->sb, "txAbort");
  
  	return;
  }
  
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
2657
   *	txLazyCommit(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
   *
   *	All transactions except those changing ipimap (COMMIT_FORCE) are
   *	processed by this routine.  This insures that the inode and block
   *	allocation maps are updated in order.  For synchronous transactions,
   *	let the user thread finish processing after txUpdateMap() is called.
   */
  static void txLazyCommit(struct tblock * tblk)
  {
  	struct jfs_log *log;
  
  	while (((tblk->flag & tblkGC_READY) == 0) &&
  	       ((tblk->flag & tblkGC_UNLOCKED) == 0)) {
  		/* We must have gotten ahead of the user thread
  		 */
  		jfs_info("jfs_lazycommit: tblk 0x%p not unlocked", tblk);
  		yield();
  	}
  
  	jfs_info("txLazyCommit: processing tblk 0x%p", tblk);
  
  	txUpdateMap(tblk);
  
  	log = (struct jfs_log *) JFS_SBI(tblk->sb)->log;
  
  	spin_lock_irq(&log->gclock);	// LOGGC_LOCK
  
  	tblk->flag |= tblkGC_COMMITTED;
  
  	if (tblk->flag & tblkGC_READY)
  		log->gcrtc--;
  
  	wake_up_all(&tblk->gcwait);	// LOGGC_WAKEUP
  
  	/*
  	 * Can't release log->gclock until we've tested tblk->flag
  	 */
  	if (tblk->flag & tblkGC_LAZY) {
  		spin_unlock_irq(&log->gclock);	// LOGGC_UNLOCK
  		txUnlock(tblk);
  		tblk->flag &= ~tblkGC_LAZY;
  		txEnd(tblk - TxBlock);	/* Convert back to tid */
  	} else
  		spin_unlock_irq(&log->gclock);	// LOGGC_UNLOCK
  
  	jfs_info("txLazyCommit: done: tblk = 0x%p", tblk);
  }
  
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
2706
   *	jfs_lazycommit(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
   *
   *	To be run as a kernel daemon.  If lbmIODone is called in an interrupt
   *	context, or where blocking is not wanted, this routine will process
   *	committed transactions from the unlock queue.
   */
  int jfs_lazycommit(void *arg)
  {
  	int WorkDone;
  	struct tblock *tblk;
  	unsigned long flags;
  	struct jfs_sb_info *sbi;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
  	do {
  		LAZY_LOCK(flags);
  		jfs_commit_thread_waking = 0;	/* OK to wake another thread */
  		while (!list_empty(&TxAnchor.unlock_queue)) {
  			WorkDone = 0;
  			list_for_each_entry(tblk, &TxAnchor.unlock_queue,
  					    cqueue) {
  
  				sbi = JFS_SBI(tblk->sb);
  				/*
  				 * For each volume, the transactions must be
  				 * handled in order.  If another commit thread
  				 * is handling a tblk for this superblock,
  				 * skip it
  				 */
  				if (sbi->commit_state & IN_LAZYCOMMIT)
  					continue;
  
  				sbi->commit_state |= IN_LAZYCOMMIT;
  				WorkDone = 1;
  
  				/*
  				 * Remove transaction from queue
  				 */
  				list_del(&tblk->cqueue);
  
  				LAZY_UNLOCK(flags);
  				txLazyCommit(tblk);
  				LAZY_LOCK(flags);
  
  				sbi->commit_state &= ~IN_LAZYCOMMIT;
  				/*
  				 * Don't continue in the for loop.  (We can't
  				 * anyway, it's unsafe!)  We want to go back to
  				 * the beginning of the list.
  				 */
  				break;
  			}
  
  			/* If there was nothing to do, don't continue */
  			if (!WorkDone)
  				break;
  		}
  		/* In case a wakeup came while all threads were active */
  		jfs_commit_thread_waking = 0;
3e1d1d28d   Christoph Lameter   [PATCH] Cleanup p...
2763
  		if (freezing(current)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2764
  			LAZY_UNLOCK(flags);
a0acae0e8   Tejun Heo   freezer: unexport...
2765
  			try_to_freeze();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2766
2767
2768
2769
2770
2771
2772
  		} else {
  			DECLARE_WAITQUEUE(wq, current);
  
  			add_wait_queue(&jfs_commit_thread_wait, &wq);
  			set_current_state(TASK_INTERRUPTIBLE);
  			LAZY_UNLOCK(flags);
  			schedule();
3cbb1c8e1   Milind Arun Choudhary   JFS: use __set_cu...
2773
  			__set_current_state(TASK_RUNNING);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2774
2775
  			remove_wait_queue(&jfs_commit_thread_wait, &wq);
  		}
91dbb4deb   Christoph Hellwig   JFS: Use the kthr...
2776
  	} while (!kthread_should_stop());
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2777
2778
2779
2780
2781
2782
  
  	if (!list_empty(&TxAnchor.unlock_queue))
  		jfs_err("jfs_lazycommit being killed w/pending transactions!");
  	else
  		jfs_info("jfs_lazycommit being killed
  ");
91dbb4deb   Christoph Hellwig   JFS: Use the kthr...
2783
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
  }
  
  void txLazyUnlock(struct tblock * tblk)
  {
  	unsigned long flags;
  
  	LAZY_LOCK(flags);
  
  	list_add_tail(&tblk->cqueue, &TxAnchor.unlock_queue);
  	/*
  	 * Don't wake up a commit thread if there is already one servicing
  	 * this superblock, or if the last one we woke up hasn't started yet.
  	 */
  	if (!(JFS_SBI(tblk->sb)->commit_state & IN_LAZYCOMMIT) &&
  	    !jfs_commit_thread_waking) {
  		jfs_commit_thread_waking = 1;
  		wake_up(&jfs_commit_thread_wait);
  	}
  	LAZY_UNLOCK(flags);
  }
  
  static void LogSyncRelease(struct metapage * mp)
  {
  	struct jfs_log *log = mp->log;
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
2808
  	assert(mp->nohomeok);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2809
  	assert(log);
7fab479be   Dave Kleikamp   [PATCH] JFS: Supp...
2810
  	metapage_homeok(mp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
  }
  
  /*
   *	txQuiesce
   *
   *	Block all new transactions and push anonymous transactions to
   *	completion
   *
   *	This does almost the same thing as jfs_sync below.  We don't
   *	worry about deadlocking when jfs_tlocks_low is set, since we would
   *	expect jfs_sync to get us out of that jam.
   */
  void txQuiesce(struct super_block *sb)
  {
  	struct inode *ip;
  	struct jfs_inode_info *jfs_ip;
  	struct jfs_log *log = JFS_SBI(sb)->log;
  	tid_t tid;
  
  	set_bit(log_QUIESCE, &log->flag);
  
  	TXN_LOCK();
  restart:
  	while (!list_empty(&TxAnchor.anon_list)) {
  		jfs_ip = list_entry(TxAnchor.anon_list.next,
  				    struct jfs_inode_info,
  				    anon_inode_list);
  		ip = &jfs_ip->vfs_inode;
  
  		/*
  		 * inode will be removed from anonymous list
  		 * when it is committed
  		 */
  		TXN_UNLOCK();
  		tid = txBegin(ip->i_sb, COMMIT_INODE | COMMIT_FORCE);
1de87444f   Ingo Molnar   JFS: semaphore to...
2846
  		mutex_lock(&jfs_ip->commit_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2847
2848
  		txCommit(tid, 1, &ip, 0);
  		txEnd(tid);
1de87444f   Ingo Molnar   JFS: semaphore to...
2849
  		mutex_unlock(&jfs_ip->commit_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
  		/*
  		 * Just to be safe.  I don't know how
  		 * long we can run without blocking
  		 */
  		cond_resched();
  		TXN_LOCK();
  	}
  
  	/*
  	 * If jfs_sync is running in parallel, there could be some inodes
  	 * on anon_list2.  Let's check.
  	 */
  	if (!list_empty(&TxAnchor.anon_list2)) {
  		list_splice(&TxAnchor.anon_list2, &TxAnchor.anon_list);
  		INIT_LIST_HEAD(&TxAnchor.anon_list2);
  		goto restart;
  	}
  	TXN_UNLOCK();
  
  	/*
  	 * We may need to kick off the group commit
  	 */
  	jfs_flush_journal(log, 0);
  }
  
  /*
   * txResume()
   *
   * Allows transactions to start again following txQuiesce
   */
  void txResume(struct super_block *sb)
  {
  	struct jfs_log *log = JFS_SBI(sb)->log;
  
  	clear_bit(log_QUIESCE, &log->flag);
  	TXN_WAKEUP(&log->syncwait);
  }
  
  /*
f720e3ba5   Dave Kleikamp   JFS: Whitespace c...
2889
   *	jfs_sync(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2890
2891
2892
2893
2894
2895
2896
2897
2898
   *
   *	To be run as a kernel daemon.  This is awakened when tlocks run low.
   *	We write any inodes that have anonymous tlocks so they will become
   *	available.
   */
  int jfs_sync(void *arg)
  {
  	struct inode *ip;
  	struct jfs_inode_info *jfs_ip;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2899
  	tid_t tid;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
  	do {
  		/*
  		 * write each inode on the anonymous inode list
  		 */
  		TXN_LOCK();
  		while (jfs_tlocks_low && !list_empty(&TxAnchor.anon_list)) {
  			jfs_ip = list_entry(TxAnchor.anon_list.next,
  					    struct jfs_inode_info,
  					    anon_inode_list);
  			ip = &jfs_ip->vfs_inode;
  
  			if (! igrab(ip)) {
  				/*
  				 * Inode is being freed
  				 */
  				list_del_init(&jfs_ip->anon_inode_list);
48ce8b056   Evgeniy Dushistov   JFS: commit_mutex...
2916
  			} else if (mutex_trylock(&jfs_ip->commit_mutex)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2917
2918
2919
2920
2921
2922
  				/*
  				 * inode will be removed from anonymous list
  				 * when it is committed
  				 */
  				TXN_UNLOCK();
  				tid = txBegin(ip->i_sb, COMMIT_INODE);
3c2c22628   Dave Kleikamp   jfs: clean up som...
2923
  				txCommit(tid, 1, &ip, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2924
  				txEnd(tid);
1de87444f   Ingo Molnar   JFS: semaphore to...
2925
  				mutex_unlock(&jfs_ip->commit_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2926
2927
2928
2929
2930
2931
2932
2933
2934
  
  				iput(ip);
  				/*
  				 * Just to be safe.  I don't know how
  				 * long we can run without blocking
  				 */
  				cond_resched();
  				TXN_LOCK();
  			} else {
1de87444f   Ingo Molnar   JFS: semaphore to...
2935
  				/* We can't get the commit mutex.  It may
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
  				 * be held by a thread waiting for tlock's
  				 * so let's not block here.  Save it to
  				 * put back on the anon_list.
  				 */
  
  				/* Take off anon_list */
  				list_del(&jfs_ip->anon_inode_list);
  
  				/* Put on anon_list2 */
  				list_add(&jfs_ip->anon_inode_list,
  					 &TxAnchor.anon_list2);
  
  				TXN_UNLOCK();
  				iput(ip);
  				TXN_LOCK();
  			}
  		}
  		/* Add anon_list2 back to anon_list */
  		list_splice_init(&TxAnchor.anon_list2, &TxAnchor.anon_list);
3e1d1d28d   Christoph Lameter   [PATCH] Cleanup p...
2955
  		if (freezing(current)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2956
  			TXN_UNLOCK();
a0acae0e8   Tejun Heo   freezer: unexport...
2957
  			try_to_freeze();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2958
  		} else {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2959
2960
2961
  			set_current_state(TASK_INTERRUPTIBLE);
  			TXN_UNLOCK();
  			schedule();
3cbb1c8e1   Milind Arun Choudhary   JFS: use __set_cu...
2962
  			__set_current_state(TASK_RUNNING);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2963
  		}
91dbb4deb   Christoph Hellwig   JFS: Use the kthr...
2964
  	} while (!kthread_should_stop());
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2965
2966
  
  	jfs_info("jfs_sync being killed");
91dbb4deb   Christoph Hellwig   JFS: Use the kthr...
2967
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2968
2969
2970
  }
  
  #if defined(CONFIG_PROC_FS) && defined(CONFIG_JFS_DEBUG)
b2e03ca74   Alexey Dobriyan   JFS: switch to se...
2971
  static int jfs_txanchor_proc_show(struct seq_file *m, void *v)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2972
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
  	char *freewait;
  	char *freelockwait;
  	char *lowlockwait;
  
  	freewait =
  	    waitqueue_active(&TxAnchor.freewait) ? "active" : "empty";
  	freelockwait =
  	    waitqueue_active(&TxAnchor.freelockwait) ? "active" : "empty";
  	lowlockwait =
  	    waitqueue_active(&TxAnchor.lowlockwait) ? "active" : "empty";
b2e03ca74   Alexey Dobriyan   JFS: switch to se...
2983
  	seq_printf(m,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
  		       "JFS TxAnchor
  "
  		       "============
  "
  		       "freetid = %d
  "
  		       "freewait = %s
  "
  		       "freelock = %d
  "
  		       "freelockwait = %s
  "
  		       "lowlockwait = %s
  "
  		       "tlocksInUse = %d
  "
  		       "jfs_tlocks_low = %d
  "
  		       "unlock_queue is %sempty
  ",
  		       TxAnchor.freetid,
  		       freewait,
  		       TxAnchor.freelock,
  		       freelockwait,
  		       lowlockwait,
  		       TxAnchor.tlocksInUse,
  		       jfs_tlocks_low,
  		       list_empty(&TxAnchor.unlock_queue) ? "" : "not ");
b2e03ca74   Alexey Dobriyan   JFS: switch to se...
3012
3013
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3014

b2e03ca74   Alexey Dobriyan   JFS: switch to se...
3015
3016
3017
  static int jfs_txanchor_proc_open(struct inode *inode, struct file *file)
  {
  	return single_open(file, jfs_txanchor_proc_show, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3018
  }
b2e03ca74   Alexey Dobriyan   JFS: switch to se...
3019
3020
3021
3022
3023
3024
3025
3026
  
  const struct file_operations jfs_txanchor_proc_fops = {
  	.owner		= THIS_MODULE,
  	.open		= jfs_txanchor_proc_open,
  	.read		= seq_read,
  	.llseek		= seq_lseek,
  	.release	= single_release,
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3027
3028
3029
  #endif
  
  #if defined(CONFIG_PROC_FS) && defined(CONFIG_JFS_STATISTICS)
b2e03ca74   Alexey Dobriyan   JFS: switch to se...
3030
  static int jfs_txstats_proc_show(struct seq_file *m, void *v)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3031
  {
b2e03ca74   Alexey Dobriyan   JFS: switch to se...
3032
  	seq_printf(m,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
  		       "JFS TxStats
  "
  		       "===========
  "
  		       "calls to txBegin = %d
  "
  		       "txBegin blocked by sync barrier = %d
  "
  		       "txBegin blocked by tlocks low = %d
  "
  		       "txBegin blocked by no free tid = %d
  "
  		       "calls to txBeginAnon = %d
  "
  		       "txBeginAnon blocked by sync barrier = %d
  "
  		       "txBeginAnon blocked by tlocks low = %d
  "
  		       "calls to txLockAlloc = %d
  "
  		       "tLockAlloc blocked by no free lock = %d
  ",
  		       TxStat.txBegin,
  		       TxStat.txBegin_barrier,
  		       TxStat.txBegin_lockslow,
  		       TxStat.txBegin_freetid,
  		       TxStat.txBeginAnon,
  		       TxStat.txBeginAnon_barrier,
  		       TxStat.txBeginAnon_lockslow,
  		       TxStat.txLockAlloc,
  		       TxStat.txLockAlloc_freelock);
b2e03ca74   Alexey Dobriyan   JFS: switch to se...
3064
3065
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3066

b2e03ca74   Alexey Dobriyan   JFS: switch to se...
3067
3068
3069
  static int jfs_txstats_proc_open(struct inode *inode, struct file *file)
  {
  	return single_open(file, jfs_txstats_proc_show, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3070
  }
b2e03ca74   Alexey Dobriyan   JFS: switch to se...
3071
3072
3073
3074
3075
3076
3077
3078
  
  const struct file_operations jfs_txstats_proc_fops = {
  	.owner		= THIS_MODULE,
  	.open		= jfs_txstats_proc_open,
  	.read		= seq_read,
  	.llseek		= seq_lseek,
  	.release	= single_release,
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3079
  #endif