linuxvfs.c 26 KB

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