Blame view
fs/hostfs/hostfs_kern.c
20.8 KB
1da177e4c
|
1 |
/* |
f1adc05e7
|
2 |
* Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
1da177e4c
|
3 4 5 6 7 |
* Licensed under the GPL * * Ported the filesystem routines to 2.5. * 2003-02-10 Petr Baudis <pasky@ucw.cz> */ |
1da177e4c
|
8 |
#include <linux/fs.h> |
2b3b9bb03
|
9 |
#include <linux/magic.h> |
1da177e4c
|
10 |
#include <linux/module.h> |
84b3db04c
|
11 |
#include <linux/mm.h> |
1da177e4c
|
12 |
#include <linux/pagemap.h> |
1da177e4c
|
13 |
#include <linux/statfs.h> |
5a0e3ad6a
|
14 |
#include <linux/slab.h> |
dd2cc4dff
|
15 |
#include <linux/seq_file.h> |
6966a9775
|
16 |
#include <linux/mount.h> |
d0352d3ed
|
17 |
#include <linux/namei.h> |
1da177e4c
|
18 |
#include "hostfs.h" |
37185b332
|
19 20 |
#include <init.h> #include <kern.h> |
1da177e4c
|
21 22 |
struct hostfs_inode_info { |
1da177e4c
|
23 |
int fd; |
aeb5d7270
|
24 |
fmode_t mode; |
1da177e4c
|
25 |
struct inode vfs_inode; |
69886e676
|
26 |
struct mutex open_mutex; |
1da177e4c
|
27 28 29 30 |
}; static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode) { |
f1adc05e7
|
31 |
return list_entry(inode, struct hostfs_inode_info, vfs_inode); |
1da177e4c
|
32 |
} |
496ad9aa8
|
33 |
#define FILE_HOSTFS_I(file) HOSTFS_I(file_inode(file)) |
1da177e4c
|
34 |
|
1da177e4c
|
35 |
/* Changed in hostfs_args before the kernel starts running */ |
a6eb0be6d
|
36 |
static char *root_ino = ""; |
1da177e4c
|
37 |
static int append = 0; |
92e1d5be9
|
38 39 |
static const struct inode_operations hostfs_iops; static const struct inode_operations hostfs_dir_iops; |
d0352d3ed
|
40 |
static const struct inode_operations hostfs_link_iops; |
1da177e4c
|
41 42 43 44 45 46 47 |
#ifndef MODULE static int __init hostfs_args(char *options, int *add) { char *ptr; ptr = strchr(options, ','); |
84b3db04c
|
48 |
if (ptr != NULL) |
1da177e4c
|
49 |
*ptr++ = '\0'; |
84b3db04c
|
50 |
if (*options != '\0') |
1da177e4c
|
51 52 53 |
root_ino = options; options = ptr; |
84b3db04c
|
54 |
while (options) { |
1da177e4c
|
55 |
ptr = strchr(options, ','); |
84b3db04c
|
56 |
if (ptr != NULL) |
1da177e4c
|
57 |
*ptr++ = '\0'; |
84b3db04c
|
58 59 |
if (*options != '\0') { if (!strcmp(options, "append")) |
1da177e4c
|
60 61 62 63 64 65 66 |
append = 1; else printf("hostfs_args - unsupported option - %s ", options); } options = ptr; } |
f1adc05e7
|
67 |
return 0; |
1da177e4c
|
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
} __uml_setup("hostfs=", hostfs_args, "hostfs=<root dir>,<flags>,... " " This is used to set hostfs parameters. The root directory argument " " is used to confine all hostfs mounts to within the specified directory " " tree on the host. If this isn't specified, then a user inside UML can " " mount anything on the host that's accessible to the user that's running " " it. " " The only flag currently supported is 'append', which specifies that all " " files opened by hostfs will be opened in append mode. " ); #endif |
e9193059b
|
90 |
static char *__dentry_name(struct dentry *dentry, char *name) |
1da177e4c
|
91 |
{ |
ec2447c27
|
92 |
char *p = dentry_path_raw(dentry, name, PATH_MAX); |
e9193059b
|
93 94 |
char *root; size_t len; |
1da177e4c
|
95 |
|
e9193059b
|
96 97 98 99 |
root = dentry->d_sb->s_fs_info; len = strlen(root); if (IS_ERR(p)) { __putname(name); |
f1adc05e7
|
100 |
return NULL; |
1da177e4c
|
101 |
} |
aad50b1e0
|
102 103 104 105 106 107 |
/* * This function relies on the fact that dentry_path_raw() will place * the path name at the end of the provided buffer. */ BUG_ON(p + strlen(p) + 1 != name + PATH_MAX); |
850a496f9
|
108 |
strlcpy(name, root, PATH_MAX); |
e9193059b
|
109 110 111 112 |
if (len > p - name) { __putname(name); return NULL; } |
c278e81b8
|
113 114 115 |
if (p > name + len) strcpy(name + len, p); |
f1adc05e7
|
116 |
return name; |
1da177e4c
|
117 |
} |
e9193059b
|
118 119 120 121 122 |
static char *dentry_name(struct dentry *dentry) { char *name = __getname(); if (!name) return NULL; |
9dcc5e8a4
|
123 |
return __dentry_name(dentry, name); |
e9193059b
|
124 |
} |
c5322220e
|
125 |
static char *inode_name(struct inode *ino) |
1da177e4c
|
126 127 |
{ struct dentry *dentry; |
ec2447c27
|
128 |
char *name; |
1da177e4c
|
129 |
|
ec2447c27
|
130 131 |
dentry = d_find_alias(ino); if (!dentry) |
e9193059b
|
132 |
return NULL; |
ec2447c27
|
133 134 135 136 137 138 |
name = dentry_name(dentry); dput(dentry); return name; |
1da177e4c
|
139 |
} |
1da177e4c
|
140 141 142 143 |
static char *follow_link(char *link) { int len, n; char *name, *resolved, *end; |
7c9509924
|
144 145 |
name = __getname(); if (!name) { |
1da177e4c
|
146 |
n = -ENOMEM; |
7c9509924
|
147 |
goto out_free; |
1da177e4c
|
148 |
} |
7c9509924
|
149 150 |
n = hostfs_do_readlink(link, name, PATH_MAX); |
84b3db04c
|
151 |
if (n < 0) |
1da177e4c
|
152 |
goto out_free; |
7c9509924
|
153 154 155 156 |
else if (n == PATH_MAX) { n = -E2BIG; goto out_free; } |
1da177e4c
|
157 |
|
84b3db04c
|
158 |
if (*name == '/') |
f1adc05e7
|
159 |
return name; |
1da177e4c
|
160 161 |
end = strrchr(link, '/'); |
84b3db04c
|
162 |
if (end == NULL) |
f1adc05e7
|
163 |
return name; |
1da177e4c
|
164 165 166 167 168 |
*(end + 1) = '\0'; len = strlen(link) + strlen(name) + 1; resolved = kmalloc(len, GFP_KERNEL); |
84b3db04c
|
169 |
if (resolved == NULL) { |
1da177e4c
|
170 171 172 173 174 |
n = -ENOMEM; goto out_free; } sprintf(resolved, "%s%s", link, name); |
7c9509924
|
175 |
__putname(name); |
1da177e4c
|
176 |
kfree(link); |
f1adc05e7
|
177 |
return resolved; |
1da177e4c
|
178 179 |
out_free: |
7c9509924
|
180 |
__putname(name); |
f1adc05e7
|
181 |
return ERR_PTR(n); |
1da177e4c
|
182 |
} |
0a370e5de
|
183 184 |
static struct inode *hostfs_iget(struct super_block *sb) { |
52b209f7b
|
185 |
struct inode *inode = new_inode(sb); |
0a370e5de
|
186 187 |
if (!inode) return ERR_PTR(-ENOMEM); |
0a370e5de
|
188 189 |
return inode; } |
9e443bc36
|
190 |
static int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf) |
1da177e4c
|
191 |
{ |
84b3db04c
|
192 193 |
/* * do_statfs uses struct statfs64 internally, but the linux kernel |
1da177e4c
|
194 195 196 197 198 199 200 201 202 |
* struct statfs still has 32-bit versions for most of these fields, * so we convert them here */ int err; long long f_blocks; long long f_bfree; long long f_bavail; long long f_files; long long f_ffree; |
601d2c38b
|
203 |
err = do_statfs(dentry->d_sb->s_fs_info, |
1da177e4c
|
204 205 |
&sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files, &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid), |
1b627d577
|
206 |
&sf->f_namelen); |
84b3db04c
|
207 |
if (err) |
f1adc05e7
|
208 |
return err; |
1da177e4c
|
209 210 211 212 213 214 |
sf->f_blocks = f_blocks; sf->f_bfree = f_bfree; sf->f_bavail = f_bavail; sf->f_files = f_files; sf->f_ffree = f_ffree; sf->f_type = HOSTFS_SUPER_MAGIC; |
f1adc05e7
|
215 |
return 0; |
1da177e4c
|
216 217 218 219 220 |
} static struct inode *hostfs_alloc_inode(struct super_block *sb) { struct hostfs_inode_info *hi; |
371fdab10
|
221 |
hi = kmalloc(sizeof(*hi), GFP_KERNEL); |
84b3db04c
|
222 |
if (hi == NULL) |
f1adc05e7
|
223 |
return NULL; |
601d2c38b
|
224 |
hi->fd = -1; |
371fdab10
|
225 |
hi->mode = 0; |
1da177e4c
|
226 |
inode_init_once(&hi->vfs_inode); |
69886e676
|
227 |
mutex_init(&hi->open_mutex); |
f1adc05e7
|
228 |
return &hi->vfs_inode; |
1da177e4c
|
229 |
} |
e971a6d7b
|
230 |
static void hostfs_evict_inode(struct inode *inode) |
1da177e4c
|
231 |
{ |
91b0abe36
|
232 |
truncate_inode_pages_final(&inode->i_data); |
dbd5768f8
|
233 |
clear_inode(inode); |
84b3db04c
|
234 |
if (HOSTFS_I(inode)->fd != -1) { |
1da177e4c
|
235 236 237 |
close_file(&HOSTFS_I(inode)->fd); HOSTFS_I(inode)->fd = -1; } |
1da177e4c
|
238 |
} |
fa0d7e3de
|
239 |
static void hostfs_i_callback(struct rcu_head *head) |
1da177e4c
|
240 |
{ |
fa0d7e3de
|
241 |
struct inode *inode = container_of(head, struct inode, i_rcu); |
1da177e4c
|
242 243 |
kfree(HOSTFS_I(inode)); } |
fa0d7e3de
|
244 245 246 247 248 |
static void hostfs_destroy_inode(struct inode *inode) { call_rcu(&inode->i_rcu, hostfs_i_callback); } |
1da177e4c
|
249 |
|
34c80b1d9
|
250 |
static int hostfs_show_options(struct seq_file *seq, struct dentry *root) |
dd2cc4dff
|
251 |
{ |
34c80b1d9
|
252 |
const char *root_path = root->d_sb->s_fs_info; |
dd2cc4dff
|
253 254 255 256 |
size_t offset = strlen(root_ino) + 1; if (strlen(root_path) > offset) seq_printf(seq, ",%s", root_path + offset); |
7f74a6687
|
257 258 |
if (append) seq_puts(seq, ",append"); |
dd2cc4dff
|
259 260 |
return 0; } |
ee9b6d61a
|
261 |
static const struct super_operations hostfs_sbops = { |
1da177e4c
|
262 |
.alloc_inode = hostfs_alloc_inode, |
1da177e4c
|
263 |
.destroy_inode = hostfs_destroy_inode, |
e971a6d7b
|
264 |
.evict_inode = hostfs_evict_inode, |
1da177e4c
|
265 |
.statfs = hostfs_statfs, |
dd2cc4dff
|
266 |
.show_options = hostfs_show_options, |
1da177e4c
|
267 |
}; |
9e443bc36
|
268 |
static int hostfs_readdir(struct file *file, struct dir_context *ctx) |
1da177e4c
|
269 270 271 272 273 |
{ void *dir; char *name; unsigned long long next, ino; int error, len; |
3ee6bd8e8
|
274 |
unsigned int type; |
1da177e4c
|
275 |
|
c5322220e
|
276 |
name = dentry_name(file->f_path.dentry); |
84b3db04c
|
277 |
if (name == NULL) |
f1adc05e7
|
278 |
return -ENOMEM; |
1da177e4c
|
279 |
dir = open_dir(name, &error); |
e9193059b
|
280 |
__putname(name); |
84b3db04c
|
281 |
if (dir == NULL) |
f1adc05e7
|
282 |
return -error; |
8e28bc7e7
|
283 |
next = ctx->pos; |
0c9bd6365
|
284 |
seek_dir(dir, next); |
3ee6bd8e8
|
285 |
while ((name = read_dir(dir, &next, &ino, &len, &type)) != NULL) { |
8e28bc7e7
|
286 287 288 |
if (!dir_emit(ctx, name, len, ino, type)) break; ctx->pos = next; |
1da177e4c
|
289 290 |
} close_dir(dir); |
f1adc05e7
|
291 |
return 0; |
1da177e4c
|
292 |
} |
4c6dcafc6
|
293 |
static int hostfs_open(struct inode *ino, struct file *file) |
1da177e4c
|
294 295 |
{ char *name; |
bd1052a24
|
296 |
fmode_t mode; |
f8ad850f1
|
297 |
int err; |
bd1052a24
|
298 |
int r, w, fd; |
1da177e4c
|
299 300 |
mode = file->f_mode & (FMODE_READ | FMODE_WRITE); |
84b3db04c
|
301 |
if ((mode & HOSTFS_I(ino)->mode) == mode) |
f1adc05e7
|
302 |
return 0; |
1da177e4c
|
303 |
|
f8ad850f1
|
304 |
mode |= HOSTFS_I(ino)->mode; |
1da177e4c
|
305 |
|
f8ad850f1
|
306 |
retry: |
a9d1958b4
|
307 |
r = w = 0; |
f8ad850f1
|
308 |
if (mode & FMODE_READ) |
1da177e4c
|
309 |
r = 1; |
f8ad850f1
|
310 |
if (mode & FMODE_WRITE) |
112a5da71
|
311 |
r = w = 1; |
1da177e4c
|
312 |
|
c5322220e
|
313 |
name = dentry_name(file->f_path.dentry); |
84b3db04c
|
314 |
if (name == NULL) |
f1adc05e7
|
315 |
return -ENOMEM; |
1da177e4c
|
316 317 |
fd = open_file(name, r, w, append); |
e9193059b
|
318 |
__putname(name); |
84b3db04c
|
319 |
if (fd < 0) |
f1adc05e7
|
320 |
return fd; |
f8ad850f1
|
321 |
|
69886e676
|
322 |
mutex_lock(&HOSTFS_I(ino)->open_mutex); |
f8ad850f1
|
323 324 |
/* somebody else had handled it first? */ if ((mode & HOSTFS_I(ino)->mode) == mode) { |
69886e676
|
325 |
mutex_unlock(&HOSTFS_I(ino)->open_mutex); |
af9556586
|
326 |
close_file(&fd); |
f8ad850f1
|
327 328 329 330 |
return 0; } if ((mode | HOSTFS_I(ino)->mode) != mode) { mode |= HOSTFS_I(ino)->mode; |
69886e676
|
331 |
mutex_unlock(&HOSTFS_I(ino)->open_mutex); |
f8ad850f1
|
332 333 334 335 336 337 338 339 340 |
close_file(&fd); goto retry; } if (HOSTFS_I(ino)->fd == -1) { HOSTFS_I(ino)->fd = fd; } else { err = replace_file(fd, HOSTFS_I(ino)->fd); close_file(&fd); if (err < 0) { |
69886e676
|
341 |
mutex_unlock(&HOSTFS_I(ino)->open_mutex); |
f8ad850f1
|
342 343 344 345 |
return err; } } HOSTFS_I(ino)->mode = mode; |
69886e676
|
346 |
mutex_unlock(&HOSTFS_I(ino)->open_mutex); |
1da177e4c
|
347 |
|
f1adc05e7
|
348 |
return 0; |
1da177e4c
|
349 |
} |
65984ff9d
|
350 351 352 353 354 355 |
static int hostfs_file_release(struct inode *inode, struct file *file) { filemap_write_and_wait(inode->i_mapping); return 0; } |
9e443bc36
|
356 357 |
static int hostfs_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
1da177e4c
|
358 |
{ |
02c24a821
|
359 360 361 362 363 364 365 366 367 368 369 370 |
struct inode *inode = file->f_mapping->host; int ret; ret = filemap_write_and_wait_range(inode->i_mapping, start, end); if (ret) return ret; mutex_lock(&inode->i_mutex); ret = fsync_file(HOSTFS_I(inode)->fd, datasync); mutex_unlock(&inode->i_mutex); return ret; |
1da177e4c
|
371 |
} |
4b6f5d20b
|
372 |
static const struct file_operations hostfs_file_fops = { |
1da177e4c
|
373 |
.llseek = generic_file_llseek, |
5ffc4ef45
|
374 |
.splice_read = generic_file_splice_read, |
aad4f8bb4
|
375 |
.read_iter = generic_file_read_iter, |
8174202b3
|
376 |
.write_iter = generic_file_write_iter, |
1da177e4c
|
377 |
.mmap = generic_file_mmap, |
4c6dcafc6
|
378 |
.open = hostfs_open, |
65984ff9d
|
379 |
.release = hostfs_file_release, |
1da177e4c
|
380 381 |
.fsync = hostfs_fsync, }; |
4b6f5d20b
|
382 |
static const struct file_operations hostfs_dir_fops = { |
1da177e4c
|
383 |
.llseek = generic_file_llseek, |
8e28bc7e7
|
384 |
.iterate = hostfs_readdir, |
1da177e4c
|
385 |
.read = generic_read_dir, |
4c6dcafc6
|
386 387 |
.open = hostfs_open, .fsync = hostfs_fsync, |
1da177e4c
|
388 |
}; |
9e443bc36
|
389 |
static int hostfs_writepage(struct page *page, struct writeback_control *wbc) |
1da177e4c
|
390 391 392 393 |
{ struct address_space *mapping = page->mapping; struct inode *inode = mapping->host; char *buffer; |
af6aa1b9c
|
394 |
loff_t base = page_offset(page); |
1da177e4c
|
395 396 397 398 399 400 401 402 |
int count = PAGE_CACHE_SIZE; int end_index = inode->i_size >> PAGE_CACHE_SHIFT; int err; if (page->index >= end_index) count = inode->i_size & (PAGE_CACHE_SIZE-1); buffer = kmap(page); |
1da177e4c
|
403 404 |
err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count); |
84b3db04c
|
405 |
if (err != count) { |
1da177e4c
|
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
ClearPageUptodate(page); goto out; } if (base > inode->i_size) inode->i_size = base; if (PageError(page)) ClearPageError(page); err = 0; out: kunmap(page); unlock_page(page); return err; } |
9e443bc36
|
423 |
static int hostfs_readpage(struct file *file, struct page *page) |
1da177e4c
|
424 425 |
{ char *buffer; |
af6aa1b9c
|
426 |
loff_t start = page_offset(page); |
b86b413a3
|
427 |
int bytes_read, ret = 0; |
1da177e4c
|
428 |
|
1da177e4c
|
429 |
buffer = kmap(page); |
41761ddfa
|
430 |
bytes_read = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer, |
1da177e4c
|
431 |
PAGE_CACHE_SIZE); |
41761ddfa
|
432 |
if (bytes_read < 0) { |
b86b413a3
|
433 434 |
ClearPageUptodate(page); SetPageError(page); |
41761ddfa
|
435 |
ret = bytes_read; |
84b3db04c
|
436 |
goto out; |
41761ddfa
|
437 |
} |
1da177e4c
|
438 |
|
41761ddfa
|
439 |
memset(buffer + bytes_read, 0, PAGE_CACHE_SIZE - bytes_read); |
1da177e4c
|
440 |
|
b86b413a3
|
441 |
ClearPageError(page); |
1da177e4c
|
442 |
SetPageUptodate(page); |
b86b413a3
|
443 |
|
1da177e4c
|
444 |
out: |
b86b413a3
|
445 |
flush_dcache_page(page); |
1da177e4c
|
446 447 |
kunmap(page); unlock_page(page); |
41761ddfa
|
448 |
return ret; |
1da177e4c
|
449 |
} |
9e443bc36
|
450 451 452 |
static int hostfs_write_begin(struct file *file, struct address_space *mapping, loff_t pos, unsigned len, unsigned flags, struct page **pagep, void **fsdata) |
1da177e4c
|
453 |
{ |
ae361ff46
|
454 |
pgoff_t index = pos >> PAGE_CACHE_SHIFT; |
1da177e4c
|
455 |
|
54566b2c1
|
456 |
*pagep = grab_cache_page_write_begin(mapping, index, flags); |
ae361ff46
|
457 458 459 |
if (!*pagep) return -ENOMEM; return 0; |
1da177e4c
|
460 |
} |
9e443bc36
|
461 462 463 |
static int hostfs_write_end(struct file *file, struct address_space *mapping, loff_t pos, unsigned len, unsigned copied, struct page *page, void *fsdata) |
1da177e4c
|
464 |
{ |
1da177e4c
|
465 |
struct inode *inode = mapping->host; |
ae361ff46
|
466 467 468 |
void *buffer; unsigned from = pos & (PAGE_CACHE_SIZE - 1); int err; |
1da177e4c
|
469 |
|
1da177e4c
|
470 |
buffer = kmap(page); |
ae361ff46
|
471 472 |
err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied); kunmap(page); |
30f04a4ef
|
473 |
|
ae361ff46
|
474 475 |
if (!PageUptodate(page) && err == PAGE_CACHE_SIZE) SetPageUptodate(page); |
30f04a4ef
|
476 |
|
84b3db04c
|
477 478 |
/* * If err > 0, write_file has added err to pos, so we are comparing |
ae361ff46
|
479 480 481 482 483 484 |
* i_size against the last byte written. */ if (err > 0 && (pos > inode->i_size)) inode->i_size = pos; unlock_page(page); page_cache_release(page); |
1da177e4c
|
485 |
|
f1adc05e7
|
486 |
return err; |
1da177e4c
|
487 |
} |
f5e54d6e5
|
488 |
static const struct address_space_operations hostfs_aops = { |
1da177e4c
|
489 490 |
.writepage = hostfs_writepage, .readpage = hostfs_readpage, |
ffa0aea68
|
491 |
.set_page_dirty = __set_page_dirty_nobuffers, |
ae361ff46
|
492 493 |
.write_begin = hostfs_write_begin, .write_end = hostfs_write_end, |
1da177e4c
|
494 |
}; |
4754b8255
|
495 |
static int read_name(struct inode *ino, char *name) |
1da177e4c
|
496 |
{ |
4754b8255
|
497 498 499 500 501 |
dev_t rdev; struct hostfs_stat st; int err = stat_file(name, &st, -1); if (err) return err; |
1da177e4c
|
502 |
|
5e2df28cc
|
503 |
/* Reencode maj and min with the kernel encoding.*/ |
4754b8255
|
504 |
rdev = MKDEV(st.maj, st.min); |
1da177e4c
|
505 |
|
4754b8255
|
506 507 |
switch (st.mode & S_IFMT) { case S_IFLNK: |
d0352d3ed
|
508 |
ino->i_op = &hostfs_link_iops; |
1da177e4c
|
509 |
break; |
4754b8255
|
510 511 512 |
case S_IFDIR: ino->i_op = &hostfs_dir_iops; ino->i_fop = &hostfs_dir_fops; |
1da177e4c
|
513 |
break; |
4754b8255
|
514 515 516 517 518 519 |
case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK: init_special_inode(ino, st.mode & S_IFMT, rdev); ino->i_op = &hostfs_iops; |
1da177e4c
|
520 |
break; |
2ad2dca6f
|
521 |
case S_IFREG: |
4754b8255
|
522 523 524 |
ino->i_op = &hostfs_iops; ino->i_fop = &hostfs_file_fops; ino->i_mapping->a_ops = &hostfs_aops; |
2ad2dca6f
|
525 526 527 |
break; default: return -EIO; |
1da177e4c
|
528 |
} |
4754b8255
|
529 530 531 |
ino->i_ino = st.ino; ino->i_mode = st.mode; |
bfe868486
|
532 |
set_nlink(ino, st.nlink); |
29f82ae56
|
533 534 |
i_uid_write(ino, st.uid); i_gid_write(ino, st.gid); |
4754b8255
|
535 536 537 538 539 540 |
ino->i_atime = st.atime; ino->i_mtime = st.mtime; ino->i_ctime = st.ctime; ino->i_size = st.size; ino->i_blocks = st.blocks; return 0; |
1da177e4c
|
541 |
} |
9e443bc36
|
542 543 |
static int hostfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl) |
1da177e4c
|
544 545 546 547 |
{ struct inode *inode; char *name; int error, fd; |
0a370e5de
|
548 549 550 |
inode = hostfs_iget(dir->i_sb); if (IS_ERR(inode)) { error = PTR_ERR(inode); |
84b3db04c
|
551 |
goto out; |
0a370e5de
|
552 |
} |
1da177e4c
|
553 |
|
1da177e4c
|
554 |
error = -ENOMEM; |
c5322220e
|
555 |
name = dentry_name(dentry); |
84b3db04c
|
556 |
if (name == NULL) |
1da177e4c
|
557 |
goto out_put; |
b98b91029
|
558 |
fd = file_create(name, mode & S_IFMT); |
4754b8255
|
559 |
if (fd < 0) |
1da177e4c
|
560 |
error = fd; |
4754b8255
|
561 |
else |
5e2df28cc
|
562 |
error = read_name(inode, name); |
1da177e4c
|
563 |
|
e9193059b
|
564 |
__putname(name); |
84b3db04c
|
565 |
if (error) |
1da177e4c
|
566 567 568 569 570 |
goto out_put; HOSTFS_I(inode)->fd = fd; HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE; d_instantiate(dentry, inode); |
f1adc05e7
|
571 |
return 0; |
1da177e4c
|
572 573 574 575 |
out_put: iput(inode); out: |
f1adc05e7
|
576 |
return error; |
1da177e4c
|
577 |
} |
9e443bc36
|
578 579 |
static struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry, unsigned int flags) |
1da177e4c
|
580 581 582 583 |
{ struct inode *inode; char *name; int err; |
0a370e5de
|
584 585 586 |
inode = hostfs_iget(ino->i_sb); if (IS_ERR(inode)) { err = PTR_ERR(inode); |
1da177e4c
|
587 |
goto out; |
0a370e5de
|
588 |
} |
1da177e4c
|
589 |
|
1da177e4c
|
590 |
err = -ENOMEM; |
c5322220e
|
591 |
name = dentry_name(dentry); |
84b3db04c
|
592 |
if (name == NULL) |
1da177e4c
|
593 594 595 |
goto out_put; err = read_name(inode, name); |
5e2df28cc
|
596 |
|
e9193059b
|
597 |
__putname(name); |
84b3db04c
|
598 |
if (err == -ENOENT) { |
1da177e4c
|
599 600 601 |
iput(inode); inode = NULL; } |
84b3db04c
|
602 |
else if (err) |
1da177e4c
|
603 604 605 |
goto out_put; d_add(dentry, inode); |
f1adc05e7
|
606 |
return NULL; |
1da177e4c
|
607 608 609 610 |
out_put: iput(inode); out: |
f1adc05e7
|
611 |
return ERR_PTR(err); |
1da177e4c
|
612 |
} |
9e443bc36
|
613 614 |
static int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from) |
1da177e4c
|
615 |
{ |
f1adc05e7
|
616 617 |
char *from_name, *to_name; int err; |
1da177e4c
|
618 |
|
c5322220e
|
619 |
if ((from_name = dentry_name(from)) == NULL) |
f1adc05e7
|
620 |
return -ENOMEM; |
c5322220e
|
621 |
to_name = dentry_name(to); |
84b3db04c
|
622 |
if (to_name == NULL) { |
e9193059b
|
623 |
__putname(from_name); |
f1adc05e7
|
624 |
return -ENOMEM; |
1da177e4c
|
625 |
} |
f1adc05e7
|
626 |
err = link_file(to_name, from_name); |
e9193059b
|
627 628 |
__putname(from_name); __putname(to_name); |
f1adc05e7
|
629 |
return err; |
1da177e4c
|
630 |
} |
9e443bc36
|
631 |
static int hostfs_unlink(struct inode *ino, struct dentry *dentry) |
1da177e4c
|
632 633 634 |
{ char *file; int err; |
84b3db04c
|
635 |
if (append) |
f1adc05e7
|
636 |
return -EPERM; |
1da177e4c
|
637 |
|
f8d7e1877
|
638 639 |
if ((file = dentry_name(dentry)) == NULL) return -ENOMEM; |
1da177e4c
|
640 |
err = unlink_file(file); |
e9193059b
|
641 |
__putname(file); |
f1adc05e7
|
642 |
return err; |
1da177e4c
|
643 |
} |
9e443bc36
|
644 645 |
static int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to) |
1da177e4c
|
646 647 648 |
{ char *file; int err; |
c5322220e
|
649 |
if ((file = dentry_name(dentry)) == NULL) |
f1adc05e7
|
650 |
return -ENOMEM; |
1da177e4c
|
651 |
err = make_symlink(file, to); |
e9193059b
|
652 |
__putname(file); |
f1adc05e7
|
653 |
return err; |
1da177e4c
|
654 |
} |
9e443bc36
|
655 |
static int hostfs_mkdir(struct inode *ino, struct dentry *dentry, umode_t mode) |
1da177e4c
|
656 657 658 |
{ char *file; int err; |
c5322220e
|
659 |
if ((file = dentry_name(dentry)) == NULL) |
f1adc05e7
|
660 |
return -ENOMEM; |
1da177e4c
|
661 |
err = do_mkdir(file, mode); |
e9193059b
|
662 |
__putname(file); |
f1adc05e7
|
663 |
return err; |
1da177e4c
|
664 |
} |
9e443bc36
|
665 |
static int hostfs_rmdir(struct inode *ino, struct dentry *dentry) |
1da177e4c
|
666 667 668 |
{ char *file; int err; |
c5322220e
|
669 |
if ((file = dentry_name(dentry)) == NULL) |
f1adc05e7
|
670 |
return -ENOMEM; |
1da177e4c
|
671 |
err = do_rmdir(file); |
e9193059b
|
672 |
__putname(file); |
f1adc05e7
|
673 |
return err; |
1da177e4c
|
674 |
} |
1a67aafb5
|
675 |
static int hostfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) |
1da177e4c
|
676 677 678 |
{ struct inode *inode; char *name; |
0a370e5de
|
679 |
int err; |
1da177e4c
|
680 |
|
0a370e5de
|
681 682 683 |
inode = hostfs_iget(dir->i_sb); if (IS_ERR(inode)) { err = PTR_ERR(inode); |
1da177e4c
|
684 |
goto out; |
0a370e5de
|
685 |
} |
1da177e4c
|
686 |
|
1da177e4c
|
687 |
err = -ENOMEM; |
c5322220e
|
688 |
name = dentry_name(dentry); |
84b3db04c
|
689 |
if (name == NULL) |
1da177e4c
|
690 691 692 |
goto out_put; init_special_inode(inode, mode, dev); |
88f6cd0c3
|
693 |
err = do_mknod(name, mode, MAJOR(dev), MINOR(dev)); |
e9193059b
|
694 |
if (!err) |
1da177e4c
|
695 696 697 |
goto out_free; err = read_name(inode, name); |
e9193059b
|
698 |
__putname(name); |
5e2df28cc
|
699 700 |
if (err) goto out_put; |
84b3db04c
|
701 |
if (err) |
1da177e4c
|
702 703 704 |
goto out_put; d_instantiate(dentry, inode); |
f1adc05e7
|
705 |
return 0; |
1da177e4c
|
706 707 |
out_free: |
e9193059b
|
708 |
__putname(name); |
1da177e4c
|
709 710 711 |
out_put: iput(inode); out: |
f1adc05e7
|
712 |
return err; |
1da177e4c
|
713 |
} |
9a423bb6e
|
714 715 716 |
static int hostfs_rename2(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry, unsigned int flags) |
1da177e4c
|
717 |
{ |
9a423bb6e
|
718 |
char *old_name, *new_name; |
1da177e4c
|
719 |
int err; |
9a423bb6e
|
720 721 722 723 724 |
if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE)) return -EINVAL; old_name = dentry_name(old_dentry); if (old_name == NULL) |
f1adc05e7
|
725 |
return -ENOMEM; |
9a423bb6e
|
726 727 728 |
new_name = dentry_name(new_dentry); if (new_name == NULL) { __putname(old_name); |
f1adc05e7
|
729 |
return -ENOMEM; |
1da177e4c
|
730 |
} |
9a423bb6e
|
731 732 733 734 735 736 737 |
if (!flags) err = rename_file(old_name, new_name); else err = rename2_file(old_name, new_name, flags); __putname(old_name); __putname(new_name); |
f1adc05e7
|
738 |
return err; |
1da177e4c
|
739 |
} |
9e443bc36
|
740 |
static int hostfs_permission(struct inode *ino, int desired) |
1da177e4c
|
741 742 743 |
{ char *name; int r = 0, w = 0, x = 0, err; |
10556cb21
|
744 |
if (desired & MAY_NOT_BLOCK) |
b74c79e99
|
745 |
return -ECHILD; |
1da177e4c
|
746 747 748 |
if (desired & MAY_READ) r = 1; if (desired & MAY_WRITE) w = 1; if (desired & MAY_EXEC) x = 1; |
c5322220e
|
749 |
name = inode_name(ino); |
f1adc05e7
|
750 751 |
if (name == NULL) return -ENOMEM; |
1da177e4c
|
752 753 |
if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) || |
84b3db04c
|
754 |
S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode)) |
1da177e4c
|
755 756 757 |
err = 0; else err = access_file(name, r, w, x); |
e9193059b
|
758 |
__putname(name); |
84b3db04c
|
759 |
if (!err) |
2830ba7f3
|
760 |
err = generic_permission(ino, desired); |
1da177e4c
|
761 762 |
return err; } |
9e443bc36
|
763 |
static int hostfs_setattr(struct dentry *dentry, struct iattr *attr) |
1da177e4c
|
764 |
{ |
2b0143b5c
|
765 |
struct inode *inode = d_inode(dentry); |
1da177e4c
|
766 767 768 |
struct hostfs_iattr attrs; char *name; int err; |
1025774ce
|
769 |
int fd = HOSTFS_I(inode)->fd; |
5822b7fac
|
770 |
|
1025774ce
|
771 |
err = inode_change_ok(inode, attr); |
1da177e4c
|
772 773 |
if (err) return err; |
84b3db04c
|
774 |
if (append) |
1da177e4c
|
775 776 777 |
attr->ia_valid &= ~ATTR_SIZE; attrs.ia_valid = 0; |
84b3db04c
|
778 |
if (attr->ia_valid & ATTR_MODE) { |
1da177e4c
|
779 780 781 |
attrs.ia_valid |= HOSTFS_ATTR_MODE; attrs.ia_mode = attr->ia_mode; } |
84b3db04c
|
782 |
if (attr->ia_valid & ATTR_UID) { |
1da177e4c
|
783 |
attrs.ia_valid |= HOSTFS_ATTR_UID; |
29f82ae56
|
784 |
attrs.ia_uid = from_kuid(&init_user_ns, attr->ia_uid); |
1da177e4c
|
785 |
} |
84b3db04c
|
786 |
if (attr->ia_valid & ATTR_GID) { |
1da177e4c
|
787 |
attrs.ia_valid |= HOSTFS_ATTR_GID; |
29f82ae56
|
788 |
attrs.ia_gid = from_kgid(&init_user_ns, attr->ia_gid); |
1da177e4c
|
789 |
} |
84b3db04c
|
790 |
if (attr->ia_valid & ATTR_SIZE) { |
1da177e4c
|
791 792 793 |
attrs.ia_valid |= HOSTFS_ATTR_SIZE; attrs.ia_size = attr->ia_size; } |
84b3db04c
|
794 |
if (attr->ia_valid & ATTR_ATIME) { |
1da177e4c
|
795 796 797 |
attrs.ia_valid |= HOSTFS_ATTR_ATIME; attrs.ia_atime = attr->ia_atime; } |
84b3db04c
|
798 |
if (attr->ia_valid & ATTR_MTIME) { |
1da177e4c
|
799 800 801 |
attrs.ia_valid |= HOSTFS_ATTR_MTIME; attrs.ia_mtime = attr->ia_mtime; } |
84b3db04c
|
802 |
if (attr->ia_valid & ATTR_CTIME) { |
1da177e4c
|
803 804 805 |
attrs.ia_valid |= HOSTFS_ATTR_CTIME; attrs.ia_ctime = attr->ia_ctime; } |
84b3db04c
|
806 |
if (attr->ia_valid & ATTR_ATIME_SET) { |
1da177e4c
|
807 808 |
attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET; } |
84b3db04c
|
809 |
if (attr->ia_valid & ATTR_MTIME_SET) { |
1da177e4c
|
810 811 |
attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET; } |
c5322220e
|
812 |
name = dentry_name(dentry); |
84b3db04c
|
813 |
if (name == NULL) |
f1adc05e7
|
814 |
return -ENOMEM; |
5822b7fac
|
815 |
err = set_attr(name, &attrs, fd); |
e9193059b
|
816 |
__putname(name); |
84b3db04c
|
817 |
if (err) |
f1adc05e7
|
818 |
return err; |
1da177e4c
|
819 |
|
1025774ce
|
820 |
if ((attr->ia_valid & ATTR_SIZE) && |
bc077320f
|
821 |
attr->ia_size != i_size_read(inode)) |
3be2be0a3
|
822 |
truncate_setsize(inode, attr->ia_size); |
1025774ce
|
823 824 825 826 |
setattr_copy(inode, attr); mark_inode_dirty(inode); return 0; |
1da177e4c
|
827 |
} |
92e1d5be9
|
828 |
static const struct inode_operations hostfs_iops = { |
1da177e4c
|
829 830 |
.permission = hostfs_permission, .setattr = hostfs_setattr, |
1da177e4c
|
831 |
}; |
92e1d5be9
|
832 |
static const struct inode_operations hostfs_dir_iops = { |
1da177e4c
|
833 834 835 836 837 838 839 840 |
.create = hostfs_create, .lookup = hostfs_lookup, .link = hostfs_link, .unlink = hostfs_unlink, .symlink = hostfs_symlink, .mkdir = hostfs_mkdir, .rmdir = hostfs_rmdir, .mknod = hostfs_mknod, |
9a423bb6e
|
841 |
.rename2 = hostfs_rename2, |
1da177e4c
|
842 843 |
.permission = hostfs_permission, .setattr = hostfs_setattr, |
1da177e4c
|
844 |
}; |
d0352d3ed
|
845 846 847 848 849 850 851 |
static void *hostfs_follow_link(struct dentry *dentry, struct nameidata *nd) { char *link = __getname(); if (link) { char *path = dentry_name(dentry); int err = -ENOMEM; if (path) { |
3b6036d14
|
852 |
err = hostfs_do_readlink(path, link, PATH_MAX); |
d0352d3ed
|
853 854 |
if (err == PATH_MAX) err = -E2BIG; |
e9193059b
|
855 |
__putname(path); |
d0352d3ed
|
856 857 858 859 860 861 862 |
} if (err < 0) { __putname(link); link = ERR_PTR(err); } } else { link = ERR_PTR(-ENOMEM); |
1da177e4c
|
863 |
} |
d0352d3ed
|
864 865 866 867 868 869 870 871 872 873 |
nd_set_link(nd, link); return NULL; } static void hostfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) { char *s = nd_get_link(nd); if (!IS_ERR(s)) __putname(s); |
1da177e4c
|
874 |
} |
d0352d3ed
|
875 876 877 878 |
static const struct inode_operations hostfs_link_iops = { .readlink = generic_readlink, .follow_link = hostfs_follow_link, .put_link = hostfs_put_link, |
1da177e4c
|
879 880 881 882 883 |
}; static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent) { struct inode *root_inode; |
75e8defbe
|
884 |
char *host_root_path, *req_root = d; |
1da177e4c
|
885 886 887 888 889 890 |
int err; sb->s_blocksize = 1024; sb->s_blocksize_bits = 10; sb->s_magic = HOSTFS_SUPER_MAGIC; sb->s_op = &hostfs_sbops; |
b26d4cd38
|
891 |
sb->s_d_op = &simple_dentry_operations; |
752fa51e4
|
892 |
sb->s_maxbytes = MAX_LFS_FILESIZE; |
1da177e4c
|
893 |
|
a6eb0be6d
|
894 |
/* NULL is printed as <NULL> by sprintf: avoid that. */ |
75e8defbe
|
895 896 |
if (req_root == NULL) req_root = ""; |
1da177e4c
|
897 898 |
err = -ENOMEM; |
601d2c38b
|
899 900 |
sb->s_fs_info = host_root_path = kmalloc(strlen(root_ino) + strlen(req_root) + 2, GFP_KERNEL); |
84b3db04c
|
901 |
if (host_root_path == NULL) |
1da177e4c
|
902 |
goto out; |
75e8defbe
|
903 |
sprintf(host_root_path, "%s/%s", root_ino, req_root); |
1da177e4c
|
904 |
|
52b209f7b
|
905 906 |
root_inode = new_inode(sb); if (!root_inode) |
601d2c38b
|
907 |
goto out; |
1da177e4c
|
908 |
|
4754b8255
|
909 910 911 |
err = read_name(root_inode, host_root_path); if (err) goto out_put; |
52b209f7b
|
912 |
|
4754b8255
|
913 |
if (S_ISLNK(root_inode->i_mode)) { |
52b209f7b
|
914 915 916 917 918 919 |
char *name = follow_link(host_root_path); if (IS_ERR(name)) err = PTR_ERR(name); else err = read_name(root_inode, name); kfree(name); |
4754b8255
|
920 921 |
if (err) goto out_put; |
52b209f7b
|
922 |
} |
1da177e4c
|
923 |
|
1da177e4c
|
924 |
err = -ENOMEM; |
48fde701a
|
925 |
sb->s_root = d_make_root(root_inode); |
84b3db04c
|
926 |
if (sb->s_root == NULL) |
48fde701a
|
927 |
goto out; |
1da177e4c
|
928 |
|
f1adc05e7
|
929 |
return 0; |
1da177e4c
|
930 |
|
f1adc05e7
|
931 932 |
out_put: iput(root_inode); |
f1adc05e7
|
933 934 |
out: return err; |
1da177e4c
|
935 |
} |
3c26ff6e4
|
936 |
static struct dentry *hostfs_read_sb(struct file_system_type *type, |
454e2398b
|
937 |
int flags, const char *dev_name, |
3c26ff6e4
|
938 |
void *data) |
1da177e4c
|
939 |
{ |
3c26ff6e4
|
940 |
return mount_nodev(type, flags, data, hostfs_fill_sb_common); |
1da177e4c
|
941 |
} |
601d2c38b
|
942 943 944 945 946 |
static void hostfs_kill_sb(struct super_block *s) { kill_anon_super(s); kfree(s->s_fs_info); } |
1da177e4c
|
947 948 949 |
static struct file_system_type hostfs_type = { .owner = THIS_MODULE, .name = "hostfs", |
3c26ff6e4
|
950 |
.mount = hostfs_read_sb, |
601d2c38b
|
951 |
.kill_sb = hostfs_kill_sb, |
1da177e4c
|
952 953 |
.fs_flags = 0, }; |
3e64fe5b2
|
954 |
MODULE_ALIAS_FS("hostfs"); |
1da177e4c
|
955 956 957 |
static int __init init_hostfs(void) { |
f1adc05e7
|
958 |
return register_filesystem(&hostfs_type); |
1da177e4c
|
959 960 961 962 963 964 965 966 967 968 |
} static void __exit exit_hostfs(void) { unregister_filesystem(&hostfs_type); } module_init(init_hostfs) module_exit(exit_hostfs) MODULE_LICENSE("GPL"); |