linuxvfs.c 24 KB

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