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