aboutsummaryrefslogtreecommitdiff
path: root/kernel/dir.c
blob: 065ac0187e242cad9cc77a0f4d0b8730ec9434b6 (plain)
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
/*
    FUSE: Filesystem in Userspace
    Copyright (C) 2001  Miklos Szeredi (mszeredi@inf.bme.hu)

    This program can be distributed under the terms of the GNU GPL.
    See the file COPYING.
*/

#include "fuse_i.h"

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/file.h>
#include <linux/slab.h>

static struct inode_operations fuse_dir_inode_operations;
static struct inode_operations fuse_file_inode_operations;
static struct inode_operations fuse_symlink_inode_operations;
static struct inode_operations fuse_special_inode_operations;

static struct file_operations fuse_dir_operations;
static struct file_operations fuse_file_operations;


static void change_attributes(struct inode *inode, struct fuse_attr *attr)
{
	inode->i_mode    = attr->mode;
	inode->i_nlink   = attr->nlink;
	inode->i_uid     = attr->uid;
	inode->i_gid     = attr->gid;
	inode->i_size    = attr->size;
	inode->i_blksize = attr->blksize;
	inode->i_blocks  = attr->blocks;
	inode->i_atime   = attr->atime;
	inode->i_mtime   = attr->mtime;
	inode->i_ctime   = attr->ctime;
}

void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
{
	change_attributes(inode, attr);
	
	if(S_ISREG(inode->i_mode)) {
		inode->i_op = &fuse_file_inode_operations;
		inode->i_fop = &fuse_file_operations;
	}
	else if(S_ISDIR(inode->i_mode)) {
		inode->i_op = &fuse_dir_inode_operations;
		inode->i_fop = &fuse_dir_operations;
	}
	else if(S_ISLNK(inode->i_mode))
		inode->i_op = &fuse_symlink_inode_operations;
	else {
		inode->i_op = &fuse_special_inode_operations;
		init_special_inode(inode, inode->i_mode, attr->rdev);
	}
}


static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry)
{
	struct fuse_conn *fc = dir->i_sb->u.generic_sbp;
	struct fuse_in in = FUSE_IN_INIT;
	struct fuse_out out = FUSE_OUT_INIT;
	struct fuse_lookup_out arg;
	struct inode *inode;
	
	in.h.opcode = FUSE_LOOKUP;
	in.h.ino = dir->i_ino;
	in.argsize = entry->d_name.len + 1;
	in.arg = entry->d_name.name;
	out.argsize = sizeof(arg);
	out.arg = &arg;
	request_send(fc, &in, &out);
	
	if(out.h.result) {
		/* Negative dentries are not hashed */
		if(out.h.result == -ENOENT)
			return NULL;
		else
			return ERR_PTR(out.h.result);
	}

	inode = iget(dir->i_sb, arg.ino);
	if(!inode) 
		return ERR_PTR(-ENOMEM);

	fuse_init_inode(inode, &arg.attr);
	d_add(entry, inode);
	return NULL;
}

/* create needs to return a positive entry, so this also does a lookup */
static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
		      int rdev)
{
	struct fuse_conn *fc = dir->i_sb->u.generic_sbp;
	struct fuse_in in = FUSE_IN_INIT;
	struct fuse_out out = FUSE_OUT_INIT;
	struct fuse_mknod_in *inarg;
	unsigned int insize;
	struct fuse_mknod_out outarg;
	struct inode *inode;
	
	insize = offsetof(struct fuse_mknod_in, name) + entry->d_name.len + 1;
	inarg = kmalloc(insize, GFP_KERNEL);
	if(!inarg)
		return -ENOMEM;
	
	inarg->mode = mode;
	inarg->rdev = rdev;
	strcpy(inarg->name, entry->d_name.name);

	in.h.opcode = FUSE_MKNOD;
	in.h.ino = dir->i_ino;
	in.argsize = insize;
	in.arg = inarg;
	out.argsize = sizeof(outarg);
	out.arg = &outarg;
	request_send(fc, &in, &out);
	kfree(inarg);

	if(out.h.result) 
		return out.h.result;

	inode = iget(dir->i_sb, outarg.ino);
	if(!inode) 
		return -ENOMEM;
	
	fuse_init_inode(inode, &outarg.attr);
	d_add(entry, inode);

	return 0;
}


static int fuse_create(struct inode *dir, struct dentry *entry, int mode)
{
	return fuse_mknod(dir, entry, mode, 0);
}

