Blame view
fs/befs/linuxvfs.c
25.5 KB
09c434b8a
|
1 |
// SPDX-License-Identifier: GPL-2.0-only |
1da177e4c
|
2 3 4 5 6 7 |
/* * linux/fs/befs/linuxvfs.c * * Copyright (C) 2001 Will Dyson <will_dyson@pobox.com * */ |
dac52fc18
|
8 |
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
1da177e4c
|
9 10 11 12 13 14 15 16 17 18 |
#include <linux/module.h> #include <linux/slab.h> #include <linux/fs.h> #include <linux/errno.h> #include <linux/stat.h> #include <linux/nls.h> #include <linux/buffer_head.h> #include <linux/vfs.h> #include <linux/parser.h> #include <linux/namei.h> |
31aba059b
|
19 |
#include <linux/sched.h> |
5b825c3af
|
20 |
#include <linux/cred.h> |
ac632f5b6
|
21 |
#include <linux/exportfs.h> |
3ab7947ac
|
22 |
#include <linux/seq_file.h> |
621c1f429
|
23 |
#include <linux/blkdev.h> |
1da177e4c
|
24 25 26 27 28 29 30 |
#include "befs.h" #include "btree.h" #include "inode.h" #include "datastream.h" #include "super.h" #include "io.h" |
1da177e4c
|
31 32 33 34 35 36 37 |
MODULE_DESCRIPTION("BeOS File System (BeFS) driver"); MODULE_AUTHOR("Will Dyson"); MODULE_LICENSE("GPL"); /* The units the vfs expects inode->i_blocks to be in */ #define VFS_BLOCK_SIZE 512 |
f0f49ef5c
|
38 |
static int befs_readdir(struct file *, struct dir_context *); |
1da177e4c
|
39 40 41 |
static int befs_get_block(struct inode *, sector_t, struct buffer_head *, int); static int befs_readpage(struct file *file, struct page *page); static sector_t befs_bmap(struct address_space *mapping, sector_t block); |
62b80719d
|
42 43 |
static struct dentry *befs_lookup(struct inode *, struct dentry *, unsigned int); |
96eb54194
|
44 |
static struct inode *befs_iget(struct super_block *, unsigned long); |
1da177e4c
|
45 |
static struct inode *befs_alloc_inode(struct super_block *sb); |
49f82a808
|
46 |
static void befs_free_inode(struct inode *inode); |
1da177e4c
|
47 |
static void befs_destroy_inodecache(void); |
11803f97f
|
48 |
static int befs_symlink_readpage(struct file *, struct page *); |
1da177e4c
|
49 50 51 52 53 54 |
static int befs_utf2nls(struct super_block *sb, const char *in, int in_len, char **out, int *out_len); static int befs_nls2utf(struct super_block *sb, const char *in, int in_len, char **out, int *out_len); static void befs_put_super(struct super_block *); static int befs_remount(struct super_block *, int *, char *); |
726c33422
|
55 |
static int befs_statfs(struct dentry *, struct kstatfs *); |
3ab7947ac
|
56 |
static int befs_show_options(struct seq_file *, struct dentry *); |
09ad0eae5
|
57 |
static int parse_options(char *, struct befs_mount_options *); |
ac632f5b6
|
58 59 60 61 |
static struct dentry *befs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len, int fh_type); static struct dentry *befs_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len, int fh_type); |
dcfd9b215
|
62 |
static struct dentry *befs_get_parent(struct dentry *child); |
1da177e4c
|
63 64 |
static const struct super_operations befs_sops = { |
1da177e4c
|
65 |
.alloc_inode = befs_alloc_inode, /* allocate a new inode */ |
49f82a808
|
66 |
.free_inode = befs_free_inode, /* deallocate an inode */ |
1da177e4c
|
67 68 69 |
.put_super = befs_put_super, /* uninit super */ .statfs = befs_statfs, /* statfs */ .remount_fs = befs_remount, |
3ab7947ac
|
70 |
.show_options = befs_show_options, |
1da177e4c
|
71 72 73 |
}; /* slab cache for befs_inode_info objects */ |
e18b890bb
|
74 |
static struct kmem_cache *befs_inode_cachep; |
1da177e4c
|
75 |
|
4b6f5d20b
|
76 |
static const struct file_operations befs_dir_operations = { |
1da177e4c
|
77 |
.read = generic_read_dir, |
e23e9aa75
|
78 |
.iterate_shared = befs_readdir, |
59af1584b
|
79 |
.llseek = generic_file_llseek, |
1da177e4c
|
80 |
}; |
754661f14
|
81 |
static const struct inode_operations befs_dir_inode_operations = { |
1da177e4c
|
82 83 |
.lookup = befs_lookup, }; |
f5e54d6e5
|
84 |
static const struct address_space_operations befs_aops = { |
1da177e4c
|
85 |
.readpage = befs_readpage, |
1da177e4c
|
86 87 |
.bmap = befs_bmap, }; |
11803f97f
|
88 89 |
static const struct address_space_operations befs_symlink_aops = { .readpage = befs_symlink_readpage, |
1da177e4c
|
90 |
}; |
ac632f5b6
|
91 92 93 |
static const struct export_operations befs_export_operations = { .fh_to_dentry = befs_fh_to_dentry, .fh_to_parent = befs_fh_to_parent, |
dcfd9b215
|
94 |
.get_parent = befs_get_parent, |
ac632f5b6
|
95 |
}; |
e60f749b6
|
96 |
/* |
1da177e4c
|
97 |
* Called by generic_file_read() to read a page of data |
e60f749b6
|
98 |
* |
1da177e4c
|
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
* In turn, simply calls a generic block read function and * passes it the address of befs_get_block, for mapping file * positions to disk blocks. */ static int befs_readpage(struct file *file, struct page *page) { return block_read_full_page(page, befs_get_block); } static sector_t befs_bmap(struct address_space *mapping, sector_t block) { return generic_block_bmap(mapping, block, befs_get_block); } |
e60f749b6
|
114 115 |
/* * Generic function to map a file position (block) to a |
1da177e4c
|
116 117 118 119 120 |
* disk offset (passed back in bh_result). * * Used by many higher level functions. * * Calls befs_fblock2brun() in datastream.c to do the real work. |
1da177e4c
|
121 122 123 124 125 126 127 128 129 |
*/ static int befs_get_block(struct inode *inode, sector_t block, struct buffer_head *bh_result, int create) { struct super_block *sb = inode->i_sb; befs_data_stream *ds = &BEFS_I(inode)->i_data.ds; befs_block_run run = BAD_IADDR; |
abcf91169
|
130 |
int res; |
1da177e4c
|
131 132 133 |
ulong disk_off; befs_debug(sb, "---> befs_get_block() for inode %lu, block %ld", |
dac52fc18
|
134 |
(unsigned long)inode->i_ino, (long)block); |
1da177e4c
|
135 136 |
if (create) { befs_error(sb, "befs_get_block() was asked to write to " |
dac52fc18
|
137 138 |
"block %ld in inode %lu", (long)block, (unsigned long)inode->i_ino); |
1da177e4c
|
139 140 141 142 143 144 |
return -EPERM; } res = befs_fblock2brun(sb, ds, block, &run); if (res != BEFS_OK) { befs_error(sb, |
dac52fc18
|
145 146 147 |
"<--- %s for inode %lu, block %ld ERROR", __func__, (unsigned long)inode->i_ino, (long)block); |
1da177e4c
|
148 149 150 151 152 153 |
return -EFBIG; } disk_off = (ulong) iaddr2blockno(sb, &run); map_bh(bh_result, inode->i_sb, disk_off); |
dac52fc18
|
154 155 156 |
befs_debug(sb, "<--- %s for inode %lu, block %ld, disk address %lu", __func__, (unsigned long)inode->i_ino, (long)block, (unsigned long)disk_off); |
1da177e4c
|
157 158 159 160 161 |
return 0; } static struct dentry * |
00cd8dd3b
|
162 |
befs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) |
1da177e4c
|
163 |
{ |
c94087636
|
164 |
struct inode *inode; |
1da177e4c
|
165 |
struct super_block *sb = dir->i_sb; |
22341d8f3
|
166 |
const befs_data_stream *ds = &BEFS_I(dir)->i_data.ds; |
1da177e4c
|
167 168 169 170 171 |
befs_off_t offset; int ret; int utfnamelen; char *utfname; const char *name = dentry->d_name.name; |
a455589f1
|
172 173 |
befs_debug(sb, "---> %s name %pd inode %ld", __func__, dentry, dir->i_ino); |
1da177e4c
|
174 175 176 177 178 179 |
/* Convert to UTF-8 */ if (BEFS_SB(sb)->nls) { ret = befs_nls2utf(sb, name, strlen(name), &utfname, &utfnamelen); if (ret < 0) { |
dac52fc18
|
180 |
befs_debug(sb, "<--- %s ERROR", __func__); |
1da177e4c
|
181 182 183 184 185 186 |
return ERR_PTR(ret); } ret = befs_btree_find(sb, ds, utfname, &offset); kfree(utfname); } else { |
f7769f9cf
|
187 |
ret = befs_btree_find(sb, ds, name, &offset); |
1da177e4c
|
188 189 190 |
} if (ret == BEFS_BT_NOT_FOUND) { |
a455589f1
|
191 |
befs_debug(sb, "<--- %s %pd not found", __func__, dentry); |
f4e4d434f
|
192 |
inode = NULL; |
1da177e4c
|
193 |
} else if (ret != BEFS_OK || offset == 0) { |
4c3897cce
|
194 |
befs_error(sb, "<--- %s Error", __func__); |
f4e4d434f
|
195 196 197 |
inode = ERR_PTR(-ENODATA); } else { inode = befs_iget(dir->i_sb, (ino_t) offset); |
1da177e4c
|
198 |
} |
dac52fc18
|
199 |
befs_debug(sb, "<--- %s", __func__); |
1da177e4c
|
200 |
|
f4e4d434f
|
201 |
return d_splice_alias(inode, dentry); |
1da177e4c
|
202 203 204 |
} static int |
f0f49ef5c
|
205 |
befs_readdir(struct file *file, struct dir_context *ctx) |
1da177e4c
|
206 |
{ |
f0f49ef5c
|
207 |
struct inode *inode = file_inode(file); |
1da177e4c
|
208 |
struct super_block *sb = inode->i_sb; |
22341d8f3
|
209 |
const befs_data_stream *ds = &BEFS_I(inode)->i_data.ds; |
1da177e4c
|
210 211 212 |
befs_off_t value; int result; size_t keysize; |
1da177e4c
|
213 |
char keybuf[BEFS_NAME_LEN + 1]; |
1da177e4c
|
214 |
|
a455589f1
|
215 216 |
befs_debug(sb, "---> %s name %pD, inode %ld, ctx->pos %lld", __func__, file, inode->i_ino, ctx->pos); |
1da177e4c
|
217 |
|
9ae51a32b
|
218 219 220 |
while (1) { result = befs_btree_read(sb, ds, ctx->pos, BEFS_NAME_LEN + 1, keybuf, &keysize, &value); |
1da177e4c
|
221 |
|
9ae51a32b
|
222 |
if (result == BEFS_ERR) { |
dac52fc18
|
223 |
befs_debug(sb, "<--- %s ERROR", __func__); |
9ae51a32b
|
224 225 226 |
befs_error(sb, "IO error reading %pD (inode %lu)", file, inode->i_ino); return -EIO; |
1da177e4c
|
227 |
|
9ae51a32b
|
228 229 230 |
} else if (result == BEFS_BT_END) { befs_debug(sb, "<--- %s END", __func__); return 0; |
1da177e4c
|
231 |
|
9ae51a32b
|
232 233 234 |
} else if (result == BEFS_BT_EMPTY) { befs_debug(sb, "<--- %s Empty directory", __func__); return 0; |
1da177e4c
|
235 |
} |
9ae51a32b
|
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
/* Convert to NLS */ if (BEFS_SB(sb)->nls) { char *nlsname; int nlsnamelen; result = befs_utf2nls(sb, keybuf, keysize, &nlsname, &nlsnamelen); if (result < 0) { befs_debug(sb, "<--- %s ERROR", __func__); return result; } if (!dir_emit(ctx, nlsname, nlsnamelen, (ino_t) value, DT_UNKNOWN)) { kfree(nlsname); return 0; } |
f0f49ef5c
|
254 |
kfree(nlsname); |
9ae51a32b
|
255 256 257 258 |
} else { if (!dir_emit(ctx, keybuf, keysize, (ino_t) value, DT_UNKNOWN)) return 0; |
f0f49ef5c
|
259 |
} |
9ae51a32b
|
260 |
ctx->pos++; |
1da177e4c
|
261 |
} |
1da177e4c
|
262 263 264 265 266 |
} static struct inode * befs_alloc_inode(struct super_block *sb) { |
111d639dd
|
267 268 269 |
struct befs_inode_info *bi; bi = kmem_cache_alloc(befs_inode_cachep, GFP_KERNEL); |
62b80719d
|
270 271 272 |
if (!bi) return NULL; return &bi->vfs_inode; |
1da177e4c
|
273 |
} |
49f82a808
|
274 |
static void befs_free_inode(struct inode *inode) |
1da177e4c
|
275 |
{ |
62b80719d
|
276 |
kmem_cache_free(befs_inode_cachep, BEFS_I(inode)); |
1da177e4c
|
277 |
} |
51cc50685
|
278 |
static void init_once(void *foo) |
1da177e4c
|
279 |
{ |
62b80719d
|
280 |
struct befs_inode_info *bi = (struct befs_inode_info *) foo; |
a35afb830
|
281 282 |
inode_init_once(&bi->vfs_inode); |
1da177e4c
|
283 |
} |
96eb54194
|
284 |
static struct inode *befs_iget(struct super_block *sb, unsigned long ino) |
1da177e4c
|
285 |
{ |
e4c7f5b10
|
286 |
struct buffer_head *bh; |
44ad80986
|
287 |
befs_inode *raw_inode; |
038428fcf
|
288 |
struct befs_sb_info *befs_sb = BEFS_SB(sb); |
d6e020336
|
289 |
struct befs_inode_info *befs_ino; |
96eb54194
|
290 |
struct inode *inode; |
1da177e4c
|
291 |
|
dac52fc18
|
292 |
befs_debug(sb, "---> %s inode = %lu", __func__, ino); |
96eb54194
|
293 294 |
inode = iget_locked(sb, ino); |
ac34a1b35
|
295 296 |
if (!inode) return ERR_PTR(-ENOMEM); |
96eb54194
|
297 298 |
if (!(inode->i_state & I_NEW)) return inode; |
1da177e4c
|
299 300 301 302 303 304 305 306 307 |
befs_ino = BEFS_I(inode); /* convert from vfs's inode number to befs's inode number */ befs_ino->i_inode_num = blockno2iaddr(sb, inode->i_ino); befs_debug(sb, " real inode number [%u, %hu, %hu]", befs_ino->i_inode_num.allocation_group, befs_ino->i_inode_num.start, befs_ino->i_inode_num.len); |
f7f675406
|
308 |
bh = sb_bread(sb, inode->i_ino); |
1da177e4c
|
309 310 311 |
if (!bh) { befs_error(sb, "unable to read inode block - " "inode = %lu", inode->i_ino); |
0418726bb
|
312 |
goto unacquire_none; |
1da177e4c
|
313 314 315 316 317 318 319 320 |
} raw_inode = (befs_inode *) bh->b_data; befs_dump_inode(sb, raw_inode); if (befs_check_inode(sb, raw_inode, inode->i_ino) != BEFS_OK) { befs_error(sb, "Bad inode: %lu", inode->i_ino); |
0418726bb
|
321 |
goto unacquire_bh; |
1da177e4c
|
322 323 324 325 326 327 328 |
} inode->i_mode = (umode_t) fs32_to_cpu(sb, raw_inode->mode); /* * set uid and gid. But since current BeOS is single user OS, so * you can change by "uid" or "gid" options. |
e60f749b6
|
329 |
*/ |
1da177e4c
|
330 331 |
inode->i_uid = befs_sb->mount_opts.use_uid ? |
31aba059b
|
332 333 |
befs_sb->mount_opts.uid : make_kuid(&init_user_ns, fs32_to_cpu(sb, raw_inode->uid)); |
1da177e4c
|
334 |
inode->i_gid = befs_sb->mount_opts.use_gid ? |
31aba059b
|
335 336 |
befs_sb->mount_opts.gid : make_kgid(&init_user_ns, fs32_to_cpu(sb, raw_inode->gid)); |
1da177e4c
|
337 |
|
bfe868486
|
338 |
set_nlink(inode, 1); |
1da177e4c
|
339 340 341 342 343 |
/* * BEFS's time is 64 bits, but current VFS is 32 bits... * BEFS don't have access time. Nor inode change time. VFS * doesn't have creation time. |
e60f749b6
|
344 |
* Also, the lower 16 bits of the last_modified_time and |
1da177e4c
|
345 346 347 348 349 350 |
* create_time are just a counter to help ensure uniqueness * for indexing purposes. (PFD, page 54) */ inode->i_mtime.tv_sec = fs64_to_cpu(sb, raw_inode->last_modified_time) >> 16; |
e60f749b6
|
351 |
inode->i_mtime.tv_nsec = 0; /* lower 16 bits are not a time */ |
1da177e4c
|
352 353 |
inode->i_ctime = inode->i_mtime; inode->i_atime = inode->i_mtime; |
1da177e4c
|
354 355 356 357 358 359 360 361 362 |
befs_ino->i_inode_num = fsrun_to_cpu(sb, raw_inode->inode_num); befs_ino->i_parent = fsrun_to_cpu(sb, raw_inode->parent); befs_ino->i_attribute = fsrun_to_cpu(sb, raw_inode->attributes); befs_ino->i_flags = fs32_to_cpu(sb, raw_inode->flags); if (S_ISLNK(inode->i_mode) && !(befs_ino->i_flags & BEFS_LONG_SYMLINK)){ inode->i_size = 0; inode->i_blocks = befs_sb->block_size / VFS_BLOCK_SIZE; |
39d7a29f8
|
363 364 |
strlcpy(befs_ino->i_data.symlink, raw_inode->data.symlink, BEFS_SYMLINK_LEN); |
1da177e4c
|
365 366 367 368 |
} else { int num_blks; befs_ino->i_data.ds = |
e0e3d32bb
|
369 |
fsds_to_cpu(sb, &raw_inode->data.datastream); |
1da177e4c
|
370 371 372 373 374 375 376 377 378 379 |
num_blks = befs_count_blocks(sb, &befs_ino->i_data.ds); inode->i_blocks = num_blks * (befs_sb->block_size / VFS_BLOCK_SIZE); inode->i_size = befs_ino->i_data.ds.size; } inode->i_mapping->a_ops = &befs_aops; if (S_ISREG(inode->i_mode)) { |
2cf069162
|
380 |
inode->i_fop = &generic_ro_fops; |
1da177e4c
|
381 382 383 384 |
} else if (S_ISDIR(inode->i_mode)) { inode->i_op = &befs_dir_inode_operations; inode->i_fop = &befs_dir_operations; } else if (S_ISLNK(inode->i_mode)) { |
d0deec199
|
385 |
if (befs_ino->i_flags & BEFS_LONG_SYMLINK) { |
11803f97f
|
386 |
inode->i_op = &page_symlink_inode_operations; |
21fc61c73
|
387 |
inode_nohighmem(inode); |
11803f97f
|
388 |
inode->i_mapping->a_ops = &befs_symlink_aops; |
d0deec199
|
389 390 391 392 |
} else { inode->i_link = befs_ino->i_data.symlink; inode->i_op = &simple_symlink_inode_operations; } |
1da177e4c
|
393 394 395 396 |
} else { befs_error(sb, "Inode %lu is not a regular file, " "directory or symlink. THAT IS WRONG! BeFS has no " "on disk special files", inode->i_ino); |
0418726bb
|
397 |
goto unacquire_bh; |
1da177e4c
|
398 399 400 |
} brelse(bh); |
dac52fc18
|
401 |
befs_debug(sb, "<--- %s", __func__); |
96eb54194
|
402 403 |
unlock_new_inode(inode); return inode; |
1da177e4c
|
404 |
|
62b80719d
|
405 |
unacquire_bh: |
1da177e4c
|
406 |
brelse(bh); |
62b80719d
|
407 |
unacquire_none: |
96eb54194
|
408 |
iget_failed(inode); |
dac52fc18
|
409 |
befs_debug(sb, "<--- %s - Bad inode", __func__); |
33c712b4f
|
410 |
return ERR_PTR(-EIO); |
1da177e4c
|
411 412 413 |
} /* Initialize the inode cache. Called at fs setup. |
20c2df83d
|
414 |
* |
1da177e4c
|
415 416 |
* Taken from NFS implementation by Al Viro. */ |
91a52ab7d
|
417 |
static int __init |
1da177e4c
|
418 419 |
befs_init_inodecache(void) { |
0fc256d3a
|
420 421 422 423 424 425 426 427 428 |
befs_inode_cachep = kmem_cache_create_usercopy("befs_inode_cache", sizeof(struct befs_inode_info), 0, (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD| SLAB_ACCOUNT), offsetof(struct befs_inode_info, i_data.symlink), sizeof_field(struct befs_inode_info, i_data.symlink), init_once); |
c08f1cb62
|
429 |
if (befs_inode_cachep == NULL) |
1da177e4c
|
430 |
return -ENOMEM; |
c08f1cb62
|
431 |
|
1da177e4c
|
432 433 434 435 |
return 0; } /* Called at fs teardown. |
e60f749b6
|
436 |
* |
1da177e4c
|
437 438 439 440 441 |
* Taken from NFS implementation by Al Viro. */ static void befs_destroy_inodecache(void) { |
8c0a85377
|
442 443 444 445 446 |
/* * Make sure all delayed rcu free inodes are flushed before we * destroy cache. */ rcu_barrier(); |
1a1d92c10
|
447 |
kmem_cache_destroy(befs_inode_cachep); |
1da177e4c
|
448 449 450 451 452 453 454 |
} /* * The inode of symbolic link is different to data stream. * The data stream become link name. Unless the LONG_SYMLINK * flag is set. */ |
11803f97f
|
455 |
static int befs_symlink_readpage(struct file *unused, struct page *page) |
1da177e4c
|
456 |
{ |
11803f97f
|
457 458 459 |
struct inode *inode = page->mapping->host; struct super_block *sb = inode->i_sb; struct befs_inode_info *befs_ino = BEFS_I(inode); |
48bc06e74
|
460 461 |
befs_data_stream *data = &befs_ino->i_data.ds; befs_off_t len = data->size; |
21fc61c73
|
462 |
char *link = page_address(page); |
1da177e4c
|
463 |
|
11803f97f
|
464 |
if (len == 0 || len > PAGE_SIZE) { |
48bc06e74
|
465 |
befs_error(sb, "Long symlink with illegal length"); |
11803f97f
|
466 |
goto fail; |
1da177e4c
|
467 |
} |
680baacbc
|
468 |
befs_debug(sb, "Follow long symlink"); |
680baacbc
|
469 |
if (befs_read_lsymlink(sb, data, link, len) != len) { |
680baacbc
|
470 |
befs_error(sb, "Failed to read entire long symlink"); |
11803f97f
|
471 |
goto fail; |
680baacbc
|
472 473 |
} link[len - 1] = '\0'; |
11803f97f
|
474 |
SetPageUptodate(page); |
11803f97f
|
475 476 477 478 |
unlock_page(page); return 0; fail: SetPageError(page); |
11803f97f
|
479 480 |
unlock_page(page); return -EIO; |
1da177e4c
|
481 |
} |
1da177e4c
|
482 |
/* |
1ca7087e5
|
483 |
* UTF-8 to NLS charset convert routine |
e60f749b6
|
484 |
* |
1ca7087e5
|
485 |
* Uses uni2char() / char2uni() rather than the nls tables directly |
1da177e4c
|
486 |
*/ |
1da177e4c
|
487 488 489 490 491 492 |
static int befs_utf2nls(struct super_block *sb, const char *in, int in_len, char **out, int *out_len) { struct nls_table *nls = BEFS_SB(sb)->nls; int i, o; |
74675a585
|
493 |
unicode_t uni; |
1da177e4c
|
494 495 |
int unilen, utflen; char *result; |
94f563c42
|
496 497 498 499 500 |
/* The utf8->nls conversion won't make the final nls string bigger * than the utf one, but if the string is pure ascii they'll have the * same width and an extra char is needed to save the additional \0 */ int maxlen = in_len + 1; |
1da177e4c
|
501 |
|
dac52fc18
|
502 |
befs_debug(sb, "---> %s", __func__); |
1da177e4c
|
503 504 |
if (!nls) { |
dac52fc18
|
505 |
befs_error(sb, "%s called with no NLS table loaded", __func__); |
1da177e4c
|
506 507 508 509 |
return -EINVAL; } *out = result = kmalloc(maxlen, GFP_NOFS); |
62b80719d
|
510 |
if (!*out) |
1da177e4c
|
511 |
return -ENOMEM; |
1da177e4c
|
512 513 514 515 |
for (i = o = 0; i < in_len; i += utflen, o += unilen) { /* convert from UTF-8 to Unicode */ |
74675a585
|
516 517 |
utflen = utf8_to_utf32(&in[i], in_len - i, &uni); if (utflen < 0) |
1da177e4c
|
518 |
goto conv_err; |
1da177e4c
|
519 520 |
/* convert from Unicode to nls */ |
74675a585
|
521 522 |
if (uni > MAX_WCHAR_T) goto conv_err; |
1da177e4c
|
523 |
unilen = nls->uni2char(uni, &result[o], in_len - o); |
74675a585
|
524 |
if (unilen < 0) |
1da177e4c
|
525 |
goto conv_err; |
1da177e4c
|
526 527 528 |
} result[o] = '\0'; *out_len = o; |
dac52fc18
|
529 |
befs_debug(sb, "<--- %s", __func__); |
1da177e4c
|
530 531 |
return o; |
62b80719d
|
532 |
conv_err: |
1da177e4c
|
533 534 |
befs_error(sb, "Name using character set %s contains a character that " "cannot be converted to unicode.", nls->charset); |
dac52fc18
|
535 |
befs_debug(sb, "<--- %s", __func__); |
1da177e4c
|
536 537 538 539 540 541 542 |
kfree(result); return -EILSEQ; } /** * befs_nls2utf - Convert NLS string to utf8 encodeing * @sb: Superblock |
817e1d902
|
543 544 545 546 |
* @in: Input string buffer in NLS format * @in_len: Length of input string in bytes * @out: The output string in UTF-8 format * @out_len: Length of the output buffer |
e60f749b6
|
547 |
* |
817e1d902
|
548 |
* Converts input string @in, which is in the format of the loaded NLS map, |
1da177e4c
|
549 |
* into a utf8 string. |
e60f749b6
|
550 |
* |
817e1d902
|
551 |
* The destination string @out is allocated by this function and the caller is |
1da177e4c
|
552 |
* responsible for freeing it with kfree() |
e60f749b6
|
553 |
* |
817e1d902
|
554 |
* On return, *@out_len is the length of @out in bytes. |
1da177e4c
|
555 556 |
* * On success, the return value is the number of utf8 characters written to |
817e1d902
|
557 |
* the output buffer @out. |
e60f749b6
|
558 |
* |
1da177e4c
|
559 560 561 562 563 564 565 566 567 568 569 570 |
* On Failure, a negative number coresponding to the error code is returned. */ static int befs_nls2utf(struct super_block *sb, const char *in, int in_len, char **out, int *out_len) { struct nls_table *nls = BEFS_SB(sb)->nls; int i, o; wchar_t uni; int unilen, utflen; char *result; |
1ca7087e5
|
571 572 573 |
/* * There are nls characters that will translate to 3-chars-wide UTF-8 * characters, an additional byte is needed to save the final \0 |
62b80719d
|
574 575 |
* in special cases */ |
94f563c42
|
576 |
int maxlen = (3 * in_len) + 1; |
1da177e4c
|
577 |
|
dac52fc18
|
578 579 |
befs_debug(sb, "---> %s ", __func__); |
1da177e4c
|
580 581 |
if (!nls) { |
dac52fc18
|
582 583 |
befs_error(sb, "%s called with no NLS table loaded.", __func__); |
1da177e4c
|
584 585 586 587 588 |
return -EINVAL; } *out = result = kmalloc(maxlen, GFP_NOFS); if (!*out) { |
1da177e4c
|
589 590 591 592 593 594 595 596 |
*out_len = 0; return -ENOMEM; } for (i = o = 0; i < in_len; i += unilen, o += utflen) { /* convert from nls to unicode */ unilen = nls->char2uni(&in[i], in_len - i, &uni); |
74675a585
|
597 |
if (unilen < 0) |
1da177e4c
|
598 |
goto conv_err; |
1da177e4c
|
599 600 |
/* convert from unicode to UTF-8 */ |
74675a585
|
601 602 |
utflen = utf32_to_utf8(uni, &result[o], 3); if (utflen <= 0) |
1da177e4c
|
603 |
goto conv_err; |
1da177e4c
|
604 605 606 607 |
} result[o] = '\0'; *out_len = o; |
dac52fc18
|
608 |
befs_debug(sb, "<--- %s", __func__); |
1da177e4c
|
609 610 |
return i; |
62b80719d
|
611 |
conv_err: |
1ca7087e5
|
612 |
befs_error(sb, "Name using character set %s contains a character that " |
1da177e4c
|
613 |
"cannot be converted to unicode.", nls->charset); |
dac52fc18
|
614 |
befs_debug(sb, "<--- %s", __func__); |
1da177e4c
|
615 616 617 |
kfree(result); return -EILSEQ; } |
ac632f5b6
|
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 |
static struct inode *befs_nfs_get_inode(struct super_block *sb, uint64_t ino, uint32_t generation) { /* No need to handle i_generation */ return befs_iget(sb, ino); } /* * Map a NFS file handle to a corresponding dentry */ static struct dentry *befs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len, int fh_type) { return generic_fh_to_dentry(sb, fid, fh_len, fh_type, befs_nfs_get_inode); } /* * Find the parent for a file specified by NFS handle */ static struct dentry *befs_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len, int fh_type) { return generic_fh_to_parent(sb, fid, fh_len, fh_type, befs_nfs_get_inode); } |
dcfd9b215
|
644 645 646 647 648 649 650 651 652 653 654 655 |
static struct dentry *befs_get_parent(struct dentry *child) { struct inode *parent; struct befs_inode_info *befs_ino = BEFS_I(d_inode(child)); parent = befs_iget(child->d_sb, (unsigned long)befs_ino->i_parent.start); if (IS_ERR(parent)) return ERR_CAST(parent); return d_obtain_alias(parent); } |
1da177e4c
|
656 657 658 |
enum { Opt_uid, Opt_gid, Opt_charset, Opt_debug, Opt_err, }; |
a447c0932
|
659 |
static const match_table_t befs_tokens = { |
1da177e4c
|
660 661 662 663 664 665 666 667 |
{Opt_uid, "uid=%d"}, {Opt_gid, "gid=%d"}, {Opt_charset, "iocharset=%s"}, {Opt_debug, "debug"}, {Opt_err, NULL} }; static int |
09ad0eae5
|
668 |
parse_options(char *options, struct befs_mount_options *opts) |
1da177e4c
|
669 670 671 672 |
{ char *p; substring_t args[MAX_OPT_ARGS]; int option; |
31aba059b
|
673 674 |
kuid_t uid; kgid_t gid; |
1da177e4c
|
675 676 |
/* Initialize options */ |
31aba059b
|
677 678 |
opts->uid = GLOBAL_ROOT_UID; opts->gid = GLOBAL_ROOT_GID; |
1da177e4c
|
679 680 681 682 683 684 685 686 687 688 |
opts->use_uid = 0; opts->use_gid = 0; opts->iocharset = NULL; opts->debug = 0; if (!options) return 1; while ((p = strsep(&options, ",")) != NULL) { int token; |
62b80719d
|
689 |
|
1da177e4c
|
690 691 692 693 694 695 696 697 |
if (!*p) continue; token = match_token(p, befs_tokens, args); switch (token) { case Opt_uid: if (match_int(&args[0], &option)) return 0; |
31aba059b
|
698 699 700 701 |
uid = INVALID_UID; if (option >= 0) uid = make_kuid(current_user_ns(), option); if (!uid_valid(uid)) { |
dac52fc18
|
702 703 704 |
pr_err("Invalid uid %d, " "using default ", option); |
1da177e4c
|
705 706 |
break; } |
31aba059b
|
707 |
opts->uid = uid; |
1da177e4c
|
708 709 710 711 712 |
opts->use_uid = 1; break; case Opt_gid: if (match_int(&args[0], &option)) return 0; |
31aba059b
|
713 714 715 716 |
gid = INVALID_GID; if (option >= 0) gid = make_kgid(current_user_ns(), option); if (!gid_valid(gid)) { |
dac52fc18
|
717 718 719 |
pr_err("Invalid gid %d, " "using default ", option); |
1da177e4c
|
720 721 |
break; } |
31aba059b
|
722 |
opts->gid = gid; |
1da177e4c
|
723 724 725 726 727 728 |
opts->use_gid = 1; break; case Opt_charset: kfree(opts->iocharset); opts->iocharset = match_strdup(&args[0]); if (!opts->iocharset) { |
dac52fc18
|
729 730 731 |
pr_err("allocation failure for " "iocharset string "); |
1da177e4c
|
732 733 734 735 736 737 738 |
return 0; } break; case Opt_debug: opts->debug = 1; break; default: |
dac52fc18
|
739 740 741 |
pr_err("Unrecognized mount option \"%s\" " "or missing value ", p); |
1da177e4c
|
742 743 744 745 746 |
return 0; } } return 1; } |
3ab7947ac
|
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 |
static int befs_show_options(struct seq_file *m, struct dentry *root) { struct befs_sb_info *befs_sb = BEFS_SB(root->d_sb); struct befs_mount_options *opts = &befs_sb->mount_opts; if (!uid_eq(opts->uid, GLOBAL_ROOT_UID)) seq_printf(m, ",uid=%u", from_kuid_munged(&init_user_ns, opts->uid)); if (!gid_eq(opts->gid, GLOBAL_ROOT_GID)) seq_printf(m, ",gid=%u", from_kgid_munged(&init_user_ns, opts->gid)); if (opts->iocharset) seq_printf(m, ",charset=%s", opts->iocharset); if (opts->debug) seq_puts(m, ",debug"); return 0; } |
1da177e4c
|
764 |
/* This function has the responsibiltiy of getting the |
e60f749b6
|
765 |
* filesystem ready for unmounting. |
25985edce
|
766 |
* Basically, we free everything that we allocated in |
1da177e4c
|
767 768 769 770 771 |
* befs_read_inode */ static void befs_put_super(struct super_block *sb) { |
f99d49adf
|
772 773 |
kfree(BEFS_SB(sb)->mount_opts.iocharset); BEFS_SB(sb)->mount_opts.iocharset = NULL; |
6d729e44a
|
774 |
unload_nls(BEFS_SB(sb)->nls); |
f99d49adf
|
775 776 |
kfree(sb->s_fs_info); sb->s_fs_info = NULL; |
1da177e4c
|
777 778 779 780 781 782 783 784 785 786 787 788 |
} /* Allocate private field of the superblock, fill it. * * Finish filling the public superblock fields * Make the root directory * Load a set of NLS translations if needed. */ static int befs_fill_super(struct super_block *sb, void *data, int silent) { struct buffer_head *bh; |
038428fcf
|
789 |
struct befs_sb_info *befs_sb; |
1da177e4c
|
790 791 |
befs_super_block *disk_sb; struct inode *root; |
96eb54194
|
792 |
long ret = -EINVAL; |
1da177e4c
|
793 794 |
const unsigned long sb_block = 0; const off_t x86_sb_off = 512; |
173b066f5
|
795 |
int blocksize; |
1da177e4c
|
796 |
|
2d4319ef5
|
797 |
sb->s_fs_info = kzalloc(sizeof(*befs_sb), GFP_KERNEL); |
c625426fb
|
798 |
if (sb->s_fs_info == NULL) |
0418726bb
|
799 |
goto unacquire_none; |
c625426fb
|
800 |
|
1da177e4c
|
801 |
befs_sb = BEFS_SB(sb); |
1da177e4c
|
802 803 |
if (!parse_options((char *) data, &befs_sb->mount_opts)) { |
dceee2e23
|
804 805 |
if (!silent) befs_error(sb, "cannot parse mount options"); |
0418726bb
|
806 |
goto unacquire_priv_sbp; |
1da177e4c
|
807 |
} |
dac52fc18
|
808 |
befs_debug(sb, "---> %s", __func__); |
1da177e4c
|
809 |
|
bc98a42c1
|
810 |
if (!sb_rdonly(sb)) { |
1da177e4c
|
811 812 |
befs_warning(sb, "No write support. Marking filesystem read-only"); |
1751e8a6c
|
813 |
sb->s_flags |= SB_RDONLY; |
1da177e4c
|
814 |
} |
1da177e4c
|
815 816 817 818 819 820 |
/* * Set dummy blocksize to read super block. * Will be set to real fs blocksize later. * * Linux 2.4.10 and later refuse to read blocks smaller than |
a441b0d09
|
821 |
* the logical block size for the device. But we also need to read at |
1da177e4c
|
822 |
* least 1k to get the second 512 bytes of the volume. |
e60f749b6
|
823 |
*/ |
173b066f5
|
824 825 |
blocksize = sb_min_blocksize(sb, 1024); if (!blocksize) { |
a64998504
|
826 827 |
if (!silent) befs_error(sb, "unable to set blocksize"); |
173b066f5
|
828 829 |
goto unacquire_priv_sbp; } |
1da177e4c
|
830 |
|
62b80719d
|
831 832 |
bh = sb_bread(sb, sb_block); if (!bh) { |
dceee2e23
|
833 834 |
if (!silent) befs_error(sb, "unable to read superblock"); |
0418726bb
|
835 |
goto unacquire_priv_sbp; |
1da177e4c
|
836 837 838 839 |
} /* account for offset of super block on x86 */ disk_sb = (befs_super_block *) bh->b_data; |
152b95a1e
|
840 841 |
if ((disk_sb->magic1 == BEFS_SUPER_MAGIC1_LE) || (disk_sb->magic1 == BEFS_SUPER_MAGIC1_BE)) { |
1da177e4c
|
842 843 844 845 846 847 |
befs_debug(sb, "Using PPC superblock location"); } else { befs_debug(sb, "Using x86 superblock location"); disk_sb = (befs_super_block *) ((void *) bh->b_data + x86_sb_off); } |
8e19189ef
|
848 849 |
if ((befs_load_sb(sb, disk_sb) != BEFS_OK) || (befs_check_sb(sb) != BEFS_OK)) |
0418726bb
|
850 |
goto unacquire_bh; |
1da177e4c
|
851 852 853 854 |
befs_dump_super_block(sb, disk_sb); brelse(bh); |
62b80719d
|
855 |
if (befs_sb->num_blocks > ~((sector_t)0)) { |
dceee2e23
|
856 857 858 |
if (!silent) befs_error(sb, "blocks count: %llu is larger than the host can use", befs_sb->num_blocks); |
0418726bb
|
859 |
goto unacquire_priv_sbp; |
1da177e4c
|
860 861 862 863 864 865 866 867 868 |
} /* * set up enough so that it can read an inode * Fill in kernel superblock fields from private sb */ sb->s_magic = BEFS_SUPER_MAGIC; /* Set real blocksize of fs */ sb_set_blocksize(sb, (ulong) befs_sb->block_size); |
b87221de6
|
869 |
sb->s_op = &befs_sops; |
ac632f5b6
|
870 |
sb->s_export_op = &befs_export_operations; |
22b139691
|
871 872 |
sb->s_time_min = 0; sb->s_time_max = 0xffffffffffffll; |
96eb54194
|
873 874 875 876 877 |
root = befs_iget(sb, iaddr2blockno(sb, &(befs_sb->root_dir))); if (IS_ERR(root)) { ret = PTR_ERR(root); goto unacquire_priv_sbp; } |
48fde701a
|
878 |
sb->s_root = d_make_root(root); |
1da177e4c
|
879 |
if (!sb->s_root) { |
dceee2e23
|
880 881 |
if (!silent) befs_error(sb, "get root inode failed"); |
0418726bb
|
882 |
goto unacquire_priv_sbp; |
1da177e4c
|
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 |
} /* load nls library */ if (befs_sb->mount_opts.iocharset) { befs_debug(sb, "Loading nls: %s", befs_sb->mount_opts.iocharset); befs_sb->nls = load_nls(befs_sb->mount_opts.iocharset); if (!befs_sb->nls) { befs_warning(sb, "Cannot load nls %s" " loading default nls", befs_sb->mount_opts.iocharset); befs_sb->nls = load_nls_default(); } /* load default nls if none is specified in mount options */ } else { befs_debug(sb, "Loading default nls"); befs_sb->nls = load_nls_default(); } return 0; |
62b80719d
|
903 904 |
unacquire_bh: |
1da177e4c
|
905 |
brelse(bh); |
62b80719d
|
906 |
unacquire_priv_sbp: |
8dd5ca532
|
907 |
kfree(befs_sb->mount_opts.iocharset); |
1da177e4c
|
908 |
kfree(sb->s_fs_info); |
30982583e
|
909 |
sb->s_fs_info = NULL; |
1da177e4c
|
910 |
|
62b80719d
|
911 |
unacquire_none: |
96eb54194
|
912 |
return ret; |
1da177e4c
|
913 914 915 916 917 |
} static int befs_remount(struct super_block *sb, int *flags, char *data) { |
02b9984d6
|
918 |
sync_filesystem(sb); |
1751e8a6c
|
919 |
if (!(*flags & SB_RDONLY)) |
1da177e4c
|
920 921 922 923 924 |
return -EINVAL; return 0; } static int |
726c33422
|
925 |
befs_statfs(struct dentry *dentry, struct kstatfs *buf) |
1da177e4c
|
926 |
{ |
726c33422
|
927 |
struct super_block *sb = dentry->d_sb; |
8587246a0
|
928 |
u64 id = huge_encode_dev(sb->s_bdev->bd_dev); |
1da177e4c
|
929 |
|
dac52fc18
|
930 |
befs_debug(sb, "---> %s", __func__); |
1da177e4c
|
931 932 933 934 935 936 937 938 |
buf->f_type = BEFS_SUPER_MAGIC; buf->f_bsize = sb->s_blocksize; buf->f_blocks = BEFS_SB(sb)->num_blocks; buf->f_bfree = BEFS_SB(sb)->num_blocks - BEFS_SB(sb)->used_blocks; buf->f_bavail = buf->f_bfree; buf->f_files = 0; /* UNKNOWN */ buf->f_ffree = 0; /* UNKNOWN */ |
6d1349c76
|
939 |
buf->f_fsid = u64_to_fsid(id); |
1da177e4c
|
940 |
buf->f_namelen = BEFS_NAME_LEN; |
dac52fc18
|
941 |
befs_debug(sb, "<--- %s", __func__); |
1da177e4c
|
942 943 944 |
return 0; } |
152a08366
|
945 946 947 |
static struct dentry * befs_mount(struct file_system_type *fs_type, int flags, const char *dev_name, void *data) |
1da177e4c
|
948 |
{ |
152a08366
|
949 |
return mount_bdev(fs_type, flags, dev_name, data, befs_fill_super); |
1da177e4c
|
950 951 952 953 954 |
} static struct file_system_type befs_fs_type = { .owner = THIS_MODULE, .name = "befs", |
152a08366
|
955 |
.mount = befs_mount, |
1da177e4c
|
956 |
.kill_sb = kill_block_super, |
e60f749b6
|
957 |
.fs_flags = FS_REQUIRES_DEV, |
1da177e4c
|
958 |
}; |
7f78e0351
|
959 |
MODULE_ALIAS_FS("befs"); |
1da177e4c
|
960 961 962 963 964 |
static int __init init_befs_fs(void) { int err; |
dac52fc18
|
965 966 |
pr_info("version: %s ", BEFS_VERSION); |
1da177e4c
|
967 968 969 |
err = befs_init_inodecache(); if (err) |
0418726bb
|
970 |
goto unacquire_none; |
1da177e4c
|
971 972 973 |
err = register_filesystem(&befs_fs_type); if (err) |
0418726bb
|
974 |
goto unacquire_inodecache; |
1da177e4c
|
975 976 |
return 0; |
0418726bb
|
977 |
unacquire_inodecache: |
1da177e4c
|
978 |
befs_destroy_inodecache(); |
0418726bb
|
979 |
unacquire_none: |
1da177e4c
|
980 981 982 983 984 985 986 987 988 989 990 991 |
return err; } static void __exit exit_befs_fs(void) { befs_destroy_inodecache(); unregister_filesystem(&befs_fs_type); } /* |
62b80719d
|
992 993 994 995 |
* Macros that typecheck the init and exit functions, * ensures that they are called at init and cleanup, * and eliminates warnings about unused functions. */ |
1da177e4c
|
996 997 |
module_init(init_befs_fs) module_exit(exit_befs_fs) |