File: reg40.c

package info (click to toggle)
reiser4progs 1.2.2-1
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 5,904 kB
  • sloc: ansic: 34,331; sh: 4,251; makefile: 994
file content (380 lines) | stat: -rw-r--r-- 9,087 bytes parent folder | download | duplicates (3)
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
/* Copyright (C) 2001-2005 by Hans Reiser, licensing governed by
   reiser4progs/COPYING.
   
   reg40.c -- reiser4 regular file plugin. */

#ifndef ENABLE_MINIMAL
#  include <unistd.h>
#endif

#include <aal/libaal.h>
#include "reiser4/plugin.h"
#include "plugin/object/obj40/obj40.h"
#include "reg40_repair.h"

/* Reads @n bytes to passed buffer @buff. Negative values are returned on
   errors. */
static int64_t reg40_read(reiser4_object_t *reg, 
			  void *buff, uint64_t n)
{
	errno_t res;
	int64_t read;
	uint64_t off;
	uint64_t fsize;
	trans_hint_t hint;

	aal_assert("umka-2511", buff != NULL);
	aal_assert("umka-2512", reg != NULL);
	
	if ((res = obj40_update(reg)))
		return res;
	
	fsize = obj40_get_size(reg);
	off = obj40_offset(reg);
	if (off > fsize)
		return 0;

	/* Correcting number of bytes to be read. It cannot be more then file
	   size value from stat data. That is because, body item itself does 
	   not know reliably how long it is. For instnace, extent40. */
	if (n > fsize - off)
		n = fsize - off;
	
	/* Reading data. */
	if ((read = obj40_read(reg, &hint, buff, off, n)) < 0)
		return read;

	/* Updating file offset if needed. */
	if (read > 0)
		obj40_seek(reg, off + read);
	
	return read;
}

#ifndef ENABLE_MINIMAL
/* Returns plugin (tail or extent) for next write operation basing on passed
   @size -- new file size. This function will use tail policy plugin to find
   what kind of next body item should be writen. */
static reiser4_item_plug_t *reg40_policy_plug(reiser4_object_t *reg, 
					      uint64_t new_size)
{
	aal_assert("umka-2394", reg != NULL);

	aal_assert("umka-2393", reiser4_pspolicy(reg) != NULL);

	/* Calling formatting policy plugin to detect body plugin. */
	if (plugcall(reiser4_pspolicy(reg), tails, new_size)) {
		/* Trying to get non-standard tail plugin from stat data. And if
		   it is not found, default one from params will be taken. */
		return reiser4_pstail(reg);
	}
	
	/* The same for extent plugin */
	return reiser4_psextent(reg);
}
#endif

/* Open regular file by passed initial info and return initialized
   instance. This @info struct contains information about the obejct, like its
   statdata coord, etc. */
static errno_t reg40_open(reiser4_object_t *reg) {
	obj40_open(reg);

#ifndef ENABLE_MINIMAL
	{
		lookup_t lookup;
		
		/* Get the body plugin in use. */
		if ((lookup = obj40_update_body(reg, NULL)) < 0) {
			return lookup;
		} else if (lookup > 0) {
			reg->body_plug = reg->body.plug;
		} else {
			reg->body_plug = reg40_policy_plug(reg, 0);
		}
	}
#endif

	return 0;
}

#ifndef ENABLE_MINIMAL
static errno_t reg40_create(reiser4_object_t *reg, object_hint_t *hint) {
	errno_t res;
	
	if ((res = obj40_create(reg, hint)))
		return res;
	
	reg->body_plug = reg40_policy_plug(reg, 0);
	return 0;
}

/* Makes tail2extent and extent2tail conversion. */
static errno_t reg40_convert(reiser4_object_t *reg, 
			     reiser4_item_plug_t *plug) 
{
	errno_t res;
	conv_hint_t hint;

	aal_assert("umka-2467", plug != NULL);
	aal_assert("umka-2466", reg != NULL);

	aal_memset(&hint, 0, sizeof(hint));
	
	/* Getting file data start key. We convert file starting from the zero
	   offset until end is reached. */
	aal_memcpy(&hint.offset, &reg->position, sizeof(hint.offset));
	objcall(&hint.offset, set_offset, 0);
	
	/* Prepare convert hint. */
	hint.plug = plug;

	if ((res = obj40_update(reg)))
		return res;
	
	hint.count = obj40_get_size(reg);
	hint.place_func = NULL;

	/* Converting file data. */
	if ((res = obj40_convert(reg, &hint)))
		return res;
	
	/* Updating stat data place */
	if ((res = obj40_update(reg)))
		return res;
	
	/* Updating stat data fields. */
	if (hint.bytes != obj40_get_bytes(reg))
		return obj40_set_bytes(reg, hint.bytes);

	return 0;
}

/* Make sure, that file body is of particular plugin type, that depends on tail
   policy plugin. If no - convert it to plugin told by tail policy
   plugin. Called from all modifying calls like write(), truncate(), etc. */
static errno_t reg40_check_body(reiser4_object_t *reg,
				uint64_t new_size)
{
	reiser4_item_plug_t *plug;
	
	aal_assert("umka-2395", reg != NULL);

	/* There is nothing to convert? */
	if (!new_size)
		return 0;

	/* Getting item plugin that should be used according to 
	   the current tail policy plugin. */
	if (!(plug = reg40_policy_plug(reg, new_size))) {
		aal_error("Can't get body plugin for new "
			  "file size %llu.", (unsigned long long)new_size);
		return -EIO;
	}

	if (!reg->body_plug) {
		reg->body_plug = plug;
		return 0;
	}
		
	/* Comparing new plugin and old one. If they are the same, conversion if
	   not needed. */
	if (plug_equal(plug, reg->body_plug))
		return 0;

	/* Convert file. */
	reg->body_plug = plug;
	return reg40_convert(reg, plug);
}

