aboutsummaryrefslogtreecommitdiffstats
path: root/net/lapb/lapb_in.c
blob: 5dba899131b35bbe83835fc9e897f7e2f2332744 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
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
443
444
445
446
447
448
449
450
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
484
485
486
487
488
489
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
 *	LAPB release 002
 *
 *	This code REQUIRES 2.1.15 or higher/ NET3.038
 *
 *	This module:
 *		This module is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 *
 *	History
 *	LAPB 001	Jonathan Naulor	Started Coding
 *	LAPB 002	Jonathan Naylor	New timer architecture.
 *	2000-10-29	Henner Eisen	lapb_data_indication() return status.
 */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/errno.h>
#include <linux/types.h>
#include <linux/socket.h>
#include <linux/in.h>
#include <linux/kernel.h>
#include <linux/timer.h>
#include <linux/string.h>
#include <linux/sockios.h>
#include <linux/net.h>
#include <linux/inet.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <linux/slab.h>
#include <net/sock.h>
#include <asm/uaccess.h>
#include <linux/fcntl.h>
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <net/lapb.h>

/*
 *	State machine for state 0, Disconnected State.
 *	The handling of the timer(s) is in file lapb_timer.c.
 */
static void lapb_state0_machine(struct lapb_cb *lapb, struct sk_buff *skb,
				struct lapb_frame *frame)
{
	switch (frame->type) {
	case LAPB_SABM:
		lapb_dbg(1, "(%p) S0 RX SABM(%d)\n", lapb->dev, frame->pf);
		if (lapb->mode & LAPB_EXTENDED) {
			lapb_dbg(1, "(%p) S0 TX DM(%d)\n",
				 lapb->dev, frame->pf);
			lapb_send_control(lapb, LAPB_DM, frame->pf,
					  LAPB_RESPONSE);
		} else {
			lapb_dbg(1, "(%p) S0 TX UA(%d)\n",
				 lapb->dev, frame->pf);
			lapb_dbg(0, "(%p) S0 -> S3\n", lapb->dev);
			lapb_send_control(lapb, LAPB_UA, frame->pf,
					  LAPB_RESPONSE);
			lapb_stop_t1timer(lapb);
			lapb_stop_t2timer(lapb);
			lapb->state     = LAPB_STATE_3;
			lapb->condition = 0x00;
			lapb->n2count   = 0;
			lapb->vs        = 0;
			lapb->vr        = 0;
			lapb->va        = 0;
			lapb_connect_indication(lapb, LAPB_OK);
		}
		break;

	case LAPB_SABME:
		lapb_dbg(1, "(%p) S0 RX SABME(%d)\n", lapb->dev, frame->pf);
		if (lapb->mode & LAPB_EXTENDED) {
			lapb_dbg(1, "(%p) S0 TX UA(%d)\n",
				 lapb->dev, frame->pf);
			lapb_dbg(0, "(%p) S0 -> S3\n", lapb->dev);
			lapb_send_control(lapb, LAPB_UA, frame->pf,
					  LAPB_RESPONSE);
			lapb_stop_t1timer(lapb);
			lapb_stop_t2timer(lapb);
			lapb->state     = LAPB_STATE_3;
			lapb->condition = 0x00;
			lapb->n2count   = 0;
			lapb->vs        = 0;
			lapb->vr        = 0;
			lapb->va        = 0;
			lapb_connect_indication(lapb, LAPB_OK);
		} else {
			lapb_dbg(1, "(%p) S0 TX DM(%d)\n",
				 lapb->dev, frame->pf);
			lapb_send_control(lapb, LAPB_DM, frame->pf,
					  LAPB_RESPONSE);
		}
		break;

	case LAPB_DISC:
		lapb_dbg(1, "(%p) S0 RX DISC(%d)\n", lapb->dev, frame->pf);
		lapb_dbg(1, "(%p) S0 TX UA(%d)\n", lapb->dev, frame->pf);
		lapb_send_control(lapb, LAPB_UA, frame->pf, LAPB_RESPONSE);
		break;

	default:
		break;
	}

	kfree_skb(skb);
}

/*
 *	State machine for state 1, Awaiting Connection State.
 *	The handling of the timer(s) is in file lapb_timer.c.
 */
static void lapb_state1_machine(struct lapb_cb *lapb, struct sk_buff *skb,
				struct lapb_frame *frame)
{
	switch (frame->type) {
	case LAPB_SABM:
		lapb_dbg(1, "(%p) S1 RX SABM(%d)\n", lapb->dev, frame->pf);
		if (lapb->mode & LAPB_EXTENDED) {
			lapb_dbg(1, "(%p) S1 TX DM(%d)\n",
				 lapb->dev, frame->pf);
			lapb_send_control(lapb, LAPB_DM, frame->pf,
					  LAPB_RESPONSE);
		} else {
			lapb_dbg(1, "(%p) S1 TX UA(%d)\n",
				 lapb->dev, frame->pf);
			lapb_send_control(lapb, LAPB_UA, frame->pf,
					  LAPB_RESPONSE);
		}
		break;

	case LAPB_SABME:
		lapb_dbg(1, "(%p) S1 RX SABME(%d)\n", lapb->dev, frame->pf);
		if (lapb->mode & LAPB_EXTENDED) {
			lapb_dbg(1, "(%p) S1 TX UA(%d)\n",
				 lapb->dev, frame->pf);
			lapb_send_control(lapb, LAPB_UA, frame->pf,
					  LAPB_RESPONSE);
		} else {
			lapb_dbg(1, "(%p) S1 TX DM(%d)\n",
				 lapb->dev, frame->pf);
			lapb_send_control(lapb, LAPB_DM, frame->pf,
					  LAPB_RESPONSE);
		}
		break;

	case LAPB_DISC:
		lapb_dbg(1, "(%p) S1 RX DISC(%d)\n", lapb->dev, frame->pf);
		lapb_dbg(1, "(%p) S1 TX DM(%d)\n", lapb->dev, frame->pf);
		lapb_send_control(lapb, LAPB_DM, frame->pf, LAPB_RESPONSE);
		break;

	case LAPB_UA:
		lapb_dbg(1, "(%p) S1 RX UA(%d)\n", lapb->dev, frame->pf);
		if (frame->pf) {
			lapb_dbg(0, "(%p) S1 -> S3\n", lapb->dev);
			lapb_stop_t1timer(lapb);
			lapb_stop_t2timer(lapb);
			lapb->state     = LAPB_STATE_3;
			lapb->condition = 0x00;
			lapb->n2count   = 0;
			lapb->vs        = 0;
			lapb->vr        = 0;
			lapb->va        = 0;
			lapb_connect_confirmation(lapb, LAPB_OK);
		}
		break;

	case LAPB_DM:
		lapb_dbg(1, "(%p) S1 RX DM(%d)\n", lapb->dev, frame->pf);
		if (frame->pf) {
			lapb_dbg(0, "(%p) S1 -> S0\n", lapb->dev);
			lapb_clear_queues(lapb);
			lapb->state = LAPB_STATE_0;
			lapb_start_t1timer(lapb);
			lapb_stop_t2timer(lapb);
			lapb_disconnect_indication(lapb, LAPB_REFUSED);
		}
		break;
	}

	kfree_skb(skb);
}

/*
 *	State machine for state 2, Awaiting Release State.
 *	The handling of the timer(s) is in file lapb_timer.c
 */
