linuxvfs.c 26 KB

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