-
Notifications
You must be signed in to change notification settings - Fork 2
/
dir.c
637 lines (533 loc) · 14.6 KB
/
dir.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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/***************************************************************************
*
* Copyright (c) 1997-2022 Jeff V. Merkey
* 7260 SE Tenino St.
* Portland, Oregon 97206
* jeffmerkey@gmail.com
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the Lesser GNU Public License as published by the
* Free Software Foundation, version 2.1, or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* Original Authorship :
* source code written by Jeff V. Merkey
*
* Original Contributors :
* Jeff V. Merkey
*
*
*
****************************************************************************
*
*
* AUTHOR : Jeff V. Merkey (jeffmerkey@gmail.com)
* FILE : DIR.C
* DESCRIP : VFS Dir Module for Linux
* DATE : December 5, 1998
*
*
***************************************************************************/
#include "globals.h"
#if (LINUX_24)
int nwfs_dentry_validate(struct dentry *dentry, int flags)
{
struct inode *inode = dentry->d_inode;
#if (VERBOSE)
NWFSPrint("dentry validate\n");
#endif
if (!inode)
return 1;
// see if this inode is the root entry
if (inode->i_sb->s_root->d_inode == inode)
return 1;
// see if this inode is still valid.
if (is_bad_inode(inode))
return 0;
if (atomic_read(&dentry->d_count) > 1)
return 1;
return 0;
}
int nwfs_delete_dentry(struct dentry * dentry)
{
#if (VERBOSE)
NWFSPrint("dentry delete\n");
#endif
if (!dentry->d_inode)
return 0;
if (is_bad_inode(dentry->d_inode))
return 1;
return 0;
}
struct dentry_operations nwfs_dentry_operations =
{
d_revalidate: nwfs_dentry_validate,
d_delete: nwfs_delete_dentry,
};
struct file_operations nwfs_dir_operations = {
read: generic_read_dir,
readdir: nwfs_dir_readdir,
ioctl: nwfs_dir_ioctl,
};
struct inode_operations nwfs_dir_inode_operations =
{
create: nwfs_create,
lookup: nwfs_dir_lookup,
// link: nwfs_link, // does not work at present.
unlink: nwfs_unlink,
symlink: nwfs_symlink,
mkdir: nwfs_mkdir,
rmdir: nwfs_rmdir,
mknod: nwfs_mknod,
rename: nwfs_rename,
setattr: nwfs_notify_change,
};
int nwfs_dir_readdir(struct file *filp, void *dirent, filldir_t filldir)
{
register ULONG i, count = 0;
struct inode *inode = filp->f_dentry->d_inode;
register VOLUME *volume = (VOLUME *) inode->i_sb->u.generic_sbp;
register HASH *hash = 0;
HASH *dirHash = 0;
if (!inode || !S_ISDIR(inode->i_mode))
return -ENOTDIR;
i = filp->f_pos;
switch (i)
{
case 0:
if (filldir(dirent, ".", 1, filp->f_pos, inode->i_ino, DT_DIR) < 0)
goto readdir_end;
filp->f_pos++;
count++;
case 1:
if (filldir(dirent, "..", 2, filp->f_pos,
filp->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
goto readdir_end;
filp->f_pos++;
count++;
default:
while (1)
{
hash = get_subdirectory_record(volume, (filp->f_pos - 2),
inode->i_ino,
&dirHash);
if (!hash)
break;
// provide name and root name space (MSDOS) record DirNo
if (filldir(dirent, hash->Name, hash->NameLength,
(hash->Root + 2), hash->Root, DT_UNKNOWN) < 0)
goto readdir_end;
filp->f_pos = (hash->Root + 2);
filp->f_pos++;
count++;
}
break;
}
readdir_end:;
#if (VERBOSE)
NWFSPrint("readdir count=%d pos1-%d pos2-%d\n", (int)count,
(int)i, (int)filp->f_pos);
#endif
return count;
}
int nwfs_dir_ioctl(struct inode * inode, struct file * filp,
unsigned int cmd, unsigned long arg)
{
#if (VERBOSE)
NWFSPrint("ioctl cmd-%d\n", (int)cmd);
#endif
switch (cmd)
{
default:
return -EINVAL;
}
return 0;
}
struct dentry *nwfs_dir_lookup(struct inode *dir, struct dentry *dentry)
{
register VOLUME *volume = (VOLUME *) dir->i_sb->u.generic_sbp;
register ino_t ino;
struct inode **result = &(dentry->d_inode);
#if (VERBOSE)
NWFSPrint("lookup %s\n", dentry->d_name.name);
#endif
*result = NULL;
if (!dir)
return ERR_PTR(-ENOENT);
if (!S_ISDIR(dir->i_mode))
return ERR_PTR(-ENOENT);
if ((dentry->d_name.len == 0) || ((dentry->d_name.name[0] == '.') &&
(dentry->d_name.len == 1)))
{
*result = dir;
return 0;
}
ino = get_directory_number(volume,
&dentry->d_name.name[0],
dentry->d_name.len,
dir->i_ino);
if (ino)
{
*result = iget(dir->i_sb, ino);
if (!(*result))
{
d_add(dentry, NULL); // assume create or mkdir being attempted
dentry->d_time = 0;
dentry->d_op = &nwfs_dentry_operations;
return 0;
}
d_add(dentry, *result);
dentry->d_time = 0;
dentry->d_op = &nwfs_dentry_operations;
return 0;
}
else
{
d_add(dentry, NULL); // assume create or mkdir being attempted
dentry->d_time = 0;
dentry->d_op = &nwfs_dentry_operations;
return 0;
}
}
#endif
#if (LINUX_22)
int nwfs_dentry_validate(struct dentry *dentry, int flags)
{
struct inode *inode = dentry->d_inode;
#if (VERBOSE)
NWFSPrint("dentry validate %s\n", dentry->d_name.name);
#endif
if (!inode)
return 1;
// see if this inode is the root entry
if (inode->i_sb->s_root->d_inode == inode)
return 1;
// see if this inode is still valid.
if (is_bad_inode(inode))
return 0;
if (dentry->d_count > 1)
return 1;
return 0;
}
void nwfs_delete_dentry(struct dentry * dentry)
{
#if (VERBOSE)
NWFSPrint("dentry delete %s\n", dentry->d_name.name);
#endif
if (!dentry->d_inode)
return;
if (is_bad_inode(dentry->d_inode))
d_drop(dentry);
return;
}
struct dentry_operations nwfs_dentry_operations =
{
nwfs_dentry_validate, // d_revalidate(struct dentry *, int)
NULL, // d_hash
NULL, // d_compare
nwfs_delete_dentry, // d_delete(struct dentry *)
NULL,
NULL,
};
ssize_t nwfs_dir_read(struct file *filp, char *buf,
size_t count, loff_t *ppos)
{
return -EISDIR;
}
int nwfs_dir_readdir(struct file *filp, void *dirent, filldir_t filldir)
{
register ULONG i, count = 0;
struct inode *inode = filp->f_dentry->d_inode;
register VOLUME *volume = (VOLUME *) inode->i_sb->u.generic_sbp;
register HASH *hash = 0;
HASH *dirHash = 0;
if (!inode || !S_ISDIR(inode->i_mode))
return -ENOTDIR;
#if (VERBOSE)
NWFSPrint("readdir called pos-%d ino-%d\n", (int)filp->f_pos,
(int)inode->i_ino);
#endif
i = filp->f_pos;
switch (i)
{
case 0:
if (filldir(dirent, ".", 1, filp->f_pos, inode->i_ino) < 0)
goto readdir_end;
filp->f_pos++;
count++;
case 1:
if (filldir(dirent, "..", 2, filp->f_pos,
filp->f_dentry->d_parent->d_inode->i_ino) < 0)
goto readdir_end;
filp->f_pos++;
count++;
default:
while (1)
{
hash = get_subdirectory_record(volume, (filp->f_pos - 2),
inode->i_ino,
&dirHash);
if (!hash)
break;
// provide name and root name space (MSDOS) record DirNo
if (filldir(dirent, hash->Name, hash->NameLength,
(hash->Root + 2), hash->Root) < 0)
goto readdir_end;
filp->f_pos = (hash->Root + 2);
filp->f_pos++;
count++;
}
break;
}
readdir_end:;
#if (VERBOSE)
NWFSPrint("readdir count-%d pos1-%d pos2-%d ino-%d\n", (int)count,
(int)i, (int)filp->f_pos, (int)inode->i_ino);
#endif
return count;
}
int nwfs_dir_ioctl(struct inode * inode, struct file * filp,
unsigned int cmd, unsigned long arg)
{
#if (VERBOSE)
NWFSPrint("ioctl cmd-%d\n", (int)cmd);
#endif
switch (cmd)
{
default:
return -EINVAL;
}
return 0;
}
struct dentry *nwfs_dir_lookup(struct inode *dir, struct dentry *dentry)
{
register VOLUME *volume = (VOLUME *) dir->i_sb->u.generic_sbp;
register ino_t ino;
struct inode **result = &(dentry->d_inode);
#if (VERBOSE)
NWFSPrint("lookup %s\n", dentry->d_name.name);
#endif
*result = NULL;
if (!dir)
return ERR_PTR(-ENOENT);
if (!S_ISDIR(dir->i_mode))
return ERR_PTR(-ENOENT);
if ((dentry->d_name.len == 0) || ((dentry->d_name.name[0] == '.') &&
(dentry->d_name.len == 1)))
{
*result = dir;
return 0;
}
ino = get_directory_number(volume,
&dentry->d_name.name[0],
dentry->d_name.len,
dir->i_ino);
if (ino)
{
#if (VERBOSE)
NWFSPrint("lookup %s (found)\n", dentry->d_name.name);
#endif
*result = iget(dir->i_sb, ino);
if (!(*result))
{
d_add(dentry, NULL); // assume create or mkdir being attempted
dentry->d_time = 0;
dentry->d_op = &nwfs_dentry_operations;
return 0;
}
d_add(dentry, *result);
dentry->d_time = 0;
dentry->d_op = &nwfs_dentry_operations;
return 0;
}
else
{
#if (VERBOSE)
NWFSPrint("lookup %s (negative)\n", dentry->d_name.name);
#endif
d_add(dentry, NULL); // assume create or mkdir being attempted
dentry->d_time = 0;
dentry->d_op = &nwfs_dentry_operations;
return 0;
}
}
struct file_operations nwfs_dir_operations = {
NULL, // lseek
nwfs_dir_read, // read
NULL, // write
nwfs_dir_readdir, // readdir
NULL, // select v2.0.x/poll v2.1.x - default
nwfs_dir_ioctl, // ioctl
NULL, // mmap
NULL, // open
NULL, // flush
NULL, // release
NULL, // fsync
NULL,
NULL,
NULL
};
struct inode_operations nwfs_dir_inode_operations =
{
&nwfs_dir_operations, // file operations
nwfs_create, // create
nwfs_dir_lookup, // lookup
// nwfs_link, // link // does not work at present
NULL, // link
nwfs_unlink, // unlink
nwfs_symlink, // symlink
nwfs_mkdir, // mkdir
nwfs_rmdir, // rmdir
nwfs_mknod, // mknod
nwfs_rename, // rename
NULL, // readlink
NULL, // follow_link
NULL, // read_page
NULL, // writepage
NULL, // bmap
NULL, // truncate
NULL, // permission
NULL, // smap
NULL, // update page
NULL, // revalidate inode
};
#endif
#if (LINUX_20)
int nwfs_dir_read(struct inode *inode, struct file *filp,
char *buf, int count)
{
return -EISDIR;
}
int nwfs_dir_readdir(struct inode *inode, struct file *filp,
void *dirent, filldir_t filldir)
{
register ULONG i, count = 0;
register VOLUME *volume = (VOLUME *) inode->i_sb->u.generic_sbp;
register HASH *hash = 0;
HASH *dirHash = 0;
if (!inode || !S_ISDIR(inode->i_mode))
return -ENOTDIR;
i = filp->f_pos;
switch (i)
{
case 0:
if (filldir(dirent, ".", 1, filp->f_pos, inode->i_ino) < 0)
goto readdir_end;
filp->f_pos++;
count++;
case 1:
if (filldir(dirent, "..", 2, filp->f_pos,
get_parent_directory_number(volume, inode->i_ino)) < 0)
goto readdir_end;
filp->f_pos++;
count++;
default:
while (1)
{
hash = get_subdirectory_record(volume, (filp->f_pos - 2),
inode->i_ino,
&dirHash);
if (!hash)
break;
// provide name and root name space (MSDOS) record DirNo
if (filldir(dirent, hash->Name, hash->NameLength,
(hash->Root + 2), hash->Root) < 0)
goto readdir_end;
filp->f_pos = (hash->Root + 2);
filp->f_pos++;
count++;
}
break;
}
readdir_end:;
return count;
}
int nwfs_dir_lookup(struct inode *dir, const char *name, int len,
struct inode **result)
{
register VOLUME *volume = (VOLUME *) dir->i_sb->u.generic_sbp;
register ino_t ino;
register struct super_block *sb;
*result = 0;
if (!dir)
{
return -ENOENT;
}
if (!S_ISDIR(dir->i_mode))
{
iput(dir);
return -ENOENT;
}
if ((len == 0) || ((name[0] == '.') && (len == 1)))
{
*result = dir;
return 0;
}
if ((name[0] == '.') &&( name[1] == '.') && (len == 2))
{
sb = dir->i_sb;
ino = get_parent_directory_number(volume, dir->i_ino);
iput(dir);
*result = iget(sb, ino);
if (!*result)
return -EACCES;
return 0;
}
ino = get_directory_number(volume, name, len, dir->i_ino);
if (!ino)
{
iput(dir);
return -ENOENT;
}
*result = iget(dir->i_sb, ino);
if (!*result)
{
iput(dir);
return -EACCES;
}
iput(dir);
return 0;
}
struct file_operations nwfs_dir_operations =
{
NULL, // lseek
nwfs_dir_read, // read
NULL, // write
nwfs_dir_readdir, // readdir
NULL, // select
NULL, // ioctl
NULL, // mmap
NULL, // open
NULL, // release
NULL, // fsync
NULL, // fasync
NULL, // check_media_change
NULL // revalidate
};
struct inode_operations nwfs_dir_inode_operations =
{
&nwfs_dir_operations, // file operations
nwfs_create, // create
nwfs_dir_lookup, // lookup
// nwfs_link, // link // does not work st present
NULL, // link
nwfs_unlink, // unlink
nwfs_symlink, // symlink
nwfs_mkdir, // mkdir
nwfs_rmdir, // rmdir
nwfs_mknod, // mknod
nwfs_rename, // rename
NULL, // readlink
NULL, // follow_link
NULL, // read_page
NULL, // writepage
NULL, // bmap
NULL, // truncate
NULL, // permission
NULL // smap
};
#endif