File Index
Files and directories
Sign in
FILE

super.c

File preview with syntax highlighting.

linux-6.19.14/fs/hfs/super.c C
  1/*
  2 *  linux/fs/hfs/super.c
  3 *
  4 * Copyright (C) 1995-1997  Paul H. Hargrove
  5 * (C) 2003 Ardis Technologies <roman@ardistech.com>
  6 * This file may be distributed under the terms of the GNU General Public License.
  7 *
  8 * This file contains hfs_read_super(), some of the super_ops and
  9 * init_hfs_fs() and exit_hfs_fs().  The remaining super_ops are in
 10 * inode.c since they deal with inodes.
 11 *
 12 * Based on the minix file system code, (C) 1991, 1992 by Linus Torvalds
 13 */
 14
 15#include <linux/module.h>
 16#include <linux/blkdev.h>
 17#include <linux/backing-dev.h>
 18#include <linux/fs_context.h>
 19#include <linux/fs_parser.h>
 20#include <linux/mount.h>
 21#include <linux/init.h>
 22#include <linux/nls.h>
 23#include <linux/seq_file.h>
 24#include <linux/slab.h>
 25#include <linux/vfs.h>
 26
 27#include "hfs_fs.h"
 28#include "btree.h"
 29
 30static struct kmem_cache *hfs_inode_cachep;
 31
 32MODULE_DESCRIPTION("Apple Macintosh file system support");
 33MODULE_LICENSE("GPL");
 34
 35static int hfs_sync_fs(struct super_block *sb, int wait)
 36{
 37	is_hfs_cnid_counts_valid(sb);
 38	hfs_mdb_commit(sb);
 39	return 0;
 40}
 41
 42/*
 43 * hfs_put_super()
 44 *
 45 * This is the put_super() entry in the super_operations structure for
 46 * HFS filesystems.  The purpose is to release the resources
 47 * associated with the superblock sb.
 48 */
 49static void hfs_put_super(struct super_block *sb)
 50{
 51	cancel_delayed_work_sync(&HFS_SB(sb)->mdb_work);
 52	hfs_mdb_close(sb);
 53	/* release the MDB's resources */
 54	hfs_mdb_put(sb);
 55}
 56
 57static void flush_mdb(struct work_struct *work)
 58{
 59	struct hfs_sb_info *sbi;
 60	struct super_block *sb;
 61
 62	sbi = container_of(work, struct hfs_sb_info, mdb_work.work);
 63	sb = sbi->sb;
 64
 65	spin_lock(&sbi->work_lock);
 66	sbi->work_queued = 0;
 67	spin_unlock(&sbi->work_lock);
 68
 69	is_hfs_cnid_counts_valid(sb);
 70
 71	hfs_mdb_commit(sb);
 72}
 73
 74void hfs_mark_mdb_dirty(struct super_block *sb)
 75{
 76	struct hfs_sb_info *sbi = HFS_SB(sb);
 77	unsigned long delay;
 78
 79	if (sb_rdonly(sb))
 80		return;
 81
 82	spin_lock(&sbi->work_lock);
 83	if (!sbi->work_queued) {
 84		delay = msecs_to_jiffies(dirty_writeback_interval * 10);
 85		queue_delayed_work(system_long_wq, &sbi->mdb_work, delay);
 86		sbi->work_queued = 1;
 87	}
 88	spin_unlock(&sbi->work_lock);
 89}
 90
 91/*
 92 * hfs_statfs()
 93 *
 94 * This is the statfs() entry in the super_operations structure for
 95 * HFS filesystems.  The purpose is to return various data about the
 96 * filesystem.
 97 *
 98 * changed f_files/f_ffree to reflect the fs_ablock/free_ablocks.
 99 */
