linuxvfs.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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("befs_inode_cache",
  375. sizeof (struct befs_inode_info),
  376. 0, (SLAB_RECLAIM_ACCOUNT|
  377. SLAB_MEM_SPREAD|SLAB_ACCOUNT),
  378. init_once);
  379. if (befs_inode_cachep == NULL)
  380. return -ENOMEM;
  381. return 0;
  382. }
  383. /* Called at fs teardown.
  384. *
  385. * Taken from NFS implementation by Al Viro.
  386. */
  387. static void
  388. befs_destroy_inodecache(void)
  389. {
  390. /*
  391. * Make sure all delayed rcu free inodes are flushed before we
  392. * destroy cache.
  393. */
  394. rcu_barrier();
  395. kmem_cache_destroy(befs_inode_cachep);
  396. }
  397. /*
  398. * The inode of symbolic link is different to data stream.
  399. * The data stream become link name. Unless the LONG_SYMLINK
  400. * flag is set.
  401. */
  402. static int befs_symlink_readpage(struct file *unused, struct page *page)
  403. {
  404. struct inode *inode = page->mapping->host;
  405. struct super_block *sb = inode->i_sb;
  406. struct befs_inode_info *befs_ino = BEFS_I(inode);
  407. befs_data_stream *data = &befs_ino->i_data.ds;
  408. befs_off_t len = data->size;
  409. char *link = page_address(page);
  410. if (len == 0 || len > PAGE_SIZE) {
  411. befs_error(sb, "Long symlink with illegal length");
  412. goto fail;
  413. }
  414. befs_debug(sb, "Follow long symlink");
  415. if (befs_read_lsymlink(sb, data, link, len) != len) {
  416. befs_error(sb, "Failed to read entire long symlink");
  417. goto fail;
  418. }
  419. link[len - 1] = '\0';
  420. SetPageUptodate(page);
  421. unlock_page(page);
  422. return 0;
  423. fail:
  424. SetPageError(page);
  425. unlock_page(page);
  426. return -EIO;
  427. }
  428. /*
  429. * UTF-8 to NLS charset convert routine
  430. *
  431. * Uses uni2char() / char2uni() rather than the nls tables directly
  432. */
  433. static int
  434. befs_utf2nls(struct super_block *sb, const char *in,
  435. int in_len, char **out, int *out_len)
  436. {
  437. struct nls_table *nls = BEFS_SB(sb)->nls;
  438. int i, o;
  439. unicode_t uni;
  440. int unilen, utflen;
  441. char *result;
  442. /* The utf8->nls conversion won't make the final nls string bigger
  443. * than the utf one, but if the string is pure ascii they'll have the
  444. * same width and an extra char is needed to save the additional \0
  445. */
  446. int maxlen = in_len + 1;
  447. befs_debug(sb, "---> %s", __func__);
  448. if (!nls) {
  449. befs_error(sb, "%s called with no NLS table loaded", __func__);
  450. return -EINVAL;
  451. }
  452. *out = result = kmalloc(maxlen, GFP_NOFS);
  453. if (!*out)
  454. return -ENOMEM;
  455. for (i = o = 0; i < in_len; i += utflen, o += unilen) {
  456. /* convert from UTF-8 to Unicode */
  457. utflen = utf8_to_utf32(&in[i], in_len - i, &uni);
  458. if (utflen < 0)
  459. goto conv_err;
  460. /* convert from Unicode to nls */
  461. if (uni > MAX_WCHAR_T)
  462. goto conv_err;
  463. unilen = nls->uni2char(uni, &result[o], in_len - o);
  464. if (unilen < 0)
  465. goto conv_err;
  466. }
  467. result[o] = '\0';
  468. *out_len = o;
  469. befs_debug(sb, "<--- %s", __func__);
  470. return o;
  471. conv_err:
  472. befs_error(sb, "Name using character set %s contains a character that "
  473. "cannot be converted to unicode.", nls->charset);
  474. befs_debug(sb, "<--- %s", __func__);
  475. kfree(result);
  476. return -EILSEQ;
  477. }
  478. /**
  479. * befs_nls2utf - Convert NLS string to utf8 encodeing
  480. * @sb: Superblock
  481. * @in: Input string buffer in NLS format
  482. * @in_len: Length of input string in bytes
  483. * @out: The output string in UTF-8 format
  484. * @out_len: Length of the output buffer
  485. *
  486. * Converts input string @in, which is in the format of the loaded NLS map,
  487. * into a utf8 string.
  488. *
  489. * The destination string @out is allocated by this function and the caller is
  490. * responsible for freeing it with kfree()
  491. *
  492. * On return, *@out_len is the length of @out in bytes.
  493. *
  494. * On success, the return value is the number of utf8 characters written to
  495. * the output buffer @out.
  496. *
  497. * On Failure, a negative number coresponding to the error code is returned.
  498. */
  499. static int
  500. befs_nls2utf(struct super_block *sb, const char *in,
  501. int in_len, char **out, int *out_len)
  502. {
  503. struct nls_table *nls = BEFS_SB(sb)->nls;
  504. int i, o;
  505. wchar_t uni;
  506. int unilen, utflen;
  507. char *result;
  508. /*
  509. * There are nls characters that will translate to 3-chars-wide UTF-8
  510. * characters, an additional byte is needed to save the final \0
  511. * in special cases
  512. */
  513. int maxlen = (3 * in_len) + 1;
  514. befs_debug(sb, "---> %s\n", __func__);
  515. if (!nls) {
  516. befs_error(sb, "%s called with no NLS table loaded.",
  517. __func__);
  518. return -EINVAL;
  519. }
  520. *out = result = kmalloc(maxlen, GFP_NOFS);
  521. if (!*out) {
  522. *out_len = 0;
  523. return -ENOMEM;
  524. }
  525. for (i = o = 0; i < in_len; i += unilen, o += utflen) {
  526. /* convert from nls to unicode */
  527. unilen = nls->char2uni(&in[i], in_len - i, &uni);
  528. if (unilen < 0)
  529. goto conv_err;
  530. /* convert from unicode to UTF-8 */
  531. utflen = utf32_to_utf8(uni, &result[o], 3);
  532. if (utflen <= 0)
  533. goto conv_err;
  534. }
  535. result[o] = '\0';
  536. *out_len = o;
  537. befs_debug(sb, "<--- %s", __func__);
  538. return i;
  539. conv_err:
  540. befs_error(sb, "Name using character set %s contains a character that "
  541. "cannot be converted to unicode.", nls->charset);
  542. befs_debug(sb, "<--- %s", __func__);
  543. kfree(result);
  544. return -EILSEQ;
  545. }
  546. static struct inode *befs_nfs_get_inode(struct super_block *sb, uint64_t ino,
  547. uint32_t generation)
  548. {
  549. /* No need to handle i_generation */
  550. return befs_iget(sb, ino);
  551. }
  552. /*
  553. * Map a NFS file handle to a corresponding dentry
  554. */
  555. static struct dentry *befs_fh_to_dentry(struct super_block *sb,
  556. struct fid *fid, int fh_len, int fh_type)
  557. {
  558. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  559. befs_nfs_get_inode);
  560. }
  561. /*
  562. * Find the parent for a file specified by NFS handle
  563. */
  564. static struct dentry *befs_fh_to_parent(struct super_block *sb,
  565. struct fid *fid, int fh_len, int fh_type)
  566. {
  567. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  568. befs_nfs_get_inode);
  569. }
  570. static struct dentry *befs_get_parent(struct dentry *child)
  571. {
  572. struct inode *parent;
  573. struct befs_inode_info *befs_ino = BEFS_I(d_inode(child));
  574. parent = befs_iget(child->d_sb,
  575. (unsigned long)befs_ino->i_parent.start);
  576. if (IS_ERR(parent))
  577. return ERR_CAST(parent);
  578. return d_obtain_alias(parent);
  579. }
  580. enum {
  581. Opt_uid, Opt_gid, Opt_charset, Opt_debug, Opt_err,
  582. };
  583. static const match_table_t befs_tokens = {
  584. {Opt_uid, "uid=%d"},
  585. {Opt_gid, "gid=%d"},
  586. {Opt_charset, "iocharset=%s"},
  587. {Opt_debug, "debug"},
  588. {Opt_err, NULL}
  589. };
  590. static int
  591. parse_options(char *options, struct befs_mount_options *opts)
  592. {
  593. char *p;
  594. substring_t args[MAX_OPT_ARGS];
  595. int option;
  596. kuid_t uid;
  597. kgid_t gid;
  598. /* Initialize options */
  599. opts->uid = GLOBAL_ROOT_UID;
  600. opts->gid = GLOBAL_ROOT_GID;
  601. opts->use_uid = 0;
  602. opts->use_gid = 0;
  603. opts->iocharset = NULL;
  604. opts->debug = 0;
  605. if (!options)
  606. return 1;
  607. while ((p = strsep(&options, ",")) != NULL) {
  608. int token;
  609. if (!*p)
  610. continue;
  611. token = match_token(p, befs_tokens, args);
  612. switch (token) {
  613. case Opt_uid:
  614. if (match_int(&args[0], &option))
  615. return 0;
  616. uid = INVALID_UID;
  617. if (option >= 0)
  618. uid = make_kuid(current_user_ns(), option);
  619. if (!uid_valid(uid)) {
  620. pr_err("Invalid uid %d, "
  621. "using default\n", option);
  622. break;
  623. }
  624. opts->uid = uid;
  625. opts->use_uid = 1;
  626. break;
  627. case Opt_gid:
  628. if (match_int(&args[0], &option))
  629. return 0;
  630. gid = INVALID_GID;
  631. if (option >= 0)
  632. gid = make_kgid(current_user_ns(), option);
  633. if (!gid_valid(gid)) {
  634. pr_err("Invalid gid %d, "
  635. "using default\n", option);
  636. break;
  637. }
  638. opts->gid = gid;
  639. opts->use_gid = 1;
  640. break;
  641. case Opt_charset:
  642. kfree(opts->iocharset);
  643. opts->iocharset = match_strdup(&args[0]);
  644. if (!opts->iocharset) {
  645. pr_err("allocation failure for "
  646. "iocharset string\n");
  647. return 0;
  648. }
  649. break;
  650. case Opt_debug:
  651. opts->debug = 1;
  652. break;
  653. default:
  654. pr_err("Unrecognized mount option \"%s\" "
  655. "or missing value\n", p);
  656. return 0;
  657. }
  658. }
  659. return 1;
  660. }
  661. static int befs_show_options(struct seq_file *m, struct dentry *root)
  662. {
  663. struct befs_sb_info *befs_sb = BEFS_SB(root->d_sb);
  664. struct befs_mount_options *opts = &befs_sb->mount_opts;
  665. if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
  666. seq_printf(m, ",uid=%u",
  667. from_kuid_munged(&init_user_ns, opts->uid));
  668. if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
  669. seq_printf(m, ",gid=%u",
  670. from_kgid_munged(&init_user_ns, opts->gid));
  671. if (opts->iocharset)
  672. seq_printf(m, ",charset=%s", opts->iocharset);
  673. if (opts->debug)
  674. seq_puts(m, ",debug");
  675. return 0;
  676. }
  677. /* This function has the responsibiltiy of getting the
  678. * filesystem ready for unmounting.
  679. * Basically, we free everything that we allocated in
  680. * befs_read_inode
  681. */
  682. static void
  683. befs_put_super(struct super_block *sb)
  684. {
  685. kfree(BEFS_SB(sb)->mount_opts.iocharset);
  686. BEFS_SB(sb)->mount_opts.iocharset = NULL;
  687. unload_nls(BEFS_SB(sb)->nls);
  688. kfree(sb->s_fs_info);
  689. sb->s_fs_info = NULL;
  690. }
  691. /* Allocate private field of the superblock, fill it.
  692. *
  693. * Finish filling the public superblock fields
  694. * Make the root directory
  695. * Load a set of NLS translations if needed.
  696. */
  697. static int
  698. befs_fill_super(struct super_block *sb, void *data, int silent)
  699. {
  700. struct buffer_head *bh;
  701. struct befs_sb_info *befs_sb;
  702. befs_super_block *disk_sb;
  703. struct inode *root;
  704. long ret = -EINVAL;
  705. const unsigned long sb_block = 0;
  706. const off_t x86_sb_off = 512;
  707. int blocksize;
  708. sb->s_fs_info = kzalloc(sizeof(*befs_sb), GFP_KERNEL);
  709. if (sb->s_fs_info == NULL)
  710. goto unacquire_none;
  711. befs_sb = BEFS_SB(sb);
  712. if (!parse_options((char *) data, &befs_sb->mount_opts)) {
  713. if (!silent)
  714. befs_error(sb, "cannot parse mount options");
  715. goto unacquire_priv_sbp;
  716. }
  717. befs_debug(sb, "---> %s", __func__);
  718. if (!sb_rdonly(sb)) {
  719. befs_warning(sb,
  720. "No write support. Marking filesystem read-only");
  721. sb->s_flags |= MS_RDONLY;
  722. }
  723. /*
  724. * Set dummy blocksize to read super block.
  725. * Will be set to real fs blocksize later.
  726. *
  727. * Linux 2.4.10 and later refuse to read blocks smaller than
  728. * the logical block size for the device. But we also need to read at
  729. * least 1k to get the second 512 bytes of the volume.
  730. */
  731. blocksize = sb_min_blocksize(sb, 1024);
  732. if (!blocksize) {
  733. if (!silent)
  734. befs_error(sb, "unable to set blocksize");
  735. goto unacquire_priv_sbp;
  736. }
  737. bh = sb_bread(sb, sb_block);
  738. if (!bh) {
  739. if (!silent)
  740. befs_error(sb, "unable to read superblock");
  741. goto unacquire_priv_sbp;
  742. }
  743. /* account for offset of super block on x86 */
  744. disk_sb = (befs_super_block *) bh->b_data;
  745. if ((disk_sb->magic1 == BEFS_SUPER_MAGIC1_LE) ||
  746. (disk_sb->magic1 == BEFS_SUPER_MAGIC1_BE)) {
  747. befs_debug(sb, "Using PPC superblock location");
  748. } else {
  749. befs_debug(sb, "Using x86 superblock location");
  750. disk_sb =
  751. (befs_super_block *) ((void *) bh->b_data + x86_sb_off);
  752. }
  753. if ((befs_load_sb(sb, disk_sb) != BEFS_OK) ||
  754. (befs_check_sb(sb) != BEFS_OK))
  755. goto unacquire_bh;
  756. befs_dump_super_block(sb, disk_sb);
  757. brelse(bh);
  758. if (befs_sb->num_blocks > ~((sector_t)0)) {
  759. if (!silent)
  760. befs_error(sb, "blocks count: %llu is larger than the host can use",
  761. befs_sb->num_blocks);
  762. goto unacquire_priv_sbp;
  763. }
  764. /*
  765. * set up enough so that it can read an inode
  766. * Fill in kernel superblock fields from private sb
  767. */
  768. sb->s_magic = BEFS_SUPER_MAGIC;
  769. /* Set real blocksize of fs */
  770. sb_set_blocksize(sb, (ulong) befs_sb->block_size);
  771. sb->s_op = &befs_sops;
  772. sb->s_export_op = &befs_export_operations;
  773. root = befs_iget(sb, iaddr2blockno(sb, &(befs_sb->root_dir)));
  774. if (IS_ERR(root)) {
  775. ret = PTR_ERR(root);
  776. goto unacquire_priv_sbp;
  777. }
  778. sb->s_root = d_make_root(root);
  779. if (!sb->s_root) {
  780. if (!silent)
  781. befs_error(sb, "get root inode failed");
  782. goto unacquire_priv_sbp;
  783. }
  784. /* load nls library */
  785. if (befs_sb->mount_opts.iocharset) {
  786. befs_debug(sb, "Loading nls: %s",
  787. befs_sb->mount_opts.iocharset);
  788. befs_sb->nls = load_nls(befs_sb->mount_opts.iocharset);
  789. if (!befs_sb->nls) {
  790. befs_warning(sb, "Cannot load nls %s"
  791. " loading default nls",
  792. befs_sb->mount_opts.iocharset);
  793. befs_sb->nls = load_nls_default();
  794. }
  795. /* load default nls if none is specified in mount options */
  796. } else {
  797. befs_debug(sb, "Loading default nls");
  798. befs_sb->nls = load_nls_default();
  799. }
  800. return 0;
  801. unacquire_bh:
  802. brelse(bh);
  803. unacquire_priv_sbp:
  804. kfree(befs_sb->mount_opts.iocharset);
  805. kfree(sb->s_fs_info);
  806. sb->s_fs_info = NULL;
  807. unacquire_none:
  808. return ret;
  809. }
  810. static int
  811. befs_remount(struct super_block *sb, int *flags, char *data)
  812. {
  813. sync_filesystem(sb);
  814. if (!(*flags & MS_RDONLY))
  815. return -EINVAL;
  816. return 0;
  817. }
  818. static int
  819. befs_statfs(struct dentry *dentry, struct kstatfs *buf)
  820. {
  821. struct super_block *sb = dentry->d_sb;
  822. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  823. befs_debug(sb, "---> %s", __func__);
  824. buf->f_type = BEFS_SUPER_MAGIC;
  825. buf->f_bsize = sb->s_blocksize;
  826. buf->f_blocks = BEFS_SB(sb)->num_blocks;
  827. buf->f_bfree = BEFS_SB(sb)->num_blocks - BEFS_SB(sb)->used_blocks;
  828. buf->f_bavail = buf->f_bfree;
  829. buf->f_files = 0; /* UNKNOWN */
  830. buf->f_ffree = 0; /* UNKNOWN */
  831. buf->f_fsid.val[0] = (u32)id;
  832. buf->f_fsid.val[1] = (u32)(id >> 32);
  833. buf->f_namelen = BEFS_NAME_LEN;
  834. befs_debug(sb, "<--- %s", __func__);
  835. return 0;
  836. }
  837. static struct dentry *
  838. befs_mount(struct file_system_type *fs_type, int flags, const char *dev_name,
  839. void *data)
  840. {
  841. return mount_bdev(fs_type, flags, dev_name, data, befs_fill_super);
  842. }
  843. static struct file_system_type befs_fs_type = {
  844. .owner = THIS_MODULE,
  845. .name = "befs",
  846. .mount = befs_mount,
  847. .kill_sb = kill_block_super,
  848. .fs_flags = FS_REQUIRES_DEV,
  849. };
  850. MODULE_ALIAS_FS("befs");
  851. static int __init
  852. init_befs_fs(void)
  853. {
  854. int err;
  855. pr_info("version: %s\n", BEFS_VERSION);
  856. err = befs_init_inodecache();
  857. if (err)
  858. goto unacquire_none;
  859. err = register_filesystem(&befs_fs_type);
  860. if (err)
  861. goto unacquire_inodecache;
  862. return 0;
  863. unacquire_inodecache:
  864. befs_destroy_inodecache();
  865. unacquire_none:
  866. return err;
  867. }
  868. static void __exit
  869. exit_befs_fs(void)
  870. {
  871. befs_destroy_inodecache();
  872. unregister_filesystem(&befs_fs_type);
  873. }
  874. /*
  875. * Macros that typecheck the init and exit functions,
  876. * ensures that they are called at init and cleanup,
  877. * and eliminates warnings about unused functions.
  878. */
  879. module_init(init_befs_fs)
  880. module_exit(exit_befs_fs)