static void lapb_state2_machine(struct lapb_cb *lapb, struct sk_buff *skb,
				struct lapb_frame *frame)
{
	switch (frame->type) {
	case LAPB_SABM:
	case LAPB_SABME:
		lapb_dbg(1, "(%p) S2 RX {SABM,SABME}(%d)\n",
			 lapb->dev, frame->pf);
		lapb_dbg(1, "(%p) S2 TX DM(%d)\n", lapb->dev, frame->pf);
		lapb_send_control(lapb, LAPB_DM, frame->pf, LAPB_RESPONSE);
		break;

	case LAPB_DISC:
		lapb_dbg(1, "(%p) S2 RX DISC(%d)\n", lapb->dev, frame->pf);
		lapb_dbg(1, "(%p) S2 TX UA(%d)\n", lapb->dev, frame->pf);
		lapb_send_control(lapb, LAPB_UA, frame->pf, LAPB_RESPONSE);
		break;

	case LAPB_UA:
		lapb_dbg(1, "(%p) S2 RX UA(%d)\n", lapb->dev, frame->pf);
		if (frame->pf) {
			lapb_dbg(0, "(%p) S2 -> S0\n", lapb->dev);
			lapb->state = LAPB_STATE_0;
			lapb_start_t1timer(lapb);
			lapb_stop_t2timer(lapb);
			lapb_disconnect_confirmation(lapb, LAPB_OK);
		}
		break;

	case LAPB_DM:
		lapb_dbg(1, "(%p) S2 RX DM(%d)\n", lapb->dev, frame->pf);
		if (frame->pf) {
			lapb_dbg(0, "(%p) S2 -> S0\n", lapb->dev);
			lapb->state = LAPB_STATE_0;
			lapb_start_t1timer(lapb);
			lapb_stop_t2timer(lapb);
			lapb_disconnect_confirmation(lapb, LAPB_NOTCONNECTED);
		}
		break;

	case LAPB_I:
	case LAPB_REJ:
	case LAPB_RNR:
	case LAPB_RR:
		lapb_dbg(1, "(%p) S2 RX {I,REJ,RNR,RR}(%d)\n",
		       lapb->dev, frame->pf);
		lapb_dbg(1, "(%p) S2 RX DM(%d)\n", lapb->dev, frame->pf);
		if (frame->pf)
			lapb_send_control(lapb, LAPB_DM, frame->pf,
					  LAPB_RESPONSE);
		break;
	}

	kfree_skb(skb);
}

/*
 *	State machine for state 3, Connected State.
 *	The handling of the timer(s) is in file lapb_timer.c
 */
static void lapb_state3_machine(struct lapb_cb *lapb, struct sk_buff *skb,
				struct lapb_frame *frame)
{
	int queued = 0;
	int modulus = (lapb->mode & LAPB_EXTENDED) ? LAPB_EMODULUS :
						     LAPB_SMODULUS;

	switch (frame->type) {
	case LAPB_SABM:
		lapb_dbg(1, "(%p) S3 RX SABM(%d)\n", lapb->dev, frame->pf);
		if (lapb->mode & LAPB_EXTENDED) {
			lapb_dbg(1, "(%p) S3 TX DM(%d)\n",
				 lapb->dev, frame->pf);
			lapb_send_control(lapb, LAPB_DM, frame->pf,
					  LAPB_RESPONSE);
		} else {
			lapb_dbg(1, "(%p) S3 TX UA(%d)\n",
				 lapb->dev, frame->pf);
			lapb_send_control(lapb, LAPB_UA, frame->pf,
					  LAPB_RESPONSE);
			lapb_stop_t1timer(lapb);
			lapb_stop_t2timer(lapb);
			lapb->condition = 0x00;
			lapb->n2count   = 0;
			lapb->vs        = 0;
			lapb->vr        = 0;
			lapb->va        = 0;
			lapb_requeue_frames(lapb);
		}
		break;

	case LAPB_SABME:
		lapb_dbg(1, "(%p) S3 RX SABME(%d)\n", lapb->dev, frame->pf);
		if (lapb->mode & LAPB_EXTENDED) {
			lapb_dbg(1, "(%p) S3 TX UA(%d)\n",
				 lapb->dev, frame->pf);
			lapb_send_control(lapb, LAPB_UA, frame->pf,
					  LAPB_RESPONSE);
			lapb_stop_t1timer(lapb);
			lapb_stop_t2timer(lapb);
			lapb->condition = 0x00;
			lapb->n2count   = 0;
			lapb->vs        = 0;
			lapb->vr        = 0;
			lapb->va        = 0;
			lapb_requeue_frames(lapb);
		} else {
			lapb_dbg(1, "(%p) S3 TX DM(%d)\n",
				 lapb->dev, frame->pf);
			lapb_send_control(lapb, LAPB_DM, frame->pf,
					  LAPB_RESPONSE);
		}
		break;

	case LAPB_DISC:
		lapb_dbg(1, "(%p) S3 RX DISC(%d)\n", lapb->dev, frame->pf);
		lapb_dbg(0, "(%p) S3 -> S0\n", lapb->dev);
		lapb_clear_queues(lapb);
		lapb_send_control(lapb, LAPB_UA, frame->pf, LAPB_RESPONSE);
		lapb_start_t1timer(lapb);
		lapb_stop_t2timer(lapb);
		lapb->state = LAPB_STATE_0;
		lapb_disconnect_indication(lapb, LAPB_OK);
		break;

	case LAPB_DM:
		lapb_dbg(1, "(%p) S3 RX DM(%d)\n", lapb->dev, frame->pf);
		lapb_dbg(0, "(%p) S3 -> S0\n", lapb->dev);
		lapb_clear_queues(lapb);
		lapb->state = LAPB_STATE_0;
		lapb_start_t1timer(lapb);
		lapb_stop_t2timer(lapb);
		lapb_disconnect_indication(lapb, LAPB_NOTCONNECTED);
		break;

	case LAPB_RNR:
		lapb_dbg(1, "(%p) S3 RX RNR(%d) R%d\n",
			 lapb->dev, frame->pf, frame->nr);
		lapb->condition |= LAPB_PEER_RX_BUSY_CONDITION;
		lapb_check_need_response(lapb, frame->cr, frame->pf);
		if (lapb_validate_nr(lapb, frame->nr)) {
			lapb_check_iframes_acked(lapb, frame->nr);
		} else {
			lapb->frmr_data = *frame;
			lapb->frmr_type = LAPB_FRMR_Z;
			lapb_transmit_frmr(lapb);
			lapb_dbg(0, "(%p) S3 -> S4\n", lapb->dev);
			lapb_start_t1timer(lapb);
			lapb_stop_t2timer(lapb);
			lapb->state   = LAPB_STATE_4;
			lapb->n2count = 0;
		}
		break;

	case LAPB_RR:
		lapb_dbg(1, "(%p) S3 RX RR(%d) R%d\n",
			 lapb->dev, frame->pf, frame->nr);
		lapb->condition &= ~LAPB_PEER_RX_BUSY_CONDITION;
		lapb_check_need_response(lapb, frame->cr, frame->pf);
		if (lapb_validate_nr(lapb, frame->nr)) {
			lapb_check_iframes_acked(lapb, frame->nr);
		} else {
			lapb->frmr_data = *frame;
			lapb->frmr_type = LAPB_FRMR_Z;
			lapb_transmit_frmr(lapb);
			lapb_dbg(0, "(%p) S3 -> S4\n", lapb->dev);
			lapb_start_t1timer(lapb);
			lapb_stop_t2timer(lapb);
			lapb->state   = LAPB_STATE_4;
			lapb->n2count = 0;
		}
		break;

	case LAPB_REJ:
		lapb_dbg(1, "(%p) S3 RX REJ(%d) R%d\n",
			 lapb->dev, frame->pf, frame->nr);
		lapb->condition &= ~LAPB_PEER_RX_BUSY_CONDITION;
		lapb_check_need_response(lapb, frame->cr, frame->pf);
		if (lapb_validate_nr(lapb, frame->nr)) {
			lapb_frames_acked(lapb, frame->nr);
			lapb_stop_t1timer(lapb);
			lapb->n2count = 0;
			lapb_requeue_frames(lapb);
		} else {
			lapb->frmr_data = *frame;
			lapb->frmr_type = LAPB_FRMR_Z;
			lapb_transmit_frmr(lapb);
			lapb_dbg(0, "(%p) S3 -> S4\n", lapb->dev);
			lapb_start_t1timer(lapb);
			lapb_stop_t2timer(lapb);
			lapb->state   = LAPB_STATE_4;
			lapb->n2count = 0;
		}
		break;

	case LAPB_I:
		lapb_dbg(1, "(%p) S3 RX I(%d) S%d R%d\n",
			 lapb->dev, frame->pf, frame->ns, frame->nr);
		if (!lapb_validate_nr(lapb, frame->nr)) {
			lapb->frmr_data = *frame;
			lapb->frmr_type = LAPB_FRMR_Z;
			lapb_transmit_frmr(lapb);
			lapb_dbg(0, "(%p) S3 -> S4\n", lapb->dev);
			lapb_start_t1timer(lapb);
			lapb_stop_t2timer(lapb);
			lapb->state   = LAPB_STATE_4;
			lapb->n2count = 0;
			break;
		}
		if (lapb->condition & LAPB_PEER_RX_BUSY_CONDITION)
			lapb_frames_acked(lapb, frame->nr);
		else
			lapb_check_iframes_acked(lapb, frame->nr);

		if (frame->ns == lapb->vr) {
			int cn;
			cn = lapb_data_indication(lapb, skb);
			queued = 1;
			/*
			 * If upper layer has dropped the frame, we
			 * basically ignore any further protocol
			 * processing. This will cause the peer
			 * to re-transmit the frame later like
			 * a frame lost on the wire.
			 */
			if (cn == NET_RX_DROP) {
				pr_debug("rx congestion\n");
				break;
			}
			lapb->vr = (lapb->vr + 1) % modulus;
			lapb->condition &= ~LAPB_REJECT_CONDITION;
			if (frame->pf)
				lapb_enquiry_response(lapb);
			else {
				if (!(lapb->condition &
				      LAPB_ACK_PENDING_CONDITION)) {
					lapb->condition |= LAPB_ACK_PENDING_CONDITION;
					lapb_start_t2timer(lapb);
				}
			}
		} else {
			if (lapb->condition & LAPB_REJECT_CONDITION) {
				if (frame->pf)
					lapb_enquiry_response(lapb);
			} else {
				lapb_dbg(1, "(%p) S3 TX REJ(%d) R%d\n",
					 lapb->dev, frame->pf, lapb->vr);
				lapb->condition |= LAPB_REJECT_CONDITION;
				lapb_send_control(lapb, LAPB_REJ, frame->pf,
						  LAPB_RESPONSE);
				lapb->condition &= ~LAPB_ACK_PENDING_CONDITION;
			}
		}
		break;

	case LAPB_FRMR:
		lapb_dbg(1, "(%p) S3 RX FRMR(%d) %02X %02X %02X %02X %02X\n",
			 lapb->dev, frame->pf,
			 skb->data[0], skb->data[1], skb->data[2],
			 skb->data[3], skb->data[4]);
		lapb_establish_data_link(lapb);
		lapb_dbg(0, "(%p) S3 -> S1\n", lapb->dev);
		lapb_requeue_frames(lapb);
		lapb->state = LAPB_STATE_1;
		break;

	case LAPB_ILLEGAL:
		lapb_dbg(1, "(%p) S3 RX ILLEGAL(%d)\n", lapb->dev, frame->pf);
		lapb->frmr_data = *frame;
		lapb->frmr_type = LAPB_FRMR_W;
		lapb_transmit_frmr(lapb);
		lapb_dbg(0, "(%p) S3 -> S4\n", lapb->dev);
		lapb_start_t1timer(lapb);
		lapb_stop_t2timer(lapb);
		lapb->state   = LAPB_STATE_4;
		lapb->n2count = 0;
		break;
	}

