blob: 079872d61f7569887316603db70e5a5fdd6e0a1e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/befs/linuxvfs.c
3 *
4 * Copyright (C) 2001 Will Dyson <will_dyson@pobox.com
5 *
6 */
7
8#include <linux/module.h>
9#include <linux/slab.h>
10#include <linux/fs.h>
11#include <linux/errno.h>
12#include <linux/stat.h>
13#include <linux/nls.h>
14#include <linux/buffer_head.h>
15#include <linux/vfs.h>
16#include <linux/parser.h>
17#include <linux/namei.h>
Eric W. Biederman31aba052012-02-10 10:51:24 -080018#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include "befs.h"
21#include "btree.h"
22#include "inode.h"
23#include "datastream.h"
24#include "super.h"
25#include "io.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27MODULE_DESCRIPTION("BeOS File System (BeFS) driver");
28MODULE_AUTHOR("Will Dyson");
29MODULE_LICENSE("GPL");
30
31/* The units the vfs expects inode->i_blocks to be in */
32#define VFS_BLOCK_SIZE 512
33
Al Virof0f49ef2013-05-22 13:44:05 -040034static int befs_readdir(struct file *, struct dir_context *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static int befs_get_block(struct inode *, sector_t, struct buffer_head *, int);
36static int befs_readpage(struct file *file, struct page *page);
37static sector_t befs_bmap(struct address_space *mapping, sector_t block);
Al Viro00cd8dd2012-06-10 17:13:09 -040038static struct dentry *befs_lookup(struct inode *, struct dentry *, unsigned int);
David Howells96eb5412008-02-07 00:15:31 -080039static struct inode *befs_iget(struct super_block *, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static struct inode *befs_alloc_inode(struct super_block *sb);
41static void befs_destroy_inode(struct inode *inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static void befs_destroy_inodecache(void);
Al Viro008b1502005-08-20 00:17:39 +010043static void *befs_follow_link(struct dentry *, struct nameidata *);
Al Viro48bc06e2013-09-16 10:35:31 -040044static void *befs_fast_follow_link(struct dentry *, struct nameidata *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static int befs_utf2nls(struct super_block *sb, const char *in, int in_len,
46 char **out, int *out_len);
47static int befs_nls2utf(struct super_block *sb, const char *in, int in_len,
48 char **out, int *out_len);
49static void befs_put_super(struct super_block *);
50static int befs_remount(struct super_block *, int *, char *);
David Howells726c3342006-06-23 02:02:58 -070051static int befs_statfs(struct dentry *, struct kstatfs *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static int parse_options(char *, befs_mount_options *);
53
54static const struct super_operations befs_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 .alloc_inode = befs_alloc_inode, /* allocate a new inode */
56 .destroy_inode = befs_destroy_inode, /* deallocate an inode */
57 .put_super = befs_put_super, /* uninit super */
58 .statfs = befs_statfs, /* statfs */
59 .remount_fs = befs_remount,
Miklos Szeredi552c3c62008-02-08 04:21:38 -080060 .show_options = generic_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
63/* slab cache for befs_inode_info objects */
Christoph Lametere18b8902006-12-06 20:33:20 -080064static struct kmem_cache *befs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080066static const struct file_operations befs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 .read = generic_read_dir,
Al Virof0f49ef2013-05-22 13:44:05 -040068 .iterate = befs_readdir,
Al Viro59af1582008-08-24 07:24:41 -040069 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
71
Arjan van de Ven754661f2007-02-12 00:55:38 -080072static const struct inode_operations befs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 .lookup = befs_lookup,
74};
75
Christoph Hellwigf5e54d62006-06-28 04:26:44 -070076static const struct address_space_operations befs_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 .readpage = befs_readpage,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 .bmap = befs_bmap,
79};
80
Al Viro48bc06e2013-09-16 10:35:31 -040081static const struct inode_operations befs_fast_symlink_inode_operations = {
82 .readlink = generic_readlink,
83 .follow_link = befs_fast_follow_link,
84};
85
Arjan van de Ven754661f2007-02-12 00:55:38 -080086static const struct inode_operations befs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 .readlink = generic_readlink,
88 .follow_link = befs_follow_link,
Al Viro48bc06e2013-09-16 10:35:31 -040089 .put_link = kfree_put_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090};
91
92/*
93 * Called by generic_file_read() to read a page of data
94 *
95 * In turn, simply calls a generic block read function and
96 * passes it the address of befs_get_block, for mapping file
97 * positions to disk blocks.
98 */
99static int
100befs_readpage(struct file *file, struct page *page)
101{
102 return block_read_full_page(page, befs_get_block);
103}
104
105static sector_t
106befs_bmap(struct address_space *mapping, sector_t block)
107{
108 return generic_block_bmap(mapping, block, befs_get_block);
109}
110
111/*
112 * Generic function to map a file position (block) to a
113 * disk offset (passed back in bh_result).
114 *
115 * Used by many higher level functions.
116 *
117 * Calls befs_fblock2brun() in datastream.c to do the real work.
118 *
119 * -WD 10-26-01
120 */
121
122static int
123befs_get_block(struct inode *inode, sector_t block,
124 struct buffer_head *bh_result, int create)
125{
126 struct super_block *sb = inode->i_sb;
127 befs_data_stream *ds = &BEFS_I(inode)->i_data.ds;
128 befs_block_run run = BAD_IADDR;
129 int res = 0;
130 ulong disk_off;
131
132 befs_debug(sb, "---> befs_get_block() for inode %lu, block %ld",
133 inode->i_ino, block);
134
135 if (block < 0) {
136 befs_error(sb, "befs_get_block() was asked for a block "
137 "number less than zero: block %ld in inode %lu",
138 block, inode->i_ino);
139 return -EIO;
140 }
141
142 if (create) {
143 befs_error(sb, "befs_get_block() was asked to write to "
144 "block %ld in inode %lu", block, inode->i_ino);
145 return -EPERM;
146 }
147
148 res = befs_fblock2brun(sb, ds, block, &run);
149 if (res != BEFS_OK) {
150 befs_error(sb,
151 "<--- befs_get_block() for inode %lu, block "
152 "%ld ERROR", inode->i_ino, block);
153 return -EFBIG;
154 }
155
156 disk_off = (ulong) iaddr2blockno(sb, &run);
157
158 map_bh(bh_result, inode->i_sb, disk_off);
159
160 befs_debug(sb, "<--- befs_get_block() for inode %lu, block %ld, "
161 "disk address %lu", inode->i_ino, block, disk_off);
162
163 return 0;
164}
165
166static struct dentry *
Al Viro00cd8dd2012-06-10 17:13:09 -0400167befs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 struct inode *inode = NULL;
170 struct super_block *sb = dir->i_sb;
171 befs_data_stream *ds = &BEFS_I(dir)->i_data.ds;
172 befs_off_t offset;
173 int ret;
174 int utfnamelen;
175 char *utfname;
176 const char *name = dentry->d_name.name;
177
178 befs_debug(sb, "---> befs_lookup() "
179 "name %s inode %ld", dentry->d_name.name, dir->i_ino);
180
181 /* Convert to UTF-8 */
182 if (BEFS_SB(sb)->nls) {
183 ret =
184 befs_nls2utf(sb, name, strlen(name), &utfname, &utfnamelen);
185 if (ret < 0) {
186 befs_debug(sb, "<--- befs_lookup() ERROR");
187 return ERR_PTR(ret);
188 }
189 ret = befs_btree_find(sb, ds, utfname, &offset);
190 kfree(utfname);
191
192 } else {
193 ret = befs_btree_find(sb, ds, dentry->d_name.name, &offset);
194 }
195
196 if (ret == BEFS_BT_NOT_FOUND) {
197 befs_debug(sb, "<--- befs_lookup() %s not found",
198 dentry->d_name.name);
199 return ERR_PTR(-ENOENT);
200
201 } else if (ret != BEFS_OK || offset == 0) {
202 befs_warning(sb, "<--- befs_lookup() Error");
203 return ERR_PTR(-ENODATA);
204 }
205
David Howells96eb5412008-02-07 00:15:31 -0800206 inode = befs_iget(dir->i_sb, (ino_t) offset);
207 if (IS_ERR(inode))
208 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 d_add(dentry, inode);
211
212 befs_debug(sb, "<--- befs_lookup()");
213
214 return NULL;
215}
216
217static int
Al Virof0f49ef2013-05-22 13:44:05 -0400218befs_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Al Virof0f49ef2013-05-22 13:44:05 -0400220 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 struct super_block *sb = inode->i_sb;
222 befs_data_stream *ds = &BEFS_I(inode)->i_data.ds;
223 befs_off_t value;
224 int result;
225 size_t keysize;
226 unsigned char d_type;
227 char keybuf[BEFS_NAME_LEN + 1];
Al Virof0f49ef2013-05-22 13:44:05 -0400228 const char *dirname = file->f_path.dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 befs_debug(sb, "---> befs_readdir() "
Al Virof0f49ef2013-05-22 13:44:05 -0400231 "name %s, inode %ld, ctx->pos %Ld",
232 dirname, inode->i_ino, ctx->pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Al Virof0f49ef2013-05-22 13:44:05 -0400234more:
235 result = befs_btree_read(sb, ds, ctx->pos, BEFS_NAME_LEN + 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 keybuf, &keysize, &value);
237
238 if (result == BEFS_ERR) {
239 befs_debug(sb, "<--- befs_readdir() ERROR");
240 befs_error(sb, "IO error reading %s (inode %lu)",
241 dirname, inode->i_ino);
242 return -EIO;
243
244 } else if (result == BEFS_BT_END) {
245 befs_debug(sb, "<--- befs_readdir() END");
246 return 0;
247
248 } else if (result == BEFS_BT_EMPTY) {
249 befs_debug(sb, "<--- befs_readdir() Empty directory");
250 return 0;
251 }
252
253 d_type = DT_UNKNOWN;
254
255 /* Convert to NLS */
256 if (BEFS_SB(sb)->nls) {
Al Virof0f49ef2013-05-22 13:44:05 -0400257 char *nlsname;
258 int nlsnamelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 result =
260 befs_utf2nls(sb, keybuf, keysize, &nlsname, &nlsnamelen);
261 if (result < 0) {
262 befs_debug(sb, "<--- befs_readdir() ERROR");
263 return result;
264 }
Al Virof0f49ef2013-05-22 13:44:05 -0400265 if (!dir_emit(ctx, nlsname, nlsnamelen,
266 (ino_t) value, d_type)) {
267 kfree(nlsname);
268 return 0;
269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 kfree(nlsname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 } else {
Al Virof0f49ef2013-05-22 13:44:05 -0400272 if (!dir_emit(ctx, keybuf, keysize,
273 (ino_t) value, d_type))
274 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
Al Virof0f49ef2013-05-22 13:44:05 -0400276 ctx->pos++;
277 goto more;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Al Virof0f49ef2013-05-22 13:44:05 -0400279 befs_debug(sb, "<--- befs_readdir() pos %Ld", ctx->pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 return 0;
282}
283
284static struct inode *
285befs_alloc_inode(struct super_block *sb)
286{
287 struct befs_inode_info *bi;
288 bi = (struct befs_inode_info *)kmem_cache_alloc(befs_inode_cachep,
Christoph Lametere94b1762006-12-06 20:33:17 -0800289 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (!bi)
291 return NULL;
292 return &bi->vfs_inode;
293}
294
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100295static void befs_i_callback(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100297 struct inode *inode = container_of(head, struct inode, i_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 kmem_cache_free(befs_inode_cachep, BEFS_I(inode));
299}
300
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100301static void befs_destroy_inode(struct inode *inode)
302{
303 call_rcu(&inode->i_rcu, befs_i_callback);
304}
305
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700306static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 struct befs_inode_info *bi = (struct befs_inode_info *) foo;
Christoph Lametera35afb82007-05-16 22:10:57 -0700309
310 inode_init_once(&bi->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
David Howells96eb5412008-02-07 00:15:31 -0800313static struct inode *befs_iget(struct super_block *sb, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 struct buffer_head *bh = NULL;
316 befs_inode *raw_inode = NULL;
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 befs_sb_info *befs_sb = BEFS_SB(sb);
319 befs_inode_info *befs_ino = NULL;
David Howells96eb5412008-02-07 00:15:31 -0800320 struct inode *inode;
321 long ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
David Howells96eb5412008-02-07 00:15:31 -0800323 befs_debug(sb, "---> befs_read_inode() " "inode = %lu", ino);
324
325 inode = iget_locked(sb, ino);
Rakesh Panditac34a1b2014-01-15 19:58:28 +0200326 if (!inode)
327 return ERR_PTR(-ENOMEM);
David Howells96eb5412008-02-07 00:15:31 -0800328 if (!(inode->i_state & I_NEW))
329 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 befs_ino = BEFS_I(inode);
332
333 /* convert from vfs's inode number to befs's inode number */
334 befs_ino->i_inode_num = blockno2iaddr(sb, inode->i_ino);
335
336 befs_debug(sb, " real inode number [%u, %hu, %hu]",
337 befs_ino->i_inode_num.allocation_group,
338 befs_ino->i_inode_num.start, befs_ino->i_inode_num.len);
339
340 bh = befs_bread(sb, inode->i_ino);
341 if (!bh) {
342 befs_error(sb, "unable to read inode block - "
343 "inode = %lu", inode->i_ino);
Adrian Bunk04187262006-06-30 18:23:04 +0200344 goto unacquire_none;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346
347 raw_inode = (befs_inode *) bh->b_data;
348
349 befs_dump_inode(sb, raw_inode);
350
351 if (befs_check_inode(sb, raw_inode, inode->i_ino) != BEFS_OK) {
352 befs_error(sb, "Bad inode: %lu", inode->i_ino);
Adrian Bunk04187262006-06-30 18:23:04 +0200353 goto unacquire_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355
356 inode->i_mode = (umode_t) fs32_to_cpu(sb, raw_inode->mode);
357
358 /*
359 * set uid and gid. But since current BeOS is single user OS, so
360 * you can change by "uid" or "gid" options.
361 */
362
363 inode->i_uid = befs_sb->mount_opts.use_uid ?
Eric W. Biederman31aba052012-02-10 10:51:24 -0800364 befs_sb->mount_opts.uid :
365 make_kuid(&init_user_ns, fs32_to_cpu(sb, raw_inode->uid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 inode->i_gid = befs_sb->mount_opts.use_gid ?
Eric W. Biederman31aba052012-02-10 10:51:24 -0800367 befs_sb->mount_opts.gid :
368 make_kgid(&init_user_ns, fs32_to_cpu(sb, raw_inode->gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Miklos Szeredibfe86842011-10-28 14:13:29 +0200370 set_nlink(inode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 /*
373 * BEFS's time is 64 bits, but current VFS is 32 bits...
374 * BEFS don't have access time. Nor inode change time. VFS
375 * doesn't have creation time.
376 * Also, the lower 16 bits of the last_modified_time and
377 * create_time are just a counter to help ensure uniqueness
378 * for indexing purposes. (PFD, page 54)
379 */
380
381 inode->i_mtime.tv_sec =
382 fs64_to_cpu(sb, raw_inode->last_modified_time) >> 16;
383 inode->i_mtime.tv_nsec = 0; /* lower 16 bits are not a time */
384 inode->i_ctime = inode->i_mtime;
385 inode->i_atime = inode->i_mtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 befs_ino->i_inode_num = fsrun_to_cpu(sb, raw_inode->inode_num);
388 befs_ino->i_parent = fsrun_to_cpu(sb, raw_inode->parent);
389 befs_ino->i_attribute = fsrun_to_cpu(sb, raw_inode->attributes);
390 befs_ino->i_flags = fs32_to_cpu(sb, raw_inode->flags);
391
392 if (S_ISLNK(inode->i_mode) && !(befs_ino->i_flags & BEFS_LONG_SYMLINK)){
393 inode->i_size = 0;
394 inode->i_blocks = befs_sb->block_size / VFS_BLOCK_SIZE;
395 strncpy(befs_ino->i_data.symlink, raw_inode->data.symlink,
Duane Griffin7df5fa02008-12-19 20:47:18 +0000396 BEFS_SYMLINK_LEN - 1);
397 befs_ino->i_data.symlink[BEFS_SYMLINK_LEN - 1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 } else {
399 int num_blks;
400
401 befs_ino->i_data.ds =
Jesper Juhle0e3d322011-01-12 17:00:26 -0800402 fsds_to_cpu(sb, &raw_inode->data.datastream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 num_blks = befs_count_blocks(sb, &befs_ino->i_data.ds);
405 inode->i_blocks =
406 num_blks * (befs_sb->block_size / VFS_BLOCK_SIZE);
407 inode->i_size = befs_ino->i_data.ds.size;
408 }
409
410 inode->i_mapping->a_ops = &befs_aops;
411
412 if (S_ISREG(inode->i_mode)) {
Christoph Hellwig2cf06912005-11-07 00:59:44 -0800413 inode->i_fop = &generic_ro_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 } else if (S_ISDIR(inode->i_mode)) {
415 inode->i_op = &befs_dir_inode_operations;
416 inode->i_fop = &befs_dir_operations;
417 } else if (S_ISLNK(inode->i_mode)) {
Al Viro48bc06e2013-09-16 10:35:31 -0400418 if (befs_ino->i_flags & BEFS_LONG_SYMLINK)
419 inode->i_op = &befs_symlink_inode_operations;
420 else
421 inode->i_op = &befs_fast_symlink_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 } else {
423 befs_error(sb, "Inode %lu is not a regular file, "
424 "directory or symlink. THAT IS WRONG! BeFS has no "
425 "on disk special files", inode->i_ino);
Adrian Bunk04187262006-06-30 18:23:04 +0200426 goto unacquire_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428
429 brelse(bh);
430 befs_debug(sb, "<--- befs_read_inode()");
David Howells96eb5412008-02-07 00:15:31 -0800431 unlock_new_inode(inode);
432 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Adrian Bunk04187262006-06-30 18:23:04 +0200434 unacquire_bh:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 brelse(bh);
436
Adrian Bunk04187262006-06-30 18:23:04 +0200437 unacquire_none:
David Howells96eb5412008-02-07 00:15:31 -0800438 iget_failed(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 befs_debug(sb, "<--- befs_read_inode() - Bad inode");
David Howells96eb5412008-02-07 00:15:31 -0800440 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
443/* Initialize the inode cache. Called at fs setup.
Paul Mundt20c2df82007-07-20 10:11:58 +0900444 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 * Taken from NFS implementation by Al Viro.
446 */
Fabian Frederick91a52ab2014-04-03 14:50:22 -0700447static int __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448befs_init_inodecache(void)
449{
450 befs_inode_cachep = kmem_cache_create("befs_inode_cache",
451 sizeof (struct befs_inode_info),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800452 0, (SLAB_RECLAIM_ACCOUNT|
453 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900454 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (befs_inode_cachep == NULL) {
456 printk(KERN_ERR "befs_init_inodecache: "
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200457 "Couldn't initialize inode slabcache\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return -ENOMEM;
459 }
460
461 return 0;
462}
463
464/* Called at fs teardown.
465 *
466 * Taken from NFS implementation by Al Viro.
467 */
468static void
469befs_destroy_inodecache(void)
470{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000471 /*
472 * Make sure all delayed rcu free inodes are flushed before we
473 * destroy cache.
474 */
475 rcu_barrier();
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700476 kmem_cache_destroy(befs_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
479/*
480 * The inode of symbolic link is different to data stream.
481 * The data stream become link name. Unless the LONG_SYMLINK
482 * flag is set.
483 */
Linus Torvaldsdb873892005-08-20 13:20:01 -0700484static void *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485befs_follow_link(struct dentry *dentry, struct nameidata *nd)
486{
Al Viro48bc06e2013-09-16 10:35:31 -0400487 struct super_block *sb = dentry->d_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 befs_inode_info *befs_ino = BEFS_I(dentry->d_inode);
Al Viro48bc06e2013-09-16 10:35:31 -0400489 befs_data_stream *data = &befs_ino->i_data.ds;
490 befs_off_t len = data->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 char *link;
492
Al Viro48bc06e2013-09-16 10:35:31 -0400493 if (len == 0) {
494 befs_error(sb, "Long symlink with illegal length");
495 link = ERR_PTR(-EIO);
496 } else {
497 befs_debug(sb, "Follow long symlink");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Al Viro48bc06e2013-09-16 10:35:31 -0400499 link = kmalloc(len, GFP_NOFS);
500 if (!link) {
501 link = ERR_PTR(-ENOMEM);
502 } else if (befs_read_lsymlink(sb, data, link, len) != len) {
503 kfree(link);
504 befs_error(sb, "Failed to read entire long symlink");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 link = ERR_PTR(-EIO);
Duane Griffin7df5fa02008-12-19 20:47:18 +0000506 } else {
Al Viro48bc06e2013-09-16 10:35:31 -0400507 link[len - 1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 nd_set_link(nd, link);
Al Viro008b1502005-08-20 00:17:39 +0100511 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Al Viro48bc06e2013-09-16 10:35:31 -0400514
515static void *
516befs_fast_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 befs_inode_info *befs_ino = BEFS_I(dentry->d_inode);
Al Viro48bc06e2013-09-16 10:35:31 -0400519 nd_set_link(nd, befs_ino->i_data.symlink);
520 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
523/*
524 * UTF-8 to NLS charset convert routine
525 *
526 *
527 * Changed 8/10/01 by Will Dyson. Now use uni2char() / char2uni() rather than
528 * the nls tables directly
529 */
530
531static int
532befs_utf2nls(struct super_block *sb, const char *in,
533 int in_len, char **out, int *out_len)
534{
535 struct nls_table *nls = BEFS_SB(sb)->nls;
536 int i, o;
Alan Stern74675a52009-04-30 10:08:18 -0400537 unicode_t uni;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 int unilen, utflen;
539 char *result;
Diego Calleja94f563c2006-08-05 12:14:55 -0700540 /* The utf8->nls conversion won't make the final nls string bigger
541 * than the utf one, but if the string is pure ascii they'll have the
542 * same width and an extra char is needed to save the additional \0
543 */
544 int maxlen = in_len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 befs_debug(sb, "---> utf2nls()");
547
548 if (!nls) {
549 befs_error(sb, "befs_utf2nls called with no NLS table loaded");
550 return -EINVAL;
551 }
552
553 *out = result = kmalloc(maxlen, GFP_NOFS);
554 if (!*out) {
555 befs_error(sb, "befs_utf2nls() cannot allocate memory");
556 *out_len = 0;
557 return -ENOMEM;
558 }
559
560 for (i = o = 0; i < in_len; i += utflen, o += unilen) {
561
562 /* convert from UTF-8 to Unicode */
Alan Stern74675a52009-04-30 10:08:18 -0400563 utflen = utf8_to_utf32(&in[i], in_len - i, &uni);
564 if (utflen < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 goto conv_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 /* convert from Unicode to nls */
Alan Stern74675a52009-04-30 10:08:18 -0400568 if (uni > MAX_WCHAR_T)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 goto conv_err;
Alan Stern74675a52009-04-30 10:08:18 -0400570 unilen = nls->uni2char(uni, &result[o], in_len - o);
571 if (unilen < 0)
572 goto conv_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574 result[o] = '\0';
575 *out_len = o;
576
577 befs_debug(sb, "<--- utf2nls()");
578
579 return o;
580
581 conv_err:
582 befs_error(sb, "Name using character set %s contains a character that "
583 "cannot be converted to unicode.", nls->charset);
584 befs_debug(sb, "<--- utf2nls()");
585 kfree(result);
586 return -EILSEQ;
587}
588
589/**
590 * befs_nls2utf - Convert NLS string to utf8 encodeing
591 * @sb: Superblock
592 * @src: Input string buffer in NLS format
593 * @srclen: Length of input string in bytes
Alexey Dobriyan4de151d2006-03-22 00:13:35 +0100594 * @dest: The output string in UTF-8 format
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 * @destlen: Length of the output buffer
596 *
597 * Converts input string @src, which is in the format of the loaded NLS map,
598 * into a utf8 string.
599 *
600 * The destination string @dest is allocated by this function and the caller is
601 * responsible for freeing it with kfree()
602 *
603 * On return, *@destlen is the length of @dest in bytes.
604 *
605 * On success, the return value is the number of utf8 characters written to
606 * the output buffer @dest.
607 *
608 * On Failure, a negative number coresponding to the error code is returned.
609 */
610
611static int
612befs_nls2utf(struct super_block *sb, const char *in,
613 int in_len, char **out, int *out_len)
614{
615 struct nls_table *nls = BEFS_SB(sb)->nls;
616 int i, o;
617 wchar_t uni;
618 int unilen, utflen;
619 char *result;
Diego Calleja94f563c2006-08-05 12:14:55 -0700620 /* There're nls characters that will translate to 3-chars-wide UTF-8
621 * characters, a additional byte is needed to save the final \0
622 * in special cases */
623 int maxlen = (3 * in_len) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 befs_debug(sb, "---> nls2utf()\n");
626
627 if (!nls) {
628 befs_error(sb, "befs_nls2utf called with no NLS table loaded.");
629 return -EINVAL;
630 }
631
632 *out = result = kmalloc(maxlen, GFP_NOFS);
633 if (!*out) {
634 befs_error(sb, "befs_nls2utf() cannot allocate memory");
635 *out_len = 0;
636 return -ENOMEM;
637 }
638
639 for (i = o = 0; i < in_len; i += unilen, o += utflen) {
640
641 /* convert from nls to unicode */
642 unilen = nls->char2uni(&in[i], in_len - i, &uni);
Alan Stern74675a52009-04-30 10:08:18 -0400643 if (unilen < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 goto conv_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 /* convert from unicode to UTF-8 */
Alan Stern74675a52009-04-30 10:08:18 -0400647 utflen = utf32_to_utf8(uni, &result[o], 3);
648 if (utflen <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 goto conv_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651
652 result[o] = '\0';
653 *out_len = o;
654
655 befs_debug(sb, "<--- nls2utf()");
656
657 return i;
658
659 conv_err:
660 befs_error(sb, "Name using charecter set %s contains a charecter that "
661 "cannot be converted to unicode.", nls->charset);
662 befs_debug(sb, "<--- nls2utf()");
663 kfree(result);
664 return -EILSEQ;
665}
666
667/**
668 * Use the
669 *
670 */
671enum {
672 Opt_uid, Opt_gid, Opt_charset, Opt_debug, Opt_err,
673};
674
Steven Whitehousea447c092008-10-13 10:46:57 +0100675static const match_table_t befs_tokens = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 {Opt_uid, "uid=%d"},
677 {Opt_gid, "gid=%d"},
678 {Opt_charset, "iocharset=%s"},
679 {Opt_debug, "debug"},
680 {Opt_err, NULL}
681};
682
683static int
684parse_options(char *options, befs_mount_options * opts)
685{
686 char *p;
687 substring_t args[MAX_OPT_ARGS];
688 int option;
Eric W. Biederman31aba052012-02-10 10:51:24 -0800689 kuid_t uid;
690 kgid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 /* Initialize options */
Eric W. Biederman31aba052012-02-10 10:51:24 -0800693 opts->uid = GLOBAL_ROOT_UID;
694 opts->gid = GLOBAL_ROOT_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 opts->use_uid = 0;
696 opts->use_gid = 0;
697 opts->iocharset = NULL;
698 opts->debug = 0;
699
700 if (!options)
701 return 1;
702
703 while ((p = strsep(&options, ",")) != NULL) {
704 int token;
705 if (!*p)
706 continue;
707
708 token = match_token(p, befs_tokens, args);
709 switch (token) {
710 case Opt_uid:
711 if (match_int(&args[0], &option))
712 return 0;
Eric W. Biederman31aba052012-02-10 10:51:24 -0800713 uid = INVALID_UID;
714 if (option >= 0)
715 uid = make_kuid(current_user_ns(), option);
716 if (!uid_valid(uid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 printk(KERN_ERR "BeFS: Invalid uid %d, "
718 "using default\n", option);
719 break;
720 }
Eric W. Biederman31aba052012-02-10 10:51:24 -0800721 opts->uid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 opts->use_uid = 1;
723 break;
724 case Opt_gid:
725 if (match_int(&args[0], &option))
726 return 0;
Eric W. Biederman31aba052012-02-10 10:51:24 -0800727 gid = INVALID_GID;
728 if (option >= 0)
729 gid = make_kgid(current_user_ns(), option);
730 if (!gid_valid(gid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 printk(KERN_ERR "BeFS: Invalid gid %d, "
732 "using default\n", option);
733 break;
734 }
Eric W. Biederman31aba052012-02-10 10:51:24 -0800735 opts->gid = gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 opts->use_gid = 1;
737 break;
738 case Opt_charset:
739 kfree(opts->iocharset);
740 opts->iocharset = match_strdup(&args[0]);
741 if (!opts->iocharset) {
742 printk(KERN_ERR "BeFS: allocation failure for "
743 "iocharset string\n");
744 return 0;
745 }
746 break;
747 case Opt_debug:
748 opts->debug = 1;
749 break;
750 default:
751 printk(KERN_ERR "BeFS: Unrecognized mount option \"%s\" "
752 "or missing value\n", p);
753 return 0;
754 }
755 }
756 return 1;
757}
758
759/* This function has the responsibiltiy of getting the
760 * filesystem ready for unmounting.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300761 * Basically, we free everything that we allocated in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 * befs_read_inode
763 */
764static void
765befs_put_super(struct super_block *sb)
766{
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800767 kfree(BEFS_SB(sb)->mount_opts.iocharset);
768 BEFS_SB(sb)->mount_opts.iocharset = NULL;
Thomas Gleixner6d729e42009-08-16 21:05:08 +0000769 unload_nls(BEFS_SB(sb)->nls);
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800770 kfree(sb->s_fs_info);
771 sb->s_fs_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774/* Allocate private field of the superblock, fill it.
775 *
776 * Finish filling the public superblock fields
777 * Make the root directory
778 * Load a set of NLS translations if needed.
779 */
780static int
781befs_fill_super(struct super_block *sb, void *data, int silent)
782{
783 struct buffer_head *bh;
784 befs_sb_info *befs_sb;
785 befs_super_block *disk_sb;
786 struct inode *root;
David Howells96eb5412008-02-07 00:15:31 -0800787 long ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 const unsigned long sb_block = 0;
789 const off_t x86_sb_off = 512;
790
Miklos Szeredi552c3c62008-02-08 04:21:38 -0800791 save_mount_options(sb, data);
792
Fabian Frederick2d4319e2014-04-03 14:50:21 -0700793 sb->s_fs_info = kzalloc(sizeof(*befs_sb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (sb->s_fs_info == NULL) {
795 printk(KERN_ERR
796 "BeFS(%s): Unable to allocate memory for private "
797 "portion of superblock. Bailing.\n", sb->s_id);
Adrian Bunk04187262006-06-30 18:23:04 +0200798 goto unacquire_none;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800 befs_sb = BEFS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 if (!parse_options((char *) data, &befs_sb->mount_opts)) {
803 befs_error(sb, "cannot parse mount options");
Adrian Bunk04187262006-06-30 18:23:04 +0200804 goto unacquire_priv_sbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806
807 befs_debug(sb, "---> befs_fill_super()");
808
809#ifndef CONFIG_BEFS_RW
810 if (!(sb->s_flags & MS_RDONLY)) {
811 befs_warning(sb,
812 "No write support. Marking filesystem read-only");
813 sb->s_flags |= MS_RDONLY;
814 }
815#endif /* CONFIG_BEFS_RW */
816
817 /*
818 * Set dummy blocksize to read super block.
819 * Will be set to real fs blocksize later.
820 *
821 * Linux 2.4.10 and later refuse to read blocks smaller than
822 * the hardsect size for the device. But we also need to read at
823 * least 1k to get the second 512 bytes of the volume.
824 * -WD 10-26-01
825 */
826 sb_min_blocksize(sb, 1024);
827
828 if (!(bh = sb_bread(sb, sb_block))) {
829 befs_error(sb, "unable to read superblock");
Adrian Bunk04187262006-06-30 18:23:04 +0200830 goto unacquire_priv_sbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832
833 /* account for offset of super block on x86 */
834 disk_sb = (befs_super_block *) bh->b_data;
Harvey Harrison152b95a2008-10-15 22:04:03 -0700835 if ((disk_sb->magic1 == BEFS_SUPER_MAGIC1_LE) ||
836 (disk_sb->magic1 == BEFS_SUPER_MAGIC1_BE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 befs_debug(sb, "Using PPC superblock location");
838 } else {
839 befs_debug(sb, "Using x86 superblock location");
840 disk_sb =
841 (befs_super_block *) ((void *) bh->b_data + x86_sb_off);
842 }
843
844 if (befs_load_sb(sb, disk_sb) != BEFS_OK)
Adrian Bunk04187262006-06-30 18:23:04 +0200845 goto unacquire_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 befs_dump_super_block(sb, disk_sb);
848
849 brelse(bh);
850
851 if (befs_check_sb(sb) != BEFS_OK)
Adrian Bunk04187262006-06-30 18:23:04 +0200852 goto unacquire_priv_sbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 if( befs_sb->num_blocks > ~((sector_t)0) ) {
855 befs_error(sb, "blocks count: %Lu "
856 "is larger than the host can use",
857 befs_sb->num_blocks);
Adrian Bunk04187262006-06-30 18:23:04 +0200858 goto unacquire_priv_sbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860
861 /*
862 * set up enough so that it can read an inode
863 * Fill in kernel superblock fields from private sb
864 */
865 sb->s_magic = BEFS_SUPER_MAGIC;
866 /* Set real blocksize of fs */
867 sb_set_blocksize(sb, (ulong) befs_sb->block_size);
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700868 sb->s_op = &befs_sops;
David Howells96eb5412008-02-07 00:15:31 -0800869 root = befs_iget(sb, iaddr2blockno(sb, &(befs_sb->root_dir)));
870 if (IS_ERR(root)) {
871 ret = PTR_ERR(root);
872 goto unacquire_priv_sbp;
873 }
Al Viro48fde702012-01-08 22:15:13 -0500874 sb->s_root = d_make_root(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (!sb->s_root) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 befs_error(sb, "get root inode failed");
Adrian Bunk04187262006-06-30 18:23:04 +0200877 goto unacquire_priv_sbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879
880 /* load nls library */
881 if (befs_sb->mount_opts.iocharset) {
882 befs_debug(sb, "Loading nls: %s",
883 befs_sb->mount_opts.iocharset);
884 befs_sb->nls = load_nls(befs_sb->mount_opts.iocharset);
885 if (!befs_sb->nls) {
886 befs_warning(sb, "Cannot load nls %s"
887 " loading default nls",
888 befs_sb->mount_opts.iocharset);
889 befs_sb->nls = load_nls_default();
890 }
891 /* load default nls if none is specified in mount options */
892 } else {
893 befs_debug(sb, "Loading default nls");
894 befs_sb->nls = load_nls_default();
895 }
896
897 return 0;
898/*****************/
Adrian Bunk04187262006-06-30 18:23:04 +0200899 unacquire_bh:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 brelse(bh);
901
Adrian Bunk04187262006-06-30 18:23:04 +0200902 unacquire_priv_sbp:
Al Viro8dd5ca52010-01-28 22:11:38 -0500903 kfree(befs_sb->mount_opts.iocharset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 kfree(sb->s_fs_info);
905
Adrian Bunk04187262006-06-30 18:23:04 +0200906 unacquire_none:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 sb->s_fs_info = NULL;
David Howells96eb5412008-02-07 00:15:31 -0800908 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
911static int
912befs_remount(struct super_block *sb, int *flags, char *data)
913{
914 if (!(*flags & MS_RDONLY))
915 return -EINVAL;
916 return 0;
917}
918
919static int
David Howells726c3342006-06-23 02:02:58 -0700920befs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
David Howells726c3342006-06-23 02:02:58 -0700922 struct super_block *sb = dentry->d_sb;
Coly Li85872462009-04-02 16:59:32 -0700923 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 befs_debug(sb, "---> befs_statfs()");
926
927 buf->f_type = BEFS_SUPER_MAGIC;
928 buf->f_bsize = sb->s_blocksize;
929 buf->f_blocks = BEFS_SB(sb)->num_blocks;
930 buf->f_bfree = BEFS_SB(sb)->num_blocks - BEFS_SB(sb)->used_blocks;
931 buf->f_bavail = buf->f_bfree;
932 buf->f_files = 0; /* UNKNOWN */
933 buf->f_ffree = 0; /* UNKNOWN */
Coly Li85872462009-04-02 16:59:32 -0700934 buf->f_fsid.val[0] = (u32)id;
935 buf->f_fsid.val[1] = (u32)(id >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 buf->f_namelen = BEFS_NAME_LEN;
937
938 befs_debug(sb, "<--- befs_statfs()");
939
940 return 0;
941}
942
Al Viro152a0832010-07-25 00:46:55 +0400943static struct dentry *
944befs_mount(struct file_system_type *fs_type, int flags, const char *dev_name,
945 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Al Viro152a0832010-07-25 00:46:55 +0400947 return mount_bdev(fs_type, flags, dev_name, data, befs_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
950static struct file_system_type befs_fs_type = {
951 .owner = THIS_MODULE,
952 .name = "befs",
Al Viro152a0832010-07-25 00:46:55 +0400953 .mount = befs_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 .kill_sb = kill_block_super,
955 .fs_flags = FS_REQUIRES_DEV,
956};
Eric W. Biederman7f78e032013-03-02 19:39:14 -0800957MODULE_ALIAS_FS("befs");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959static int __init
960init_befs_fs(void)
961{
962 int err;
963
964 printk(KERN_INFO "BeFS version: %s\n", BEFS_VERSION);
965
966 err = befs_init_inodecache();
967 if (err)
Adrian Bunk04187262006-06-30 18:23:04 +0200968 goto unacquire_none;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 err = register_filesystem(&befs_fs_type);
971 if (err)
Adrian Bunk04187262006-06-30 18:23:04 +0200972 goto unacquire_inodecache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 return 0;
975
Adrian Bunk04187262006-06-30 18:23:04 +0200976unacquire_inodecache:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 befs_destroy_inodecache();
978
Adrian Bunk04187262006-06-30 18:23:04 +0200979unacquire_none:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return err;
981}
982
983static void __exit
984exit_befs_fs(void)
985{
986 befs_destroy_inodecache();
987
988 unregister_filesystem(&befs_fs_type);
989}
990
991/*
992Macros that typecheck the init and exit functions,
993ensures that they are called at init and cleanup,
994and eliminates warnings about unused functions.
995*/
996module_init(init_befs_fs)
997module_exit(exit_befs_fs)