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