static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
{
	struct fuse_conn *fc = dir->i_sb->u.generic_sbp;
	struct fuse_in in = FUSE_IN_INIT;
	struct fuse_out out = FUSE_OUT_INIT;
	struct fuse_mkdir_in *inarg;
	unsigned int insize;
	
	insize = offsetof(struct fuse_mkdir_in, name) + entry->d_name.len + 1;
	inarg = kmalloc(insize, GFP_KERNEL);
	if(!inarg)
		return -ENOMEM;
	
	inarg->mode = mode;
	strcpy(inarg->name, entry->d_name.name);

	in.h.opcode = FUSE_MKDIR;
	in.h.ino = dir->i_ino;
	in.argsize = insize;
	in.arg = inarg;
	request_send(fc, &in, &out);
	kfree(inarg);

	return out.h.result;
}

static int fuse_symlink(struct inode *dir, struct dentry *entry,
			const char *link)
{
	struct fuse_conn *fc = dir->i_sb->u.generic_sbp;
	struct fuse_in in = FUSE_IN_INIT;
	struct fuse_out out = FUSE_OUT_INIT;
	char *inarg;
	unsigned int insize;
	
	insize = entry->d_name.len + 1 + strlen(link) + 1;
	inarg = kmalloc(insize, GFP_KERNEL);
	if(!inarg)
		return -ENOMEM;
	
	strcpy(inarg, entry->d_name.name);
	strcpy(inarg + entry->d_name.len + 1, link);

	in.h.opcode = FUSE_SYMLINK;
	in.h.ino = dir->i_ino;
	in.argsize = insize;
	in.arg = inarg;
	request_send(fc, &in, &out);
	kfree(inarg);
	
	return out.h.result;
}

static int fuse_remove(struct inode *dir, struct dentry *entry, 
		       enum fuse_opcode op)
{
	struct fuse_conn *fc = dir->i_sb->u.generic_sbp;
	struct fuse_in in = FUSE_IN_INIT;
	struct fuse_out out = FUSE_OUT_INIT;

	in.h.opcode = op;
	in.h.ino = dir->i_ino;
	in.argsize = entry->d_name.len + 1;
	in.arg = entry->d_name.name;
	request_send(fc, &in, &out);
	if(!out.h.result) {
		d_drop(entry);
		entry->d_inode->i_nlink = 0;
	}

	return out.h.result;
}

static int fuse_unlink(struct inode *dir, struct dentry *entry)
{
	return fuse_remove(dir, entry, FUSE_UNLINK);
}

static int fuse_rmdir(struct inode *dir, struct dentry *entry)
{
	return fuse_remove(dir, entry, FUSE_RMDIR);
}

static int fuse_rename(struct inode *olddir, struct dentry *oldent,
		       struct inode *newdir, struct dentry *newent)
{
	struct fuse_conn *fc = olddir->i_sb->u.generic_sbp;
	struct fuse_in in = FUSE_IN_INIT;
	struct fuse_out out = FUSE_OUT_INIT;
	struct fuse_rename_in *inarg;
	unsigned int oldnamsize = oldent->d_name.len + 1;
	unsigned int newnamsize = newent->d_name.len + 1;
	unsigned int insize;
	
	insize = offsetof(struct fuse_rename_in, names) + oldnamsize +
		newnamsize;
	inarg = kmalloc(insize, GFP_KERNEL);
	if(!inarg)
		return -ENOMEM;
	
	inarg->newdir = newdir->i_ino;
	strcpy(inarg->names, oldent->d_name.name);
	strcpy(inarg->names + oldnamsize, newent->d_name.name);

	in.h.opcode = FUSE_RENAME;
	in.h.ino = olddir->i_ino;
	in.argsize = insize;
	in.arg = inarg;
	request_send(fc, &in, &out);
	kfree(inarg);

	return out.h.result;
}

static int fuse_permission(struct inode *inode, int mask)
{
	return 0;
}

static int fuse_revalidate(struct dentry *dentry)
{
	struct inode *inode = dentry->d_inode;
	struct fuse_conn *fc = inode->i_sb->u.generic_sbp;
	struct fuse_in in = FUSE_IN_INIT;
	struct fuse_out out = FUSE_OUT_INIT;
	struct fuse_getattr_out arg;
	
	in.h.opcode = FUSE_GETATTR;
	in.h.ino = inode->i_ino;
	out.argsize = sizeof(arg);
	out.arg = &arg;
	request_send(fc, &in, &out);
	
	if(out.h.result == 0)
		change_attributes(inode, &arg.attr);
	else
		d_drop(dentry);

	return out.h.result;
}

static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
			 void *dstbuf, filldir_t filldir)
{
	while(nbytes >= FUSE_NAME_OFFSET) {
		struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
		size_t reclen = FUSE_DIRENT_SIZE(dirent);
		int over;
		if(dirent->namelen > NAME_MAX) {
			printk("fuse_readdir: name too long\n");
			return -EPROTO;
		}
		if(reclen > nbytes)
			break;

		over = filldir(dstbuf, dirent->name, dirent->namelen,
			      file->f_pos, dirent->ino, dirent->type);
		if(over)
			break;

		buf += reclen;
		file->f_pos += reclen;
		nbytes -= reclen;
	}

	return 0;
}

