linuxvfs.c 26 KB

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