100static int hfs_statfs(struct dentry *dentry, struct kstatfs *buf)
101{
102	struct super_block *sb = dentry->d_sb;
103	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
104
105	buf->f_type = HFS_SUPER_MAGIC;
106	buf->f_bsize = sb->s_blocksize;
107	buf->f_blocks = (u32)HFS_SB(sb)->fs_ablocks * HFS_SB(sb)->fs_div;
108	buf->f_bfree = (u32)HFS_SB(sb)->free_ablocks * HFS_SB(sb)->fs_div;
109	buf->f_bavail = buf->f_bfree;
110	buf->f_files = HFS_SB(sb)->fs_ablocks;
111	buf->f_ffree = HFS_SB(sb)->free_ablocks;
112	buf->f_fsid = u64_to_fsid(id);
113	buf->f_namelen = HFS_NAMELEN;
114
115	return 0;
116}
117
118static int hfs_reconfigure(struct fs_context *fc)
119{
120	struct super_block *sb = fc->root->d_sb;
121
122	sync_filesystem(sb);
123	fc->sb_flags |= SB_NODIRATIME;
124	if ((bool)(fc->sb_flags & SB_RDONLY) == sb_rdonly(sb))
125		return 0;
126
127	if (!(fc->sb_flags & SB_RDONLY)) {
128		if (!(HFS_SB(sb)->mdb->drAtrb & cpu_to_be16(HFS_SB_ATTRIB_UNMNT))) {
129			pr_warn("filesystem was not cleanly unmounted, running fsck.hfs is recommended.  leaving read-only.\n");
130			sb->s_flags |= SB_RDONLY;
131			fc->sb_flags |= SB_RDONLY;
132		} else if (HFS_SB(sb)->mdb->drAtrb & cpu_to_be16(HFS_SB_ATTRIB_SLOCK)) {
133			pr_warn("filesystem is marked locked, leaving read-only.\n");
134			sb->s_flags |= SB_RDONLY;
135			fc->sb_flags |= SB_RDONLY;
136		}
137	}
138	return 0;
139}
140
141static int hfs_show_options(struct seq_file *seq, struct dentry *root)
142{
143	struct hfs_sb_info *sbi = HFS_SB(root->d_sb);
144
145	if (sbi->s_creator != cpu_to_be32(0x3f3f3f3f))
146		seq_show_option_n(seq, "creator", (char *)&sbi->s_creator, 4);
147	if (sbi->s_type != cpu_to_be32(0x3f3f3f3f))
148		seq_show_option_n(seq, "type", (char *)&sbi->s_type, 4);
149	seq_printf(seq, ",uid=%u,gid=%u",
150			from_kuid_munged(&init_user_ns, sbi->s_uid),
151			from_kgid_munged(&init_user_ns, sbi->s_gid));
152	if (sbi->s_file_umask != 0133)
153		seq_printf(seq, ",file_umask=%o", sbi->s_file_umask);
154	if (sbi->s_dir_umask != 0022)
155		seq_printf(seq, ",dir_umask=%o", sbi->s_dir_umask);
156	if (sbi->part >= 0)
157		seq_printf(seq, ",part=%u", sbi->part);
158	if (sbi->session >= 0)
159		seq_printf(seq, ",session=%u", sbi->session);
160	if (sbi->nls_disk)
161		seq_printf(seq, ",codepage=%s", sbi->nls_disk->charset);
162	if (sbi->nls_io)
163		seq_printf(seq, ",iocharset=%s", sbi->nls_io->charset);
164	if (sbi->s_quiet)
165		seq_printf(seq, ",quiet");
166	return 0;
167}
168
169static struct inode *hfs_alloc_inode(struct super_block *sb)
170{
171	struct hfs_inode_info *i;
172
173	i = alloc_inode_sb(sb, hfs_inode_cachep, GFP_KERNEL);
174	return i ? &i->vfs_inode : NULL;
175}
176
177static void hfs_free_inode(struct inode *inode)
178{
179	kmem_cache_free(hfs_inode_cachep, HFS_I(inode));
180}
181
182static const struct super_operations hfs_super_operations = {
183	.alloc_inode	= hfs_alloc_inode,
184	.free_inode	= hfs_free_inode,
185	.write_inode	= hfs_write_inode,
186	.evict_inode	= hfs_evict_inode,
187	.put_super	= hfs_put_super,
188	.sync_fs	= hfs_sync_fs,
189	.statfs		= hfs_statfs,
190	.show_options	= hfs_show_options,
191};
192
193enum {
194	opt_uid, opt_gid, opt_umask, opt_file_umask, opt_dir_umask,
195	opt_part, opt_session, opt_type, opt_creator, opt_quiet,
196	opt_codepage, opt_iocharset,
197};
198
199static const struct fs_parameter_spec hfs_param_spec[] = {
200	fsparam_u32	("uid",		opt_uid),
201	fsparam_u32	("gid",		opt_gid),
202	fsparam_u32oct	("umask",	opt_umask),
203	fsparam_u32oct	("file_umask",	opt_file_umask),
204	fsparam_u32oct	("dir_umask",	opt_dir_umask),
205	fsparam_u32	("part",	opt_part),
206	fsparam_u32	("session",	opt_session),
207	fsparam_string	("type",	opt_type),
208	fsparam_string	("creator",	opt_creator),
209	fsparam_flag	("quiet",	opt_quiet),
210	fsparam_string	("codepage",	opt_codepage),
211	fsparam_string	("iocharset",	opt_iocharset),
212	{}
213};
214
215/*
216 * hfs_parse_param()
217 *
218 * This function is called by the vfs to parse the mount options.
219 */
220static int hfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
221{
222	struct hfs_sb_info *hsb = fc->s_fs_info;
223	struct fs_parse_result result;
224	int opt;
225
226	/* hfs does not honor any fs-specific options on remount */
227	if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE)
228		return 0;
229
230	opt = fs_parse(fc, hfs_param_spec, param, &result);
231	if (opt < 0)
232		return opt;
233
234	switch (opt) {
235	case opt_uid:
236		hsb->s_uid = result.uid;
237		break;
238	case opt_gid:
239		hsb->s_gid = result.gid;
240		break;
241	case opt_umask:
242		hsb->s_file_umask = (umode_t)result.uint_32;
243		hsb->s_dir_umask = (umode_t)result.uint_32;
244		break;
245	case opt_file_umask:
246		hsb->s_file_umask = (umode_t)result.uint_32;
247		break;
248	case opt_dir_umask:
249		hsb->s_dir_umask = (umode_t)result.uint_32;
250		break;
251	case opt_part:
252		hsb->part = result.uint_32;
253		break;
254	case opt_session:
255		hsb->session = result.uint_32;
256		break;
257	case opt_type:
258		if (strlen(param->string) != 4) {
259			pr_err("type requires a 4 character value\n");
260			return -EINVAL;
261		}
262		memcpy(&hsb->s_type, param->string, 4);
263		break;
264	case opt_creator:
265		if (strlen(param->string) != 4) {
266			pr_err("creator requires a 4 character value\n");
267			return -EINVAL;
268		}
269		memcpy(&hsb->s_creator, param->string, 4);
270		break;
271	case opt_quiet:
272		hsb->s_quiet = 1;
273		break;
274	case opt_codepage:
275		if (hsb->nls_disk) {
276			pr_err("unable to change codepage\n");
277			return -EINVAL;
278		}
279		hsb->nls_disk = load_nls(param->string);
280		if (!hsb->nls_disk) {
281			pr_err("unable to load codepage \"%s\"\n",
282					param->string);
283			return -EINVAL;
284		}
285		break;
286	case opt_iocharset:
287		if (hsb->nls_io) {
288			pr_err("unable to change iocharset\n");
289			return -EINVAL;
290		}
291		hsb->nls_io = load_nls(param->string);
292		if (!hsb->nls_io) {
293			pr_err("unable to load iocharset \"%s\"\n",
294					param->string);
295			return -EINVAL;
296		}
297		break;
298	default:
299		return -EINVAL;
300	}
301
302	return 0;
303}
304
305/*
306 * hfs_read_super()
307 *
308 * This is the function that is responsible for mounting an HFS
309 * filesystem.	It performs all the tasks necessary to get enough data
310 * from the disk to read the root inode.  This includes parsing the
311 * mount options, dealing with Macintosh partitions, reading the
312 * superblock and the allocation bitmap blocks, calling
313 * hfs_btree_init() to get the necessary data about the extents and
314 * catalog B-trees and, finally, reading the root inode into memory.
315 */
316static int hfs_fill_super(struct super_block *sb, struct fs_context *fc)
317{
318	struct hfs_sb_info *sbi = HFS_SB(sb);
319	struct hfs_find_data fd;
320	hfs_cat_rec rec;
321	struct inode *root_inode;
322	int silent = fc->sb_flags & SB_SILENT;
323	int res;
324
325	atomic64_set(&sbi->file_count, 0);
326	atomic64_set(&sbi->folder_count, 0);
327	atomic64_set(&sbi->next_id, 0);
328
329	/* load_nls_default does not fail */
330	if (sbi->nls_disk && !sbi->nls_io)
331		sbi->nls_io = load_nls_default();
332	sbi->s_dir_umask &= 0777;
333	sbi->s_file_umask &= 0577;
334
335	spin_lock_init(&sbi->work_lock);
336	INIT_DELAYED_WORK(&sbi->mdb_work, flush_mdb);
337
338	sbi->sb = sb;
339	sb->s_op = &hfs_super_operations;
340	sb->s_xattr = hfs_xattr_handlers;
341	sb->s_flags |= SB_NODIRATIME;
342	mutex_init(&sbi->bitmap_lock);
343
344	res = hfs_mdb_get(sb);
345	if (res) {
346		if (!silent)
347			pr_warn("can't find a HFS filesystem on dev %s\n",
348				hfs_mdb_name(sb));
349		res = -EINVAL;
350		goto bail;
351	}
352
353	/* try to get the root inode */
354	res = hfs_find_init(HFS_SB(sb)->cat_tree, &fd);
355	if (res)
356		goto bail_no_root;
357	res = hfs_cat_find_brec(sb, HFS_ROOT_CNID, &fd);
358	if (!res) {
359		if (fd.entrylength != sizeof(rec.dir)) {
360			res =  -EIO;
361			goto bail_hfs_find;
362		}
363		hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, fd.entrylength);
364		if (rec.type != HFS_CDR_DIR)
365			res = -EIO;
366	}
367	if (res)
368		goto bail_hfs_find;
369	res = -EINVAL;
370	root_inode = hfs_iget(sb, &fd.search_key->cat, &rec);
371	hfs_find_exit(&fd);
372	if (!root_inode)
373		goto bail_no_root;
374
375	set_default_d_op(sb, &hfs_dentry_operations);
376	res = -ENOMEM;
377	sb->s_root = d_make_root(root_inode);
378	if (!sb->s_root)
379		goto bail_no_root;
380
381	/* everything's okay */
382	return 0;
383
384bail_hfs_find:
385	hfs_find_exit(&fd);
386bail_no_root:
387	pr_err("get root inode failed\n");
388bail:
389	hfs_mdb_put(sb);
390	return res;
391}
392
393static int hfs_get_tree(struct fs_context *fc)
394{
395	return get_tree_bdev(fc, hfs_fill_super);
396}
397
398static void hfs_free_fc(struct fs_context *fc)
399{
400	kfree(fc->s_fs_info);
401}
402
403static const struct fs_context_operations hfs_context_ops = {
404	.parse_param	= hfs_parse_param,
405	.get_tree	= hfs_get_tree,
406	.reconfigure	= hfs_reconfigure,
407	.free		= hfs_free_fc,
408};
409
410static int hfs_init_fs_context(struct fs_context *fc)
411{
412	struct hfs_sb_info *hsb;
413
414	hsb = kzalloc(sizeof(struct hfs_sb_info), GFP_KERNEL);
415	if (!hsb)
416		return -ENOMEM;
417
418	fc->s_fs_info = hsb;
419	fc->ops = &hfs_context_ops;
420
421	if (fc->purpose != FS_CONTEXT_FOR_RECONFIGURE) {
422		/* initialize options with defaults */
423		hsb->s_uid = current_uid();
424		hsb->s_gid = current_gid();
425		hsb->s_file_umask = 0133;
426		hsb->s_dir_umask = 0022;
427		hsb->s_type = cpu_to_be32(0x3f3f3f3f); /* == '????' */
428		hsb->s_creator = cpu_to_be32(0x3f3f3f3f); /* == '????' */
429		hsb->s_quiet = 0;
430		hsb->part = -1;
431		hsb->session = -1;
432	}
433
434	return 0;
435}
436
437static void hfs_kill_super(struct super_block *sb)
438{
439	struct hfs_sb_info *hsb = HFS_SB(sb);
440
441	kill_block_super(sb);
442	kfree(hsb);
443}
444
445static struct file_system_type hfs_fs_type = {
446	.owner		= THIS_MODULE,
447	.name		= "hfs",
448	.kill_sb	= hfs_kill_super,
449	.fs_flags	= FS_REQUIRES_DEV,
450	.init_fs_context = hfs_init_fs_context,
451};
452MODULE_ALIAS_FS("hfs");
453
454static void hfs_init_once(void *p)
455{
456	struct hfs_inode_info *i = p;
457
458	inode_init_once(&i->vfs_inode);
459}
460
461static int __init init_hfs_fs(void)
462{
463	int err;
464
465	hfs_inode_cachep = kmem_cache_create("hfs_inode_cache",
466		sizeof(struct hfs_inode_info), 0,
467		SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT, hfs_init_once);
468	if (!hfs_inode_cachep)
469		return -ENOMEM;
470	err = register_filesystem(&hfs_fs_type);
471	if (err)
472		kmem_cache_destroy(hfs_inode_cachep);
473	return err;
474}
475
476static void __exit exit_hfs_fs(void)
477{
478	unregister_filesystem(&hfs_fs_type);
479
480	/*
481	 * Make sure all delayed rcu free inodes are flushed before we
482	 * destroy cache.
483	 */
484	rcu_barrier();
485	kmem_cache_destroy(hfs_inode_cachep);
486}
487
488module_init(init_hfs_fs)
489module_exit(exit_hfs_fs)
~/ Read-only file preview.