original development tree for Linux kernel GTP module; now long in mainline.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

994 lines
25 KiB

befs: annotate fs32 on tests for superblock endianness Does compile-time byteswapping rather than runtime. Noticed by sparse: fs/befs/super.c:29:6: warning: cast to restricted __le32 fs/befs/super.c:29:6: warning: cast from restricted fs32 fs/befs/super.c:31:11: warning: cast to restricted __be32 fs/befs/super.c:31:11: warning: cast from restricted fs32 fs/befs/super.c:31:11: warning: cast to restricted __be32 fs/befs/super.c:31:11: warning: cast from restricted fs32 fs/befs/super.c:31:11: warning: cast to restricted __be32 fs/befs/super.c:31:11: warning: cast from restricted fs32 fs/befs/super.c:31:11: warning: cast to restricted __be32 fs/befs/super.c:31:11: warning: cast from restricted fs32 fs/befs/super.c:31:11: warning: cast to restricted __be32 fs/befs/super.c:31:11: warning: cast from restricted fs32 fs/befs/super.c:31:11: warning: cast to restricted __be32 fs/befs/super.c:31:11: warning: cast from restricted fs32 fs/befs/linuxvfs.c:811:7: warning: cast to restricted __le32 fs/befs/linuxvfs.c:811:7: warning: cast from restricted fs32 fs/befs/linuxvfs.c:812:7: warning: cast to restricted __be32 fs/befs/linuxvfs.c:812:7: warning: cast from restricted fs32 fs/befs/linuxvfs.c:812:7: warning: cast to restricted __be32 fs/befs/linuxvfs.c:812:7: warning: cast from restricted fs32 fs/befs/linuxvfs.c:812:7: warning: cast to restricted __be32 fs/befs/linuxvfs.c:812:7: warning: cast from restricted fs32 fs/befs/linuxvfs.c:812:7: warning: cast to restricted __be32 fs/befs/linuxvfs.c:812:7: warning: cast from restricted fs32 fs/befs/linuxvfs.c:812:7: warning: cast to restricted __be32 fs/befs/linuxvfs.c:812:7: warning: cast from restricted fs32 fs/befs/linuxvfs.c:812:7: warning: cast to restricted __be32 fs/befs/linuxvfs.c:812:7: warning: cast from restricted fs32 Signed-off-by: Harvey Harrison <harvey.harrison@gmail.com> Cc: "Sergey S. Kostyliov" <rathamahata@php4.ru> Cc: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
13 years ago
fs: Limit sys_mount to only request filesystem modules. Modify the request_module to prefix the file system type with "fs-" and add aliases to all of the filesystems that can be built as modules to match. A common practice is to build all of the kernel code and leave code that is not commonly needed as modules, with the result that many users are exposed to any bug anywhere in the kernel. Looking for filesystems with a fs- prefix limits the pool of possible modules that can be loaded by mount to just filesystems trivially making things safer with no real cost. Using aliases means user space can control the policy of which filesystem modules are auto-loaded by editing /etc/modprobe.d/*.conf with blacklist and alias directives. Allowing simple, safe, well understood work-arounds to known problematic software. This also addresses a rare but unfortunate problem where the filesystem name is not the same as it's module name and module auto-loading would not work. While writing this patch I saw a handful of such cases. The most significant being autofs that lives in the module autofs4. This is relevant to user namespaces because we can reach the request module in get_fs_type() without having any special permissions, and people get uncomfortable when a user specified string (in this case the filesystem type) goes all of the way to request_module. After having looked at this issue I don't think there is any particular reason to perform any filtering or permission checks beyond making it clear in the module request that we want a filesystem module. The common pattern in the kernel is to call request_module() without regards to the users permissions. In general all a filesystem module does once loaded is call register_filesystem() and go to sleep. Which means there is not much attack surface exposed by loading a filesytem module unless the filesystem is mounted. In a user namespace filesystems are not mounted unless .fs_flags = FS_USERNS_MOUNT, which most filesystems do not set today. Acked-by: Serge Hallyn <serge.hallyn@canonical.com> Acked-by: Kees Cook <keescook@chromium.org> Reported-by: Kees Cook <keescook@google.com> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
9 years ago
  1. /*
  2. * linux/fs/befs/linuxvfs.c
  3. *
  4. * Copyright (C) 2001 Will Dyson <will_dyson@pobox.com
  5. *
  6. */
  7. #include <linux/module.h>
  8. #include <linux/slab.h>
  9. #include <linux/fs.h>
  10. #include <linux/errno.h>
  11. #include <linux/stat.h>
  12. #include <linux/nls.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/vfs.h>
  15. #include <linux/parser.h>
  16. #include <linux/namei.h>
  17. #include <linux/sched.h>
  18. #include "befs.h"
  19. #include "btree.h"
  20. #include "inode.h"
  21. #include "datastream.h"
  22. #include "super.h"
  23. #include "io.h"
  24. MODULE_DESCRIPTION("BeOS File System (BeFS) driver");
  25. MODULE_AUTHOR("Will Dyson");
  26. MODULE_LICENSE("GPL");
  27. /* The units the vfs expects inode->i_blocks to be in */
  28. #define VFS_BLOCK_SIZE 512
  29. static int befs_readdir(struct file *, void *, filldir_t);
  30. static int befs_get_block(struct inode *, sector_t, struct buffer_head *, int);
  31. static int befs_readpage(struct file *file, struct page *page);
  32. static sector_t befs_bmap(struct address_space *mapping, sector_t block);
  33. static struct dentry *befs_lookup(struct inode *, struct dentry *, unsigned int);
  34. static struct inode *befs_iget(struct super_block *, unsigned long);
  35. static struct inode *befs_alloc_inode(struct super_block *sb);
  36. static void befs_destroy_inode(struct inode *inode);
  37. static int befs_init_inodecache(void);
  38. static void befs_destroy_inodecache(void);
  39. static void *befs_follow_link(struct dentry *, struct nameidata *);
  40. static void befs_put_link(struct dentry *, struct nameidata *, void *);
  41. static int befs_utf2nls(struct super_block *sb, const char *in, int in_len,
  42. char **out, int *out_len);
  43. static int befs_nls2utf(struct super_block *sb, const char *in, int in_len,
  44. char **out, int *out_len);
  45. static void befs_put_super(struct super_block *);
  46. static int befs_remount(struct super_block *, int *, char *);
  47. static int befs_statfs(struct dentry *, struct kstatfs *);
  48. static int parse_options(char *, befs_mount_options *);
  49. static const struct super_operations befs_sops = {
  50. .alloc_inode = befs_alloc_inode, /* allocate a new inode */
  51. .destroy_inode = befs_destroy_inode, /* deallocate an inode */
  52. .put_super = befs_put_super, /* uninit super */
  53. .statfs = befs_statfs, /* statfs */
  54. .remount_fs = befs_remount,
  55. .show_options = generic_show_options,
  56. };
  57. /* slab cache for befs_inode_info objects */
  58. static struct kmem_cache *befs_inode_cachep;
  59. static const struct file_operations befs_dir_operations = {
  60. .read = generic_read_dir,
  61. .readdir = befs_readdir,
  62. .llseek = generic_file_llseek,
  63. };
  64. static const struct inode_operations befs_dir_inode_operations = {
  65. .lookup = befs_lookup,
  66. };
  67. static const struct address_space_operations befs_aops = {
  68. .readpage = befs_readpage,
  69. .bmap = befs_bmap,
  70. };
  71. static const struct inode_operations befs_symlink_inode_operations = {
  72. .readlink = generic_readlink,
  73. .follow_link = befs_follow_link,
  74. .put_link = befs_put_link,
  75. };
  76. /*
  77. * Called by generic_file_read() to read a page of data
  78. *
  79. * In turn, simply calls a generic block read function and
  80. * passes it the address of befs_get_block, for mapping file
  81. * positions to disk blocks.
  82. */
  83. static int
  84. befs_readpage(struct file *file, struct page *page)
  85. {
  86. return block_read_full_page(page, befs_get_block);
  87. }
  88. static sector_t
  89. befs_bmap(struct address_space *mapping, sector_t block)
  90. {
  91. return generic_block_bmap(mapping, block, befs_get_block);
  92. }
  93. /*
  94. * Generic function to map a file position (block) to a
  95. * disk offset (passed back in bh_result).
  96. *
  97. * Used by many higher level functions.
  98. *
  99. * Calls befs_fblock2brun() in datastream.c to do the real work.
  100. *
  101. * -WD 10-26-01
  102. */
  103. static int
  104. befs_get_block(struct inode *inode, sector_t block,
  105. struct buffer_head *bh_result, int create)
  106. {
  107. struct super_block *sb = inode->i_sb;
  108. befs_data_stream *ds = &BEFS_I(inode)->i_data.ds;
  109. befs_block_run run = BAD_IADDR;
  110. int res = 0;
  111. ulong disk_off;
  112. befs_debug(sb, "---> befs_get_block() for inode %lu, block %ld",
  113. inode->i_ino, block);
  114. if (block < 0) {
  115. befs_error(sb, "befs_get_block() was asked for a block "
  116. "number less than zero: block %ld in inode %lu",
  117. block, inode->i_ino);
  118. return -EIO;
  119. }
  120. if (create) {
  121. befs_error(sb, "befs_get_block() was asked to write to "
  122. "block %ld in inode %lu", block, inode->i_ino);
  123. return -EPERM;
  124. }
  125. res = befs_fblock2brun(sb, ds, block, &run);
  126. if (res != BEFS_OK) {
  127. befs_error(sb,
  128. "<--- befs_get_block() for inode %lu, block "
  129. "%ld ERROR", inode->i_ino, block);
  130. return -EFBIG;
  131. }
  132. disk_off = (ulong) iaddr2blockno(sb, &run);
  133. map_bh(bh_result, inode->i_sb, disk_off);
  134. befs_debug(sb, "<--- befs_get_block() for inode %lu, block %ld, "
  135. "disk address %lu", inode->i_ino, block, disk_off);
  136. return 0;
  137. }
  138. static struct dentry *
  139. befs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  140. {
  141. struct inode *inode = NULL;
  142. struct super_block *sb = dir->i_sb;
  143. befs_data_stream *ds = &BEFS_I(dir)->i_data.ds;
  144. befs_off_t offset;
  145. int ret;
  146. int utfnamelen;
  147. char *utfname;
  148. const char *name = dentry->d_name.name;
  149. befs_debug(sb, "---> befs_lookup() "
  150. "name %s inode %ld", dentry->d_name.name, dir->i_ino);
  151. /* Convert to UTF-8 */
  152. if (BEFS_SB(sb)->nls) {
  153. ret =
  154. befs_nls2utf(sb, name, strlen(name), &utfname, &utfnamelen);
  155. if (ret < 0) {
  156. befs_debug(sb, "<--- befs_lookup() ERROR");
  157. return ERR_PTR(ret);
  158. }
  159. ret = befs_btree_find(sb, ds, utfname, &offset);
  160. kfree(utfname);
  161. } else {
  162. ret = befs_btree_find(sb, ds, dentry->d_name.name, &offset);
  163. }
  164. if (ret == BEFS_BT_NOT_FOUND) {
  165. befs_debug(sb, "<--- befs_lookup() %s not found",
  166. dentry->d_name.name);
  167. return ERR_PTR(-ENOENT);
  168. } else if (ret != BEFS_OK || offset == 0) {
  169. befs_warning(sb, "<--- befs_lookup() Error");
  170. return ERR_PTR(-ENODATA);
  171. }
  172. inode = befs_iget(dir->i_sb, (ino_t) offset);
  173. if (IS_ERR(inode))
  174. return ERR_CAST(inode);
  175. d_add(dentry, inode);
  176. befs_debug(sb, "<--- befs_lookup()");
  177. return NULL;
  178. }
  179. static int
  180. befs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  181. {
  182. struct inode *inode = file_inode(filp);
  183. struct super_block *sb = inode->i_sb;
  184. befs_data_stream *ds = &BEFS_I(inode)->i_data.ds;
  185. befs_off_t value;
  186. int result;
  187. size_t keysize;
  188. unsigned char d_type;
  189. char keybuf[BEFS_NAME_LEN + 1];
  190. char *nlsname;
  191. int nlsnamelen;
  192. const char *dirname = filp->f_path.dentry->d_name.name;
  193. befs_debug(sb, "---> befs_readdir() "
  194. "name %s, inode %ld, filp->f_pos %Ld",
  195. dirname, inode->i_ino, filp->f_pos);
  196. result = befs_btree_read(sb, ds, filp->f_pos, BEFS_NAME_LEN + 1,
  197. keybuf, &keysize, &value);
  198. if (result == BEFS_ERR) {
  199. befs_debug(sb, "<--- befs_readdir() ERROR");
  200. befs_error(sb, "IO error reading %s (inode %lu)",
  201. dirname, inode->i_ino);
  202. return -EIO;
  203. } else if (result == BEFS_BT_END) {
  204. befs_debug(sb, "<--- befs_readdir() END");
  205. return 0;
  206. } else if (result == BEFS_BT_EMPTY) {
  207. befs_debug(sb, "<--- befs_readdir() Empty directory");
  208. return 0;
  209. }
  210. d_type = DT_UNKNOWN;
  211. /* Convert to NLS */
  212. if (BEFS_SB(sb)->nls) {
  213. result =
  214. befs_utf2nls(sb, keybuf, keysize, &nlsname, &nlsnamelen);
  215. if (result < 0) {
  216. befs_debug(sb, "<--- befs_readdir() ERROR");
  217. return result;
  218. }
  219. result = filldir(dirent, nlsname, nlsnamelen, filp->f_pos,
  220. (ino_t) value, d_type);
  221. kfree(nlsname);
  222. } else {
  223. result = filldir(dirent, keybuf, keysize, filp->f_pos,
  224. (ino_t) value, d_type);
  225. }
  226. filp->f_pos++;
  227. befs_debug(sb, "<--- befs_readdir() filp->f_pos %Ld", filp->f_pos);
  228. return 0;
  229. }
  230. static struct inode *
  231. befs_alloc_inode(struct super_block *sb)
  232. {
  233. struct befs_inode_info *bi;
  234. bi = (struct befs_inode_info *)kmem_cache_alloc(befs_inode_cachep,
  235. GFP_KERNEL);
  236. if (!bi)
  237. return NULL;
  238. return &bi->vfs_inode;
  239. }
  240. static void befs_i_callback(struct rcu_head *head)
  241. {
  242. struct inode *inode = container_of(head, struct inode, i_rcu);
  243. kmem_cache_free(befs_inode_cachep, BEFS_I(inode));
  244. }
  245. static void befs_destroy_inode(struct inode *inode)
  246. {
  247. call_rcu(&inode->i_rcu, befs_i_callback);
  248. }
  249. static void init_once(void *foo)
  250. {
  251. struct befs_inode_info *bi = (struct befs_inode_info *) foo;
  252. inode_init_once(&bi->vfs_inode);
  253. }
  254. static struct inode *befs_iget(struct super_block *sb, unsigned long ino)
  255. {
  256. struct buffer_head *bh = NULL;
  257. befs_inode *raw_inode = NULL;
  258. befs_sb_info *befs_sb = BEFS_SB(sb);
  259. befs_inode_info *befs_ino = NULL;
  260. struct inode *inode;
  261. long ret = -EIO;
  262. befs_debug(sb, "---> befs_read_inode() " "inode = %lu", ino);
  263. inode = iget_locked(sb, ino);
  264. if (IS_ERR(inode))
  265. return inode;
  266. if (!(inode->i_state & I_NEW))
  267. return inode;
  268. befs_ino = BEFS_I(inode);
  269. /* convert from vfs's inode number to befs's inode number */
  270. befs_ino->i_inode_num = blockno2iaddr(sb, inode->i_ino);
  271. befs_debug(sb, " real inode number [%u, %hu, %hu]",
  272. befs_ino->i_inode_num.allocation_group,
  273. befs_ino->i_inode_num.start, befs_ino->i_inode_num.len);
  274. bh = befs_bread(sb, inode->i_ino);
  275. if (!bh) {
  276. befs_error(sb, "unable to read inode block - "
  277. "inode = %lu", inode->i_ino);
  278. goto unacquire_none;
  279. }
  280. raw_inode = (befs_inode *) bh->b_data;
  281. befs_dump_inode(sb, raw_inode);
  282. if (befs_check_inode(sb, raw_inode, inode->i_ino) != BEFS_OK) {
  283. befs_error(sb, "Bad inode: %lu", inode->i_ino);
  284. goto unacquire_bh;
  285. }
  286. inode->i_mode = (umode_t) fs32_to_cpu(sb, raw_inode->mode);
  287. /*
  288. * set uid and gid. But since current BeOS is single user OS, so
  289. * you can change by "uid" or "gid" options.
  290. */
  291. inode->i_uid = befs_sb->mount_opts.use_uid ?
  292. befs_sb->mount_opts.uid :
  293. make_kuid(&init_user_ns, fs32_to_cpu(sb, raw_inode->uid));
  294. inode->i_gid = befs_sb->mount_opts.use_gid ?
  295. befs_sb->mount_opts.gid :
  296. make_kgid(&init_user_ns, fs32_to_cpu(sb, raw_inode->gid));
  297. set_nlink(inode, 1);
  298. /*
  299. * BEFS's time is 64 bits, but current VFS is 32 bits...
  300. * BEFS don't have access time. Nor inode change time. VFS
  301. * doesn't have creation time.
  302. * Also, the lower 16 bits of the last_modified_time and
  303. * create_time are just a counter to help ensure uniqueness
  304. * for indexing purposes. (PFD, page 54)
  305. */
  306. inode->i_mtime.tv_sec =
  307. fs64_to_cpu(sb, raw_inode->last_modified_time) >> 16;
  308. inode->i_mtime.tv_nsec = 0; /* lower 16 bits are not a time */
  309. inode->i_ctime = inode->i_mtime;
  310. inode->i_atime = inode->i_mtime;
  311. befs_ino->i_inode_num = fsrun_to_cpu(sb, raw_inode->inode_num);
  312. befs_ino->i_parent = fsrun_to_cpu(sb, raw_inode->parent);
  313. befs_ino->i_attribute = fsrun_to_cpu(sb, raw_inode->attributes);
  314. befs_ino->i_flags = fs32_to_cpu(sb, raw_inode->flags);
  315. if (S_ISLNK(inode->i_mode) && !(befs_ino->i_flags & BEFS_LONG_SYMLINK)){
  316. inode->i_size = 0;
  317. inode->i_blocks = befs_sb->block_size / VFS_BLOCK_SIZE;
  318. strncpy(befs_ino->i_data.symlink, raw_inode->data.symlink,
  319. BEFS_SYMLINK_LEN - 1);
  320. befs_ino->i_data.symlink[BEFS_SYMLINK_LEN - 1] = '\0';
  321. } else {
  322. int num_blks;
  323. befs_ino->i_data.ds =
  324. fsds_to_cpu(sb, &raw_inode->data.datastream);
  325. num_blks = befs_count_blocks(sb, &befs_ino->i_data.ds);
  326. inode->i_blocks =
  327. num_blks * (befs_sb->block_size / VFS_BLOCK_SIZE);
  328. inode->i_size = befs_ino->i_data.ds.size;
  329. }
  330. inode->i_mapping->a_ops = &befs_aops;
  331. if (S_ISREG(inode->i_mode)) {
  332. inode->i_fop = &generic_ro_fops;
  333. } else if (S_ISDIR(inode->i_mode)) {
  334. inode->i_op = &befs_dir_inode_operations;
  335. inode->i_fop = &befs_dir_operations;
  336. } else if (S_ISLNK(inode->i_mode)) {
  337. inode->i_op = &befs_symlink_inode_operations;
  338. } else {
  339. befs_error(sb, "Inode %lu is not a regular file, "
  340. "directory or symlink. THAT IS WRONG! BeFS has no "
  341. "on disk special files", inode->i_ino);
  342. goto unacquire_bh;
  343. }
  344. brelse(bh);
  345. befs_debug(sb, "<--- befs_read_inode()");
  346. unlock_new_inode(inode);
  347. return inode;
  348. unacquire_bh:
  349. brelse(bh);
  350. unacquire_none:
  351. iget_failed(inode);
  352. befs_debug(sb, "<--- befs_read_inode() - Bad inode");
  353. return ERR_PTR(ret);
  354. }
  355. /* Initialize the inode cache. Called at fs setup.
  356. *
  357. * Taken from NFS implementation by Al Viro.
  358. */
  359. static int
  360. befs_init_inodecache(void)
  361. {
  362. befs_inode_cachep = kmem_cache_create("befs_inode_cache",
  363. sizeof (struct befs_inode_info),
  364. 0, (SLAB_RECLAIM_ACCOUNT|
  365. SLAB_MEM_SPREAD),
  366. init_once);
  367. if (befs_inode_cachep == NULL) {
  368. printk(KERN_ERR "befs_init_inodecache: "
  369. "Couldn't initialize inode slabcache\n");
  370. return -ENOMEM;
  371. }
  372. return 0;
  373. }
  374. /* Called at fs teardown.
  375. *
  376. * Taken from NFS implementation by Al Viro.
  377. */
  378. static void
  379. befs_destroy_inodecache(void)
  380. {
  381. /*
  382. * Make sure all delayed rcu free inodes are flushed before we
  383. * destroy cache.
  384. */
  385. rcu_barrier();
  386. kmem_cache_destroy(befs_inode_cachep);
  387. }
  388. /*
  389. * The inode of symbolic link is different to data stream.
  390. * The data stream become link name. Unless the LONG_SYMLINK
  391. * flag is set.
  392. */
  393. static void *
  394. befs_follow_link(struct dentry *dentry, struct nameidata *nd)
  395. {
  396. befs_inode_info *befs_ino = BEFS_I(dentry->d_inode);
  397. char *link;
  398. if (befs_ino->i_flags & BEFS_LONG_SYMLINK) {
  399. struct super_block *sb = dentry->d_sb;
  400. befs_data_stream *data = &befs_ino->i_data.ds;
  401. befs_off_t len = data->size;
  402. if (len == 0) {
  403. befs_error(sb, "Long symlink with illegal length");
  404. link = ERR_PTR(-EIO);
  405. } else {
  406. befs_debug(sb, "Follow long symlink");
  407. link = kmalloc(len, GFP_NOFS);
  408. if (!link) {
  409. link = ERR_PTR(-ENOMEM);
  410. } else if (befs_read_lsymlink(sb, data, link, len) != len) {
  411. kfree(link);
  412. befs_error(sb, "Failed to read entire long symlink");
  413. link = ERR_PTR(-EIO);
  414. } else {
  415. link[len - 1] = '\0';
  416. }
  417. }
  418. } else {
  419. link = befs_ino->i_data.symlink;
  420. }
  421. nd_set_link(nd, link);
  422. return NULL;
  423. }
  424. static void befs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
  425. {
  426. befs_inode_info *befs_ino = BEFS_I(dentry->d_inode);
  427. if (befs_ino->i_flags & BEFS_LONG_SYMLINK) {
  428. char *link = nd_get_link(nd);
  429. if (!IS_ERR(link))
  430. kfree(link);
  431. }
  432. }
  433. /*
  434. * UTF-8 to NLS charset convert routine
  435. *
  436. *
  437. * Changed 8/10/01 by Will Dyson. Now use uni2char() / char2uni() rather than
  438. * the nls tables directly
  439. */
  440. static int
  441. befs_utf2nls(struct super_block *sb, const char *in,
  442. int in_len, char **out, int *out_len)
  443. {
  444. struct nls_table *nls = BEFS_SB(sb)->nls;
  445. int i, o;
  446. unicode_t uni;
  447. int unilen, utflen;
  448. char *result;
  449. /* The utf8->nls conversion won't make the final nls string bigger
  450. * than the utf one, but if the string is pure ascii they'll have the
  451. * same width and an extra char is needed to save the additional \0
  452. */
  453. int maxlen = in_len + 1;
  454. befs_debug(sb, "---> utf2nls()");
  455. if (!nls) {
  456. befs_error(sb, "befs_utf2nls called with no NLS table loaded");
  457. return -EINVAL;
  458. }
  459. *out = result = kmalloc(maxlen, GFP_NOFS);
  460. if (!*out) {
  461. befs_error(sb, "befs_utf2nls() cannot allocate memory");
  462. *out_len = 0;
  463. return -ENOMEM;
  464. }
  465. for (i = o = 0; i < in_len; i += utflen, o += unilen) {
  466. /* convert from UTF-8 to Unicode */
  467. utflen = utf8_to_utf32(&in[i], in_len - i, &uni);
  468. if (utflen < 0)
  469. goto conv_err;
  470. /* convert from Unicode to nls */
  471. if (uni > MAX_WCHAR_T)
  472. goto conv_err;
  473. unilen = nls->uni2char(uni, &result[o], in_len - o);
  474. if (unilen < 0)
  475. goto conv_err;
  476. }
  477. result[o] = '\0';
  478. *out_len = o;
  479. befs_debug(sb, "<--- utf2nls()");
  480. return o;
  481. conv_err:
  482. befs_error(sb, "Name using character set %s contains a character that "
  483. "cannot be converted to unicode.", nls->charset);
  484. befs_debug(sb, "<--- utf2nls()");
  485. kfree(result);
  486. return -EILSEQ;
  487. }
  488. /**
  489. * befs_nls2utf - Convert NLS string to utf8 encodeing
  490. * @sb: Superblock
  491. * @src: Input string buffer in NLS format
  492. * @srclen: Length of input string in bytes
  493. * @dest: The output string in UTF-8 format
  494. * @destlen: Length of the output buffer
  495. *
  496. * Converts input string @src, which is in the format of the loaded NLS map,
  497. * into a utf8 string.
  498. *
  499. * The destination string @dest is allocated by this function and the caller is
  500. * responsible for freeing it with kfree()
  501. *
  502. * On return, *@destlen is the length of @dest in bytes.
  503. *
  504. * On success, the return value is the number of utf8 characters written to
  505. * the output buffer @dest.
  506. *
  507. * On Failure, a negative number coresponding to the error code is returned.
  508. */
  509. static int
  510. befs_nls2utf(struct super_block *sb, const char *in,
  511. int in_len, char **out, int *out_len)
  512. {
  513. struct nls_table *nls = BEFS_SB(sb)->nls;
  514. int i, o;
  515. wchar_t uni;
  516. int unilen, utflen;
  517. char *result;
  518. /* There're nls characters that will translate to 3-chars-wide UTF-8
  519. * characters, a additional byte is needed to save the final \0
  520. * in special cases */
  521. int maxlen = (3 * in_len) + 1;
  522. befs_debug(sb, "---> nls2utf()\n");
  523. if (!nls) {
  524. befs_error(sb, "befs_nls2utf called with no NLS table loaded.");
  525. return -EINVAL;
  526. }
  527. *out = result = kmalloc(maxlen, GFP_NOFS);
  528. if (!*out) {
  529. befs_error(sb, "befs_nls2utf() cannot allocate memory");
  530. *out_len = 0;
  531. return -ENOMEM;
  532. }
  533. for (i = o = 0; i < in_len; i += unilen, o += utflen) {
  534. /* convert from nls to unicode */
  535. unilen = nls->char2uni(&in[i], in_len - i, &uni);
  536. if (unilen < 0)
  537. goto conv_err;
  538. /* convert from unicode to UTF-8 */
  539. utflen = utf32_to_utf8(uni, &result[o], 3);
  540. if (utflen <= 0)
  541. goto conv_err;
  542. }
  543. result[o] = '\0';
  544. *out_len = o;
  545. befs_debug(sb, "<--- nls2utf()");
  546. return i;
  547. conv_err:
  548. befs_error(sb, "Name using charecter set %s contains a charecter that "
  549. "cannot be converted to unicode.", nls->charset);
  550. befs_debug(sb, "<--- nls2utf()");
  551. kfree(result);
  552. return -EILSEQ;
  553. }
  554. /**
  555. * Use the
  556. *
  557. */
  558. enum {
  559. Opt_uid, Opt_gid, Opt_charset, Opt_debug, Opt_err,
  560. };
  561. static const match_table_t befs_tokens = {
  562. {Opt_uid, "uid=%d"},
  563. {Opt_gid, "gid=%d"},
  564. {Opt_charset, "iocharset=%s"},
  565. {Opt_debug, "debug"},
  566. {Opt_err, NULL}
  567. };
  568. static int
  569. parse_options(char *options, befs_mount_options * opts)
  570. {
  571. char *p;
  572. substring_t args[MAX_OPT_ARGS];
  573. int option;
  574. kuid_t uid;
  575. kgid_t gid;
  576. /* Initialize options */
  577. opts->uid = GLOBAL_ROOT_UID;
  578. opts->gid = GLOBAL_ROOT_GID;
  579. opts->use_uid = 0;
  580. opts->use_gid = 0;
  581. opts->iocharset = NULL;
  582. opts->debug = 0;
  583. if (!options)
  584. return 1;
  585. while ((p = strsep(&options, ",")) != NULL) {
  586. int token;
  587. if (!*p)
  588. continue;
  589. token = match_token(p, befs_tokens, args);
  590. switch (token) {
  591. case Opt_uid:
  592. if (match_int(&args[0], &option))
  593. return 0;
  594. uid = INVALID_UID;
  595. if (option >= 0)
  596. uid = make_kuid(current_user_ns(), option);
  597. if (!uid_valid(uid)) {
  598. printk(KERN_ERR "BeFS: Invalid uid %d, "
  599. "using default\n", option);
  600. break;
  601. }
  602. opts->uid = uid;
  603. opts->use_uid = 1;
  604. break;
  605. case Opt_gid:
  606. if (match_int(&args[0], &option))
  607. return 0;
  608. gid = INVALID_GID;
  609. if (option >= 0)
  610. gid = make_kgid(current_user_ns(), option);
  611. if (!gid_valid(gid)) {
  612. printk(KERN_ERR "BeFS: Invalid gid %d, "
  613. "using default\n", option);
  614. break;
  615. }
  616. opts->gid = gid;
  617. opts->use_gid = 1;
  618. break;
  619. case Opt_charset:
  620. kfree(opts->iocharset);
  621. opts->iocharset = match_strdup(&args[0]);
  622. if (!opts->iocharset) {
  623. printk(KERN_ERR "BeFS: allocation failure for "
  624. "iocharset string\n");
  625. return 0;
  626. }
  627. break;
  628. case Opt_debug:
  629. opts->debug = 1;
  630. break;
  631. default:
  632. printk(KERN_ERR "BeFS: Unrecognized mount option \"%s\" "
  633. "or missing value\n", p);
  634. return 0;
  635. }
  636. }
  637. return 1;
  638. }
  639. /* This function has the responsibiltiy of getting the
  640. * filesystem ready for unmounting.
  641. * Basically, we free everything that we allocated in
  642. * befs_read_inode
  643. */
  644. static void
  645. befs_put_super(struct super_block *sb)
  646. {
  647. kfree(BEFS_SB(sb)->mount_opts.iocharset);
  648. BEFS_SB(sb)->mount_opts.iocharset = NULL;
  649. unload_nls(BEFS_SB(sb)->nls);
  650. kfree(sb->s_fs_info);
  651. sb->s_fs_info = NULL;
  652. }
  653. /* Allocate private field of the superblock, fill it.
  654. *
  655. * Finish filling the public superblock fields
  656. * Make the root directory
  657. * Load a set of NLS translations if needed.
  658. */
  659. static int
  660. befs_fill_super(struct super_block *sb, void *data, int silent)
  661. {
  662. struct buffer_head *bh;
  663. befs_sb_info *befs_sb;
  664. befs_super_block *disk_sb;
  665. struct inode *root;
  666. long ret = -EINVAL;
  667. const unsigned long sb_block = 0;
  668. const off_t x86_sb_off = 512;
  669. save_mount_options(sb, data);
  670. sb->s_fs_info = kmalloc(sizeof (*befs_sb), GFP_KERNEL);
  671. if (sb->s_fs_info == NULL) {
  672. printk(KERN_ERR
  673. "BeFS(%s): Unable to allocate memory for private "
  674. "portion of superblock. Bailing.\n", sb->s_id);
  675. goto unacquire_none;
  676. }
  677. befs_sb = BEFS_SB(sb);
  678. memset(befs_sb, 0, sizeof(befs_sb_info));
  679. if (!parse_options((char *) data, &befs_sb->mount_opts)) {
  680. befs_error(sb, "cannot parse mount options");
  681. goto unacquire_priv_sbp;
  682. }
  683. befs_debug(sb, "---> befs_fill_super()");
  684. #ifndef CONFIG_BEFS_RW
  685. if (!(sb->s_flags & MS_RDONLY)) {
  686. befs_warning(sb,
  687. "No write support. Marking filesystem read-only");
  688. sb->s_flags |= MS_RDONLY;
  689. }
  690. #endif /* CONFIG_BEFS_RW */
  691. /*
  692. * Set dummy blocksize to read super block.
  693. * Will be set to real fs blocksize later.
  694. *
  695. * Linux 2.4.10 and later refuse to read blocks smaller than
  696. * the hardsect size for the device. But we also need to read at
  697. * least 1k to get the second 512 bytes of the volume.
  698. * -WD 10-26-01
  699. */
  700. sb_min_blocksize(sb, 1024);
  701. if (!(bh = sb_bread(sb, sb_block))) {
  702. befs_error(sb, "unable to read superblock");
  703. goto unacquire_priv_sbp;
  704. }
  705. /* account for offset of super block on x86 */
  706. disk_sb = (befs_super_block *) bh->b_data;
  707. if ((disk_sb->magic1 == BEFS_SUPER_MAGIC1_LE) ||
  708. (disk_sb->magic1 == BEFS_SUPER_MAGIC1_BE)) {
  709. befs_debug(sb, "Using PPC superblock location");
  710. } else {
  711. befs_debug(sb, "Using x86 superblock location");
  712. disk_sb =
  713. (befs_super_block *) ((void *) bh->b_data + x86_sb_off);
  714. }
  715. if (befs_load_sb(sb, disk_sb) != BEFS_OK)
  716. goto unacquire_bh;
  717. befs_dump_super_block(sb, disk_sb);
  718. brelse(bh);
  719. if (befs_check_sb(sb) != BEFS_OK)
  720. goto unacquire_priv_sbp;
  721. if( befs_sb->num_blocks > ~((sector_t)0) ) {
  722. befs_error(sb, "blocks count: %Lu "
  723. "is larger than the host can use",
  724. befs_sb->num_blocks);
  725. goto unacquire_priv_sbp;
  726. }
  727. /*
  728. * set up enough so that it can read an inode
  729. * Fill in kernel superblock fields from private sb
  730. */
  731. sb->s_magic = BEFS_SUPER_MAGIC;
  732. /* Set real blocksize of fs */
  733. sb_set_blocksize(sb, (ulong) befs_sb->block_size);
  734. sb->s_op = &befs_sops;
  735. root = befs_iget(sb, iaddr2blockno(sb, &(befs_sb->root_dir)));
  736. if (IS_ERR(root)) {
  737. ret = PTR_ERR(root);
  738. goto unacquire_priv_sbp;
  739. }
  740. sb->s_root = d_make_root(root);
  741. if (!sb->s_root) {
  742. befs_error(sb, "get root inode failed");
  743. goto unacquire_priv_sbp;
  744. }
  745. /* load nls library */
  746. if (befs_sb->mount_opts.iocharset) {
  747. befs_debug(sb, "Loading nls: %s",
  748. befs_sb->mount_opts.iocharset);
  749. befs_sb->nls = load_nls(befs_sb->mount_opts.iocharset);
  750. if (!befs_sb->nls) {
  751. befs_warning(sb, "Cannot load nls %s"
  752. " loading default nls",
  753. befs_sb->mount_opts.iocharset);
  754. befs_sb->nls = load_nls_default();
  755. }
  756. /* load default nls if none is specified in mount options */
  757. } else {
  758. befs_debug(sb, "Loading default nls");
  759. befs_sb->nls = load_nls_default();
  760. }
  761. return 0;
  762. /*****************/
  763. unacquire_bh:
  764. brelse(bh);
  765. unacquire_priv_sbp:
  766. kfree(befs_sb->mount_opts.iocharset);
  767. kfree(sb->s_fs_info);
  768. unacquire_none:
  769. sb->s_fs_info = NULL;
  770. return ret;
  771. }
  772. static int
  773. befs_remount(struct super_block *sb, int *flags, char *data)
  774. {
  775. if (!(*flags & MS_RDONLY))
  776. return -EINVAL;
  777. return 0;
  778. }
  779. static int
  780. befs_statfs(struct dentry *dentry, struct kstatfs *buf)
  781. {
  782. struct super_block *sb = dentry->d_sb;
  783. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  784. befs_debug(sb, "---> befs_statfs()");
  785. buf->f_type = BEFS_SUPER_MAGIC;
  786. buf->f_bsize = sb->s_blocksize;
  787. buf->f_blocks = BEFS_SB(sb)->num_blocks;
  788. buf->f_bfree = BEFS_SB(sb)->num_blocks - BEFS_SB(sb)->used_blocks;
  789. buf->f_bavail = buf->f_bfree;
  790. buf->f_files = 0; /* UNKNOWN */
  791. buf->f_ffree = 0; /* UNKNOWN */
  792. buf->f_fsid.val[0] = (u32)id;
  793. buf->f_fsid.val[1] = (u32)(id >> 32);
  794. buf->f_namelen = BEFS_NAME_LEN;
  795. befs_debug(sb, "<--- befs_statfs()");
  796. return 0;
  797. }
  798. static struct dentry *
  799. befs_mount(struct file_system_type *fs_type, int flags, const char *dev_name,
  800. void *data)
  801. {
  802. return mount_bdev(fs_type, flags, dev_name, data, befs_fill_super);
  803. }
  804. static struct file_system_type befs_fs_type = {
  805. .owner = THIS_MODULE,
  806. .name = "befs",
  807. .mount = befs_mount,
  808. .kill_sb = kill_block_super,
  809. .fs_flags = FS_REQUIRES_DEV,
  810. };
  811. MODULE_ALIAS_FS("befs");
  812. static int __init
  813. init_befs_fs(void)
  814. {
  815. int err;
  816. printk(KERN_INFO "BeFS version: %s\n", BEFS_VERSION);
  817. err = befs_init_inodecache();
  818. if (err)
  819. goto unacquire_none;
  820. err = register_filesystem(&befs_fs_type);
  821. if (err)
  822. goto unacquire_inodecache;
  823. return 0;
  824. unacquire_inodecache:
  825. befs_destroy_inodecache();
  826. unacquire_none:
  827. return err;
  828. }
  829. static void __exit
  830. exit_befs_fs(void)
  831. {
  832. befs_destroy_inodecache();
  833. unregister_filesystem(&befs_fs_type);
  834. }
  835. /*
  836. Macros that typecheck the init and exit functions,
  837. ensures that they are called at init and cleanup,
  838. and eliminates warnings about unused functions.
  839. */
  840. module_init(init_befs_fs)
  841. module_exit(exit_befs_fs)