-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamei.c
542 lines (469 loc) · 13.6 KB
/
namei.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
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
/*
* Chunkfs
*
* Chunks is a file system designed to be checked and repaired in
* small, mostly independent chunks. This allows quick recovery from
* file system corruption.
*
* (C) 2007-2008 Valerie Henson <[email protected]>
*
*/
#include "chunkfs.h"
#include "chunkfs_pool.h"
#include "chunkfs_dev.h"
#include "chunkfs_chunk.h"
#include "chunkfs_i.h"
#include <linux/mount.h>
#include <linux/slab.h>
void
chunkfs_release_nd(struct dentry *dentry)
{
struct nameidata *nd = get_client_nd(dentry);
dput(nd->path.dentry);
mntput(nd->path.mnt);
}
/*
* Call this to initialize our client nameidata.
*/
void
chunkfs_init_nd(struct inode *dir, struct dentry *dentry,
struct dentry *client_dentry, u64 chunk_id)
{
struct nameidata *nd = get_client_nd(dentry);
struct chunkfs_chunk_info *chunk;
chunk = chunkfs_find_chunk(CHUNKFS_PI(dir->i_sb), chunk_id);
BUG_ON(!chunk); /* XXX */
/* Probably don't need dget/mntget */
nd->path.dentry = dget(client_dentry);
nd->path.mnt = mntget(chunk->ci_mnt);
chunkfs_debug("dentry %p name %s client_dentry %p mnt %s\n",
dentry, dentry->d_iname, client_dentry,
nd->path.mnt->mnt_sb->s_type->name);
}
/*
* The client file system may read the following parts of the nameidata:
*
* In open, it read the intent's mode or flags.
*
* The client file system may alter the nameidata in the following cases:
*
* When following symbolic links (up to N levels of links saved in
* nd), it may set the saved_names (using the depth) with nd_set_link.
*/
static void
__chunkfs_copy_nd(struct nameidata *dst, struct nameidata *src)
{
dst->flags = src->flags;
dst->seq = src->seq;
dst->depth = src->depth;
dst->saved_names[dst->depth] = src->saved_names[dst->depth];
}
void
chunkfs_copy_up_nd(struct nameidata *nd, struct nameidata *client_nd)
{
__chunkfs_copy_nd(nd, client_nd);
}
void
chunkfs_copy_down_nd(struct nameidata *nd, struct nameidata *client_nd)
{
__chunkfs_copy_nd(client_nd, nd);
}
static void
chunkfs_remove_dentry(struct dentry *dentry)
{
struct chunkfs_dentry_priv *dp = CHUNKFS_D(dentry);
dput(dp->dp_client_dentry);
}
void
chunkfs_free_dentry(struct dentry *dentry)
{
struct chunkfs_dentry_priv *dp = CHUNKFS_D(dentry);
kfree(dp->dp_client_nd);
kfree(dp);
dentry->d_fsdata = NULL;
}
/*
* Called when a dentry is evicted from cache.
*/
void
chunkfs_release_dentry(struct dentry *dentry)
{
chunkfs_debug("name %s\n", dentry->d_name.name);
/*
* Root dentry can be legitimately released on umount, but is
* also a common manifestation of refcounting problems. Catch
* for debugging.
*/
WARN_ON(strcmp(dentry->d_name.name, "/") == 0);
chunkfs_release_nd(dentry);
/*
* Negative dentries need client dentries too, so they can be
* easily converted into responsible positive dentries. We
* should never have a dentry without a client dentry.
*/
chunkfs_remove_dentry(dentry);
chunkfs_free_dentry(dentry);
}
struct dentry_operations chunkfs_dops = {
.d_release = chunkfs_release_dentry,
};
/*
* Initialize a new chunkfs dentry.
*/
int
chunkfs_init_dentry(struct dentry *dentry)
{
struct chunkfs_dentry_priv *dp;
struct nameidata *nd;
BUG_ON(dentry->d_fsdata);
dp = kzalloc(sizeof(*dp), GFP_KERNEL);
if (!dp)
return -ENOMEM;
nd = kzalloc(sizeof(*nd), GFP_KERNEL);
if (!nd)
goto out;
dp->dp_client_nd = nd;
dentry->d_fsdata = dp;
dentry->d_op = &chunkfs_dops;
return 0;
out:
kfree(dp);
return -ENOMEM;
}
/*
* This function takes a chunkfs dentry and constructs a new dentry
* for the client fs.
*/
static struct dentry *
chunkfs_clone_dentry(struct dentry *dentry)
{
struct dentry *client_parent = get_client_dentry(dentry->d_parent);
struct dentry *client_dentry;
client_dentry = d_alloc_name(client_parent, dentry->d_name.name);
if (!client_dentry)
return ERR_PTR(-ENOMEM);
return client_dentry;
}
void
chunkfs_add_dentry(struct dentry *dentry, struct dentry *client_dentry,
struct vfsmount *mnt)
{
struct chunkfs_dentry_priv *dp = CHUNKFS_D(dentry);
dp->dp_client_dentry = client_dentry;
}
static int
chunkfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
bool excl)
{
struct inode *client_dir = get_client_inode(dir);
struct dentry *client_dentry = get_client_dentry(dentry);
struct nameidata *client_nd = get_client_nd(dentry);
u64 chunk_id = UINO_TO_CHUNK_ID(dir->i_ino);
struct inode *inode;
int err;
struct nameidata nd;
chunkfs_debug("dir ino %0lx i_count %d\n",
dir->i_ino, atomic_read(&dir->i_count));
err = chunkfs_new_inode(dir->i_sb, &inode);
if (err)
goto out;
// TODO: or d_flags OR i_size_seqcount ?
nd.flags = dir->i_flags;
// nd.seq = ;
// nd.depth = ;
// nd.saved_names[nd.depth] = ;
chunkfs_copy_down_nd(&nd, client_nd);
err = client_dir->i_op->create(client_dir, client_dentry, mode,
client_nd);
if (err)
goto out_inode;
err = chunkfs_init_cont_data(client_dentry);
if (err)
goto out_inode;
chunkfs_start_inode(inode, client_dentry->d_inode, chunk_id);
chunkfs_copy_up_inode(dir, client_dir);
chunkfs_copy_up_nd(&nd, client_nd);
/* Now put our new inode into the dentry */
d_instantiate(dentry, inode);
chunkfs_debug("dentry %p name %s inode %p ino %0lx\n",
dentry, dentry->d_iname, dentry->d_inode, dentry->d_inode->i_ino);
chunkfs_debug("client dentry %p name %s inode %p ino %0lx\n",
client_dentry, client_dentry->d_iname, client_dentry->d_inode,
client_dentry->d_inode->i_ino);
return 0;
out_inode:
iput(inode);
out:
return err;
}
static struct dentry *
chunkfs_lookup(struct inode * dir, struct dentry *dentry, unsigned int flags)
{
struct inode *client_dir = get_client_inode(dir);
u64 chunk_id = UINO_TO_CHUNK_ID(dir->i_ino);
struct dentry *client_dentry;
struct dentry *new_dentry;
struct nameidata *client_nd;
struct inode *inode;
int err;
chunkfs_debug("name %s dir ino %0lx i_count %d\n",
dentry->d_iname, dir->i_ino, atomic_read(&dir->i_count));
err = chunkfs_init_dentry(dentry);
if (err)
goto out;
client_dentry = chunkfs_clone_dentry(dentry);
if (IS_ERR(client_dentry))
goto out_dentry;
/* XXX What if we have root inode here ? */
chunkfs_init_nd(dir, dentry, client_dentry, chunk_id);
client_nd = get_client_nd(dentry);
/*
* Fill out the client dentry.
*/
new_dentry = client_dir->i_op->lookup(client_dir, client_dentry,
client_nd->flags);
/*
* Possible return values:
*
* NULL: Nothing went wrong with lookup, you may or may not
* have found a matching inode and attached it. If the inode
* is NULL, we still have to create a negative dentry.
*
* Address of a dentry: The dentry already existed (and was
* root and disconnected - something about knfsd), so the
* dentry we passed in needs to be thrown away and we should
* use the one returned.
*
* IS_ERR(): Something went wrong, return the error.
*/
if (IS_ERR(new_dentry)) {
err = PTR_ERR(new_dentry);
goto out_dput;
} else if (new_dentry) {
dput(client_dentry);
client_dentry = new_dentry;
}
/*
* If the client found an inode, fill in the chunkfs inode.
*/
if (client_dentry->d_inode) {
err = chunkfs_new_inode(dir->i_sb, &inode);
if (err)
goto out_dput;
err = chunkfs_init_cont_data(client_dentry);
if (err)
goto out_dput;
chunkfs_start_inode(inode, client_dentry->d_inode,
chunk_id);
} else {
inode = NULL;
}
/* Hook up the client and parent dentries. */
chunkfs_add_dentry(dentry, client_dentry, client_nd->path.mnt);
chunkfs_debug("dentry %p name %s inode %p\n",
dentry, dentry->d_iname, dentry->d_inode);
chunkfs_debug("client dentry %p name %s inode %p\n",
client_dentry, client_dentry->d_iname, client_dentry->d_inode);
return d_splice_alias(inode, dentry);
out_dput:
dput(client_dentry);
chunkfs_release_nd(dentry);
out_dentry:
chunkfs_remove_dentry(dentry);
out:
chunkfs_free_dentry(dentry);
chunkfs_debug("name %s returning %d\n", dentry->d_iname, err);
return ERR_PTR(err);
}
static int
chunkfs_link(struct dentry *old_dentry, struct inode *dir,
struct dentry *new_dentry)
{
struct inode *client_dir = get_client_inode(dir);
struct inode *old_inode = old_dentry->d_inode;
struct inode *client_old_inode = get_client_inode(old_inode);
struct dentry *client_old_dentry = get_client_dentry(old_dentry);
struct dentry *client_new_dentry = get_client_dentry(new_dentry);
int err = 0;
chunkfs_debug("enter\n");
err = client_dir->i_op->link(client_old_dentry, client_dir,
client_new_dentry);
if (err)
goto out;
/* Copy up inode takes care of link counts */
chunkfs_copy_up_inode(old_inode, client_old_inode);
/*
* For some reason, this is the one place where the VFS
* doesn't increment the inode ref count for us.
*/
atomic_inc(&dir->i_count);
d_instantiate(new_dentry, old_inode);
out:
return err;
}
static int
chunkfs_unlink(struct inode *dir, struct dentry *dentry)
{
struct inode *client_dir = get_client_inode(dir);
struct dentry *client_dentry = get_client_dentry(dentry);
struct inode *inode = dentry->d_inode;
struct inode *client_inode = get_client_inode(inode);
int err = 0;
chunkfs_debug("enter\n");
err = client_dir->i_op->unlink(client_dir, client_dentry);
if (err)
goto out;
chunkfs_copy_up_inode(dir, client_dir);
chunkfs_copy_up_inode(inode, client_inode);
out:
return err;
}
static int
chunkfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
{
struct inode *client_dir = get_client_inode(dir);
struct dentry *client_dentry = get_client_dentry(dentry);
u64 chunk_id = UINO_TO_CHUNK_ID(dir->i_ino);
struct inode *inode;
int err;
chunkfs_debug("dir ino %0lx i_count %d\n",
dir->i_ino, atomic_read(&dir->i_count));
err = chunkfs_new_inode(dir->i_sb, &inode);
if (err)
goto out;
err = client_dir->i_op->symlink(client_dir, client_dentry, oldname);
if (err)
goto out_inode;
err = chunkfs_init_cont_data(client_dentry);
if (err)
goto out_inode;
chunkfs_start_inode(inode, client_dentry->d_inode, chunk_id);
chunkfs_copy_up_inode(dir, client_dir);
/* Now put our new inode into the dentry */
d_instantiate(dentry, inode);
chunkfs_debug("dentry %p name %s inode %p ino %0lx\n",
dentry, dentry->d_iname, dentry->d_inode,
dentry->d_inode->i_ino);
chunkfs_debug("client dentry %p name %s inode %p ino %0lx\n",
client_dentry, client_dentry->d_iname, client_dentry->d_inode,
client_dentry->d_inode->i_ino);
return 0;
out_inode:
iput(inode);
out:
return err;
}
static int
chunkfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
{
struct inode *client_dir = get_client_inode(dir);
struct inode *client_inode;
struct dentry *client_dentry = get_client_dentry(dentry);
u64 chunk_id = UINO_TO_CHUNK_ID(dir->i_ino);
struct inode *inode;
int err;
chunkfs_debug("name %s dir ino %0lx i_count %d\n",
dentry->d_iname, dir->i_ino, atomic_read(&dir->i_count));
err = chunkfs_new_inode(dir->i_sb, &inode);
if (err)
goto out;
err = client_dir->i_op->mkdir(client_dir, client_dentry, mode);
if (err)
goto out_inode;
client_inode = client_dentry->d_inode;
err = chunkfs_init_cont_data(client_dentry);
if (err)
goto out_inode;
chunkfs_start_inode(inode, client_inode, chunk_id);
chunkfs_copy_up_inode(dir, client_dir);
d_instantiate(dentry, inode);
return 0;
out_inode:
iput(inode);
out:
chunkfs_debug("name %s returning %d\n", dentry->d_iname, err);
return err;
}
static int
chunkfs_rmdir(struct inode *dir, struct dentry *dentry)
{
struct inode *client_dir = get_client_inode(dir);
struct dentry *client_dentry = get_client_dentry(dentry);
struct inode *inode = dentry->d_inode;
int err;
chunkfs_debug("enter\n");
err = client_dir->i_op->rmdir(client_dir, client_dentry);
if (err)
return err;
chunkfs_copy_up_inode(dir, client_dir);
chunkfs_copy_up_inode(inode, client_dentry->d_inode);
return 0;
}
static int
chunkfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
{
struct inode *client_dir = get_client_inode(dir);
struct dentry *client_dentry = get_client_dentry(dentry);
u64 chunk_id = UINO_TO_CHUNK_ID(dir->i_ino);
struct inode *inode;
int err;
chunkfs_debug("name %s dir ino %0lx i_count %d\n",
dentry->d_iname, dir->i_ino, atomic_read(&dir->i_count));
err = chunkfs_new_inode(dir->i_sb, &inode);
if (err)
goto out;
err = client_dir->i_op->mknod(client_dir, client_dentry, mode, dev);
if (err)
goto out_inode;
err = chunkfs_init_cont_data(client_dentry);
if (err)
goto out_inode;
chunkfs_start_inode(inode, client_dentry->d_inode, chunk_id);
chunkfs_copy_up_inode(dir, client_dir);
d_instantiate(dentry, inode);
return 0;
out_inode:
iput(inode);
out:
chunkfs_debug("name %s returning %d\n", dentry->d_iname, err);
return err;
}
static int
chunkfs_rename(struct inode *old_dir, struct dentry *old_dentry,
struct inode *new_dir, struct dentry *new_dentry)
{
struct inode *client_old_dir = get_client_inode(old_dir);
struct inode *client_new_dir = get_client_inode(new_dir);
struct dentry *client_old_dentry = get_client_dentry(old_dentry);
struct dentry *client_new_dentry = get_client_dentry(new_dentry);
int err = 0;
return -ENOSYS;
/* Not reached */
err = client_old_dir->i_op->rename(client_old_dir,
client_old_dentry,
client_new_dir,
client_new_dentry);
if (err)
goto out;
chunkfs_copy_up_inode(old_dir, client_old_dir);
chunkfs_copy_up_inode(new_dir, client_new_dir);
out:
return err;
}
struct inode_operations chunkfs_dir_iops = {
.create = chunkfs_create,
.lookup = chunkfs_lookup,
.link = chunkfs_link,
.unlink = chunkfs_unlink,
.symlink = chunkfs_symlink,
.mkdir = chunkfs_mkdir,
.rmdir = chunkfs_rmdir,
.mknod = chunkfs_mknod,
.rename = chunkfs_rename,
.setattr = chunkfs_setattr,
.permission = chunkfs_permission,
};
struct inode_operations chunkfs_special_iops = {
.setattr = chunkfs_setattr,
.permission = chunkfs_permission,
};