	if (!queued)
		kfree_skb(skb);
}

/*
 *	State machine for state 4, Frame Reject State.
 *	The handling of the timer(s) is in file lapb_timer.c.
 */
static void lapb_state4_machine(struct lapb_cb *lapb, struct sk_buff *skb,
				struct lapb_frame *frame)
{
	switch (frame->type) {
	case LAPB_SABM:
		lapb_dbg(1, "(%p) S4 RX SABM(%d)\n", lapb->dev, frame->pf);
		if (lapb->mode & LAPB_EXTENDED) {
			lapb_dbg(1, "(%p) S4 TX DM(%d)\n",
				 lapb->dev, frame->pf);
			lapb_send_control(lapb, LAPB_DM, frame->pf,
					  LAPB_RESPONSE);
		} else {
			lapb_dbg(1, "(%p) S4 TX UA(%d)\n",
				 lapb->dev, frame->pf);
			lapb_dbg(0, "(%p) S4 -> S3\n", lapb->dev);
			lapb_send_control(lapb, LAPB_UA, frame->pf,
					  LAPB_RESPONSE);
			lapb_stop_t1timer(lapb);
			lapb_stop_t2timer(lapb);
			lapb->state     = LAPB_STATE_3;
			lapb->condition = 0x00;
			lapb->n2count   = 0;
			lapb->vs        = 0;
			lapb->vr        = 0;
			lapb->va        = 0;
			lapb_connect_indication(lapb, LAPB_OK);
		}
		break;

	case LAPB_SABME:
		lapb_dbg(1, "(%p) S4 RX SABME(%d)\n", lapb->dev, frame->pf);
		if (lapb->mode & LAPB_EXTENDED) {
			lapb_dbg(1, "(%p) S4 TX UA(%d)\n",
				 lapb->dev, frame->pf);
			lapb_dbg(0, "(%p) S4 -> S3\n", lapb->dev);
			lapb_send_control(lapb, LAPB_UA, frame->pf,
					  LAPB_RESPONSE);
			lapb_stop_t1timer(lapb);
			lapb_stop_t2timer(lapb);
			lapb->state     = LAPB_STATE_3;
			lapb->condition = 0x00;
			lapb->n2count   = 0;
			lapb->vs        = 0;
			lapb->vr        = 0;
			lapb->va        = 0;
			lapb_connect_indication(lapb, LAPB_OK);
		} else {
			lapb_dbg(1, "(%p) S4 TX DM(%d)\n",
				 lapb->dev, frame->pf);
			lapb_send_control(lapb, LAPB_DM, frame->pf,
					  LAPB_RESPONSE);
		}
		break;
	}

	kfree_skb(skb);
}

/*
 *	Process an incoming LAPB frame
 */
