linuxvfs.c 25 KB

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