linuxvfs.c 24 KB

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