linuxvfs.c 26 KB

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