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