linuxvfs.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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 "befs.h"
  18. #include "btree.h"
  19. #include "inode.h"
  20. #include "datastream.h"
  21. #include "super.h"
  22. #include "io.h"
  23. #include "endian.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 *, struct nameidata *);
  34. static void befs_read_inode(struct inode *ino);
  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 super_block *, struct kstatfs *);
  48. static int parse_options(char *, befs_mount_options *);
  49. static const struct super_operations befs_sops = {
  50. .read_inode = befs_read_inode, /* initialize & read inode */
  51. .alloc_inode = befs_alloc_inode, /* allocate a new inode */
  52. .destroy_inode = befs_destroy_inode, /* deallocate an inode */
  53. .put_super = befs_put_super, /* uninit super */
  54. .statfs = befs_statfs, /* statfs */
  55. .remount_fs = befs_remount,
  56. };
  57. /* slab cache for befs_inode_info objects */
  58. static kmem_cache_t *befs_inode_cachep;
  59. static struct file_operations befs_dir_operations = {
  60. .read = generic_read_dir,
  61. .readdir = befs_readdir,
  62. };
  63. static struct inode_operations befs_dir_inode_operations = {
  64. .lookup = befs_lookup,
  65. };
  66. static struct file_operations befs_file_operations = {
  67. .llseek = default_llseek,
  68. .read = generic_file_read,
  69. .mmap = generic_file_readonly_mmap,
  70. };
  71. static struct address_space_operations befs_aops = {
  72. .readpage = befs_readpage,
  73. .sync_page = block_sync_page,
  74. .bmap = befs_bmap,
  75. };
  76. static struct inode_operations befs_symlink_inode_operations = {
  77. .readlink = generic_readlink,
  78. .follow_link = befs_follow_link,
  79. .put_link = befs_put_link,
  80. };
  81. /*
  82. * Called by generic_file_read() to read a page of data
  83. *
  84. * In turn, simply calls a generic block read function and
  85. * passes it the address of befs_get_block, for mapping file
  86. * positions to disk blocks.
  87. */
  88. static int
  89. befs_readpage(struct file *file, struct page *page)
  90. {
  91. return block_read_full_page(page, befs_get_block);
  92. }
  93. static sector_t
  94. befs_bmap(struct address_space *mapping, sector_t block)
  95. {
  96. return generic_block_bmap(mapping, block, befs_get_block);
  97. }
  98. /*
  99. * Generic function to map a file position (block) to a
  100. * disk offset (passed back in bh_result).
  101. *
  102. * Used by many higher level functions.
  103. *
  104. * Calls befs_fblock2brun() in datastream.c to do the real work.
  105. *
  106. * -WD 10-26-01
  107. */
  108. static int
  109. befs_get_block(struct inode *inode, sector_t block,
  110. struct buffer_head *bh_result, int create)
  111. {
  112. struct super_block *sb = inode->i_sb;
  113. befs_data_stream *ds = &BEFS_I(inode)->i_data.ds;
  114. befs_block_run run = BAD_IADDR;
  115. int res = 0;
  116. ulong disk_off;
  117. befs_debug(sb, "---> befs_get_block() for inode %lu, block %ld",
  118. inode->i_ino, block);
  119. if (block < 0) {
  120. befs_error(sb, "befs_get_block() was asked for a block "
  121. "number less than zero: block %ld in inode %lu",
  122. block, inode->i_ino);
  123. return -EIO;
  124. }
  125. if (create) {
  126. befs_error(sb, "befs_get_block() was asked to write to "
  127. "block %ld in inode %lu", block, inode->i_ino);
  128. return -EPERM;
  129. }
  130. res = befs_fblock2brun(sb, ds, block, &run);
  131. if (res != BEFS_OK) {
  132. befs_error(sb,
  133. "<--- befs_get_block() for inode %lu, block "
  134. "%ld ERROR", inode->i_ino, block);
  135. return -EFBIG;
  136. }
  137. disk_off = (ulong) iaddr2blockno(sb, &run);
  138. map_bh(bh_result, inode->i_sb, disk_off);
  139. befs_debug(sb, "<--- befs_get_block() for inode %lu, block %ld, "
  140. "disk address %lu", inode->i_ino, block, disk_off);
  141. return 0;
  142. }
  143. static struct dentry *
  144. befs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  145. {
  146. struct inode *inode = NULL;
  147. struct super_block *sb = dir->i_sb;
  148. befs_data_stream *ds = &BEFS_I(dir)->i_data.ds;
  149. befs_off_t offset;
  150. int ret;
  151. int utfnamelen;
  152. char *utfname;
  153. const char *name = dentry->d_name.name;
  154. befs_debug(sb, "---> befs_lookup() "
  155. "name %s inode %ld", dentry->d_name.name, dir->i_ino);
  156. /* Convert to UTF-8 */
  157. if (BEFS_SB(sb)->nls) {
  158. ret =
  159. befs_nls2utf(sb, name, strlen(name), &utfname, &utfnamelen);
  160. if (ret < 0) {
  161. befs_debug(sb, "<--- befs_lookup() ERROR");
  162. return ERR_PTR(ret);
  163. }
  164. ret = befs_btree_find(sb, ds, utfname, &offset);
  165. kfree(utfname);
  166. } else {
  167. ret = befs_btree_find(sb, ds, dentry->d_name.name, &offset);
  168. }
  169. if (ret == BEFS_BT_NOT_FOUND) {
  170. befs_debug(sb, "<--- befs_lookup() %s not found",
  171. dentry->d_name.name);
  172. return ERR_PTR(-ENOENT);
  173. } else if (ret != BEFS_OK || offset == 0) {
  174. befs_warning(sb, "<--- befs_lookup() Error");
  175. return ERR_PTR(-ENODATA);
  176. }
  177. inode = iget(dir->i_sb, (ino_t) offset);
  178. if (!inode)
  179. return ERR_PTR(-EACCES);
  180. d_add(dentry, inode);
  181. befs_debug(sb, "<--- befs_lookup()");
  182. return NULL;
  183. }
  184. static int
  185. befs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  186. {
  187. struct inode *inode = filp->f_dentry->d_inode;
  188. struct super_block *sb = inode->i_sb;
  189. befs_data_stream *ds = &BEFS_I(inode)->i_data.ds;
  190. befs_off_t value;
  191. int result;
  192. size_t keysize;
  193. unsigned char d_type;
  194. char keybuf[BEFS_NAME_LEN + 1];
  195. char *nlsname;
  196. int nlsnamelen;
  197. const char *dirname = filp->f_dentry->d_name.name;
  198. befs_debug(sb, "---> befs_readdir() "
  199. "name %s, inode %ld, filp->f_pos %Ld",
  200. dirname, inode->i_ino, filp->f_pos);
  201. result = befs_btree_read(sb, ds, filp->f_pos, BEFS_NAME_LEN + 1,
  202. keybuf, &keysize, &value);
  203. if (result == BEFS_ERR) {
  204. befs_debug(sb, "<--- befs_readdir() ERROR");
  205. befs_error(sb, "IO error reading %s (inode %lu)",
  206. dirname, inode->i_ino);
  207. return -EIO;
  208. } else if (result == BEFS_BT_END) {
  209. befs_debug(sb, "<--- befs_readdir() END");
  210. return 0;
  211. } else if (result == BEFS_BT_EMPTY) {
  212. befs_debug(sb, "<--- befs_readdir() Empty directory");
  213. return 0;
  214. }
  215. d_type = DT_UNKNOWN;
  216. /* Convert to NLS */
  217. if (BEFS_SB(sb)->nls) {
  218. result =
  219. befs_utf2nls(sb, keybuf, keysize, &nlsname, &nlsnamelen);
  220. if (result < 0) {
  221. befs_debug(sb, "<--- befs_readdir() ERROR");
  222. return result;
  223. }
  224. result = filldir(dirent, nlsname, nlsnamelen, filp->f_pos,
  225. (ino_t) value, d_type);
  226. kfree(nlsname);
  227. } else {
  228. result = filldir(dirent, keybuf, keysize, filp->f_pos,
  229. (ino_t) value, d_type);
  230. }
  231. filp->f_pos++;
  232. befs_debug(sb, "<--- befs_readdir() filp->f_pos %Ld", filp->f_pos);
  233. return 0;
  234. }
  235. static struct inode *
  236. befs_alloc_inode(struct super_block *sb)
  237. {
  238. struct befs_inode_info *bi;
  239. bi = (struct befs_inode_info *)kmem_cache_alloc(befs_inode_cachep,
  240. SLAB_KERNEL);
  241. if (!bi)
  242. return NULL;
  243. return &bi->vfs_inode;
  244. }
  245. static void
  246. befs_destroy_inode(struct inode *inode)
  247. {
  248. kmem_cache_free(befs_inode_cachep, BEFS_I(inode));
  249. }
  250. static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
  251. {
  252. struct befs_inode_info *bi = (struct befs_inode_info *) foo;
  253. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  254. SLAB_CTOR_CONSTRUCTOR) {
  255. inode_init_once(&bi->vfs_inode);
  256. }
  257. }
  258. static void
  259. befs_read_inode(struct inode *inode)
  260. {
  261. struct buffer_head *bh = NULL;
  262. befs_inode *raw_inode = NULL;
  263. struct super_block *sb = inode->i_sb;
  264. befs_sb_info *befs_sb = BEFS_SB(sb);
  265. befs_inode_info *befs_ino = NULL;
  266. befs_debug(sb, "---> befs_read_inode() " "inode = %lu", inode->i_ino);
  267. befs_ino = BEFS_I(inode);
  268. /* convert from vfs's inode number to befs's inode number */
  269. befs_ino->i_inode_num = blockno2iaddr(sb, inode->i_ino);
  270. befs_debug(sb, " real inode number [%u, %hu, %hu]",
  271. befs_ino->i_inode_num.allocation_group,
  272. befs_ino->i_inode_num.start, befs_ino->i_inode_num.len);
  273. bh = befs_bread(sb, inode->i_ino);
  274. if (!bh) {
  275. befs_error(sb, "unable to read inode block - "
  276. "inode = %lu", inode->i_ino);
  277. goto unaquire_none;
  278. }
  279. raw_inode = (befs_inode *) bh->b_data;
  280. befs_dump_inode(sb, raw_inode);
  281. if (befs_check_inode(sb, raw_inode, inode->i_ino) != BEFS_OK) {
  282. befs_error(sb, "Bad inode: %lu", inode->i_ino);
  283. goto unaquire_bh;
  284. }
  285. inode->i_mode = (umode_t) fs32_to_cpu(sb, raw_inode->mode);
  286. /*
  287. * set uid and gid. But since current BeOS is single user OS, so
  288. * you can change by "uid" or "gid" options.
  289. */
  290. inode->i_uid = befs_sb->mount_opts.use_uid ?
  291. befs_sb->mount_opts.uid : (uid_t) fs32_to_cpu(sb, raw_inode->uid);
  292. inode->i_gid = befs_sb->mount_opts.use_gid ?
  293. befs_sb->mount_opts.gid : (gid_t) fs32_to_cpu(sb, raw_inode->gid);
  294. inode->i_nlink = 1;
  295. /*
  296. * BEFS's time is 64 bits, but current VFS is 32 bits...
  297. * BEFS don't have access time. Nor inode change time. VFS
  298. * doesn't have creation time.
  299. * Also, the lower 16 bits of the last_modified_time and
  300. * create_time are just a counter to help ensure uniqueness
  301. * for indexing purposes. (PFD, page 54)
  302. */
  303. inode->i_mtime.tv_sec =
  304. fs64_to_cpu(sb, raw_inode->last_modified_time) >> 16;
  305. inode->i_mtime.tv_nsec = 0; /* lower 16 bits are not a time */
  306. inode->i_ctime = inode->i_mtime;
  307. inode->i_atime = inode->i_mtime;
  308. inode->i_blksize = befs_sb->block_size;
  309. befs_ino->i_inode_num = fsrun_to_cpu(sb, raw_inode->inode_num);
  310. befs_ino->i_parent = fsrun_to_cpu(sb, raw_inode->parent);
  311. befs_ino->i_attribute = fsrun_to_cpu(sb, raw_inode->attributes);
  312. befs_ino->i_flags = fs32_to_cpu(sb, raw_inode->flags);
  313. if (S_ISLNK(inode->i_mode) && !(befs_ino->i_flags & BEFS_LONG_SYMLINK)){
  314. inode->i_size = 0;
  315. inode->i_blocks = befs_sb->block_size / VFS_BLOCK_SIZE;
  316. strncpy(befs_ino->i_data.symlink, raw_inode->data.symlink,
  317. BEFS_SYMLINK_LEN);
  318. } else {
  319. int num_blks;
  320. befs_ino->i_data.ds =
  321. fsds_to_cpu(sb, raw_inode->data.datastream);
  322. num_blks = befs_count_blocks(sb, &befs_ino->i_data.ds);
  323. inode->i_blocks =
  324. num_blks * (befs_sb->block_size / VFS_BLOCK_SIZE);
  325. inode->i_size = befs_ino->i_data.ds.size;
  326. }
  327. inode->i_mapping->a_ops = &befs_aops;
  328. if (S_ISREG(inode->i_mode)) {
  329. inode->i_fop = &befs_file_operations;
  330. } else if (S_ISDIR(inode->i_mode)) {
  331. inode->i_op = &befs_dir_inode_operations;
  332. inode->i_fop = &befs_dir_operations;
  333. } else if (S_ISLNK(inode->i_mode)) {
  334. inode->i_op = &befs_symlink_inode_operations;
  335. } else {
  336. befs_error(sb, "Inode %lu is not a regular file, "
  337. "directory or symlink. THAT IS WRONG! BeFS has no "
  338. "on disk special files", inode->i_ino);
  339. goto unaquire_bh;
  340. }
  341. brelse(bh);
  342. befs_debug(sb, "<--- befs_read_inode()");
  343. return;
  344. unaquire_bh:
  345. brelse(bh);
  346. unaquire_none:
  347. make_bad_inode(inode);
  348. befs_debug(sb, "<--- befs_read_inode() - Bad inode");
  349. return;
  350. }
  351. /* Initialize the inode cache. Called at fs setup.
  352. *
  353. * Taken from NFS implementation by Al Viro.
  354. */
  355. static int
  356. befs_init_inodecache(void)
  357. {
  358. befs_inode_cachep = kmem_cache_create("befs_inode_cache",
  359. sizeof (struct befs_inode_info),
  360. 0, SLAB_RECLAIM_ACCOUNT,
  361. init_once, NULL);
  362. if (befs_inode_cachep == NULL) {
  363. printk(KERN_ERR "befs_init_inodecache: "
  364. "Couldn't initalize inode slabcache\n");
  365. return -ENOMEM;
  366. }
  367. return 0;
  368. }
  369. /* Called at fs teardown.
  370. *
  371. * Taken from NFS implementation by Al Viro.
  372. */
  373. static void
  374. befs_destroy_inodecache(void)
  375. {
  376. if (kmem_cache_destroy(befs_inode_cachep))
  377. printk(KERN_ERR "befs_destroy_inodecache: "
  378. "not all structures were freed\n");
  379. }
  380. /*
  381. * The inode of symbolic link is different to data stream.
  382. * The data stream become link name. Unless the LONG_SYMLINK
  383. * flag is set.
  384. */
  385. static void *
  386. befs_follow_link(struct dentry *dentry, struct nameidata *nd)
  387. {
  388. befs_inode_info *befs_ino = BEFS_I(dentry->d_inode);
  389. char *link;
  390. if (befs_ino->i_flags & BEFS_LONG_SYMLINK) {
  391. struct super_block *sb = dentry->d_sb;
  392. befs_data_stream *data = &befs_ino->i_data.ds;
  393. befs_off_t len = data->size;
  394. befs_debug(sb, "Follow long symlink");
  395. link = kmalloc(len, GFP_NOFS);
  396. if (!link) {
  397. link = ERR_PTR(-ENOMEM);
  398. } else if (befs_read_lsymlink(sb, data, link, len) != len) {
  399. kfree(link);
  400. befs_error(sb, "Failed to read entire long symlink");
  401. link = ERR_PTR(-EIO);
  402. }
  403. } else {
  404. link = befs_ino->i_data.symlink;
  405. }
  406. nd_set_link(nd, link);
  407. return NULL;
  408. }
  409. static void befs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
  410. {
  411. befs_inode_info *befs_ino = BEFS_I(dentry->d_inode);
  412. if (befs_ino->i_flags & BEFS_LONG_SYMLINK) {
  413. char *p = nd_get_link(nd);
  414. if (!IS_ERR(p))
  415. kfree(p);
  416. }
  417. }
  418. /*
  419. * UTF-8 to NLS charset convert routine
  420. *
  421. *
  422. * Changed 8/10/01 by Will Dyson. Now use uni2char() / char2uni() rather than
  423. * the nls tables directly
  424. */
  425. static int
  426. befs_utf2nls(struct super_block *sb, const char *in,
  427. int in_len, char **out, int *out_len)
  428. {
  429. struct nls_table *nls = BEFS_SB(sb)->nls;
  430. int i, o;
  431. wchar_t uni;
  432. int unilen, utflen;
  433. char *result;
  434. int maxlen = in_len; /* The utf8->nls conversion can't make more chars */
  435. befs_debug(sb, "---> utf2nls()");
  436. if (!nls) {
  437. befs_error(sb, "befs_utf2nls called with no NLS table loaded");
  438. return -EINVAL;
  439. }
  440. *out = result = kmalloc(maxlen, GFP_NOFS);
  441. if (!*out) {
  442. befs_error(sb, "befs_utf2nls() cannot allocate memory");
  443. *out_len = 0;
  444. return -ENOMEM;
  445. }
  446. for (i = o = 0; i < in_len; i += utflen, o += unilen) {
  447. /* convert from UTF-8 to Unicode */
  448. utflen = utf8_mbtowc(&uni, &in[i], in_len - i);
  449. if (utflen < 0) {
  450. goto conv_err;
  451. }
  452. /* convert from Unicode to nls */
  453. unilen = nls->uni2char(uni, &result[o], in_len - o);
  454. if (unilen < 0) {
  455. goto conv_err;
  456. }
  457. }
  458. result[o] = '\0';
  459. *out_len = o;
  460. befs_debug(sb, "<--- utf2nls()");
  461. return o;
  462. conv_err:
  463. befs_error(sb, "Name using character set %s contains a character that "
  464. "cannot be converted to unicode.", nls->charset);
  465. befs_debug(sb, "<--- utf2nls()");
  466. kfree(result);
  467. return -EILSEQ;
  468. }
  469. /**
  470. * befs_nls2utf - Convert NLS string to utf8 encodeing
  471. * @sb: Superblock
  472. * @src: Input string buffer in NLS format
  473. * @srclen: Length of input string in bytes
  474. * @dest: The output string in UTF8 format
  475. * @destlen: Length of the output buffer
  476. *
  477. * Converts input string @src, which is in the format of the loaded NLS map,
  478. * into a utf8 string.
  479. *
  480. * The destination string @dest is allocated by this function and the caller is
  481. * responsible for freeing it with kfree()
  482. *
  483. * On return, *@destlen is the length of @dest in bytes.
  484. *
  485. * On success, the return value is the number of utf8 characters written to
  486. * the output buffer @dest.
  487. *
  488. * On Failure, a negative number coresponding to the error code is returned.
  489. */
  490. static int
  491. befs_nls2utf(struct super_block *sb, const char *in,
  492. int in_len, char **out, int *out_len)
  493. {
  494. struct nls_table *nls = BEFS_SB(sb)->nls;
  495. int i, o;
  496. wchar_t uni;
  497. int unilen, utflen;
  498. char *result;
  499. int maxlen = 3 * in_len;
  500. befs_debug(sb, "---> nls2utf()\n");
  501. if (!nls) {
  502. befs_error(sb, "befs_nls2utf called with no NLS table loaded.");
  503. return -EINVAL;
  504. }
  505. *out = result = kmalloc(maxlen, GFP_NOFS);
  506. if (!*out) {
  507. befs_error(sb, "befs_nls2utf() cannot allocate memory");
  508. *out_len = 0;
  509. return -ENOMEM;
  510. }
  511. for (i = o = 0; i < in_len; i += unilen, o += utflen) {
  512. /* convert from nls to unicode */
  513. unilen = nls->char2uni(&in[i], in_len - i, &uni);
  514. if (unilen < 0) {
  515. goto conv_err;
  516. }
  517. /* convert from unicode to UTF-8 */
  518. utflen = utf8_wctomb(&result[o], uni, 3);
  519. if (utflen <= 0) {
  520. goto conv_err;
  521. }
  522. }
  523. result[o] = '\0';
  524. *out_len = o;
  525. befs_debug(sb, "<--- nls2utf()");
  526. return i;
  527. conv_err:
  528. befs_error(sb, "Name using charecter set %s contains a charecter that "
  529. "cannot be converted to unicode.", nls->charset);
  530. befs_debug(sb, "<--- nls2utf()");
  531. kfree(result);
  532. return -EILSEQ;
  533. }
  534. /**
  535. * Use the
  536. *
  537. */
  538. enum {
  539. Opt_uid, Opt_gid, Opt_charset, Opt_debug, Opt_err,
  540. };
  541. static match_table_t befs_tokens = {
  542. {Opt_uid, "uid=%d"},
  543. {Opt_gid, "gid=%d"},
  544. {Opt_charset, "iocharset=%s"},
  545. {Opt_debug, "debug"},
  546. {Opt_err, NULL}
  547. };
  548. static int
  549. parse_options(char *options, befs_mount_options * opts)
  550. {
  551. char *p;
  552. substring_t args[MAX_OPT_ARGS];
  553. int option;
  554. /* Initialize options */
  555. opts->uid = 0;
  556. opts->gid = 0;
  557. opts->use_uid = 0;
  558. opts->use_gid = 0;
  559. opts->iocharset = NULL;
  560. opts->debug = 0;
  561. if (!options)
  562. return 1;
  563. while ((p = strsep(&options, ",")) != NULL) {
  564. int token;
  565. if (!*p)
  566. continue;
  567. token = match_token(p, befs_tokens, args);
  568. switch (token) {
  569. case Opt_uid:
  570. if (match_int(&args[0], &option))
  571. return 0;
  572. if (option < 0) {
  573. printk(KERN_ERR "BeFS: Invalid uid %d, "
  574. "using default\n", option);
  575. break;
  576. }
  577. opts->uid = option;
  578. opts->use_uid = 1;
  579. break;
  580. case Opt_gid:
  581. if (match_int(&args[0], &option))
  582. return 0;
  583. if (option < 0) {
  584. printk(KERN_ERR "BeFS: Invalid gid %d, "
  585. "using default\n", option);
  586. break;
  587. }
  588. opts->gid = option;
  589. opts->use_gid = 1;
  590. break;
  591. case Opt_charset:
  592. kfree(opts->iocharset);
  593. opts->iocharset = match_strdup(&args[0]);
  594. if (!opts->iocharset) {
  595. printk(KERN_ERR "BeFS: allocation failure for "
  596. "iocharset string\n");
  597. return 0;
  598. }
  599. break;
  600. case Opt_debug:
  601. opts->debug = 1;
  602. break;
  603. default:
  604. printk(KERN_ERR "BeFS: Unrecognized mount option \"%s\" "
  605. "or missing value\n", p);
  606. return 0;
  607. }
  608. }
  609. return 1;
  610. }
  611. /* This function has the responsibiltiy of getting the
  612. * filesystem ready for unmounting.
  613. * Basicly, we free everything that we allocated in
  614. * befs_read_inode
  615. */
  616. static void
  617. befs_put_super(struct super_block *sb)
  618. {
  619. if (BEFS_SB(sb)->mount_opts.iocharset) {
  620. kfree(BEFS_SB(sb)->mount_opts.iocharset);
  621. BEFS_SB(sb)->mount_opts.iocharset = NULL;
  622. }
  623. if (BEFS_SB(sb)->nls) {
  624. unload_nls(BEFS_SB(sb)->nls);
  625. BEFS_SB(sb)->nls = NULL;
  626. }
  627. if (sb->s_fs_info) {
  628. kfree(sb->s_fs_info);
  629. sb->s_fs_info = NULL;
  630. }
  631. return;
  632. }
  633. /* Allocate private field of the superblock, fill it.
  634. *
  635. * Finish filling the public superblock fields
  636. * Make the root directory
  637. * Load a set of NLS translations if needed.
  638. */
  639. static int
  640. befs_fill_super(struct super_block *sb, void *data, int silent)
  641. {
  642. struct buffer_head *bh;
  643. befs_sb_info *befs_sb;
  644. befs_super_block *disk_sb;
  645. struct inode *root;
  646. const unsigned long sb_block = 0;
  647. const off_t x86_sb_off = 512;
  648. sb->s_fs_info = kmalloc(sizeof (*befs_sb), GFP_KERNEL);
  649. if (sb->s_fs_info == NULL) {
  650. printk(KERN_ERR
  651. "BeFS(%s): Unable to allocate memory for private "
  652. "portion of superblock. Bailing.\n", sb->s_id);
  653. goto unaquire_none;
  654. }
  655. befs_sb = BEFS_SB(sb);
  656. memset(befs_sb, 0, sizeof(befs_sb_info));
  657. if (!parse_options((char *) data, &befs_sb->mount_opts)) {
  658. befs_error(sb, "cannot parse mount options");
  659. goto unaquire_priv_sbp;
  660. }
  661. befs_debug(sb, "---> befs_fill_super()");
  662. #ifndef CONFIG_BEFS_RW
  663. if (!(sb->s_flags & MS_RDONLY)) {
  664. befs_warning(sb,
  665. "No write support. Marking filesystem read-only");
  666. sb->s_flags |= MS_RDONLY;
  667. }
  668. #endif /* CONFIG_BEFS_RW */
  669. /*
  670. * Set dummy blocksize to read super block.
  671. * Will be set to real fs blocksize later.
  672. *
  673. * Linux 2.4.10 and later refuse to read blocks smaller than
  674. * the hardsect size for the device. But we also need to read at
  675. * least 1k to get the second 512 bytes of the volume.
  676. * -WD 10-26-01
  677. */
  678. sb_min_blocksize(sb, 1024);
  679. if (!(bh = sb_bread(sb, sb_block))) {
  680. befs_error(sb, "unable to read superblock");
  681. goto unaquire_priv_sbp;
  682. }
  683. /* account for offset of super block on x86 */
  684. disk_sb = (befs_super_block *) bh->b_data;
  685. if ((le32_to_cpu(disk_sb->magic1) == BEFS_SUPER_MAGIC1) ||
  686. (be32_to_cpu(disk_sb->magic1) == BEFS_SUPER_MAGIC1)) {
  687. befs_debug(sb, "Using PPC superblock location");
  688. } else {
  689. befs_debug(sb, "Using x86 superblock location");
  690. disk_sb =
  691. (befs_super_block *) ((void *) bh->b_data + x86_sb_off);
  692. }
  693. if (befs_load_sb(sb, disk_sb) != BEFS_OK)
  694. goto unaquire_bh;
  695. befs_dump_super_block(sb, disk_sb);
  696. brelse(bh);
  697. if (befs_check_sb(sb) != BEFS_OK)
  698. goto unaquire_priv_sbp;
  699. if( befs_sb->num_blocks > ~((sector_t)0) ) {
  700. befs_error(sb, "blocks count: %Lu "
  701. "is larger than the host can use",
  702. befs_sb->num_blocks);
  703. goto unaquire_priv_sbp;
  704. }
  705. /*
  706. * set up enough so that it can read an inode
  707. * Fill in kernel superblock fields from private sb
  708. */
  709. sb->s_magic = BEFS_SUPER_MAGIC;
  710. /* Set real blocksize of fs */
  711. sb_set_blocksize(sb, (ulong) befs_sb->block_size);
  712. sb->s_op = (struct super_operations *) &befs_sops;
  713. root = iget(sb, iaddr2blockno(sb, &(befs_sb->root_dir)));
  714. sb->s_root = d_alloc_root(root);
  715. if (!sb->s_root) {
  716. iput(root);
  717. befs_error(sb, "get root inode failed");
  718. goto unaquire_priv_sbp;
  719. }
  720. /* load nls library */
  721. if (befs_sb->mount_opts.iocharset) {
  722. befs_debug(sb, "Loading nls: %s",
  723. befs_sb->mount_opts.iocharset);
  724. befs_sb->nls = load_nls(befs_sb->mount_opts.iocharset);
  725. if (!befs_sb->nls) {
  726. befs_warning(sb, "Cannot load nls %s"
  727. " loading default nls",
  728. befs_sb->mount_opts.iocharset);
  729. befs_sb->nls = load_nls_default();
  730. }
  731. /* load default nls if none is specified in mount options */
  732. } else {
  733. befs_debug(sb, "Loading default nls");
  734. befs_sb->nls = load_nls_default();
  735. }
  736. return 0;
  737. /*****************/
  738. unaquire_bh:
  739. brelse(bh);
  740. unaquire_priv_sbp:
  741. kfree(sb->s_fs_info);
  742. unaquire_none:
  743. sb->s_fs_info = NULL;
  744. return -EINVAL;
  745. }
  746. static int
  747. befs_remount(struct super_block *sb, int *flags, char *data)
  748. {
  749. if (!(*flags & MS_RDONLY))
  750. return -EINVAL;
  751. return 0;
  752. }
  753. static int
  754. befs_statfs(struct super_block *sb, struct kstatfs *buf)
  755. {
  756. befs_debug(sb, "---> befs_statfs()");
  757. buf->f_type = BEFS_SUPER_MAGIC;
  758. buf->f_bsize = sb->s_blocksize;
  759. buf->f_blocks = BEFS_SB(sb)->num_blocks;
  760. buf->f_bfree = BEFS_SB(sb)->num_blocks - BEFS_SB(sb)->used_blocks;
  761. buf->f_bavail = buf->f_bfree;
  762. buf->f_files = 0; /* UNKNOWN */
  763. buf->f_ffree = 0; /* UNKNOWN */
  764. buf->f_namelen = BEFS_NAME_LEN;
  765. befs_debug(sb, "<--- befs_statfs()");
  766. return 0;
  767. }
  768. static struct super_block *
  769. befs_get_sb(struct file_system_type *fs_type, int flags, const char *dev_name,
  770. void *data)
  771. {
  772. return get_sb_bdev(fs_type, flags, dev_name, data, befs_fill_super);
  773. }
  774. static struct file_system_type befs_fs_type = {
  775. .owner = THIS_MODULE,
  776. .name = "befs",
  777. .get_sb = befs_get_sb,
  778. .kill_sb = kill_block_super,
  779. .fs_flags = FS_REQUIRES_DEV,
  780. };
  781. static int __init
  782. init_befs_fs(void)
  783. {
  784. int err;
  785. printk(KERN_INFO "BeFS version: %s\n", BEFS_VERSION);
  786. err = befs_init_inodecache();
  787. if (err)
  788. goto unaquire_none;
  789. err = register_filesystem(&befs_fs_type);
  790. if (err)
  791. goto unaquire_inodecache;
  792. return 0;
  793. unaquire_inodecache:
  794. befs_destroy_inodecache();
  795. unaquire_none:
  796. return err;
  797. }
  798. static void __exit
  799. exit_befs_fs(void)
  800. {
  801. befs_destroy_inodecache();
  802. unregister_filesystem(&befs_fs_type);
  803. }
  804. /*
  805. Macros that typecheck the init and exit functions,
  806. ensures that they are called at init and cleanup,
  807. and eliminates warnings about unused functions.
  808. */
  809. module_init(init_befs_fs)
  810. module_exit(exit_befs_fs)