-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcont.c
452 lines (395 loc) · 11.1 KB
/
cont.c
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
/*
* Chunkfs continuation routines
*
* (C) 2007-2008 Valerie Henson <[email protected]>
*
*/
#include <linux/xattr.h>
#include "chunkfs.h"
#include "chunkfs_pool.h"
#include "chunkfs_dev.h"
#include "chunkfs_chunk.h"
#include "chunkfs_i.h"
/*
* Wow... all hack, all the time. Don't try this at home, kids.
*/
static int
get_set_cont_data(struct dentry *dentry, char *name, u64 value,
u64 *ret_value, int type)
{
/* Yaaaaaaay potential buffer overflow */
char value_str[50]; /* XXX */
char full_name[50]; /* XXX */
/* Our continuation data is stored in the "user" xattr namespace */
char prefix[] = "user.";
ssize_t size;
int err = 0;
chunkfs_debug("%s for inode %lu (%s)",
name, dentry->d_inode->i_ino, type ? "set" : "get");
/* Make our "user.name" xattr name */
sprintf(full_name, "%s%s", prefix, name);
if (type == 0) {
size = generic_getxattr(dentry, full_name, value_str,
sizeof(value_str));
if (size >= 0) {
/* No automatic null termination... */
value_str[size] = '\0';
*ret_value = simple_strtoull(value_str, NULL, 10);
} else {
err = size;
}
chunkfs_debug("%s=%llu ", name, *ret_value);
} else {
/* XXX Pad out to max number of characters to avoid ENOSPC */
sprintf(value_str, "%llu", value);
err = generic_setxattr(dentry, full_name, value_str,
strlen(value_str) + 1, 0);
chunkfs_debug("%s=%s ", name, value_str);
}
/* XXX ENOSPC handling */
chunkfs_debug("err %d\n", err);
return err;
}
static int
set_cont_data(struct dentry *dentry, struct chunkfs_cont_data *cd)
{
int err;
err = get_set_cont_data(dentry, "next", cd->cd_next, NULL, 1);
if (err)
goto out;
err = get_set_cont_data(dentry, "prev", cd->cd_prev, NULL, 1);
if (err)
goto out;
err = get_set_cont_data(dentry, "start", cd->cd_start, NULL, 1);
if (err)
goto out;
err = get_set_cont_data(dentry, "len", cd->cd_len, NULL, 1);
if (err)
goto out;
mark_inode_dirty(dentry->d_inode);
out:
chunkfs_debug("inode %lu err %d next %llu prev %llu start %llu len %llu\n",
dentry->d_inode->i_ino, err,
cd->cd_next, cd->cd_prev, cd->cd_start, cd->cd_len);
return err;
}
/*
* Get the continuation info out of the underlying client inode and
* stick it into the continuation info for an element of the inode
* list for a chunkfs inode. Currently stored in an xattr, so can use
* nice pretty fs-independent xattr routines.
*/
static int
get_cont_data(struct dentry *dentry, struct chunkfs_cont_data *cd)
{
int err;
err = get_set_cont_data(dentry, "next", 0, &cd->cd_next, 0);
if (err)
return err;
err = get_set_cont_data(dentry, "prev", 0, &cd->cd_prev, 0);
if (err)
return err;
err = get_set_cont_data(dentry, "start", 0, &cd->cd_start, 0);
if (err)
return err;
err = get_set_cont_data(dentry, "len", 0, &cd->cd_len, 0);
if (err)
return err;
chunkfs_debug("inode %lu err %d next %llu prev %llu start %llu len %llu\n",
dentry->d_inode->i_ino, err,
cd->cd_next, cd->cd_prev, cd->cd_start, cd->cd_len);
return 0;
}
/*
* inode based interface to get cont data
*/
static int
get_cont_data_inode(struct inode *inode, struct chunkfs_cont_data *cd)
{
struct dentry fake_dentry;
int err;
fake_dentry.d_inode = inode;
fake_dentry.d_sb = inode->i_sb;
err = get_cont_data(&fake_dentry, cd);
return err;
}
/*
* Read an existing continuation into memory.
*
* XXX - dget/iget on client?
*/
static int
load_continuation(struct inode *head_inode, struct dentry *client_dentry,
u64 chunk_id, struct chunkfs_continuation **ret_cont)
{
struct chunkfs_pool_info *pi = CHUNKFS_PI(head_inode->i_sb);
struct chunkfs_continuation *cont;
struct chunkfs_chunk_info *ci;
int err;
chunkfs_debug("chunk_id %llu\n", chunk_id);
cont = kzalloc(sizeof(*cont), GFP_ATOMIC);
if (cont == NULL)
return -ENOMEM;
cont->co_inode = client_dentry->d_inode;
cont->co_dentry = client_dentry;
cont->co_chunk_id = chunk_id;
/* Hm. Think I could pass in the mnt, too... */
ci = chunkfs_find_chunk(pi, chunk_id);
BUG_ON(ci == NULL); /* XXX */
cont->co_mnt = ci->ci_mnt;
cont->co_uino = MAKE_UINO(chunk_id, cont->co_inode->i_ino);
err = get_cont_data(cont->co_dentry, &cont->co_cd);
if (err)
goto out;
*ret_cont = cont;
return 0;
out:
kfree(cont);
return err;
}
void
chunkfs_put_continuation(struct chunkfs_continuation *cont)
{
dput(cont->co_dentry);
/* Should be doing mntput but don't do mntget either */
kfree(cont);
}
/*
* Inode list lock must be held.
*
* Huuuuuge simplification - only load a continuation into memory
* while it's being used. No in-memory linked list.
*
*/
int
chunkfs_get_next_cont(struct dentry *head_dentry,
struct chunkfs_continuation *prev_cont,
struct chunkfs_continuation **next_cont)
{
char *path = NULL;
struct inode *head_inode = head_dentry->d_inode;
struct chunkfs_cont_data *cd;
struct dentry *client_dentry;
struct nameidata nd;
u64 from_chunk_id;
u64 chunk_id;
u64 from_ino;
u64 next_uino;
int err;
chunkfs_debug("prev_cont %p\n", prev_cont);
/*
* Get the dentry for the continuation we want.
*/
if (prev_cont == NULL) {
client_dentry = dget(get_client_dentry(head_dentry));
chunk_id = UINO_TO_CHUNK_ID(head_inode->i_ino);
} else {
cd = &prev_cont->co_cd;
/* If it's the head inode again, return */
if (cd->cd_next == head_inode->i_ino) {
*next_cont = NULL;
return 0;
}
/* If there is no next continuation, return */
if (cd->cd_next == 0) {
*next_cont = NULL;
return 0;
}
/* Laboriously construct the path and look it up */
next_uino = cd->cd_next;
chunk_id = UINO_TO_CHUNK_ID(next_uino);
from_chunk_id = prev_cont->co_chunk_id;
from_ino = UINO_TO_INO(prev_cont->co_uino);
path = __getname();
err = PTR_ERR_OR_ZERO(path);
if (err)
return err;
sprintf(path, "/chunk%llu/%llu/%llu",
chunk_id, from_chunk_id, from_ino);
err = kern_path(path, 0, &nd.path);
__putname(path);
if (err)
return -ENOENT;
client_dentry = dget(nd.path.dentry);
path_put(&nd.path);
}
/* Now we know the dentry of the continuation we want. */
err = load_continuation(head_inode, client_dentry, chunk_id,
next_cont);
chunkfs_debug("returning err %d\n", err);
return err;
}
int
chunkfs_get_cont_at_offset(struct dentry *dentry, loff_t offset,
struct chunkfs_continuation **ret_cont)
{
struct chunkfs_inode_info *ii = CHUNKFS_I(dentry->d_inode);
struct chunkfs_continuation *prev_cont = NULL;
struct chunkfs_continuation *next_cont;
struct chunkfs_cont_data *cd;
int err;
chunkfs_debug("reading ino %0lx offset %llu\n",
dentry->d_inode->i_ino, offset);
mutex_lock(&ii->ii_continuations_lock);
while (1) {
err = chunkfs_get_next_cont(dentry, prev_cont, &next_cont);
if (err || (next_cont == NULL))
break;
cd = &next_cont->co_cd;
chunkfs_debug("offset %llu start %llu len %llu err %d\n",
offset, cd->cd_start, cd->cd_len, err);
if ((offset >= cd->cd_start) &&
(offset < (cd->cd_start + cd->cd_len))) {
chunkfs_debug("found it!\n");
*ret_cont = next_cont;
break;
}
chunkfs_debug("not this one\n");
prev_cont = next_cont;
}
mutex_unlock(&ii->ii_continuations_lock);
/* If we didn't find a cont at all, return -ENOENT */
if (next_cont == NULL)
err = -ENOENT;
*ret_cont = next_cont;
return err;
}
/*
* Traverse the list of continuations using iget() only.
*/
int
chunkfs_get_next_inode(struct inode *head_inode,
struct inode *prev_inode, struct inode **ret_inode)
{
struct chunkfs_pool_info *pi = CHUNKFS_PI(head_inode->i_sb);
struct chunkfs_chunk_info *ci;
struct chunkfs_cont_data cd;
struct inode *next_inode;
u64 next_uino;
ino_t next_ino;
u64 chunk_id;
int err;
/* Starting the list... */
if (prev_inode == NULL) {
prev_inode = get_client_inode(head_inode);
next_inode = iget_locked(prev_inode->i_sb, prev_inode->i_ino);
BUG_ON(!next_inode);
goto found_inode;
} else
iput(prev_inode);
/* Find the superblock and inode for the next one */
err = get_cont_data_inode(prev_inode, &cd);
if (err)
return err;
next_uino = cd.cd_next;
if (next_uino == 0) {
*ret_inode = NULL;
return 0;
}
next_ino = UINO_TO_INO(next_uino);
chunk_id = UINO_TO_CHUNK_ID(next_uino);
chunkfs_debug("next_uino %llu next_ino %lu, next chunk_id %llu\n",
next_uino, next_ino, chunk_id);
ci = chunkfs_find_chunk(pi, chunk_id);
BUG_ON(ci == NULL); /* XXX */
next_inode = iget_locked(ci->ci_sb, next_ino);
BUG_ON(!next_inode);
found_inode:
unlock_inode(next_inode);
if (is_bad_inode(next_inode))
return -EIO;
*ret_inode = next_inode;
return 0;
}
/*
* Create a new continuation in this chunk. Never called on the head.
* Length is set arbitrarily so be sure to write continuously.
*
* We have to bootstrap ourselves up, starting with a dentry. We are,
* in fact, creating a file from the kernel. Bleah.
*/
int
chunkfs_create_continuation(struct file *file, loff_t *ppos,
struct file **client_file,
struct chunkfs_continuation **ret_cont)
{
char *path = NULL;
struct chunkfs_continuation *prev_cont = NULL;
struct chunkfs_continuation *next_cont;
struct chunkfs_continuation *new_cont;
struct file *new_file;
u64 from_chunk_id;
u64 to_chunk_id;
u64 from_ino;
struct dentry *dentry;
struct chunkfs_cont_data cd;
int err;
chunkfs_debug("enter\n");
/* Get the last continuation */
while (1) {
err = chunkfs_get_next_cont(file->f_dentry, prev_cont,
&next_cont);
if (err)
return err;
if (next_cont == NULL)
break;
prev_cont = next_cont;
}
/* Figure out what chunk and inode we are continuing from. */
from_chunk_id = prev_cont->co_chunk_id;
from_ino = UINO_TO_INO(prev_cont->co_uino);
/* Temporary hack, do the next chunk for creation. */
to_chunk_id = from_chunk_id + 1;
chunkfs_debug("to chunk %llu\n", to_chunk_id);
/* Now we need the filename for the continuation inode. */
path = __getname();
err = PTR_ERR_OR_ZERO(path);
if (err)
goto out;
sprintf(path, "/chunk%llu/%llu/%llu", to_chunk_id, from_chunk_id,
from_ino);
/* Create the file */
new_file = filp_open(path, O_CREAT | O_RDWR, MAY_WRITE | MAY_READ | MAY_APPEND);
if (IS_ERR(new_file)) {
err = PTR_ERR(new_file);
chunkfs_debug("open_namei for %s: err %d\n", path, err);
chunkfs_debug("dentry_open: err %d\n", err);
goto out_free;
}
*client_file = new_file;
dentry = dget(new_file->f_dentry);
/* Fill in next/prev/etc. data */
cd.cd_next = 0;
cd.cd_prev = prev_cont->co_uino;
cd.cd_start = prev_cont->co_cd.cd_start + prev_cont->co_cd.cd_len;
cd.cd_len = 10 * 4096;
set_cont_data(dentry, &cd);
/* Now update prev */
prev_cont->co_cd.cd_next = MAKE_UINO(to_chunk_id,
dentry->d_inode->i_ino);
set_cont_data(prev_cont->co_dentry, &prev_cont->co_cd);
/* Now! It's all in the inode and we can load it like normal. */
err = load_continuation(file->f_dentry->d_inode, dentry,
to_chunk_id, &new_cont);
chunkfs_copy_down_file(file, ppos, new_file, new_cont->co_cd.cd_start);
*ret_cont = new_cont;
out_free:
__putname(path);
out:
if (err)
chunkfs_put_continuation(prev_cont);
chunkfs_debug("start %llu returning %d\n", cd.cd_start, err);
return err;
}
int
chunkfs_init_cont_data(struct dentry *client_dentry)
{
struct chunkfs_cont_data cd;
int err;
cd.cd_prev = 0;
cd.cd_next = 0;
cd.cd_start = 0;
cd.cd_len = 10 * 4096;
err = set_cont_data(client_dentry, &cd);
return err;
}