linuxvfs.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/befs/linuxvfs.c
  4. *
  5. * Copyright (C) 2001 Will Dyson <will_dyson@pobox.com
  6. *
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/fs.h>
  12. #include <linux/errno.h>
  13. #include <linux/stat.h>
  14. #include <linux/nls.h>
  15. #include <linux/buffer_head.h>
  16. #include <linux/vfs.h>
  17. #include <linux/parser.h>
  18. #include <linux/namei.h>
  19. #include <linux/sched.h>
  20. #include <linux/cred.h>
  21. #include <linux/exportfs.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/blkdev.h>
  24. #include "befs.h"
  25. #include "btree.h"
  26. #include "inode.h"
  27. #include "datastream.h"
  28. #include "super.h"
  29. #include "io.h"
  30. MODULE_DESCRIPTION("BeOS File System (BeFS) driver");
  31. MODULE_AUTHOR("Will Dyson");
  32. MODULE_LICENSE("GPL");
  33. MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
  34. /* The units the vfs expects inode->i_blocks to be in */
  35. #define VFS_BLOCK_SIZE 512
  36. static int befs_readdir(struct file *, struct dir_context *);
  37. static int befs_get_block(struct inode *, sector_t, struct buffer_head *, int);
  38. static int befs_readpage(struct file *file, struct page *page);
  39. static sector_t befs_bmap(struct address_space *mapping, sector_t block);
  40. static struct dentry *befs_lookup(struct inode *, struct dentry *,
  41. unsigned int);
  42. static struct inode *befs_iget(struct super_block *, unsigned long);
  43. static struct inode *befs_alloc_inode(struct super_block *sb);
  44. static void befs_free_inode(struct inode *inode);
  45. static void befs_destroy_inodecache(void);
  46. static int befs_symlink_readpage(struct file *, struct page *);
  47. static int befs_utf2nls(struct super_block *sb, const char *in, int in_len,
  48. char **out, int *out_len);
  49. static int befs_nls2utf(struct super_block *sb, const char *in, int in_len,
  50. char **out, int *out_len);
  51. static void befs_put_super(struct super_block *);
  52. static int befs_remount(struct super_block *, int *, char *);
  53. static int befs_statfs(struct dentry *, struct kstatfs *);
  54. static int befs_show_options(struct seq_file *, struct dentry *);
  55. static int parse_options(char *, struct befs_mount_options *);
  56. static struct dentry *befs_fh_to_dentry(struct super_block *sb,
  57. struct fid *fid, int fh_len, int fh_type);
  58. static struct dentry *befs_fh_to_parent(struct super_block *sb,
  59. struct fid *fid, int fh_len, int fh_type);
  60. static struct dentry *befs_get_parent(struct dentry *child);
  61. static const struct super_operations befs_sops = {
  62. .alloc_inode = befs_alloc_inode, /* allocate a new inode */
  63. .free_inode = befs_free_inode, /* deallocate an inode */
  64. .put_super = befs_put_super, /* uninit super */
  65. .statfs = befs_statfs, /* statfs */
  66. .remount_fs = befs_remount,
  67. .show_options = befs_show_options,
  68. };
  69. /* slab cache for befs_inode_info objects */
  70. static struct kmem_cache *befs_inode_cachep;
  71. static const struct file_operations befs_dir_operations = {
  72. .read = generic_read_dir,
  73. .iterate_shared = befs_readdir,
  74. .llseek = generic_file_llseek,
  75. };
  76. static const struct inode_operations befs_dir_inode_operations = {
  77. .lookup = befs_lookup,
  78. };
  79. static const struct address_space_operations befs_aops = {
  80. .readpage = befs_readpage,
  81. .bmap = befs_bmap,
  82. };
  83. static const struct address_space_operations befs_symlink_aops = {
  84. .readpage = befs_symlink_readpage,
  85. };
  86. static const struct export_operations befs_export_operations = {
  87. .fh_to_dentry = befs_fh_to_dentry,
  88. .fh_to_parent = befs_fh_to_parent,
  89. .get_parent = befs_get_parent,
  90. };
  91. /*
  92. * Called by generic_file_read() to read a page of data
  93. *
  94. * In turn, simply calls a generic block read function and
  95. * passes it the address of befs_get_block, for mapping file
  96. * positions to disk blocks.
  97. */
  98. static int
  99. befs_readpage(struct file *file, struct page *page)
  100. {
  101. return block_read_full_page(page, befs_get_block);
  102. }
  103. static sector_t
  104. befs_bmap(struct address_space *mapping, sector_t block)
  105. {
  106. return generic_block_bmap(mapping, block, befs_get_block);
  107. }
  108. /*
  109. * Generic function to map a file position (block) to a
  110. * disk offset (passed back in bh_result).
  111. *
  112. * Used by many higher level functions.
  113. *
  114. * Calls befs_fblock2brun() in datastream.c to do the real work.
  115. */
  116. static int
  117. befs_get_block(struct inode *inode, sector_t block,
  118. struct buffer_head *bh_result, int create)
  119. {
  120. struct super_block *sb = inode->i_sb;
  121. befs_data_stream *ds = &BEFS_I(inode)->i_data.ds;
  122. befs_block_run run = BAD_IADDR;
  123. int res;
  124. ulong disk_off;
  125. befs_debug(sb, "---> befs_get_block() for inode %lu, block %ld",
  126. (unsigned long)inode->i_ino, (long)block);
  127. if (create) {
  128. befs_error(sb, "befs_get_block() was asked to write to "
  129. "block %ld in inode %lu", (long)block,
  130. (unsigned long)inode->i_ino);
  131. return -EPERM;
  132. }
  133. res = befs_fblock2brun(sb, ds, block, &run);
  134. if (res != BEFS_OK) {
  135. befs_error(sb,
  136. "<--- %s for inode %lu, block %ld ERROR",
  137. __func__, (unsigned long)inode->i_ino,
  138. (long)block);
  139. return -EFBIG;
  140. }
  141. disk_off = (ulong) iaddr2blockno(sb, &run);
  142. map_bh(bh_result, inode->i_sb, disk_off);
  143. befs_debug(sb, "<--- %s for inode %lu, block %ld, disk address %lu",
  144. __func__, (unsigned long)inode->i_ino, (long)block,
  145. (unsigned long)disk_off);
  146. return 0;
  147. }
  148. static struct dentry *
  149. befs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  150. {
  151. struct inode *inode;
  152. struct super_block *sb = dir->i_sb;
  153. const befs_data_stream *ds = &BEFS_I(dir)->i_data.ds;
  154. befs_off_t offset;
  155. int ret;
  156. int utfnamelen;
  157. char *utfname;
  158. const char *name = dentry->d_name.name;
  159. befs_debug(sb, "---> %s name %pd inode %ld", __func__,
  160. dentry, dir->i_ino);
  161. /* Convert to UTF-8 */
  162. if (BEFS_SB(sb)->nls) {
  163. ret =
  164. befs_nls2utf(sb, name, strlen(name), &utfname, &utfnamelen);
  165. if (ret < 0) {
  166. befs_debug(sb, "<--- %s ERROR", __func__);
  167. return ERR_PTR(ret);
  168. }
  169. ret = befs_btree_find(sb, ds, utfname, &offset);
  170. kfree(utfname);
  171. } else {
  172. ret = befs_btree_find(sb, ds, name, &offset);
  173. }
  174. if (ret == BEFS_BT_NOT_FOUND) {
  175. befs_debug(sb, "<--- %s %pd not found", __func__, dentry);
  176. inode = NULL;
  177. } else if (ret != BEFS_OK || offset == 0) {
  178. befs_error(sb, "<--- %s Error", __func__);
  179. inode = ERR_PTR(-ENODATA);
  180. } else {
  181. inode = befs_iget(dir->i_sb, (ino_t) offset);
  182. }
  183. befs_debug(sb, "<--- %s", __func__);
  184. return d_splice_alias(inode, dentry);
  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. const befs_data_stream *ds = &BEFS_I(inode)->i_data.ds;
  192. befs_off_t value;
  193. int result;
  194. size_t keysize;
  195. char keybuf[BEFS_NAME_LEN + 1];
  196. befs_debug(sb, "---> %s name %pD, inode %ld, ctx->pos %lld",
  197. __func__, file, inode->i_ino, ctx->pos);
  198. while (1) {
  199. result = befs_btree_read(sb, ds, ctx->pos, BEFS_NAME_LEN + 1,
  200. keybuf, &keysize, &value);
  201. if (result == BEFS_ERR) {
  202. befs_debug(sb, "<--- %s ERROR", __func__);
  203. befs_error(sb, "IO error reading %pD (inode %lu)",
  204. file, inode->i_ino);
  205. return -EIO;
  206. } else if (result == BEFS_BT_END) {
  207. befs_debug(sb, "<--- %s END", __func__);
  208. return 0;
  209. } else if (result == BEFS_BT_EMPTY) {
  210. befs_debug(sb, "<--- %s Empty directory", __func__);
  211. return 0;
  212. }
  213. /* Convert to NLS */
  214. if (BEFS_SB(sb)->nls) {
  215. char *nlsname;
  216. int nlsnamelen;
  217. result =
  218. befs_utf2nls(sb, keybuf, keysize, &nlsname,
  219. &nlsnamelen);
  220. if (result < 0) {
  221. befs_debug(sb, "<--- %s ERROR", __func__);
  222. return result;
  223. }
  224. if (!dir_emit(ctx, nlsname, nlsnamelen,
  225. (ino_t) value, DT_UNKNOWN)) {
  226. kfree(nlsname);
  227. return 0;
  228. }
  229. kfree(nlsname);
  230. } else {
  231. if (!dir_emit(ctx, keybuf, keysize,
  232. (ino_t) value, DT_UNKNOWN))
  233. return 0;
  234. }
  235. ctx->pos++;
  236. }
  237. }
  238. static struct inode *
  239. befs_alloc_inode(struct super_block *sb)
  240. {
  241. struct befs_inode_info *bi;
  242. bi = kmem_cache_alloc(befs_inode_cachep, GFP_KERNEL);
  243. if (!bi)
  244. return NULL;
  245. return &bi->vfs_inode;
  246. }
  247. static void befs_free_inode(struct inode *inode)
  248. {
  249. kmem_cache_free(befs_inode_cachep, BEFS_I(inode));
  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_usercopy("befs_inode_cache",
  370. sizeof(struct befs_inode_info), 0,
  371. (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
  372. SLAB_ACCOUNT),
  373. offsetof(struct befs_inode_info,
  374. i_data.symlink),
  375. sizeof_field(struct befs_inode_info,
  376. i_data.symlink),
  377. init_once);
  378. if (befs_inode_cachep == NULL)
  379. return -ENOMEM;
  380. return 0;
  381. }
  382. /* Called at fs teardown.
  383. *
  384. * Taken from NFS implementation by Al Viro.
  385. */
  386. static void
  387. befs_destroy_inodecache(void)
  388. {
  389. /*
  390. * Make sure all delayed rcu free inodes are flushed before we
  391. * destroy cache.
  392. */
  393. rcu_barrier();
  394. kmem_cache_destroy(befs_inode_cachep);
  395. }
  396. /*
  397. * The inode of symbolic link is different to data stream.
  398. * The data stream become link name. Unless the LONG_SYMLINK
  399. * flag is set.
  400. */
  401. static int befs_symlink_readpage(struct file *unused, struct page *page)
  402. {
  403. struct inode *inode = page->mapping->host;
  404. struct super_block *sb = inode->i_sb;
  405. struct befs_inode_info *befs_ino = BEFS_I(inode);
  406. befs_data_stream *data = &befs_ino->i_data.ds;
  407. befs_off_t len = data->size;
  408. char *link = page_address(page);
  409. if (len == 0 || len > PAGE_SIZE) {
  410. befs_error(sb, "Long symlink with illegal length");
  411. goto fail;
  412. }
  413. befs_debug(sb, "Follow long symlink");
  414. if (befs_read_lsymlink(sb, data, link, len) != len) {
  415. befs_error(sb, "Failed to read entire long symlink");
  416. goto fail;
  417. }
  418. link[len - 1] = '\0';
  419. SetPageUptodate(page);
  420. unlock_page(page);
  421. return 0;
  422. fail:
  423. SetPageError(page);
  424. unlock_page(page);
  425. return -EIO;
  426. }
  427. /*
  428. * UTF-8 to NLS charset convert routine
  429. *
  430. * Uses uni2char() / char2uni() rather than the nls tables directly
  431. */
  432. static int
  433. befs_utf2nls(struct super_block *sb, const char *in,
  434. int in_len, char **out, int *out_len)
  435. {
  436. struct nls_table *nls = BEFS_SB(sb)->nls;
  437. int i, o;
  438. unicode_t uni;
  439. int unilen, utflen;
  440. char *result;
  441. /* The utf8->nls conversion won't make the final nls string bigger
  442. * than the utf one, but if the string is pure ascii they'll have the
  443. * same width and an extra char is needed to save the additional \0
  444. */
  445. int maxlen = in_len + 1;
  446. befs_debug(sb, "---> %s", __func__);
  447. if (!nls) {
  448. befs_error(sb, "%s called with no NLS table loaded", __func__);
  449. return -EINVAL;
  450. }
  451. *out = result = kmalloc(maxlen, GFP_NOFS);
  452. if (!*out)
  453. return -ENOMEM;
  454. for (i = o = 0; i < in_len; i += utflen, o += unilen) {
  455. /* convert from UTF-8 to Unicode */
  456. utflen = utf8_to_utf32(&in[i], in_len - i, &uni);
  457. if (utflen < 0)
  458. goto conv_err;
  459. /* convert from Unicode to nls */
  460. if (uni > MAX_WCHAR_T)
  461. goto conv_err;
  462. unilen = nls->uni2char(uni, &result[o], in_len - o);
  463. if (unilen < 0)
  464. goto conv_err;
  465. }
  466. result[o] = '\0';
  467. *out_len = o;
  468. befs_debug(sb, "<--- %s", __func__);
  469. return o;
  470. conv_err:
  471. befs_error(sb, "Name using character set %s contains a character that "
  472. "cannot be converted to unicode.", nls->charset);
  473. befs_debug(sb, "<--- %s", __func__);
  474. kfree(result);
  475. return -EILSEQ;
  476. }
  477. /**
  478. * befs_nls2utf - Convert NLS string to utf8 encodeing
  479. * @sb: Superblock
  480. * @in: Input string buffer in NLS format
  481. * @in_len: Length of input string in bytes
  482. * @out: The output string in UTF-8 format
  483. * @out_len: Length of the output buffer
  484. *
  485. * Converts input string @in, which is in the format of the loaded NLS map,
  486. * into a utf8 string.
  487. *
  488. * The destination string @out is allocated by this function and the caller is
  489. * responsible for freeing it with kfree()
  490. *
  491. * On return, *@out_len is the length of @out in bytes.
  492. *
  493. * On success, the return value is the number of utf8 characters written to
  494. * the output buffer @out.
  495. *
  496. * On Failure, a negative number coresponding to the error code is returned.
  497. */
  498. static int
  499. befs_nls2utf(struct super_block *sb, const char *in,
  500. int in_len, char **out, int *out_len)
  501. {
  502. struct nls_table *nls = BEFS_SB(sb)->nls;
  503. int i, o;
  504. wchar_t uni;
  505. int unilen, utflen;
  506. char *result;
  507. /*
  508. * There are nls characters that will translate to 3-chars-wide UTF-8
  509. * characters, an additional byte is needed to save the final \0
  510. * in special cases
  511. */
  512. int maxlen = (3 * in_len) + 1;
  513. befs_debug(sb, "---> %s\n", __func__);
  514. if (!nls) {
  515. befs_error(sb, "%s called with no NLS table loaded.",
  516. __func__);
  517. return -EINVAL;
  518. }
  519. *out = result = kmalloc(maxlen, GFP_NOFS);
  520. if (!*out) {
  521. *out_len = 0;
  522. return -ENOMEM;
  523. }
  524. for (i = o = 0; i < in_len; i += unilen, o += utflen) {
  525. /* convert from nls to unicode */
  526. unilen = nls->char2uni(&in[i], in_len - i, &uni);
  527. if (unilen < 0)
  528. goto conv_err;
  529. /* convert from unicode to UTF-8 */
  530. utflen = utf32_to_utf8(uni, &result[o], 3);
  531. if (utflen <= 0)
  532. goto conv_err;
  533. }
  534. result[o] = '\0';
  535. *out_len = o;
  536. befs_debug(sb, "<--- %s", __func__);
  537. return i;
  538. conv_err:
  539. befs_error(sb, "Name using character set %s contains a character that "
  540. "cannot be converted to unicode.", nls->charset);
  541. befs_debug(sb, "<--- %s", __func__);
  542. kfree(result);
  543. return -EILSEQ;
  544. }
  545. static struct inode *befs_nfs_get_inode(struct super_block *sb, uint64_t ino,
  546. uint32_t generation)
  547. {
  548. /* No need to handle i_generation */
  549. return befs_iget(sb, ino);
  550. }
  551. /*
  552. * Map a NFS file handle to a corresponding dentry
  553. */
  554. static struct dentry *befs_fh_to_dentry(struct super_block *sb,
  555. struct fid *fid, int fh_len, int fh_type)
  556. {
  557. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  558. befs_nfs_get_inode);
  559. }
  560. /*
  561. * Find the parent for a file specified by NFS handle
  562. */
  563. static struct dentry *befs_fh_to_parent(struct super_block *sb,
  564. struct fid *fid, int fh_len, int fh_type)
  565. {
  566. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  567. befs_nfs_get_inode);
  568. }
  569. static struct dentry *befs_get_parent(struct dentry *child)
  570. {
  571. struct inode *parent;
  572. struct befs_inode_info *befs_ino = BEFS_I(d_inode(child));
  573. parent = befs_iget(child->d_sb,
  574. (unsigned long)befs_ino->i_parent.start);
  575. if (IS_ERR(parent))
  576. return ERR_CAST(parent);
  577. return d_obtain_alias(parent);
  578. }
  579. enum {
  580. Opt_uid, Opt_gid, Opt_charset, Opt_debug, Opt_err,
  581. };
  582. static const match_table_t befs_tokens = {
  583. {Opt_uid, "uid=%d"},
  584. {Opt_gid, "gid=%d"},
  585. {Opt_charset, "iocharset=%s"},
  586. {Opt_debug, "debug"},
  587. {Opt_err, NULL}
  588. };
  589. static int
  590. parse_options(char *options, struct befs_mount_options *opts)
  591. {
  592. char *p;
  593. substring_t args[MAX_OPT_ARGS];
  594. int option;
  595. kuid_t uid;
  596. kgid_t gid;
  597. /* Initialize options */
  598. opts->uid = GLOBAL_ROOT_UID;
  599. opts->gid = GLOBAL_ROOT_GID;
  600. opts->use_uid = 0;
  601. opts->use_gid = 0;
  602. opts->iocharset = NULL;
  603. opts->debug = 0;
  604. if (!options)
  605. return 1;
  606. while ((p = strsep(&options, ",")) != NULL) {
  607. int token;
  608. if (!*p)
  609. continue;
  610. token = match_token(p, befs_tokens, args);
  611. switch (token) {
  612. case Opt_uid:
  613. if (match_int(&args[0], &option))
  614. return 0;
  615. uid = INVALID_UID;
  616. if (option >= 0)
  617. uid = make_kuid(current_user_ns(), option);
  618. if (!uid_valid(uid)) {
  619. pr_err("Invalid uid %d, "
  620. "using default\n", option);
  621. break;
  622. }
  623. opts->uid = uid;
  624. opts->use_uid = 1;
  625. break;
  626. case Opt_gid:
  627. if (match_int(&args[0], &option))
  628. return 0;
  629. gid = INVALID_GID;
  630. if (option >= 0)
  631. gid = make_kgid(current_user_ns(), option);
  632. if (!gid_valid(gid)) {
  633. pr_err("Invalid gid %d, "
  634. "using default\n", option);
  635. break;
  636. }
  637. opts->gid = gid;
  638. opts->use_gid = 1;
  639. break;
  640. case Opt_charset:
  641. kfree(opts->iocharset);
  642. opts->iocharset = match_strdup(&args[0]);
  643. if (!opts->iocharset) {
  644. pr_err("allocation failure for "
  645. "iocharset string\n");
  646. return 0;
  647. }
  648. break;
  649. case Opt_debug:
  650. opts->debug = 1;
  651. break;
  652. default:
  653. pr_err("Unrecognized mount option \"%s\" "
  654. "or missing value\n", p);
  655. return 0;
  656. }
  657. }
  658. return 1;
  659. }
  660. static int befs_show_options(struct seq_file *m, struct dentry *root)
  661. {
  662. struct befs_sb_info *befs_sb = BEFS_SB(root->d_sb);
  663. struct befs_mount_options *opts = &befs_sb->mount_opts;
  664. if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
  665. seq_printf(m, ",uid=%u",
  666. from_kuid_munged(&init_user_ns, opts->uid));
  667. if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
  668. seq_printf(m, ",gid=%u",
  669. from_kgid_munged(&init_user_ns, opts->gid));
  670. if (opts->iocharset)
  671. seq_printf(m, ",charset=%s", opts->iocharset);
  672. if (opts->debug)
  673. seq_puts(m, ",debug");
  674. return 0;
  675. }
  676. /* This function has the responsibiltiy of getting the
  677. * filesystem ready for unmounting.
  678. * Basically, we free everything that we allocated in
  679. * befs_read_inode
  680. */
  681. static void
  682. befs_put_super(struct super_block *sb)
  683. {
  684. kfree(BEFS_SB(sb)->mount_opts.iocharset);
  685. BEFS_SB(sb)->mount_opts.iocharset = NULL;
  686. unload_nls(BEFS_SB(sb)->nls);
  687. kfree(sb->s_fs_info);
  688. sb->s_fs_info = NULL;
  689. }
  690. /* Allocate private field of the superblock, fill it.
  691. *
  692. * Finish filling the public superblock fields
  693. * Make the root directory
  694. * Load a set of NLS translations if needed.
  695. */
  696. static int
  697. befs_fill_super(struct super_block *sb, void *data, int silent)
  698. {
  699. struct buffer_head *bh;
  700. struct befs_sb_info *befs_sb;
  701. befs_super_block *disk_sb;
  702. struct inode *root;
  703. long ret = -EINVAL;
  704. const unsigned long sb_block = 0;
  705. const off_t x86_sb_off = 512;
  706. int blocksize;
  707. sb->s_fs_info = kzalloc(sizeof(*befs_sb), GFP_KERNEL);
  708. if (sb->s_fs_info == NULL)
  709. goto unacquire_none;
  710. befs_sb = BEFS_SB(sb);
  711. if (!parse_options((char *) data, &befs_sb->mount_opts)) {
  712. if (!silent)
  713. befs_error(sb, "cannot parse mount options");
  714. goto unacquire_priv_sbp;
  715. }
  716. befs_debug(sb, "---> %s", __func__);
  717. if (!sb_rdonly(sb)) {
  718. befs_warning(sb,
  719. "No write support. Marking filesystem read-only");
  720. sb->s_flags |= SB_RDONLY;
  721. }
  722. /*
  723. * Set dummy blocksize to read super block.
  724. * Will be set to real fs blocksize later.
  725. *
  726. * Linux 2.4.10 and later refuse to read blocks smaller than
  727. * the logical block size for the device. But we also need to read at
  728. * least 1k to get the second 512 bytes of the volume.
  729. */
  730. blocksize = sb_min_blocksize(sb, 1024);
  731. if (!blocksize) {
  732. if (!silent)
  733. befs_error(sb, "unable to set blocksize");
  734. goto unacquire_priv_sbp;
  735. }
  736. bh = sb_bread(sb, sb_block);
  737. if (!bh) {
  738. if (!silent)
  739. befs_error(sb, "unable to read superblock");
  740. goto unacquire_priv_sbp;
  741. }
  742. /* account for offset of super block on x86 */
  743. disk_sb = (befs_super_block *) bh->b_data;
  744. if ((disk_sb->magic1 == BEFS_SUPER_MAGIC1_LE) ||
  745. (disk_sb->magic1 == BEFS_SUPER_MAGIC1_BE)) {
  746. befs_debug(sb, "Using PPC superblock location");
  747. } else {
  748. befs_debug(sb, "Using x86 superblock location");
  749. disk_sb =
  750. (befs_super_block *) ((void *) bh->b_data + x86_sb_off);
  751. }
  752. if ((befs_load_sb(sb, disk_sb) != BEFS_OK) ||
  753. (befs_check_sb(sb) != BEFS_OK))
  754. goto unacquire_bh;
  755. befs_dump_super_block(sb, disk_sb);
  756. brelse(bh);
  757. if (befs_sb->num_blocks > ~((sector_t)0)) {
  758. if (!silent)
  759. befs_error(sb, "blocks count: %llu is larger than the host can use",
  760. befs_sb->num_blocks);
  761. goto unacquire_priv_sbp;
  762. }
  763. /*
  764. * set up enough so that it can read an inode
  765. * Fill in kernel superblock fields from private sb
  766. */
  767. sb->s_magic = BEFS_SUPER_MAGIC;
  768. /* Set real blocksize of fs */
  769. sb_set_blocksize(sb, (ulong) befs_sb->block_size);
  770. sb->s_op = &befs_sops;
  771. sb->s_export_op = &befs_export_operations;
  772. sb->s_time_min = 0;
  773. sb->s_time_max = 0xffffffffffffll;
  774. root = befs_iget(sb, iaddr2blockno(sb, &(befs_sb->root_dir)));
  775. if (IS_ERR(root)) {
  776. ret = PTR_ERR(root);
  777. goto unacquire_priv_sbp;
  778. }
  779. sb->s_root = d_make_root(root);
  780. if (!sb->s_root) {
  781. if (!silent)
  782. befs_error(sb, "get root inode failed");
  783. goto unacquire_priv_sbp;
  784. }
  785. /* load nls library */
  786. if (befs_sb->mount_opts.iocharset) {
  787. befs_debug(sb, "Loading nls: %s",
  788. befs_sb->mount_opts.iocharset);
  789. befs_sb->nls = load_nls(befs_sb->mount_opts.iocharset);
  790. if (!befs_sb->nls) {
  791. befs_warning(sb, "Cannot load nls %s"
  792. " loading default nls",
  793. befs_sb->mount_opts.iocharset);
  794. befs_sb->nls = load_nls_default();
  795. }
  796. /* load default nls if none is specified in mount options */
  797. } else {
  798. befs_debug(sb, "Loading default nls");
  799. befs_sb->nls = load_nls_default();
  800. }
  801. return 0;
  802. unacquire_bh:
  803. brelse(bh);
  804. unacquire_priv_sbp:
  805. kfree(befs_sb->mount_opts.iocharset);
  806. kfree(sb->s_fs_info);
  807. sb->s_fs_info = NULL;
  808. unacquire_none:
  809. return ret;
  810. }
  811. static int
  812. befs_remount(struct super_block *sb, int *flags, char *data)
  813. {
  814. sync_filesystem(sb);
  815. if (!(*flags & SB_RDONLY))
  816. return -EINVAL;
  817. return 0;
  818. }
  819. static int
  820. befs_statfs(struct dentry *dentry, struct kstatfs *buf)
  821. {
  822. struct super_block *sb = dentry->d_sb;
  823. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  824. befs_debug(sb, "---> %s", __func__);
  825. buf->f_type = BEFS_SUPER_MAGIC;
  826. buf->f_bsize = sb->s_blocksize;
  827. buf->f_blocks = BEFS_SB(sb)->num_blocks;
  828. buf->f_bfree = BEFS_SB(sb)->num_blocks - BEFS_SB(sb)->used_blocks;
  829. buf->f_bavail = buf->f_bfree;
  830. buf->f_files = 0; /* UNKNOWN */
  831. buf->f_ffree = 0; /* UNKNOWN */
  832. buf->f_fsid = u64_to_fsid(id);
  833. buf->f_namelen = BEFS_NAME_LEN;
  834. befs_debug(sb, "<--- %s", __func__);
  835. return 0;
  836. }
  837. static struct dentry *
  838. befs_mount(struct file_system_type *fs_type, int flags, const char *dev_name,
  839. void *data)
  840. {
  841. return mount_bdev(fs_type, flags, dev_name, data, befs_fill_super);
  842. }
  843. static struct file_system_type befs_fs_type = {
  844. .owner = THIS_MODULE,
  845. .name = "befs",
  846. .mount = befs_mount,
  847. .kill_sb = kill_block_super,
  848. .fs_flags = FS_REQUIRES_DEV,
  849. };
  850. MODULE_ALIAS_FS("befs");
  851. static int __init
  852. init_befs_fs(void)
  853. {
  854. int err;
  855. pr_info("version: %s\n", BEFS_VERSION);
  856. err = befs_init_inodecache();
  857. if (err)
  858. goto unacquire_none;
  859. err = register_filesystem(&befs_fs_type);
  860. if (err)
  861. goto unacquire_inodecache;
  862. return 0;
  863. unacquire_inodecache:
  864. befs_destroy_inodecache();
  865. unacquire_none:
  866. return err;
  867. }
  868. static void __exit
  869. exit_befs_fs(void)
  870. {
  871. befs_destroy_inodecache();
  872. unregister_filesystem(&befs_fs_type);
  873. }
  874. /*
  875. * Macros that typecheck the init and exit functions,
  876. * ensures that they are called at init and cleanup,
  877. * and eliminates warnings about unused functions.
  878. */
  879. module_init(init_befs_fs)
  880. module_exit(exit_befs_fs)