linuxvfs.c 24 KB

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