void lapb_data_input(struct lapb_cb *lapb, struct sk_buff *skb)
{
	struct lapb_frame frame;

	if (lapb_decode(lapb, skb, &frame) < 0) {
		kfree_skb(skb);
		return;
	}

	switch (lapb->state) {
	case LAPB_STATE_0:
		lapb_state0_machine(lapb, skb, &frame); break;
	case LAPB_STATE_1:
		lapb_state1_machine(lapb, skb, &frame); break;
	case LAPB_STATE_2:
		lapb_state2_machine(lapb, skb, &frame); break;
	case LAPB_STATE_3:
		lapb_state3_machine(lapb, skb, &frame); break;
	case LAPB_STATE_4:
		lapb_state4_machine(lapb, skb, &frame); break;
	}

	lapb_kick(lapb);
}
re using it. */ magic_empty = const_cpu_to_le32(0xffffffff) /* Record is empty. */ }; typedef le32 NTFS_RECORD_TYPE; /* * Generic magic comparison macros. Finally found a use for the ## preprocessor * operator! (-8 */ static inline bool __ntfs_is_magic(le32 x, NTFS_RECORD_TYPE r) { return (x == r); } #define ntfs_is_magic(x, m) __ntfs_is_magic(x, magic_##m) static inline bool __ntfs_is_magicp(le32 *p, NTFS_RECORD_TYPE r) { return (*p == r); } #define ntfs_is_magicp(p, m) __ntfs_is_magicp(p, magic_##m) /* * Specialised magic comparison macros for the NTFS_RECORD_TYPEs defined above. */ #define ntfs_is_file_record(x) ( ntfs_is_magic (x, FILE) ) #define ntfs_is_file_recordp(p) ( ntfs_is_magicp(p, FILE) ) #define ntfs_is_mft_record(x) ( ntfs_is_file_record (x) ) #define ntfs_is_mft_recordp(p) ( ntfs_is_file_recordp(p) ) #define ntfs_is_indx_record(x) ( ntfs_is_magic (x, INDX) ) #define ntfs_is_indx_recordp(p) ( ntfs_is_magicp(p, INDX) ) #define ntfs_is_hole_record(x) ( ntfs_is_magic (x, HOLE) ) #define ntfs_is_hole_recordp(p) ( ntfs_is_magicp(p, HOLE) ) #define ntfs_is_rstr_record(x) ( ntfs_is_magic (x, RSTR) ) #define ntfs_is_rstr_recordp(p) ( ntfs_is_magicp(p, RSTR) ) #define ntfs_is_rcrd_record(x) ( ntfs_is_magic (x, RCRD) ) #define ntfs_is_rcrd_recordp(p) ( ntfs_is_magicp(p, RCRD) ) #define ntfs_is_chkd_record(x) ( ntfs_is_magic (x, CHKD) ) #define ntfs_is_chkd_recordp(p) ( ntfs_is_magicp(p, CHKD) ) #define ntfs_is_baad_record(x) ( ntfs_is_magic (x, BAAD) ) #define ntfs_is_baad_recordp(p) ( ntfs_is_magicp(p, BAAD) ) #define ntfs_is_empty_record(x) ( ntfs_is_magic (x, empty) ) #define ntfs_is_empty_recordp(p) ( ntfs_is_magicp(p, empty) ) /* * The Update Sequence Array (usa) is an array of the le16 values which belong * to the end of each sector protected by the update sequence record in which * this array is contained. Note that the first entry is the Update Sequence * Number (usn), a cyclic counter of how many times the protected record has * been written to disk. The values 0 and -1 (ie. 0xffff) are not used. All * last le16's of each sector have to be equal to the usn (during reading) or * are set to it (during writing). If they are not, an incomplete multi sector * transfer has occurred when the data was written. * The maximum size for the update sequence array is fixed to: * maximum size = usa_ofs + (usa_count * 2) = 510 bytes * The 510 bytes comes from the fact that the last le16 in the array has to * (obviously) finish before the last le16 of the first 512-byte sector. * This formula can be used as a consistency check in that usa_ofs + * (usa_count * 2) has to be less than or equal to 510. */ typedef struct { NTFS_RECORD_TYPE magic; /* A four-byte magic identifying the record type and/or status. */ le16 usa_ofs; /* Offset to the Update Sequence Array (usa) from the start of the ntfs record. */ le16 usa_count; /* Number of le16 sized entries in the usa including the Update Sequence Number (usn), thus the number of fixups is the usa_count minus 1. */ } __attribute__ ((__packed__)) NTFS_RECORD; /* * System files mft record numbers. All these files are always marked as used * in the bitmap attribute of the mft; presumably in order to avoid accidental * allocation for random other mft records. Also, the sequence number for each * of the system files is always equal to their mft record number and it is * never modified. */ typedef enum { FILE_MFT = 0, /* Master file table (mft). Data attribute contains the entries and bitmap attribute records which ones are in use (bit==1). */ FILE_MFTMirr = 1, /* Mft mirror: copy of first four mft records in data attribute. If cluster size > 4kiB, copy of first N mft records, with N = cluster_size / mft_record_size. */ FILE_LogFile = 2, /* Journalling log in data attribute. */ FILE_Volume = 3, /* Volume name attribute and volume information attribute (flags and ntfs version). Windows refers to this file as volume DASD (Direct Access Storage Device). */ FILE_AttrDef = 4, /* Array of attribute definitions in data attribute. */ FILE_root = 5, /* Root directory. */ FILE_Bitmap = 6, /* Allocation bitmap of all clusters (lcns) in data attribute. */ FILE_Boot = 7, /* Boot sector (always at cluster 0) in data attribute. */ FILE_BadClus = 8, /* Contains all bad clusters in the non-resident data attribute. */ FILE_Secure = 9, /* Shared security descriptors in data attribute and two indexes into the descriptors. Appeared in Windows 2000. Before that, this file was named $Quota but was unused. */ FILE_UpCase = 10, /* Uppercase equivalents of all 65536 Unicode characters in data attribute. */ FILE_Extend = 11, /* Directory containing other system files (eg. $ObjId, $Quota, $Reparse and $UsnJrnl). This is new to NTFS3.0. */ FILE_reserved12 = 12, /* Reserved for future use (records 12-15). */ FILE_reserved13 = 13, FILE_reserved14 = 14, FILE_reserved15 = 15, FILE_first_user = 16, /* First user file, used as test limit for whether to allow opening a file or not. */ } NTFS_SYSTEM_FILES; /* * These are the so far known MFT_RECORD_* flags (16-bit) which contain * information about the mft record in which they are present. */ enum { MFT_RECORD_IN_USE = const_cpu_to_le16(0x0001), MFT_RECORD_IS_DIRECTORY = const_cpu_to_le16(0x0002), } __attribute__ ((__packed__)); typedef le16 MFT_RECORD_FLAGS; /* * mft references (aka file references or file record segment references) are * used whenever a structure needs to refer to a record in the mft. * * A reference consists of a 48-bit index into the mft and a 16-bit sequence * number used to detect stale references. * * For error reporting purposes we treat the 48-bit index as a signed quantity. * * The sequence number is a circular counter (skipping 0) describing how many * times the referenced mft record has been (re)used. This has to match the * sequence number of the mft record being referenced, otherwise the reference * is considered stale and removed (FIXME: only ntfsck or the driver itself?). * * If the sequence number is zero it is assumed that no sequence number * consistency checking should be performed. * * FIXME: Since inodes are 32-bit as of now, the driver needs to always check * for high_part being 0 and if not either BUG(), cause a panic() or handle * the situation in some other way. This shouldn't be a problem as a volume has * to become HUGE in order to need more than 32-bits worth of mft records. * Assuming the standard mft record size of 1kb only the records (never mind * the non-resident attributes, etc.) would require 4Tb of space on their own * for the first 32 bits worth of records. This is only if some strange person * doesn't decide to foul play and make the mft sparse which would be a really * horrible thing to do as it would trash our current driver implementation. )-: * Do I hear screams "we want 64-bit inodes!" ?!? (-; * * FIXME: The mft zone is defined as the first 12% of the volume. This space is * reserved so that the mft can grow contiguously and hence doesn't become * fragmented. Volume free space includes the empty part of the mft zone and * when the volume's free 88% are used up, the mft zone is shrunk by a factor * of 2, thus making more space available for more files/data. This process is * repeated everytime there is no more free space except for the mft zone until * there really is no more free space. */ /* * Typedef the MFT_REF as a 64-bit value for easier handling. * Also define two unpacking macros to get to the reference (MREF) and * sequence number (MSEQNO) respectively. * The _LE versions are to be applied on little endian MFT_REFs. * Note: The _LE versions will return a CPU endian formatted value! */ #define MFT_REF_MASK_CPU 0x0000ffffffffffffULL #define MFT_REF_MASK_LE const_cpu_to_le64(MFT_REF_MASK_CPU) typedef u64 MFT_REF; typedef le64 leMFT_REF; #define MK_MREF(m, s) ((MFT_REF)(((MFT_REF)(s) << 48) | \ ((MFT_REF)(m) & MFT_REF_MASK_CPU))) #define MK_LE_MREF(m, s) cpu_to_le64(MK_MREF(m, s)) #define MREF(x) ((unsigned long)((x) & MFT_REF_MASK_CPU)) #define MSEQNO(x) ((u16)(((x) >> 48) & 0xffff)) #define MREF_LE(x) ((unsigned long)(le64_to_cpu(x) & MFT_REF_MASK_CPU)) #define MSEQNO_LE(x) ((u16)((le64_to_cpu(x) >> 48) & 0xffff)) #define IS_ERR_MREF(x) (((x) & 0x0000800000000000ULL) ? true : false) #define ERR_MREF(x) ((u64)((s64)(x))) #define MREF_ERR(x) ((int)((s64)(x))) /* * The mft record header present at the beginning of every record in the mft. * This is followed by a sequence of variable length attribute records which * is terminated by an attribute of type AT_END which is a truncated attribute * in that it only consists of the attribute type code AT_END and none of the * other members of the attribute structure are present. */ typedef struct { /*Ofs*/ /* 0 NTFS_RECORD; -- Unfolded here as gcc doesn't like unnamed structs. */ NTFS_RECORD_TYPE magic; /* Usually the magic is "FILE". */ le16 usa_ofs; /* See NTFS_RECORD definition above. */ le16 usa_count; /* See NTFS_RECORD definition above. */ /* 8*/ le64 lsn; /* $LogFile sequence number for this record. Changed every time the record is modified. */ /* 16*/ le16 sequence_number; /* Number of times this mft record has been reused. (See description for MFT_REF above.) NOTE: The increment (skipping zero) is done when the file is deleted. NOTE: If this is zero it is left zero. */ /* 18*/ le16 link_count; /* Number of hard links, i.e. the number of directory entries referencing this record. NOTE: Only used in mft base records. NOTE: When deleting a directory entry we check the link_count and if it is 1 we delete the file. Otherwise we delete the FILE_NAME_ATTR being referenced by the directory entry from the mft record and decrement the link_count. FIXME: Careful with Win32 + DOS names! */ /* 20*/ le16 attrs_offset; /* Byte offset to the first attribute in this mft record from the start of the mft record. NOTE: Must be aligned to 8-byte boundary. */ /* 22*/ MFT_RECORD_FLAGS flags; /* Bit array of MFT_RECORD_FLAGS. When a file is deleted, the MFT_RECORD_IN_USE flag is set to zero. */ /* 24*/ le32 bytes_in_use; /* Number of bytes used in this mft record. NOTE: Must be aligned to 8-byte boundary. */ /* 28*/ le32 bytes_allocated; /* Number of bytes allocated for this mft record. This should be equal to the mft record size. */ /* 32*/ leMFT_REF base_mft_record;/* This is zero for base mft records. When it is not zero it is a mft reference pointing to the base mft record to which this record belongs (this is then used to locate the attribute list attribute present in the base record which describes this extension record and hence might need modification when the extension record itself is modified, also locating the attribute list also means finding the other potential extents, belonging to the non-base mft record). */ /* 40*/ le16 next_attr_instance;/* The instance number that will be assigned to the next attribute added to this mft record. NOTE: Incremented each time after it is used. NOTE: Every time the mft record is reused this number is set to zero. NOTE: The first instance number is always 0. */ /* The below fields are specific to NTFS 3.1+ (Windows XP and above): */ /* 42*/ le16 reserved; /* Reserved/alignment. */ /* 44*/ le32 mft_record_number; /* Number of this mft record. */ /* sizeof() = 48 bytes */ /* * When (re)using the mft record, we place the update sequence array at this * offset, i.e. before we start with the attributes. This also makes sense, * otherwise we could run into problems with the update sequence array * containing in itself the last two bytes of a sector which would mean that * multi sector transfer protection wouldn't work. As you can't protect data * by overwriting it since you then can't get it back... * When reading we obviously use the data from the ntfs record header. */ } __attribute__ ((__packed__)) MFT_RECORD; /* This is the version without the NTFS 3.1+ specific fields. */ typedef struct { /*Ofs*/ /* 0 NTFS_RECORD; -- Unfolded here as gcc doesn't like unnamed structs. */ NTFS_RECORD_TYPE magic; /* Usually the magic is "FILE". */ le16 usa_ofs; /* See NTFS_RECORD definition above. */ le16 usa_count; /* See NTFS_RECORD definition above. */ /* 8*/ le64 lsn; /* $LogFile sequence number for this record. Changed every time the record is modified. */ /* 16*/ le16 sequence_number; /* Number of times this mft record has been reused. (See description for MFT_REF above.) NOTE: The increment (skipping zero) is done when the file is deleted. NOTE: If this is zero it is left zero. */ /* 18*/ le16 link_count; /* Number of hard links, i.e. the number of directory entries referencing this record. NOTE: Only used in mft base records. NOTE: When deleting a directory entry we check the link_count and if it is 1 we delete the file. Otherwise we delete the FILE_NAME_ATTR being referenced by the directory entry from the mft record and decrement the link_count. FIXME: Careful with Win32 + DOS names! */ /* 20*/ le16 attrs_offset; /* Byte offset to the first attribute in this mft record from the start of the mft record. NOTE: Must be aligned to 8-byte boundary. */ /* 22*/ MFT_RECORD_FLAGS flags; /* Bit array of MFT_RECORD_FLAGS. When a file is deleted, the MFT_RECORD_IN_USE flag is set to zero. */ /* 24*/ le32 bytes_in_use; /* Number of bytes used in this mft record. NOTE: Must be aligned to 8-byte boundary. */ /* 28*/ le32 bytes_allocated; /* Number of bytes allocated for this mft record. This should be equal to the mft record size. */ /* 32*/ leMFT_REF base_mft_record;/* This is zero for base mft records. When it is not zero it is a mft reference pointing to the base mft record to which this record belongs (this is then used to locate the attribute list attribute present in the base record which describes this extension record and hence might need modification when the extension record itself is modified, also locating the attribute list also means finding the other potential extents, belonging to the non-base mft record). */ /* 40*/ le16 next_attr_instance;/* The instance number that will be assigned to the next attribute added to this mft record. NOTE: Incremented each time after it is used. NOTE: Every time the mft record is reused this number is set to zero. NOTE: The first instance number is always 0. */ /* sizeof() = 42 bytes */ /* * When (re)using the mft record, we place the update sequence array at this * offset, i.e. before we start with the attributes. This also makes sense, * otherwise we could run into problems with the update sequence array * containing in itself the last two bytes of a sector which would mean that * multi sector transfer protection wouldn't work. As you can't protect data * by overwriting it since you then can't get it back... * When reading we obviously use the data from the ntfs record header. */ } __attribute__ ((__packed__)) MFT_RECORD_OLD; /* * System defined attributes (32-bit). Each attribute type has a corresponding * attribute name (Unicode string of maximum 64 character length) as described * by the attribute definitions present in the data attribute of the $AttrDef * system file. On NTFS 3.0 volumes the names are just as the types are named * in the below defines exchanging AT_ for the dollar sign ($). If that is not * a revealing choice of symbol I do not know what is... (-; */ enum { AT_UNUSED = const_cpu_to_le32( 0), AT_STANDARD_INFORMATION = const_cpu_to_le32( 0x10), AT_ATTRIBUTE_LIST = const_cpu_to_le32( 0x20), AT_FILE_NAME = const_cpu_to_le32( 0x30), AT_OBJECT_ID = const_cpu_to_le32( 0x40), AT_SECURITY_DESCRIPTOR = const_cpu_to_le32( 0x50), AT_VOLUME_NAME = const_cpu_to_le32( 0x60), AT_VOLUME_INFORMATION = const_cpu_to_le32( 0x70), AT_DATA = const_cpu_to_le32( 0x80), AT_INDEX_ROOT = const_cpu_to_le32( 0x90), AT_INDEX_ALLOCATION = const_cpu_to_le32( 0xa0), AT_BITMAP = const_cpu_to_le32( 0xb0), AT_REPARSE_POINT = const_cpu_to_le32( 0xc0), AT_EA_INFORMATION = const_cpu_to_le32( 0xd0), AT_EA = const_cpu_to_le32( 0xe0), AT_PROPERTY_SET = const_cpu_to_le32( 0xf0), AT_LOGGED_UTILITY_STREAM = const_cpu_to_le32( 0x100), AT_FIRST_USER_DEFINED_ATTRIBUTE = const_cpu_to_le32( 0x1000), AT_END = const_cpu_to_le32(0xffffffff) }; typedef le32 ATTR_TYPE; /* * The collation rules for sorting views/indexes/etc (32-bit). * * COLLATION_BINARY - Collate by binary compare where the first byte is most * significant. * COLLATION_UNICODE_STRING - Collate Unicode strings by comparing their binary * Unicode values, except that when a character can be uppercased, the * upper case value collates before the lower case one. * COLLATION_FILE_NAME - Collate file names as Unicode strings. The collation * is done very much like COLLATION_UNICODE_STRING. In fact I have no idea * what the difference is. Perhaps the difference is that file names * would treat some special characters in an odd way (see * unistr.c::ntfs_collate_names() and unistr.c::legal_ansi_char_array[] * for what I mean but COLLATION_UNICODE_STRING would not give any special * treatment to any characters at all, but this is speculation. * COLLATION_NTOFS_ULONG - Sorting is done according to ascending le32 key * values. E.g. used for $SII index in FILE_Secure, which sorts by * security_id (le32). * COLLATION_NTOFS_SID - Sorting is done according to ascending SID values. * E.g. used for $O index in FILE_Extend/$Quota. * COLLATION_NTOFS_SECURITY_HASH - Sorting is done first by ascending hash * values and second by ascending security_id values. E.g. used for $SDH * index in FILE_Secure. * COLLATION_NTOFS_ULONGS - Sorting is done according to a sequence of ascending * le32 key values. E.g. used for $O index in FILE_Extend/$ObjId, which * sorts by object_id (16-byte), by splitting up the object_id in four * le32 values and using them as individual keys. E.g. take the following * two security_ids, stored as follows on disk: * 1st: a1 61 65 b7 65 7b d4 11 9e 3d 00 e0 81 10 42 59 * 2nd: 38 14 37 d2 d2 f3 d4 11 a5 21 c8 6b 79 b1 97 45 * To compare them, they are split into four le32 values each, like so: * 1st: 0xb76561a1 0x11d47b65 0xe0003d9e 0x59421081 * 2nd: 0xd2371438 0x11d4f3d2 0x6bc821a5 0x4597b179 * Now, it is apparent why the 2nd object_id collates after the 1st: the * first le32 value of the 1st object_id is less than the first le32 of * the 2nd object_id. If the first le32 values of both object_ids were * equal then the second le32 values would be compared, etc. */ enum { COLLATION_BINARY = const_cpu_to_le32(0x00), COLLATION_FILE_NAME = const_cpu_to_le32(0x01), COLLATION_UNICODE_STRING = const_cpu_to_le32(0x02), COLLATION_NTOFS_ULONG = const_cpu_to_le32(0x10), COLLATION_NTOFS_SID = const_cpu_to_le32(0x11), COLLATION_NTOFS_SECURITY_HASH = const_cpu_to_le32(0x12), COLLATION_NTOFS_ULONGS = const_cpu_to_le32(0x13), }; typedef le32 COLLATION_RULE; /* * The flags (32-bit) describing attribute properties in the attribute * definition structure. FIXME: This information is based on Regis's * information and, according to him, it is not certain and probably * incomplete. The INDEXABLE flag is fairly certainly correct as only the file * name attribute has this flag set and this is the only attribute indexed in * NT4. */ enum { ATTR_DEF_INDEXABLE = const_cpu_to_le32(0x02), /* Attribute can be indexed. */ ATTR_DEF_MULTIPLE = const_cpu_to_le32(0x04), /* Attribute type can be present multiple times in the mft records of an inode. */ ATTR_DEF_NOT_ZERO = const_cpu_to_le32(0x08), /* Attribute value must contain at least one non-zero byte. */ ATTR_DEF_INDEXED_UNIQUE = const_cpu_to_le32(0x10), /* Attribute must be indexed and the attribute value must be unique for the attribute type in all of the mft records of an inode. */ ATTR_DEF_NAMED_UNIQUE = const_cpu_to_le32(0x20), /* Attribute must be named and the name must be unique for the attribute type in all of the mft records of an inode. */ ATTR_DEF_RESIDENT = const_cpu_to_le32(0x40), /* Attribute must be resident. */ ATTR_DEF_ALWAYS_LOG = const_cpu_to_le32(0x80), /* Always log modifications to this attribute, regardless of whether it is resident or non-resident. Without this, only log modifications if the attribute is resident. */ }; typedef le32 ATTR_DEF_FLAGS; /* * The data attribute of FILE_AttrDef contains a sequence of attribute * definitions for the NTFS volume. With this, it is supposed to be safe for an * older NTFS driver to mount a volume containing a newer NTFS version without * damaging it (that's the theory. In practice it's: not damaging it too much). * Entries are sorted by attribute type. The flags describe whether the * attribute can be resident/non-resident and possibly other things, but the * actual bits are unknown. */ typedef struct { /*hex ofs*/ /* 0*/ ntfschar name[0x40]; /* Unicode name of the attribute. Zero terminated. */ /* 80*/ ATTR_TYPE type; /* Type of the attribute. */ /* 84*/ le32 display_rule; /* Default display rule. FIXME: What does it mean? (AIA) */ /* 88*/ COLLATION_RULE collation_rule; /* Default collation rule. */ /* 8c*/ ATTR_DEF_FLAGS flags; /* Flags describing the attribute. */ /* 90*/ sle64 min_size; /* Optional minimum attribute size. */ /* 98*/ sle64 max_size; /* Maximum size of attribute. */ /* sizeof() = 0xa0 or 160 bytes */ } __attribute__ ((__packed__)) ATTR_DEF; /* * Attribute flags (16-bit). */ enum { ATTR_IS_COMPRESSED = const_cpu_to_le16(0x0001), ATTR_COMPRESSION_MASK = const_cpu_to_le16(0x00ff), /* Compression method mask. Also, first illegal value. */ ATTR_IS_ENCRYPTED = const_cpu_to_le16(0x4000), ATTR_IS_SPARSE = const_cpu_to_le16(0x8000), } __attribute__ ((__packed__)); typedef le16 ATTR_FLAGS; /* * Attribute compression. * * Only the data attribute is ever compressed in the current ntfs driver in * Windows. Further, compression is only applied when the data attribute is * non-resident. Finally, to use compression, the maximum allowed cluster size * on a volume is 4kib. * * The compression method is based on independently compressing blocks of X * clusters, where X is determined from the compression_unit value found in the * non-resident attribute record header (more precisely: X = 2^compression_unit * clusters). On Windows NT/2k, X always is 16 clusters (compression_unit = 4). * * There are three different cases of how a compression block of X clusters * can be stored: * * 1) The data in the block is all zero (a sparse block): * This is stored as a sparse block in the runlist, i.e. the runlist * entry has length = X and lcn = -1. The mapping pairs array actually * uses a delta_lcn value length of 0, i.e. delta_lcn is not present at * all, which is then interpreted by the driver as lcn = -1. * NOTE: Even uncompressed files can be sparse on NTFS 3.0 volumes, then * the same principles apply as above, except that the length is not * restricted to being any particular value. * * 2) The data in the block is not compressed: * This happens when compression doesn't reduce the size of the block * in clusters. I.e. if compression has a small effect so that the * compressed data still occupies X clusters, then the uncompressed data * is stored in the block. * This case is recognised by the fact that the runlist entry has * length = X and lcn >= 0. The mapping pairs array stores this as * normal with a run length of X and some specific delta_lcn, i.e. * delta_lcn has to be present. * * 3) The data in the block is compressed: * The common case. This case is recognised by the fact that the run * list entry has length L < X and lcn >= 0. The mapping pairs array * stores this as normal with a run length of X and some specific * delta_lcn, i.e. delta_lcn has to be present. This runlist entry is * immediately followed by a sparse entry with length = X - L and * lcn = -1. The latter entry is to make up the vcn counting to the * full compression block size X. * * In fact, life is more complicated because adjacent entries of the same type * can be coalesced. This means that one has to keep track of the number of * clusters handled and work on a basis of X clusters at a time being one * block. An example: if length L > X this means that this particular runlist * entry contains a block of length X and part of one or more blocks of length * L - X. Another example: if length L < X, this does not necessarily mean that * the block is compressed as it might be that the lcn changes inside the block * and hence the following runlist entry describes the continuation of the * potentially compressed block. The block would be compressed if the * following runlist entry describes at least X - L sparse clusters, thus * making up the compression block length as described in point 3 above. (Of * course, there can be several runlist entries with small lengths so that the * sparse entry does not follow the first data containing entry with * length < X.) * * NOTE: At the end of the compressed attribute value, there most likely is not * just the right amount of data to make up a compression block, thus this data * is not even attempted to be compressed. It is just stored as is, unless * the number of clusters it occupies is reduced when compressed in which case * it is stored as a compressed compression block, complete with sparse * clusters at the end. */ /* * Flags of resident attributes (8-bit). */ enum { RESIDENT_ATTR_IS_INDEXED = 0x01, /* Attribute is referenced in an index (has implications for deleting and modifying the attribute). */ } __attribute__ ((__packed__)); typedef u8 RESIDENT_ATTR_FLAGS; /* * Attribute record header. Always aligned to 8-byte boundary. */ typedef struct { /*Ofs*/ /* 0*/ ATTR_TYPE type; /* The (32-bit) type of the attribute. */ /* 4*/ le32 length; /* Byte size of the resident part of the attribute (aligned to 8-byte boundary). Used to get to the next attribute. */ /* 8*/ u8 non_resident; /* If 0, attribute is resident. If 1, attribute is non-resident. */ /* 9*/ u8 name_length; /* Unicode character size of name of attribute. 0 if unnamed. */ /* 10*/ le16 name_offset; /* If name_length != 0, the byte offset to the beginning of the name from the attribute record. Note that the name is stored as a Unicode string. When creating, place offset just at the end of the record header. Then, follow with attribute value or mapping pairs array, resident and non-resident attributes respectively, aligning to an 8-byte boundary. */ /* 12*/ ATTR_FLAGS flags; /* Flags describing the attribute. */ /* 14*/ le16 instance; /* The instance of this attribute record. This number is unique within this mft record (see MFT_RECORD/next_attribute_instance notes in in mft.h for more details). */ /* 16*/ union { /* Resident attributes. */ struct { /* 16 */ le32 value_length;/* Byte size of attribute value. */ /* 20 */ le16 value_offset;/* Byte offset of the attribute value from the start of the attribute record. When creating, align to 8-byte boundary if we have a name present as this might not have a length of a multiple of 8-bytes. */ /* 22 */ RESIDENT_ATTR_FLAGS flags; /* See above. */ /* 23 */ s8 reserved; /* Reserved/alignment to 8-byte boundary. */ } __attribute__ ((__packed__)) resident; /* Non-resident attributes. */ struct { /* 16*/ leVCN lowest_vcn;/* Lowest valid virtual cluster number for this portion of the attribute value or 0 if this is the only extent (usually the case). - Only when an attribute list is used does lowest_vcn != 0 ever occur. */ /* 24*/ leVCN highest_vcn;/* Highest valid vcn of this extent of the attribute value. - Usually there is only one portion, so this usually equals the attribute value size in clusters minus 1. Can be -1 for zero length files. Can be 0 for "single extent" attributes. */ /* 32*/ le16 mapping_pairs_offset; /* Byte offset from the beginning of the structure to the mapping pairs array which contains the mappings between the vcns and the logical cluster numbers (lcns). When creating, place this at the end of this record header aligned to 8-byte boundary. */ /* 34*/ u8 compression_unit; /* The compression unit expressed as the log to the base 2 of the number of clusters in a compression unit. 0 means not compressed. (This effectively limits the compression unit size to be a power of two clusters.) WinNT4 only uses a value of 4. Sparse files have this set to 0 on XPSP2. */ /* 35*/ u8 reserved[5]; /* Align to 8-byte boundary. */ /* The sizes below are only used when lowest_vcn is zero, as otherwise it would be difficult to keep them up-to-date.*/ /* 40*/ sle64 allocated_size; /* Byte size of disk space allocated to hold the attribute value. Always is a multiple of the cluster size. When a file is compressed, this field is a multiple of the compression block size (2^compression_unit) and it represents the logically allocated space rather than the actual on disk usage. For this use the compressed_size (see below). */ /* 48*/ sle64 data_size; /* Byte size of the attribute value. Can be larger than allocated_size if attribute value is compressed or sparse. */ /* 56*/ sle64 initialized_size; /* Byte size of initialized portion of the attribute value. Usually equals data_size. */ /* sizeof(uncompressed attr) = 64*/ /* 64*/ sle64 compressed_size; /* Byte size of the attribute value after compression. Only present when compressed or sparse. Always is a multiple of the cluster size. Represents the actual amount of disk space being used on the disk. */ /* sizeof(compressed attr) = 72*/ } __attribute__ ((__packed__)) non_resident; } __attribute__ ((__packed__)) data; } __attribute__ ((__packed__)) ATTR_RECORD; typedef ATTR_RECORD ATTR_REC; /* * File attribute flags (32-bit) appearing in the file_attributes fields of the * STANDARD_INFORMATION attribute of MFT_RECORDs and the FILENAME_ATTR * attributes of MFT_RECORDs and directory index entries. * * All of the below flags appear in the directory index entries but only some * appear in the STANDARD_INFORMATION attribute whilst only some others appear * in the FILENAME_ATTR attribute of MFT_RECORDs. Unless otherwise stated the * flags appear in all of the above. */ enum { FILE_ATTR_READONLY = const_cpu_to_le32(0x00000001), FILE_ATTR_HIDDEN = const_cpu_to_le32(0x00000002), FILE_ATTR_SYSTEM = const_cpu_to_le32(0x00000004), /* Old DOS volid. Unused in NT. = const_cpu_to_le32(0x00000008), */ FILE_ATTR_DIRECTORY = const_cpu_to_le32(0x00000010), /* Note, FILE_ATTR_DIRECTORY is not considered valid in NT. It is reserved for the DOS SUBDIRECTORY flag. */ FILE_ATTR_ARCHIVE = const_cpu_to_le32(0x00000020), FILE_ATTR_DEVICE = const_cpu_to_le32(0x00000040), FILE_ATTR_NORMAL = const_cpu_to_le32(0x00000080), FILE_ATTR_TEMPORARY = const_cpu_to_le32(0x00000100), FILE_ATTR_SPARSE_FILE = const_cpu_to_le32(0x00000200), FILE_ATTR_REPARSE_POINT = const_cpu_to_le32(0x00000400), FILE_ATTR_COMPRESSED = const_cpu_to_le32(0x00000800), FILE_ATTR_OFFLINE = const_cpu_to_le32(0x00001000), FILE_ATTR_NOT_CONTENT_INDEXED = const_cpu_to_le32(0x00002000), FILE_ATTR_ENCRYPTED = const_cpu_to_le32(0x00004000), FILE_ATTR_VALID_FLAGS = const_cpu_to_le32(0x00007fb7), /* Note, FILE_ATTR_VALID_FLAGS masks out the old DOS VolId and the FILE_ATTR_DEVICE and preserves everything else. This mask is used to obtain all flags that are valid for reading. */ FILE_ATTR_VALID_SET_FLAGS = const_cpu_to_le32(0x000031a7), /* Note, FILE_ATTR_VALID_SET_FLAGS masks out the old DOS VolId, the F_A_DEVICE, F_A_DIRECTORY, F_A_SPARSE_FILE, F_A_REPARSE_POINT, F_A_COMPRESSED, and F_A_ENCRYPTED and preserves the rest. This mask is used to to obtain all flags that are valid for setting. */ /* * The flag FILE_ATTR_DUP_FILENAME_INDEX_PRESENT is present in all * FILENAME_ATTR attributes but not in the STANDARD_INFORMATION * attribute of an mft record. */ FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT = const_cpu_to_le32(0x10000000), /* Note, this is a copy of the corresponding bit from the mft record, telling us whether this is a directory or not, i.e. whether it has an index root attribute or not. */ FILE_ATTR_DUP_VIEW_INDEX_PRESENT = const_cpu_to_le32(0x20000000), /* Note, this is a copy of the corresponding bit from the mft record, telling us whether this file has a view index present (eg. object id index, quota index, one of the security indexes or the encrypting filesystem related indexes). */ }; typedef le32 FILE_ATTR_FLAGS; /* * NOTE on times in NTFS: All times are in MS standard time format, i.e. they * are the number of 100-nanosecond intervals since 1st January 1601, 00:00:00 * universal coordinated time (UTC). (In Linux time starts 1st January 1970, * 00:00:00 UTC and is stored as the number of 1-second intervals since then.) */ /* * Attribute: Standard information (0x10). * * NOTE: Always resident. * NOTE: Present in all base file records on a volume. * NOTE: There is conflicting information about the meaning of each of the time * fields but the meaning as defined below has been verified to be * correct by practical experimentation on Windows NT4 SP6a and is hence * assumed to be the one and only correct interpretation. */ typedef struct { /*Ofs*/ /* 0*/ sle64 creation_time; /* Time file was created. Updated when a filename is changed(?). */ /* 8*/ sle64 last_data_change_time; /* Time the data attribute was last modified. */ /* 16*/ sle64 last_mft_change_time; /* Time this mft record was last modified. */ /* 24*/ sle64 last_access_time; /* Approximate time when the file was last accessed (obviously this is not updated on read-only volumes). In Windows this is only updated when accessed if some time delta has passed since the last update. Also, last access time updates can be disabled altogether for speed. */ /* 32*/ FILE_ATTR_FLAGS file_attributes; /* Flags describing the file. */ /* 36*/ union { /* NTFS 1.2 */ struct { /* 36*/ u8 reserved12[12]; /* Reserved/alignment to 8-byte boundary. */ } __attribute__ ((__packed__)) v1; /* sizeof() = 48 bytes */ /* NTFS 3.x */ struct { /* * If a volume has been upgraded from a previous NTFS version, then these * fields are present only if the file has been accessed since the upgrade. * Recognize the difference by comparing the length of the resident attribute * value. If it is 48, then the following fields are missing. If it is 72 then * the fields are present. Maybe just check like this: * if (resident.ValueLength < sizeof(STANDARD_INFORMATION)) { * Assume NTFS 1.2- format. * If (volume version is 3.x) * Upgrade attribute to NTFS 3.x format. * else * Use NTFS 1.2- format for access. * } else * Use NTFS 3.x format for access. * Only problem is that it might be legal to set the length of the value to * arbitrarily large values thus spoiling this check. - But chkdsk probably * views that as a corruption, assuming that it behaves like this for all * attributes. */ /* 36*/ le32 maximum_versions; /* Maximum allowed versions for file. Zero if version numbering is disabled. */ /* 40*/ le32 version_number; /* This file's version (if any). Set to zero if maximum_versions is zero. */ /* 44*/ le32 class_id; /* Class id from bidirectional class id index (?). */ /* 48*/ le32 owner_id; /* Owner_id of the user owning the file. Translate via $Q index in FILE_Extend /$Quota to the quota control entry for the user owning the file. Zero if quotas are disabled. */ /* 52*/ le32 security_id; /* Security_id for the file. Translate via $SII index and $SDS data stream in FILE_Secure to the security descriptor. */ /* 56*/ le64 quota_charged; /* Byte size of the charge to the quota for all streams of the file. Note: Is zero if quotas are disabled. */ /* 64*/ leUSN usn; /* Last update sequence number of the file. This is a direct index into the transaction log file ($UsnJrnl). It is zero if the usn journal is disabled or this file has not been subject to logging yet. See usnjrnl.h for details. */ } __attribute__ ((__packed__)) v3; /* sizeof() = 72 bytes (NTFS 3.x) */ } __attribute__ ((__packed__)) ver; } __attribute__ ((__packed__)) STANDARD_INFORMATION; /* * Attribute: Attribute list (0x20). * * - Can be either resident or non-resident. * - Value consists of a sequence of variable length, 8-byte aligned, * ATTR_LIST_ENTRY records. * - The list is not terminated by anything at all! The only way to know when * the end is reached is to keep track of the current offset and compare it to * the attribute value size. * - The attribute list attribute contains one entry for each attribute of * the file in which the list is located, except for the list attribute * itself. The list is sorted: first by attribute type, second by attribute * name (if present), third by instance number. The extents of one * non-resident attribute (if present) immediately follow after the initial * extent. They are ordered by lowest_vcn and have their instace set to zero. * It is not allowed to have two attributes with all sorting keys equal. * - Further restrictions: * - If not resident, the vcn to lcn mapping array has to fit inside the * base mft record. * - The attribute list attribute value has a maximum size of 256kb. This * is imposed by the Windows cache manager. * - Attribute lists are only used when the attributes of mft record do not * fit inside the mft record despite all attributes (that can be made * non-resident) having been made non-resident. This can happen e.g. when: * - File has a large number of hard links (lots of file name * attributes present). * - The mapping pairs array of some non-resident attribute becomes so * large due to fragmentation that it overflows the mft record. * - The security descriptor is very complex (not applicable to * NTFS 3.0 volumes). * - There are many named streams. */ typedef struct { /*Ofs*/ /* 0*/ ATTR_TYPE type; /* Type of referenced attribute. */ /* 4*/ le16 length; /* Byte size of this entry (8-byte aligned). */ /* 6*/ u8 name_length; /* Size in Unicode chars of the name of the attribute or 0 if unnamed. */ /* 7*/ u8 name_offset; /* Byte offset to beginning of attribute name (always set this to where the name would start even if unnamed). */ /* 8*/ leVCN lowest_vcn; /* Lowest virtual cluster number of this portion of the attribute value. This is usually 0. It is non-zero for the case where one attribute does not fit into one mft record and thus several mft records are allocated to hold this attribute. In the latter case, each mft record holds one extent of the attribute and there is one attribute list entry for each extent. NOTE: This is DEFINITELY a signed value! The windows driver uses cmp, followed by jg when comparing this, thus it treats it as signed. */ /* 16*/ leMFT_REF mft_reference;/* The reference of the mft record holding the ATTR_RECORD for this portion of the attribute value. */ /* 24*/ le16 instance; /* If lowest_vcn = 0, the instance of the attribute being referenced; otherwise 0. */ /* 26*/ ntfschar name[0]; /* Use when creating only. When reading use name_offset to determine the location of the name. */ /* sizeof() = 26 + (attribute_name_length * 2) bytes */ } __attribute__ ((__packed__)) ATTR_LIST_ENTRY; /* * The maximum allowed length for a file name. */ #define MAXIMUM_FILE_NAME_LENGTH 255 /* * Possible namespaces for filenames in ntfs (8-bit). */ enum { FILE_NAME_POSIX = 0x00, /* This is the largest namespace. It is case sensitive and allows all Unicode characters except for: '\0' and '/'. Beware that in WinNT/2k/2003 by default files which eg have the same name except for their case will not be distinguished by the standard utilities and thus a "del filename" will delete both "filename" and "fileName" without warning. However if for example Services For Unix (SFU) are installed and the case sensitive option was enabled at installation time, then you can create/access/delete such files. Note that even SFU places restrictions on the filenames beyond the '\0' and '/' and in particular the following set of characters is not allowed: '"', '/', '<', '>', '\'. All other characters, including the ones no allowed in WIN32 namespace are allowed. Tested with SFU 3.5 (this is now free) running on Windows XP. */ FILE_NAME_WIN32 = 0x01, /* The standard WinNT/2k NTFS long filenames. Case insensitive. All Unicode chars except: '\0', '"', '*', '/', ':', '<', '>', '?', '\', and '|'. Further, names cannot end with a '.' or a space. */ FILE_NAME_DOS = 0x02, /* The standard DOS filenames (8.3 format). Uppercase only. All 8-bit characters greater space, except: '"', '*', '+', ',', '/', ':', ';', '<', '=', '>', '?', and '\'. */ FILE_NAME_WIN32_AND_DOS = 0x03, /* 3 means that both the Win32 and the DOS filenames are identical and hence have been saved in this single filename record. */ } __attribute__ ((__packed__)); typedef u8 FILE_NAME_TYPE_FLAGS; /* * Attribute: Filename (0x30). * * NOTE: Always resident. * NOTE: All fields, except the parent_directory, are only updated when the * filename is changed. Until then, they just become out of sync with