#define DIR_BUFSIZE 2048
static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
{
	struct file *cfile = file->private_data;
	char *buf;
	int ret;
	
	buf = kmalloc(DIR_BUFSIZE, GFP_KERNEL);
	if(!buf)
		return -ENOMEM;

	ret = kernel_read(cfile, file->f_pos, buf, DIR_BUFSIZE);
	if(ret < 0)
		printk("fuse_readdir: failed to read container file\n");
	else 
		ret = parse_dirfile(buf, ret, file, dstbuf, filldir);

	kfree(buf);	
	return ret;
}

static int read_link(struct dentry *dentry, char **bufp)
{
	struct inode *inode = dentry->d_inode;
	struct fuse_conn *fc = inode->i_sb->u.generic_sbp;
	struct fuse_in in = FUSE_IN_INIT;
	struct fuse_out out = FUSE_OUT_INIT;
	unsigned long page;

	page = __get_free_page(GFP_KERNEL);
	if(!page)
		return -ENOMEM;

	in.h.opcode = FUSE_READLINK;
	in.h.ino = inode->i_ino;
	out.arg = (void *) page;
	out.argsize = PAGE_SIZE - 1;
	out.argvar = 1;
	request_send(fc, &in, &out);
	if(out.h.result) {
		free_page(page);
		return out.h.result;
	}

	*bufp = (char *) page;
	(*bufp)[out.argsize] = '\0';
	return 0;
}

static void free_link(char *link)
{
	free_page((unsigned long) link);
}

static int fuse_readlink(struct dentry *dentry, char *buffer, int buflen)
{
	int ret;
	char *link;

	ret = read_link(dentry, &link);
	if(ret)
		return ret;

	ret = vfs_readlink(dentry, buffer, buflen, link);
	free_link(link);
	return ret;
}

static int fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
{
	int ret;
	char *link;

	ret = read_link(dentry, &link);
	if(ret)
		return ret;

	ret = vfs_follow_link(nd, link);
	free_link(link);
	return ret;
}

static int fuse_dir_open(struct inode *inode, struct file *file)
{
	struct fuse_conn *fc = inode->i_sb->u.generic_sbp;
	struct fuse_in in = FUSE_IN_INIT;
	struct fuse_out out = FUSE_OUT_INIT;
	struct fuse_getdir_out outarg;

	if(!(file->f_flags & O_DIRECTORY))
		return -EISDIR;
	
	in.h.opcode = FUSE_GETDIR;
	in.h.ino = inode->i_ino;
	out.argsize = sizeof(outarg);
	out.arg = &outarg;
	request_send(fc, &in, &out);
	if(out.h.result == 0) {
		struct file *cfile = outarg.file;
		struct inode *inode;
		if(!cfile) {
			printk("fuse_getdir: invalid file\n");
			return -EPROTO;
		}
		inode = cfile->f_dentry->d_inode;
		if(!S_ISREG(inode->i_mode)) {
			printk("fuse_getdir: not a regular file\n");
			fput(cfile);
			return -EPROTO;
		}

		file->private_data = cfile;
	}

	return out.h.result;
}

static int fuse_dir_release(struct inode *inode, struct file *file)
{
	struct file *cfile = file->private_data;

	if(!cfile)
		BUG();
	
	fput(cfile);

	return 0;
}

static struct inode_operations fuse_dir_inode_operations =
{
	lookup:		fuse_lookup,
	create:         fuse_create,
	mknod:          fuse_mknod,
	mkdir:          fuse_mkdir,
	symlink:        fuse_symlink,
	unlink:         fuse_unlink,
	rmdir:          fuse_rmdir,
	rename:         fuse_rename,
#if 0
	link:           fuse_link,
	setattr:	fuse_setattr,
#endif
	permission:	fuse_permission,
        revalidate:	fuse_revalidate,
};

static struct file_operations fuse_dir_operations = {
	read:		generic_read_dir,
	readdir:	fuse_readdir,
	open:		fuse_dir_open,
	release:	fuse_dir_release,
};

static struct inode_operations fuse_file_inode_operations = {
	permission:	fuse_permission,
        revalidate:	fuse_revalidate,
};

static struct inode_operations fuse_special_inode_operations = {
	permission:	fuse_permission,
        revalidate:	fuse_revalidate,
};

static struct file_operations fuse_file_operations = {
};

static struct inode_operations fuse_symlink_inode_operations =
{
	readlink:	fuse_readlink,
	follow_link:	fuse_follow_link,
        revalidate:	fuse_revalidate,
};

/* 
 * Local Variables:
 * indent-tabs-mode: t
 * c-basic-offset: 8
 * End:
 */