/* Writes @n bytes from @buff to passed file */
static int64_t reg40_write(reiser4_object_t *reg, 
			   void *buff, uint64_t n) 
{
	sdhint_unix_t unixh;
	trans_hint_t hint;
	stat_hint_t stat;
	sdhint_lw_t lwh;

	int64_t count;
	uint64_t off;
	int64_t res;
	int dirty;

	aal_assert("umka-2281", reg != NULL);
	
	if ((res = obj40_update(reg)))
		return res;
	
	aal_memset(&stat, 0, sizeof(stat));
	stat.ext[SDEXT_LW_ID] = &lwh;
	stat.ext[SDEXT_UNIX_ID] = &unixh;
	
	if ((res = obj40_load_stat(reg, &stat)))
		return res;
	
	off = obj40_offset(reg);
	dirty = 0;
	
	/* Inserting holes if needed. */
	if (off > lwh.size) {
		count = off - lwh.size;
		
		/* Fill the hole with zeroes. */
		if ((res = obj40_write(reg, &hint, NULL, lwh.size, count,
				       reg->body_plug, NULL, NULL)) < 0)
		{
			return res;
		}
		
		lwh.size += res;
		unixh.bytes += hint.bytes;
		if (res || hint.bytes)
			dirty = 1;
		
		/* If not enough bytes are written, the hole is not 
		   filled yet, cannot continue, return 0. */
		if (res != count) {
			if ((res = obj40_save_stat(reg, &stat)))
				return res;
			
			return 0;
		}
	} 
	
	/* Putting data to tree. */
	if ((count = obj40_write(reg, &hint, buff, off, n,
				 reg->body_plug, NULL, NULL)) < 0)
	{
		return count;
	}
	
	off += count;
	if (hint.bytes) {
		unixh.bytes += hint.bytes;
		dirty = 1;
	}
	if (off > lwh.size) {
		lwh.size = off;
		dirty = 1;
	}
	
	if ((res = obj40_update(reg)))
		return res;
	
	/* Updating the SD place and update size, bytes there. */
	if (dirty && (res = obj40_save_stat(reg, &stat)))
		return res;
	
	/* Convert body items if needed. */
	if ((res = reg40_check_body(reg, lwh.size))) {
		aal_error("Can't perform tail conversion.");
		return res;
	}
	
	obj40_seek(reg, off);
	
	return count;
}

/* Truncates file to passed size @n. */
static errno_t reg40_truncate(reiser4_object_t *reg, uint64_t n) {
	errno_t res;
	
	/* Cutting items/units */
	if ((res = obj40_truncate(reg, n, reg->body_plug)) < 0)
		return res;
	
	/* Converting body if needed. */
	if ((res = reg40_check_body(reg, n)))
		aal_error("Can't perform tail conversion.");
		
	return res;
}

/* Removes file body items and file stat data item. */
static errno_t reg40_clobber(reiser4_object_t *reg) {
	errno_t res;
	
	aal_assert("umka-2299", reg != NULL);

	if ((res = reg40_truncate(reg, 0)))
		return res;

	return obj40_clobber(reg);
}

/* Enumerates all blocks belong to file and calls passed @region_func for each
   of them. It is needed for calculating fragmentation, printing, etc. */
static errno_t reg40_layout(reiser4_object_t *reg,
			    region_func_t func,
			    void *data)
{
	obj40_reset(reg);
	return obj40_layout(reg, func, NULL, data);
}

/* Implements metadata() function. It traverses items belong to file. This is
   needed for printing, getting metadata, etc. */
static errno_t reg40_metadata(reiser4_object_t *reg,
			      place_func_t place_func,
			      void *data)
{
	obj40_reset(reg);
	return obj40_traverse(reg, place_func, NULL, data);
}
#endif

/* Regular file plugin. */
reiser4_object_plug_t reg40_plug = {
	.p = {
		.id    = {OBJECT_REG40_ID, REG_OBJECT, OBJECT_PLUG_TYPE},
#ifndef ENABLE_MINIMAL
		.label = "reg40",
		.desc  = "Unix-file regular file plugin.",
#endif
	},

#ifndef ENABLE_MINIMAL
	.inherit	= obj40_inherit,
	.create	        = reg40_create,
	.write	        = reg40_write,
	.truncate       = reg40_truncate,
	.layout         = reg40_layout,
	.metadata       = reg40_metadata,
	.convert        = reg40_convert,
	.link           = obj40_link,
	.unlink         = obj40_unlink,
	.linked         = obj40_linked,
	.clobber        = reg40_clobber,
	.recognize	= obj40_recognize,
	.check_struct   = reg40_check_struct,
	
	.add_entry      = NULL,
	.rem_entry      = NULL,
	.build_entry    = NULL,
	.attach         = NULL,
	.detach         = NULL,
	
	.fake		= NULL,
	.check_attach 	= NULL,
#endif
	.lookup	        = NULL,
	.follow         = NULL,
	.readdir        = NULL,
	.telldir        = NULL,
	.seekdir        = NULL,
		
	.stat           = obj40_load_stat,
	.open	        = reg40_open,
	.close	        = NULL,
	.reset	        = obj40_reset,
	.seek	        = obj40_seek,
	.offset	        = obj40_offset,
	.read	        = reg40_read,

#ifndef ENABLE_MINIMAL
	.sdext_mandatory = (1 << SDEXT_LW_ID),
	.sdext_unknown   = (1 << SDEXT_SYMLINK_ID),
#endif
};