linuxvfs.c 25 KB

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