linuxvfs.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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 struct inode *befs_iget(struct super_block *, unsigned long);
  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. .alloc_inode = befs_alloc_inode, /* allocate a new inode */
  50. .destroy_inode = befs_destroy_inode, /* deallocate an inode */
  51. .put_super = befs_put_super, /* uninit super */
  52. .statfs = befs_statfs, /* statfs */
  53. .remount_fs = befs_remount,
  54. .show_options = generic_show_options,
  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. .llseek = generic_file_llseek,
  62. };
  63. static const struct inode_operations befs_dir_inode_operations = {
  64. .lookup = befs_lookup,
  65. };
  66. static const struct address_space_operations befs_aops = {
  67. .readpage = befs_readpage,
  68. .sync_page = block_sync_page,
  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, struct nameidata *nd)
  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 = filp->f_path.dentry->d_inode;
  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. INIT_LIST_HEAD(&inode->i_dentry);
  244. kmem_cache_free(befs_inode_cachep, BEFS_I(inode));
  245. }
  246. static void befs_destroy_inode(struct inode *inode)
  247. {
  248. call_rcu(&inode->i_rcu, befs_i_callback);
  249. }
  250. static void init_once(void *foo)
  251. {
  252. struct befs_inode_info *bi = (struct befs_inode_info *) foo;
  253. inode_init_once(&bi->vfs_inode);
  254. }
  255. static struct inode *befs_iget(struct super_block *sb, unsigned long ino)
  256. {
  257. struct buffer_head *bh = NULL;
  258. befs_inode *raw_inode = NULL;
  259. befs_sb_info *befs_sb = BEFS_SB(sb);
  260. befs_inode_info *befs_ino = NULL;
  261. struct inode *inode;
  262. long ret = -EIO;
  263. befs_debug(sb, "---> befs_read_inode() " "inode = %lu", ino);
  264. inode = iget_locked(sb, ino);
  265. if (IS_ERR(inode))
  266. return inode;
  267. if (!(inode->i_state & I_NEW))
  268. return inode;
  269. befs_ino = BEFS_I(inode);
  270. /* convert from vfs's inode number to befs's inode number */
  271. befs_ino->i_inode_num = blockno2iaddr(sb, inode->i_ino);
  272. befs_debug(sb, " real inode number [%u, %hu, %hu]",
  273. befs_ino->i_inode_num.allocation_group,
  274. befs_ino->i_inode_num.start, befs_ino->i_inode_num.len);
  275. bh = befs_bread(sb, inode->i_ino);
  276. if (!bh) {
  277. befs_error(sb, "unable to read inode block - "
  278. "inode = %lu", inode->i_ino);
  279. goto unacquire_none;
  280. }
  281. raw_inode = (befs_inode *) bh->b_data;
  282. befs_dump_inode(sb, raw_inode);
  283. if (befs_check_inode(sb, raw_inode, inode->i_ino) != BEFS_OK) {
  284. befs_error(sb, "Bad inode: %lu", inode->i_ino);
  285. goto unacquire_bh;
  286. }
  287. inode->i_mode = (umode_t) fs32_to_cpu(sb, raw_inode->mode);
  288. /*
  289. * set uid and gid. But since current BeOS is single user OS, so
  290. * you can change by "uid" or "gid" options.
  291. */
  292. inode->i_uid = befs_sb->mount_opts.use_uid ?
  293. befs_sb->mount_opts.uid : (uid_t) fs32_to_cpu(sb, raw_inode->uid);
  294. inode->i_gid = befs_sb->mount_opts.use_gid ?
  295. befs_sb->mount_opts.gid : (gid_t) fs32_to_cpu(sb, raw_inode->gid);
  296. inode->i_nlink = 1;
  297. /*
  298. * BEFS's time is 64 bits, but current VFS is 32 bits...
  299. * BEFS don't have access time. Nor inode change time. VFS
  300. * doesn't have creation time.
  301. * Also, the lower 16 bits of the last_modified_time and
  302. * create_time are just a counter to help ensure uniqueness
  303. * for indexing purposes. (PFD, page 54)
  304. */
  305. inode->i_mtime.tv_sec =
  306. fs64_to_cpu(sb, raw_inode->last_modified_time) >> 16;
  307. inode->i_mtime.tv_nsec = 0; /* lower 16 bits are not a time */
  308. inode->i_ctime = inode->i_mtime;
  309. inode->i_atime = inode->i_mtime;
  310. befs_ino->i_inode_num = fsrun_to_cpu(sb, raw_inode->inode_num);
  311. befs_ino->i_parent = fsrun_to_cpu(sb, raw_inode->parent);
  312. befs_ino->i_attribute = fsrun_to_cpu(sb, raw_inode->attributes);
  313. befs_ino->i_flags = fs32_to_cpu(sb, raw_inode->flags);
  314. if (S_ISLNK(inode->i_mode) && !(befs_ino->i_flags & BEFS_LONG_SYMLINK)){
  315. inode->i_size = 0;
  316. inode->i_blocks = befs_sb->block_size / VFS_BLOCK_SIZE;
  317. strncpy(befs_ino->i_data.symlink, raw_inode->data.symlink,
  318. BEFS_SYMLINK_LEN - 1);
  319. befs_ino->i_data.symlink[BEFS_SYMLINK_LEN - 1] = '\0';
  320. } else {
  321. int num_blks;
  322. befs_ino->i_data.ds =
  323. fsds_to_cpu(sb, &raw_inode->data.datastream);
  324. num_blks = befs_count_blocks(sb, &befs_ino->i_data.ds);
  325. inode->i_blocks =
  326. num_blks * (befs_sb->block_size / VFS_BLOCK_SIZE);
  327. inode->i_size = befs_ino->i_data.ds.size;
  328. }
  329. inode->i_mapping->a_ops = &befs_aops;
  330. if (S_ISREG(inode->i_mode)) {
  331. inode->i_fop = &generic_ro_fops;
  332. } else if (S_ISDIR(inode->i_mode)) {
  333. inode->i_op = &befs_dir_inode_operations;
  334. inode->i_fop = &befs_dir_operations;
  335. } else if (S_ISLNK(inode->i_mode)) {
  336. inode->i_op = &befs_symlink_inode_operations;
  337. } else {
  338. befs_error(sb, "Inode %lu is not a regular file, "
  339. "directory or symlink. THAT IS WRONG! BeFS has no "
  340. "on disk special files", inode->i_ino);
  341. goto unacquire_bh;
  342. }
  343. brelse(bh);
  344. befs_debug(sb, "<--- befs_read_inode()");
  345. unlock_new_inode(inode);
  346. return inode;
  347. unacquire_bh:
  348. brelse(bh);
  349. unacquire_none:
  350. iget_failed(inode);
  351. befs_debug(sb, "<--- befs_read_inode() - Bad inode");
  352. return ERR_PTR(ret);
  353. }
  354. /* Initialize the inode cache. Called at fs setup.
  355. *
  356. * Taken from NFS implementation by Al Viro.
  357. */
  358. static int
  359. befs_init_inodecache(void)
  360. {
  361. befs_inode_cachep = kmem_cache_create("befs_inode_cache",
  362. sizeof (struct befs_inode_info),
  363. 0, (SLAB_RECLAIM_ACCOUNT|
  364. SLAB_MEM_SPREAD),
  365. init_once);
  366. if (befs_inode_cachep == NULL) {
  367. printk(KERN_ERR "befs_init_inodecache: "
  368. "Couldn't initialize inode slabcache\n");
  369. return -ENOMEM;
  370. }
  371. return 0;
  372. }
  373. /* Called at fs teardown.
  374. *
  375. * Taken from NFS implementation by Al Viro.
  376. */
  377. static void
  378. befs_destroy_inodecache(void)
  379. {
  380. kmem_cache_destroy(befs_inode_cachep);
  381. }
  382. /*
  383. * The inode of symbolic link is different to data stream.
  384. * The data stream become link name. Unless the LONG_SYMLINK
  385. * flag is set.
  386. */
  387. static void *
  388. befs_follow_link(struct dentry *dentry, struct nameidata *nd)
  389. {
  390. befs_inode_info *befs_ino = BEFS_I(dentry->d_inode);
  391. char *link;
  392. if (befs_ino->i_flags & BEFS_LONG_SYMLINK) {
  393. struct super_block *sb = dentry->d_sb;
  394. befs_data_stream *data = &befs_ino->i_data.ds;
  395. befs_off_t len = data->size;
  396. befs_debug(sb, "Follow long symlink");
  397. link = kmalloc(len, GFP_NOFS);
  398. if (!link) {
  399. link = ERR_PTR(-ENOMEM);
  400. } else if (befs_read_lsymlink(sb, data, link, len) != len) {
  401. kfree(link);
  402. befs_error(sb, "Failed to read entire long symlink");
  403. link = ERR_PTR(-EIO);
  404. } else {
  405. link[len - 1] = '\0';
  406. }
  407. } else {
  408. link = befs_ino->i_data.symlink;
  409. }
  410. nd_set_link(nd, link);
  411. return NULL;
  412. }
  413. static void befs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
  414. {
  415. befs_inode_info *befs_ino = BEFS_I(dentry->d_inode);
  416. if (befs_ino->i_flags & BEFS_LONG_SYMLINK) {
  417. char *link = nd_get_link(nd);
  418. if (!IS_ERR(link))
  419. kfree(link);
  420. }
  421. }
  422. /*
  423. * UTF-8 to NLS charset convert routine
  424. *
  425. *
  426. * Changed 8/10/01 by Will Dyson. Now use uni2char() / char2uni() rather than
  427. * the nls tables directly
  428. */
  429. static int
  430. befs_utf2nls(struct super_block *sb, const char *in,
  431. int in_len, char **out, int *out_len)
  432. {
  433. struct nls_table *nls = BEFS_SB(sb)->nls;
  434. int i, o;
  435. unicode_t uni;
  436. int unilen, utflen;
  437. char *result;
  438. /* The utf8->nls conversion won't make the final nls string bigger
  439. * than the utf one, but if the string is pure ascii they'll have the
  440. * same width and an extra char is needed to save the additional \0
  441. */
  442. int maxlen = in_len + 1;
  443. befs_debug(sb, "---> utf2nls()");
  444. if (!nls) {
  445. befs_error(sb, "befs_utf2nls called with no NLS table loaded");
  446. return -EINVAL;
  447. }
  448. *out = result = kmalloc(maxlen, GFP_NOFS);
  449. if (!*out) {
  450. befs_error(sb, "befs_utf2nls() cannot allocate memory");
  451. *out_len = 0;
  452. return -ENOMEM;
  453. }
  454. for (i = o = 0; i < in_len; i += utflen, o += unilen) {
  455. /* convert from UTF-8 to Unicode */
  456. utflen = utf8_to_utf32(&in[i], in_len - i, &uni);
  457. if (utflen < 0)
  458. goto conv_err;
  459. /* convert from Unicode to nls */
  460. if (uni > MAX_WCHAR_T)
  461. goto conv_err;
  462. unilen = nls->uni2char(uni, &result[o], in_len - o);
  463. if (unilen < 0)
  464. goto conv_err;
  465. }
  466. result[o] = '\0';
  467. *out_len = o;
  468. befs_debug(sb, "<--- utf2nls()");
  469. return o;
  470. conv_err:
  471. befs_error(sb, "Name using character set %s contains a character that "
  472. "cannot be converted to unicode.", nls->charset);
  473. befs_debug(sb, "<--- utf2nls()");
  474. kfree(result);
  475. return -EILSEQ;
  476. }
  477. /**
  478. * befs_nls2utf - Convert NLS string to utf8 encodeing
  479. * @sb: Superblock
  480. * @src: Input string buffer in NLS format
  481. * @srclen: Length of input string in bytes
  482. * @dest: The output string in UTF-8 format
  483. * @destlen: Length of the output buffer
  484. *
  485. * Converts input string @src, which is in the format of the loaded NLS map,
  486. * into a utf8 string.
  487. *
  488. * The destination string @dest is allocated by this function and the caller is
  489. * responsible for freeing it with kfree()
  490. *
  491. * On return, *@destlen is the length of @dest in bytes.
  492. *
  493. * On success, the return value is the number of utf8 characters written to
  494. * the output buffer @dest.
  495. *
  496. * On Failure, a negative number coresponding to the error code is returned.
  497. */
  498. static int
  499. befs_nls2utf(struct super_block *sb, const char *in,
  500. int in_len, char **out, int *out_len)
  501. {
  502. struct nls_table *nls = BEFS_SB(sb)->nls;
  503. int i, o;
  504. wchar_t uni;
  505. int unilen, utflen;
  506. char *result;
  507. /* There're nls characters that will translate to 3-chars-wide UTF-8
  508. * characters, a additional byte is needed to save the final \0
  509. * in special cases */
  510. int maxlen = (3 * in_len) + 1;
  511. befs_debug(sb, "---> nls2utf()\n");
  512. if (!nls) {
  513. befs_error(sb, "befs_nls2utf called with no NLS table loaded.");
  514. return -EINVAL;
  515. }
  516. *out = result = kmalloc(maxlen, GFP_NOFS);
  517. if (!*out) {
  518. befs_error(sb, "befs_nls2utf() cannot allocate memory");
  519. *out_len = 0;
  520. return -ENOMEM;
  521. }
  522. for (i = o = 0; i < in_len; i += unilen, o += utflen) {
  523. /* convert from nls to unicode */
  524. unilen = nls->char2uni(&in[i], in_len - i, &uni);
  525. if (unilen < 0)
  526. goto conv_err;
  527. /* convert from unicode to UTF-8 */
  528. utflen = utf32_to_utf8(uni, &result[o], 3);
  529. if (utflen <= 0)
  530. goto conv_err;
  531. }
  532. result[o] = '\0';
  533. *out_len = o;
  534. befs_debug(sb, "<--- nls2utf()");
  535. return i;
  536. conv_err:
  537. befs_error(sb, "Name using charecter set %s contains a charecter that "
  538. "cannot be converted to unicode.", nls->charset);
  539. befs_debug(sb, "<--- nls2utf()");
  540. kfree(result);
  541. return -EILSEQ;
  542. }
  543. /**
  544. * Use the
  545. *
  546. */
  547. enum {
  548. Opt_uid, Opt_gid, Opt_charset, Opt_debug, Opt_err,
  549. };
  550. static const match_table_t befs_tokens = {
  551. {Opt_uid, "uid=%d"},
  552. {Opt_gid, "gid=%d"},
  553. {Opt_charset, "iocharset=%s"},
  554. {Opt_debug, "debug"},
  555. {Opt_err, NULL}
  556. };
  557. static int
  558. parse_options(char *options, befs_mount_options * opts)
  559. {
  560. char *p;
  561. substring_t args[MAX_OPT_ARGS];
  562. int option;
  563. /* Initialize options */
  564. opts->uid = 0;
  565. opts->gid = 0;
  566. opts->use_uid = 0;
  567. opts->use_gid = 0;
  568. opts->iocharset = NULL;
  569. opts->debug = 0;
  570. if (!options)
  571. return 1;
  572. while ((p = strsep(&options, ",")) != NULL) {
  573. int token;
  574. if (!*p)
  575. continue;
  576. token = match_token(p, befs_tokens, args);
  577. switch (token) {
  578. case Opt_uid:
  579. if (match_int(&args[0], &option))
  580. return 0;
  581. if (option < 0) {
  582. printk(KERN_ERR "BeFS: Invalid uid %d, "
  583. "using default\n", option);
  584. break;
  585. }
  586. opts->uid = option;
  587. opts->use_uid = 1;
  588. break;
  589. case Opt_gid:
  590. if (match_int(&args[0], &option))
  591. return 0;
  592. if (option < 0) {
  593. printk(KERN_ERR "BeFS: Invalid gid %d, "
  594. "using default\n", option);
  595. break;
  596. }
  597. opts->gid = option;
  598. opts->use_gid = 1;
  599. break;
  600. case Opt_charset:
  601. kfree(opts->iocharset);
  602. opts->iocharset = match_strdup(&args[0]);
  603. if (!opts->iocharset) {
  604. printk(KERN_ERR "BeFS: allocation failure for "
  605. "iocharset string\n");
  606. return 0;
  607. }
  608. break;
  609. case Opt_debug:
  610. opts->debug = 1;
  611. break;
  612. default:
  613. printk(KERN_ERR "BeFS: Unrecognized mount option \"%s\" "
  614. "or missing value\n", p);
  615. return 0;
  616. }
  617. }
  618. return 1;
  619. }
  620. /* This function has the responsibiltiy of getting the
  621. * filesystem ready for unmounting.
  622. * Basicly, we free everything that we allocated in
  623. * befs_read_inode
  624. */
  625. static void
  626. befs_put_super(struct super_block *sb)
  627. {
  628. kfree(BEFS_SB(sb)->mount_opts.iocharset);
  629. BEFS_SB(sb)->mount_opts.iocharset = NULL;
  630. unload_nls(BEFS_SB(sb)->nls);
  631. kfree(sb->s_fs_info);
  632. sb->s_fs_info = NULL;
  633. }
  634. /* Allocate private field of the superblock, fill it.
  635. *
  636. * Finish filling the public superblock fields
  637. * Make the root directory
  638. * Load a set of NLS translations if needed.
  639. */
  640. static int
  641. befs_fill_super(struct super_block *sb, void *data, int silent)
  642. {
  643. struct buffer_head *bh;
  644. befs_sb_info *befs_sb;
  645. befs_super_block *disk_sb;
  646. struct inode *root;
  647. long ret = -EINVAL;
  648. const unsigned long sb_block = 0;
  649. const off_t x86_sb_off = 512;
  650. save_mount_options(sb, data);
  651. sb->s_fs_info = kmalloc(sizeof (*befs_sb), GFP_KERNEL);
  652. if (sb->s_fs_info == NULL) {
  653. printk(KERN_ERR
  654. "BeFS(%s): Unable to allocate memory for private "
  655. "portion of superblock. Bailing.\n", sb->s_id);
  656. goto unacquire_none;
  657. }
  658. befs_sb = BEFS_SB(sb);
  659. memset(befs_sb, 0, sizeof(befs_sb_info));
  660. if (!parse_options((char *) data, &befs_sb->mount_opts)) {
  661. befs_error(sb, "cannot parse mount options");
  662. goto unacquire_priv_sbp;
  663. }
  664. befs_debug(sb, "---> befs_fill_super()");
  665. #ifndef CONFIG_BEFS_RW
  666. if (!(sb->s_flags & MS_RDONLY)) {
  667. befs_warning(sb,
  668. "No write support. Marking filesystem read-only");
  669. sb->s_flags |= MS_RDONLY;
  670. }
  671. #endif /* CONFIG_BEFS_RW */
  672. /*
  673. * Set dummy blocksize to read super block.
  674. * Will be set to real fs blocksize later.
  675. *
  676. * Linux 2.4.10 and later refuse to read blocks smaller than
  677. * the hardsect size for the device. But we also need to read at
  678. * least 1k to get the second 512 bytes of the volume.
  679. * -WD 10-26-01
  680. */
  681. sb_min_blocksize(sb, 1024);
  682. if (!(bh = sb_bread(sb, sb_block))) {
  683. befs_error(sb, "unable to read superblock");
  684. goto unacquire_priv_sbp;
  685. }
  686. /* account for offset of super block on x86 */
  687. disk_sb = (befs_super_block *) bh->b_data;
  688. if ((disk_sb->magic1 == BEFS_SUPER_MAGIC1_LE) ||
  689. (disk_sb->magic1 == BEFS_SUPER_MAGIC1_BE)) {
  690. befs_debug(sb, "Using PPC superblock location");
  691. } else {
  692. befs_debug(sb, "Using x86 superblock location");
  693. disk_sb =
  694. (befs_super_block *) ((void *) bh->b_data + x86_sb_off);
  695. }
  696. if (befs_load_sb(sb, disk_sb) != BEFS_OK)
  697. goto unacquire_bh;
  698. befs_dump_super_block(sb, disk_sb);
  699. brelse(bh);
  700. if (befs_check_sb(sb) != BEFS_OK)
  701. goto unacquire_priv_sbp;
  702. if( befs_sb->num_blocks > ~((sector_t)0) ) {
  703. befs_error(sb, "blocks count: %Lu "
  704. "is larger than the host can use",
  705. befs_sb->num_blocks);
  706. goto unacquire_priv_sbp;
  707. }
  708. /*
  709. * set up enough so that it can read an inode
  710. * Fill in kernel superblock fields from private sb
  711. */
  712. sb->s_magic = BEFS_SUPER_MAGIC;
  713. /* Set real blocksize of fs */
  714. sb_set_blocksize(sb, (ulong) befs_sb->block_size);
  715. sb->s_op = &befs_sops;
  716. root = befs_iget(sb, iaddr2blockno(sb, &(befs_sb->root_dir)));
  717. if (IS_ERR(root)) {
  718. ret = PTR_ERR(root);
  719. goto unacquire_priv_sbp;
  720. }
  721. sb->s_root = d_alloc_root(root);
  722. if (!sb->s_root) {
  723. iput(root);
  724. befs_error(sb, "get root inode failed");
  725. goto unacquire_priv_sbp;
  726. }
  727. /* load nls library */
  728. if (befs_sb->mount_opts.iocharset) {
  729. befs_debug(sb, "Loading nls: %s",
  730. befs_sb->mount_opts.iocharset);
  731. befs_sb->nls = load_nls(befs_sb->mount_opts.iocharset);
  732. if (!befs_sb->nls) {
  733. befs_warning(sb, "Cannot load nls %s"
  734. " loading default nls",
  735. befs_sb->mount_opts.iocharset);
  736. befs_sb->nls = load_nls_default();
  737. }
  738. /* load default nls if none is specified in mount options */
  739. } else {
  740. befs_debug(sb, "Loading default nls");
  741. befs_sb->nls = load_nls_default();
  742. }
  743. return 0;
  744. /*****************/
  745. unacquire_bh:
  746. brelse(bh);
  747. unacquire_priv_sbp:
  748. kfree(befs_sb->mount_opts.iocharset);
  749. kfree(sb->s_fs_info);
  750. unacquire_none:
  751. sb->s_fs_info = NULL;
  752. return ret;
  753. }
  754. static int
  755. befs_remount(struct super_block *sb, int *flags, char *data)
  756. {
  757. if (!(*flags & MS_RDONLY))
  758. return -EINVAL;
  759. return 0;
  760. }
  761. static int
  762. befs_statfs(struct dentry *dentry, struct kstatfs *buf)
  763. {
  764. struct super_block *sb = dentry->d_sb;
  765. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  766. befs_debug(sb, "---> befs_statfs()");
  767. buf->f_type = BEFS_SUPER_MAGIC;
  768. buf->f_bsize = sb->s_blocksize;
  769. buf->f_blocks = BEFS_SB(sb)->num_blocks;
  770. buf->f_bfree = BEFS_SB(sb)->num_blocks - BEFS_SB(sb)->used_blocks;
  771. buf->f_bavail = buf->f_bfree;
  772. buf->f_files = 0; /* UNKNOWN */
  773. buf->f_ffree = 0; /* UNKNOWN */
  774. buf->f_fsid.val[0] = (u32)id;
  775. buf->f_fsid.val[1] = (u32)(id >> 32);
  776. buf->f_namelen = BEFS_NAME_LEN;
  777. befs_debug(sb, "<--- befs_statfs()");
  778. return 0;
  779. }
  780. static struct dentry *
  781. befs_mount(struct file_system_type *fs_type, int flags, const char *dev_name,
  782. void *data)
  783. {
  784. return mount_bdev(fs_type, flags, dev_name, data, befs_fill_super);
  785. }
  786. static struct file_system_type befs_fs_type = {
  787. .owner = THIS_MODULE,
  788. .name = "befs",
  789. .mount = befs_mount,
  790. .kill_sb = kill_block_super,
  791. .fs_flags = FS_REQUIRES_DEV,
  792. };
  793. static int __init
  794. init_befs_fs(void)
  795. {
  796. int err;
  797. printk(KERN_INFO "BeFS version: %s\n", BEFS_VERSION);
  798. err = befs_init_inodecache();
  799. if (err)
  800. goto unacquire_none;
  801. err = register_filesystem(&befs_fs_type);
  802. if (err)
  803. goto unacquire_inodecache;
  804. return 0;
  805. unacquire_inodecache:
  806. befs_destroy_inodecache();
  807. unacquire_none:
  808. return err;
  809. }
  810. static void __exit
  811. exit_befs_fs(void)
  812. {
  813. befs_destroy_inodecache();
  814. unregister_filesystem(&befs_fs_type);
  815. }
  816. /*
  817. Macros that typecheck the init and exit functions,
  818. ensures that they are called at init and cleanup,
  819. and eliminates warnings about unused functions.
  820. */
  821. module_init(init_befs_fs)
  822. module_exit(exit_befs_fs)