linuxvfs.c 24 KB

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