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/fs_context.h>
  13. #include <linux/fs_parser.h>
  14. #include <linux/errno.h>
  15. #include <linux/filelock.h>
  16. #include <linux/stat.h>
  17. #include <linux/nls.h>
  18. #include <linux/buffer_head.h>
  19. #include <linux/vfs.h>
  20. #include <linux/namei.h>
  21. #include <linux/sched.h>
  22. #include <linux/cred.h>
  23. #include <linux/exportfs.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/blkdev.h>
  26. #include "befs.h"
  27. #include "btree.h"
  28. #include "inode.h"
  29. #include "datastream.h"
  30. #include "super.h"
  31. #include "io.h"
  32. MODULE_DESCRIPTION("BeOS File System (BeFS) driver");
  33. MODULE_AUTHOR("Will Dyson");
  34. MODULE_LICENSE("GPL");
  35. /* The units the vfs expects inode->i_blocks to be in */
  36. #define VFS_BLOCK_SIZE 512
  37. static int befs_readdir(struct file *, struct dir_context *);
  38. static int befs_get_block(struct inode *, sector_t, struct buffer_head *, int);
  39. static int befs_read_folio(struct file *file, struct folio *folio);
  40. static sector_t befs_bmap(struct address_space *mapping, sector_t block);
  41. static struct dentry *befs_lookup(struct inode *, struct dentry *,
  42. unsigned int);
  43. static struct inode *befs_iget(struct super_block *, unsigned long);
  44. static struct inode *befs_alloc_inode(struct super_block *sb);
  45. static void befs_free_inode(struct inode *inode);
  46. static void befs_destroy_inodecache(void);
  47. static int befs_symlink_read_folio(struct file *, struct folio *);
  48. static int befs_utf2nls(struct super_block *sb, const char *in, int in_len,
  49. char **out, int *out_len);
  50. static int befs_nls2utf(struct super_block *sb, const char *in, int in_len,
  51. char **out, int *out_len);
  52. static void befs_put_super(struct super_block *);
  53. static int befs_statfs(struct dentry *, struct kstatfs *);
  54. static int befs_show_options(struct seq_file *, struct dentry *);
  55. static struct dentry *befs_fh_to_dentry(struct super_block *sb,
  56. struct fid *fid, int fh_len, int fh_type);
  57. static struct dentry *befs_fh_to_parent(struct super_block *sb,
  58. struct fid *fid, int fh_len, int fh_type);
  59. static struct dentry *befs_get_parent(struct dentry *child);
  60. static void befs_free_fc(struct fs_context *fc);
  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. .show_options = befs_show_options,
  67. };
  68. /* slab cache for befs_inode_info objects */
  69. static struct kmem_cache *befs_inode_cachep;
  70. static const struct file_operations befs_dir_operations = {
  71. .read = generic_read_dir,
  72. .iterate_shared = befs_readdir,
  73. .llseek = generic_file_llseek,
  74. .setlease = generic_setlease,
  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. .read_folio = befs_read_folio,
  81. .bmap = befs_bmap,
  82. };
  83. static const struct address_space_operations befs_symlink_aops = {
  84. .read_folio = befs_symlink_read_folio,
  85. };
  86. static const struct export_operations befs_export_operations = {
  87. .encode_fh = generic_encode_ino32_fh,
  88. .fh_to_dentry = befs_fh_to_dentry,
  89. .fh_to_parent = befs_fh_to_parent,
  90. .get_parent = befs_get_parent,
  91. };
  92. /*
  93. * Called by generic_file_read() to read a folio of data
  94. *
  95. * In turn, simply calls a generic block read function and
  96. * passes it the address of befs_get_block, for mapping file
  97. * positions to disk blocks.
  98. */
  99. static int befs_read_folio(struct file *file, struct folio *folio)
  100. {
  101. return block_read_full_folio(folio, 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 = alloc_inode_sb(sb, 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_state_read_once(inode) & 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_set_mtime(inode,
  308. fs64_to_cpu(sb, raw_inode->last_modified_time) >> 16,
  309. 0);/* lower 16 bits are not a time */
  310. inode_set_ctime_to_ts(inode, inode_get_mtime(inode));
  311. inode_set_atime_to_ts(inode, inode_get_mtime(inode));
  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. strscpy(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_ACCOUNT,
  372. offsetof(struct befs_inode_info,
  373. i_data.symlink),
  374. sizeof_field(struct befs_inode_info,
  375. i_data.symlink),
  376. init_once);
  377. if (befs_inode_cachep == NULL)
  378. return -ENOMEM;
  379. return 0;
  380. }
  381. /* Called at fs teardown.
  382. *
  383. * Taken from NFS implementation by Al Viro.
  384. */
  385. static void
  386. befs_destroy_inodecache(void)
  387. {
  388. /*
  389. * Make sure all delayed rcu free inodes are flushed before we
  390. * destroy cache.
  391. */
  392. rcu_barrier();
  393. kmem_cache_destroy(befs_inode_cachep);
  394. }
  395. /*
  396. * The inode of symbolic link is different to data stream.
  397. * The data stream become link name. Unless the LONG_SYMLINK
  398. * flag is set.
  399. */
  400. static int befs_symlink_read_folio(struct file *unused, struct folio *folio)
  401. {
  402. struct inode *inode = folio->mapping->host;
  403. struct super_block *sb = inode->i_sb;
  404. struct befs_inode_info *befs_ino = BEFS_I(inode);
  405. befs_data_stream *data = &befs_ino->i_data.ds;
  406. befs_off_t len = data->size;
  407. char *link = folio_address(folio);
  408. int err = -EIO;
  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. err = 0;
  420. fail:
  421. folio_end_read(folio, err == 0);
  422. return err;
  423. }
  424. /*
  425. * UTF-8 to NLS charset convert routine
  426. *
  427. * Uses uni2char() / char2uni() rather than the nls tables directly
  428. */
  429. static int
  430. befs_utf2nls(struct super_block *sb, const char *in,
  431. int in_len, char **out, int *out_len)
  432. {
  433. struct nls_table *nls = BEFS_SB(sb)->nls;
  434. int i, o;
  435. unicode_t uni;
  436. int unilen, utflen;
  437. char *result;
  438. /* The utf8->nls conversion won't make the final nls string bigger
  439. * than the utf one, but if the string is pure ascii they'll have the
  440. * same width and an extra char is needed to save the additional \0
  441. */
  442. int maxlen = in_len + 1;
  443. befs_debug(sb, "---> %s", __func__);
  444. if (!nls) {
  445. befs_error(sb, "%s called with no NLS table loaded", __func__);
  446. return -EINVAL;
  447. }
  448. *out = result = kmalloc(maxlen, GFP_NOFS);
  449. if (!*out)
  450. return -ENOMEM;
  451. for (i = o = 0; i < in_len; i += utflen, o += unilen) {
  452. /* convert from UTF-8 to Unicode */
  453. utflen = utf8_to_utf32(&in[i], in_len - i, &uni);
  454. if (utflen < 0)
  455. goto conv_err;
  456. /* convert from Unicode to nls */
  457. if (uni > MAX_WCHAR_T)
  458. goto conv_err;
  459. unilen = nls->uni2char(uni, &result[o], in_len - o);
  460. if (unilen < 0)
  461. goto conv_err;
  462. }
  463. result[o] = '\0';
  464. *out_len = o;
  465. befs_debug(sb, "<--- %s", __func__);
  466. return o;
  467. conv_err:
  468. befs_error(sb, "Name using character set %s contains a character that "
  469. "cannot be converted to unicode.", nls->charset);
  470. befs_debug(sb, "<--- %s", __func__);
  471. kfree(result);
  472. return -EILSEQ;
  473. }
  474. /**
  475. * befs_nls2utf - Convert NLS string to utf8 encodeing
  476. * @sb: Superblock
  477. * @in: Input string buffer in NLS format
  478. * @in_len: Length of input string in bytes
  479. * @out: The output string in UTF-8 format
  480. * @out_len: Length of the output buffer
  481. *
  482. * Converts input string @in, which is in the format of the loaded NLS map,
  483. * into a utf8 string.
  484. *
  485. * The destination string @out is allocated by this function and the caller is
  486. * responsible for freeing it with kfree()
  487. *
  488. * On return, *@out_len is the length of @out in bytes.
  489. *
  490. * On success, the return value is the number of utf8 characters written to
  491. * the output buffer @out.
  492. *
  493. * On Failure, a negative number coresponding to the error code is returned.
  494. */
  495. static int
  496. befs_nls2utf(struct super_block *sb, const char *in,
  497. int in_len, char **out, int *out_len)
  498. {
  499. struct nls_table *nls = BEFS_SB(sb)->nls;
  500. int i, o;
  501. wchar_t uni;
  502. int unilen, utflen;
  503. char *result;
  504. /*
  505. * There are nls characters that will translate to 3-chars-wide UTF-8
  506. * characters, an additional byte is needed to save the final \0
  507. * in special cases
  508. */
  509. int maxlen = (3 * in_len) + 1;
  510. befs_debug(sb, "---> %s\n", __func__);
  511. if (!nls) {
  512. befs_error(sb, "%s called with no NLS table loaded.",
  513. __func__);
  514. return -EINVAL;
  515. }
  516. *out = result = kmalloc(maxlen, GFP_NOFS);
  517. if (!*out) {
  518. *out_len = 0;
  519. return -ENOMEM;
  520. }
  521. for (i = o = 0; i < in_len; i += unilen, o += utflen) {
  522. /* convert from nls to unicode */
  523. unilen = nls->char2uni(&in[i], in_len - i, &uni);
  524. if (unilen < 0)
  525. goto conv_err;
  526. /* convert from unicode to UTF-8 */
  527. utflen = utf32_to_utf8(uni, &result[o], 3);
  528. if (utflen <= 0)
  529. goto conv_err;
  530. }
  531. result[o] = '\0';
  532. *out_len = o;
  533. befs_debug(sb, "<--- %s", __func__);
  534. return i;
  535. conv_err:
  536. befs_error(sb, "Name using character set %s contains a character that "
  537. "cannot be converted to unicode.", nls->charset);
  538. befs_debug(sb, "<--- %s", __func__);
  539. kfree(result);
  540. return -EILSEQ;
  541. }
  542. static struct inode *befs_nfs_get_inode(struct super_block *sb, uint64_t ino,
  543. uint32_t generation)
  544. {
  545. /* No need to handle i_generation */
  546. return befs_iget(sb, ino);
  547. }
  548. /*
  549. * Map a NFS file handle to a corresponding dentry
  550. */
  551. static struct dentry *befs_fh_to_dentry(struct super_block *sb,
  552. struct fid *fid, int fh_len, int fh_type)
  553. {
  554. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  555. befs_nfs_get_inode);
  556. }
  557. /*
  558. * Find the parent for a file specified by NFS handle
  559. */
  560. static struct dentry *befs_fh_to_parent(struct super_block *sb,
  561. struct fid *fid, int fh_len, int fh_type)
  562. {
  563. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  564. befs_nfs_get_inode);
  565. }
  566. static struct dentry *befs_get_parent(struct dentry *child)
  567. {
  568. struct inode *parent;
  569. struct befs_inode_info *befs_ino = BEFS_I(d_inode(child));
  570. parent = befs_iget(child->d_sb,
  571. (unsigned long)befs_ino->i_parent.start);
  572. return d_obtain_alias(parent);
  573. }
  574. enum {
  575. Opt_uid, Opt_gid, Opt_charset, Opt_debug,
  576. };
  577. static const struct fs_parameter_spec befs_param_spec[] = {
  578. fsparam_uid ("uid", Opt_uid),
  579. fsparam_gid ("gid", Opt_gid),
  580. fsparam_string ("iocharset", Opt_charset),
  581. fsparam_flag ("debug", Opt_debug),
  582. {}
  583. };
  584. static int
  585. befs_parse_param(struct fs_context *fc, struct fs_parameter *param)
  586. {
  587. struct befs_mount_options *opts = fc->fs_private;
  588. int token;
  589. struct fs_parse_result result;
  590. /* befs ignores all options on remount */
  591. if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE)
  592. return 0;
  593. token = fs_parse(fc, befs_param_spec, param, &result);
  594. if (token < 0)
  595. return token;
  596. switch (token) {
  597. case Opt_uid:
  598. opts->uid = result.uid;
  599. opts->use_uid = 1;
  600. break;
  601. case Opt_gid:
  602. opts->gid = result.gid;
  603. opts->use_gid = 1;
  604. break;
  605. case Opt_charset:
  606. kfree(opts->iocharset);
  607. opts->iocharset = param->string;
  608. param->string = NULL;
  609. break;
  610. case Opt_debug:
  611. opts->debug = 1;
  612. break;
  613. default:
  614. return -EINVAL;
  615. }
  616. return 0;
  617. }
  618. static int befs_show_options(struct seq_file *m, struct dentry *root)
  619. {
  620. struct befs_sb_info *befs_sb = BEFS_SB(root->d_sb);
  621. struct befs_mount_options *opts = &befs_sb->mount_opts;
  622. if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
  623. seq_printf(m, ",uid=%u",
  624. from_kuid_munged(&init_user_ns, opts->uid));
  625. if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
  626. seq_printf(m, ",gid=%u",
  627. from_kgid_munged(&init_user_ns, opts->gid));
  628. if (opts->iocharset)
  629. seq_printf(m, ",charset=%s", opts->iocharset);
  630. if (opts->debug)
  631. seq_puts(m, ",debug");
  632. return 0;
  633. }
  634. /* This function has the responsibiltiy of getting the
  635. * filesystem ready for unmounting.
  636. * Basically, we free everything that we allocated in
  637. * befs_read_inode
  638. */
  639. static void
  640. befs_put_super(struct super_block *sb)
  641. {
  642. kfree(BEFS_SB(sb)->mount_opts.iocharset);
  643. BEFS_SB(sb)->mount_opts.iocharset = NULL;
  644. unload_nls(BEFS_SB(sb)->nls);
  645. kfree(sb->s_fs_info);
  646. sb->s_fs_info = NULL;
  647. }
  648. /*
  649. * Copy the parsed options into the sbi mount_options member
  650. */
  651. static void
  652. befs_set_options(struct befs_sb_info *sbi, struct befs_mount_options *opts)
  653. {
  654. sbi->mount_opts.uid = opts->uid;
  655. sbi->mount_opts.gid = opts->gid;
  656. sbi->mount_opts.use_uid = opts->use_uid;
  657. sbi->mount_opts.use_gid = opts->use_gid;
  658. sbi->mount_opts.debug = opts->debug;
  659. sbi->mount_opts.iocharset = opts->iocharset;
  660. opts->iocharset = 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, struct fs_context *fc)
  670. {
  671. struct buffer_head *bh;
  672. struct 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. int blocksize;
  679. struct befs_mount_options *parsed_opts = fc->fs_private;
  680. int silent = fc->sb_flags & SB_SILENT;
  681. sb->s_fs_info = kzalloc_obj(*befs_sb);
  682. if (sb->s_fs_info == NULL)
  683. goto unacquire_none;
  684. befs_sb = BEFS_SB(sb);
  685. befs_set_options(befs_sb, parsed_opts);
  686. befs_debug(sb, "---> %s", __func__);
  687. if (!sb_rdonly(sb)) {
  688. befs_warning(sb,
  689. "No write support. Marking filesystem read-only");
  690. sb->s_flags |= SB_RDONLY;
  691. }
  692. /*
  693. * Set dummy blocksize to read super block.
  694. * Will be set to real fs blocksize later.
  695. *
  696. * Linux 2.4.10 and later refuse to read blocks smaller than
  697. * the logical block size for the device. But we also need to read at
  698. * least 1k to get the second 512 bytes of the volume.
  699. */
  700. blocksize = sb_min_blocksize(sb, 1024);
  701. if (!blocksize) {
  702. if (!silent)
  703. befs_error(sb, "unable to set blocksize");
  704. goto unacquire_priv_sbp;
  705. }
  706. bh = sb_bread(sb, sb_block);
  707. if (!bh) {
  708. if (!silent)
  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. (befs_check_sb(sb) != BEFS_OK))
  724. goto unacquire_bh;
  725. befs_dump_super_block(sb, disk_sb);
  726. brelse(bh);
  727. if (befs_sb->num_blocks > ~((sector_t)0)) {
  728. if (!silent)
  729. befs_error(sb, "blocks count: %llu is larger than the host can use",
  730. befs_sb->num_blocks);
  731. goto unacquire_priv_sbp;
  732. }
  733. /*
  734. * set up enough so that it can read an inode
  735. * Fill in kernel superblock fields from private sb
  736. */
  737. sb->s_magic = BEFS_SUPER_MAGIC;
  738. /* Set real blocksize of fs */
  739. sb_set_blocksize(sb, (ulong) befs_sb->block_size);
  740. sb->s_op = &befs_sops;
  741. sb->s_export_op = &befs_export_operations;
  742. sb->s_time_min = 0;
  743. sb->s_time_max = 0xffffffffffffll;
  744. root = befs_iget(sb, iaddr2blockno(sb, &(befs_sb->root_dir)));
  745. if (IS_ERR(root)) {
  746. ret = PTR_ERR(root);
  747. goto unacquire_priv_sbp;
  748. }
  749. sb->s_root = d_make_root(root);
  750. if (!sb->s_root) {
  751. if (!silent)
  752. befs_error(sb, "get root inode failed");
  753. goto unacquire_priv_sbp;
  754. }
  755. /* load nls library */
  756. if (befs_sb->mount_opts.iocharset) {
  757. befs_debug(sb, "Loading nls: %s",
  758. befs_sb->mount_opts.iocharset);
  759. befs_sb->nls = load_nls(befs_sb->mount_opts.iocharset);
  760. if (!befs_sb->nls) {
  761. befs_warning(sb, "Cannot load nls %s"
  762. " loading default nls",
  763. befs_sb->mount_opts.iocharset);
  764. befs_sb->nls = load_nls_default();
  765. }
  766. /* load default nls if none is specified in mount options */
  767. } else {
  768. befs_debug(sb, "Loading default nls");
  769. befs_sb->nls = load_nls_default();
  770. }
  771. return 0;
  772. unacquire_bh:
  773. brelse(bh);
  774. unacquire_priv_sbp:
  775. kfree(befs_sb->mount_opts.iocharset);
  776. kfree(sb->s_fs_info);
  777. sb->s_fs_info = NULL;
  778. unacquire_none:
  779. return ret;
  780. }
  781. static int
  782. befs_reconfigure(struct fs_context *fc)
  783. {
  784. sync_filesystem(fc->root->d_sb);
  785. if (!(fc->sb_flags & SB_RDONLY))
  786. return -EINVAL;
  787. return 0;
  788. }
  789. static int
  790. befs_statfs(struct dentry *dentry, struct kstatfs *buf)
  791. {
  792. struct super_block *sb = dentry->d_sb;
  793. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  794. befs_debug(sb, "---> %s", __func__);
  795. buf->f_type = BEFS_SUPER_MAGIC;
  796. buf->f_bsize = sb->s_blocksize;
  797. buf->f_blocks = BEFS_SB(sb)->num_blocks;
  798. buf->f_bfree = BEFS_SB(sb)->num_blocks - BEFS_SB(sb)->used_blocks;
  799. buf->f_bavail = buf->f_bfree;
  800. buf->f_files = 0; /* UNKNOWN */
  801. buf->f_ffree = 0; /* UNKNOWN */
  802. buf->f_fsid = u64_to_fsid(id);
  803. buf->f_namelen = BEFS_NAME_LEN;
  804. befs_debug(sb, "<--- %s", __func__);
  805. return 0;
  806. }
  807. static int befs_get_tree(struct fs_context *fc)
  808. {
  809. return get_tree_bdev(fc, befs_fill_super);
  810. }
  811. static const struct fs_context_operations befs_context_ops = {
  812. .parse_param = befs_parse_param,
  813. .get_tree = befs_get_tree,
  814. .reconfigure = befs_reconfigure,
  815. .free = befs_free_fc,
  816. };
  817. static int befs_init_fs_context(struct fs_context *fc)
  818. {
  819. struct befs_mount_options *opts;
  820. opts = kzalloc_obj(*opts);
  821. if (!opts)
  822. return -ENOMEM;
  823. /* Initialize options */
  824. opts->uid = GLOBAL_ROOT_UID;
  825. opts->gid = GLOBAL_ROOT_GID;
  826. fc->fs_private = opts;
  827. fc->ops = &befs_context_ops;
  828. return 0;
  829. }
  830. static void befs_free_fc(struct fs_context *fc)
  831. {
  832. struct befs_mount_options *opts = fc->fs_private;
  833. kfree(opts->iocharset);
  834. kfree(fc->fs_private);
  835. }
  836. static struct file_system_type befs_fs_type = {
  837. .owner = THIS_MODULE,
  838. .name = "befs",
  839. .kill_sb = kill_block_super,
  840. .fs_flags = FS_REQUIRES_DEV,
  841. .init_fs_context = befs_init_fs_context,
  842. .parameters = befs_param_spec,
  843. };
  844. MODULE_ALIAS_FS("befs");
  845. static int __init
  846. init_befs_fs(void)
  847. {
  848. int err;
  849. pr_info("version: %s\n", BEFS_VERSION);
  850. err = befs_init_inodecache();
  851. if (err)
  852. goto unacquire_none;
  853. err = register_filesystem(&befs_fs_type);
  854. if (err)
  855. goto unacquire_inodecache;
  856. return 0;
  857. unacquire_inodecache:
  858. befs_destroy_inodecache();
  859. unacquire_none:
  860. return err;
  861. }
  862. static void __exit
  863. exit_befs_fs(void)
  864. {
  865. befs_destroy_inodecache();
  866. unregister_filesystem(&befs_fs_type);
  867. }
  868. /*
  869. * Macros that typecheck the init and exit functions,
  870. * ensures that they are called at init and cleanup,
  871. * and eliminates warnings about unused functions.
  872. */
  873. module_init(init_befs_fs)
  874. module_exit(exit_befs_fs)