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.
8 * This file contains inode-related functions which do not depend on
9 * which scheme is being used to represent forks.
11 * Based on the minix file system code, (C) 1991, 1992 by Linus Torvalds
14 #include <linux/pagemap.h>
15 #include <linux/mpage.h>
16 #include <linux/sched.h>
17 #include <linux/cred.h>
18 #include <linux/uio.h>
19 #include <linux/xattr.h>
20 #include <linux/blkdev.h>
25 static const struct file_operations hfs_file_operations;
26 static const struct inode_operations hfs_file_inode_operations;
28 /*================ Variable-like macros ================*/
30 #define HFS_VALID_MODE_BITS (S_IFREG | S_IFDIR | S_IRWXUGO)
32 static int hfs_writepage(struct page *page, struct writeback_control *wbc)
34 return block_write_full_page(page, hfs_get_block, wbc);
37 static int hfs_read_folio(struct file *file, struct folio *folio)
39 return block_read_full_folio(folio, hfs_get_block);
42 static void hfs_write_failed(struct address_space *mapping, loff_t to)
44 struct inode *inode = mapping->host;
46 if (to > inode->i_size) {
47 truncate_pagecache(inode, inode->i_size);
48 hfs_file_truncate(inode);
52 int hfs_write_begin(struct file *file, struct address_space *mapping,
53 loff_t pos, unsigned len, struct page **pagep, void **fsdata)
58 ret = cont_write_begin(file, mapping, pos, len, pagep, fsdata,
60 &HFS_I(mapping->host)->phys_size);
62 hfs_write_failed(mapping, pos + len);
67 static sector_t hfs_bmap(struct address_space *mapping, sector_t block)
69 return generic_block_bmap(mapping, block, hfs_get_block);
72 static bool hfs_release_folio(struct folio *folio, gfp_t mask)
74 struct inode *inode = folio->mapping->host;
75 struct super_block *sb = inode->i_sb;
76 struct hfs_btree *tree;
77 struct hfs_bnode *node;
82 switch (inode->i_ino) {
84 tree = HFS_SB(sb)->ext_tree;
87 tree = HFS_SB(sb)->cat_tree;
97 if (tree->node_size >= PAGE_SIZE) {
98 nidx = folio->index >> (tree->node_size_shift - PAGE_SHIFT);
99 spin_lock(&tree->hash_lock);
100 node = hfs_bnode_findhash(tree, nidx);
103 else if (atomic_read(&node->refcnt))
106 hfs_bnode_unhash(node);
107 hfs_bnode_free(node);
109 spin_unlock(&tree->hash_lock);
111 nidx = folio->index << (PAGE_SHIFT - tree->node_size_shift);
112 i = 1 << (PAGE_SHIFT - tree->node_size_shift);
113 spin_lock(&tree->hash_lock);
115 node = hfs_bnode_findhash(tree, nidx++);
118 if (atomic_read(&node->refcnt)) {
122 hfs_bnode_unhash(node);
123 hfs_bnode_free(node);
124 } while (--i && nidx < tree->node_count);
125 spin_unlock(&tree->hash_lock);
127 return res ? try_to_free_buffers(folio) : false;
130 static ssize_t hfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
132 struct file *file = iocb->ki_filp;
133 struct address_space *mapping = file->f_mapping;
134 struct inode *inode = mapping->host;
135 size_t count = iov_iter_count(iter);
138 ret = blockdev_direct_IO(iocb, inode, iter, hfs_get_block);
141 * In case of error extending write may have instantiated a few
142 * blocks outside i_size. Trim these off again.
144 if (unlikely(iov_iter_rw(iter) == WRITE && ret < 0)) {
145 loff_t isize = i_size_read(inode);
146 loff_t end = iocb->ki_pos + count;
149 hfs_write_failed(mapping, end);
155 static int hfs_writepages(struct address_space *mapping,
156 struct writeback_control *wbc)
158 return mpage_writepages(mapping, wbc, hfs_get_block);
161 const struct address_space_operations hfs_btree_aops = {
162 .dirty_folio = block_dirty_folio,
163 .invalidate_folio = block_invalidate_folio,
164 .read_folio = hfs_read_folio,
165 .writepage = hfs_writepage,
166 .write_begin = hfs_write_begin,
167 .write_end = generic_write_end,
169 .release_folio = hfs_release_folio,
172 const struct address_space_operations hfs_aops = {
173 .dirty_folio = block_dirty_folio,
174 .invalidate_folio = block_invalidate_folio,
175 .read_folio = hfs_read_folio,
176 .write_begin = hfs_write_begin,
177 .write_end = generic_write_end,
179 .direct_IO = hfs_direct_IO,
180 .writepages = hfs_writepages,
181 .migrate_folio = buffer_migrate_folio,
187 struct inode *hfs_new_inode(struct inode *dir, const struct qstr *name, umode_t mode)
189 struct super_block *sb = dir->i_sb;
190 struct inode *inode = new_inode(sb);
194 mutex_init(&HFS_I(inode)->extents_lock);
195 INIT_LIST_HEAD(&HFS_I(inode)->open_dir_list);
196 spin_lock_init(&HFS_I(inode)->open_dir_lock);
197 hfs_cat_build_key(sb, (btree_key *)&HFS_I(inode)->cat_key, dir->i_ino, name);
198 inode->i_ino = HFS_SB(sb)->next_id++;
199 inode->i_mode = mode;
200 inode->i_uid = current_fsuid();
201 inode->i_gid = current_fsgid();
203 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
204 HFS_I(inode)->flags = 0;
205 HFS_I(inode)->rsrc_inode = NULL;
206 HFS_I(inode)->fs_blocks = 0;
209 HFS_SB(sb)->folder_count++;
210 if (dir->i_ino == HFS_ROOT_CNID)
211 HFS_SB(sb)->root_dirs++;
212 inode->i_op = &hfs_dir_inode_operations;
213 inode->i_fop = &hfs_dir_operations;
214 inode->i_mode |= S_IRWXUGO;
215 inode->i_mode &= ~HFS_SB(inode->i_sb)->s_dir_umask;
216 } else if (S_ISREG(mode)) {
217 HFS_I(inode)->clump_blocks = HFS_SB(sb)->clumpablks;
218 HFS_SB(sb)->file_count++;
219 if (dir->i_ino == HFS_ROOT_CNID)
220 HFS_SB(sb)->root_files++;
221 inode->i_op = &hfs_file_inode_operations;
222 inode->i_fop = &hfs_file_operations;
223 inode->i_mapping->a_ops = &hfs_aops;
224 inode->i_mode |= S_IRUGO|S_IXUGO;
226 inode->i_mode |= S_IWUGO;
227 inode->i_mode &= ~HFS_SB(inode->i_sb)->s_file_umask;
228 HFS_I(inode)->phys_size = 0;
229 HFS_I(inode)->alloc_blocks = 0;
230 HFS_I(inode)->first_blocks = 0;
231 HFS_I(inode)->cached_start = 0;
232 HFS_I(inode)->cached_blocks = 0;
233 memset(HFS_I(inode)->first_extents, 0, sizeof(hfs_extent_rec));
234 memset(HFS_I(inode)->cached_extents, 0, sizeof(hfs_extent_rec));
236 insert_inode_hash(inode);
237 mark_inode_dirty(inode);
238 set_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags);
239 hfs_mark_mdb_dirty(sb);
244 void hfs_delete_inode(struct inode *inode)
246 struct super_block *sb = inode->i_sb;
248 hfs_dbg(INODE, "delete_inode: %lu\n", inode->i_ino);
249 if (S_ISDIR(inode->i_mode)) {
250 HFS_SB(sb)->folder_count--;
251 if (HFS_I(inode)->cat_key.ParID == cpu_to_be32(HFS_ROOT_CNID))
252 HFS_SB(sb)->root_dirs--;
253 set_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags);
254 hfs_mark_mdb_dirty(sb);
257 HFS_SB(sb)->file_count--;
258 if (HFS_I(inode)->cat_key.ParID == cpu_to_be32(HFS_ROOT_CNID))
259 HFS_SB(sb)->root_files--;
260 if (S_ISREG(inode->i_mode)) {
261 if (!inode->i_nlink) {
263 hfs_file_truncate(inode);
266 set_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags);
267 hfs_mark_mdb_dirty(sb);
270 void hfs_inode_read_fork(struct inode *inode, struct hfs_extent *ext,
271 __be32 __log_size, __be32 phys_size, u32 clump_size)
273 struct super_block *sb = inode->i_sb;
274 u32 log_size = be32_to_cpu(__log_size);
278 memcpy(HFS_I(inode)->first_extents, ext, sizeof(hfs_extent_rec));
279 for (count = 0, i = 0; i < 3; i++)
280 count += be16_to_cpu(ext[i].count);
281 HFS_I(inode)->first_blocks = count;
283 inode->i_size = HFS_I(inode)->phys_size = log_size;
284 HFS_I(inode)->fs_blocks = (log_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
285 inode_set_bytes(inode, HFS_I(inode)->fs_blocks << sb->s_blocksize_bits);
286 HFS_I(inode)->alloc_blocks = be32_to_cpu(phys_size) /
287 HFS_SB(sb)->alloc_blksz;
288 HFS_I(inode)->clump_blocks = clump_size / HFS_SB(sb)->alloc_blksz;
289 if (!HFS_I(inode)->clump_blocks)
290 HFS_I(inode)->clump_blocks = HFS_SB(sb)->clumpablks;
293 struct hfs_iget_data {
294 struct hfs_cat_key *key;
298 static int hfs_test_inode(struct inode *inode, void *data)
300 struct hfs_iget_data *idata = data;
306 return inode->i_ino == be32_to_cpu(rec->dir.DirID);
308 return inode->i_ino == be32_to_cpu(rec->file.FlNum);
318 static int hfs_read_inode(struct inode *inode, void *data)
320 struct hfs_iget_data *idata = data;
321 struct hfs_sb_info *hsb = HFS_SB(inode->i_sb);
324 HFS_I(inode)->flags = 0;
325 HFS_I(inode)->rsrc_inode = NULL;
326 mutex_init(&HFS_I(inode)->extents_lock);
327 INIT_LIST_HEAD(&HFS_I(inode)->open_dir_list);
328 spin_lock_init(&HFS_I(inode)->open_dir_lock);
330 /* Initialize the inode */
331 inode->i_uid = hsb->s_uid;
332 inode->i_gid = hsb->s_gid;
336 HFS_I(inode)->cat_key = *idata->key;
338 HFS_I(inode)->flags |= HFS_FLG_RSRC;
339 HFS_I(inode)->tz_secondswest = sys_tz.tz_minuteswest * 60;
344 if (!HFS_IS_RSRC(inode)) {
345 hfs_inode_read_fork(inode, rec->file.ExtRec, rec->file.LgLen,
346 rec->file.PyLen, be16_to_cpu(rec->file.ClpSize));
348 hfs_inode_read_fork(inode, rec->file.RExtRec, rec->file.RLgLen,
349 rec->file.RPyLen, be16_to_cpu(rec->file.ClpSize));
352 inode->i_ino = be32_to_cpu(rec->file.FlNum);
353 inode->i_mode = S_IRUGO | S_IXUGO;
354 if (!(rec->file.Flags & HFS_FIL_LOCK))
355 inode->i_mode |= S_IWUGO;
356 inode->i_mode &= ~hsb->s_file_umask;
357 inode->i_mode |= S_IFREG;
358 inode->i_ctime = inode->i_atime = inode->i_mtime =
359 hfs_m_to_utime(rec->file.MdDat);
360 inode->i_op = &hfs_file_inode_operations;
361 inode->i_fop = &hfs_file_operations;
362 inode->i_mapping->a_ops = &hfs_aops;
365 inode->i_ino = be32_to_cpu(rec->dir.DirID);
366 inode->i_size = be16_to_cpu(rec->dir.Val) + 2;
367 HFS_I(inode)->fs_blocks = 0;
368 inode->i_mode = S_IFDIR | (S_IRWXUGO & ~hsb->s_dir_umask);
369 inode->i_ctime = inode->i_atime = inode->i_mtime =
370 hfs_m_to_utime(rec->dir.MdDat);
371 inode->i_op = &hfs_dir_inode_operations;
372 inode->i_fop = &hfs_dir_operations;
375 make_bad_inode(inode);
383 * Given the MDB for a HFS filesystem, a 'key' and an 'entry' in
384 * the catalog B-tree and the 'type' of the desired file return the
385 * inode for that file/directory or NULL. Note that 'type' indicates
386 * whether we want the actual file or directory, or the corresponding
387 * metadata (AppleDouble header file or CAP metadata file).
389 struct inode *hfs_iget(struct super_block *sb, struct hfs_cat_key *key, hfs_cat_rec *rec)
391 struct hfs_iget_data data = { key, rec };
397 cnid = be32_to_cpu(rec->dir.DirID);
400 cnid = be32_to_cpu(rec->file.FlNum);
405 inode = iget5_locked(sb, cnid, hfs_test_inode, hfs_read_inode, &data);
406 if (inode && (inode->i_state & I_NEW))
407 unlock_new_inode(inode);
411 void hfs_inode_write_fork(struct inode *inode, struct hfs_extent *ext,
412 __be32 *log_size, __be32 *phys_size)
414 memcpy(ext, HFS_I(inode)->first_extents, sizeof(hfs_extent_rec));
417 *log_size = cpu_to_be32(inode->i_size);
419 *phys_size = cpu_to_be32(HFS_I(inode)->alloc_blocks *
420 HFS_SB(inode->i_sb)->alloc_blksz);
423 int hfs_write_inode(struct inode *inode, struct writeback_control *wbc)
425 struct inode *main_inode = inode;
426 struct hfs_find_data fd;
430 hfs_dbg(INODE, "hfs_write_inode: %lu\n", inode->i_ino);
431 res = hfs_ext_write_extent(inode);
435 if (inode->i_ino < HFS_FIRSTUSER_CNID) {
436 switch (inode->i_ino) {
440 hfs_btree_write(HFS_SB(inode->i_sb)->ext_tree);
443 hfs_btree_write(HFS_SB(inode->i_sb)->cat_tree);
451 if (HFS_IS_RSRC(inode))
452 main_inode = HFS_I(inode)->rsrc_inode;
454 if (!main_inode->i_nlink)
457 if (hfs_find_init(HFS_SB(main_inode->i_sb)->cat_tree, &fd))
461 if (HFS_I(main_inode)->cat_key.CName.len > HFS_NAMELEN)
463 fd.search_key->cat = HFS_I(main_inode)->cat_key;
464 if (hfs_brec_find(&fd))
468 if (S_ISDIR(main_inode->i_mode)) {
469 WARN_ON(fd.entrylength < sizeof(struct hfs_cat_dir));
470 hfs_bnode_read(fd.bnode, &rec, fd.entryoffset,
471 sizeof(struct hfs_cat_dir));
472 if (rec.type != HFS_CDR_DIR ||
473 be32_to_cpu(rec.dir.DirID) != inode->i_ino) {
476 rec.dir.MdDat = hfs_u_to_mtime(inode->i_mtime);
477 rec.dir.Val = cpu_to_be16(inode->i_size - 2);
479 hfs_bnode_write(fd.bnode, &rec, fd.entryoffset,
480 sizeof(struct hfs_cat_dir));
481 } else if (HFS_IS_RSRC(inode)) {
482 hfs_bnode_read(fd.bnode, &rec, fd.entryoffset,
483 sizeof(struct hfs_cat_file));
484 hfs_inode_write_fork(inode, rec.file.RExtRec,
485 &rec.file.RLgLen, &rec.file.RPyLen);
486 hfs_bnode_write(fd.bnode, &rec, fd.entryoffset,
487 sizeof(struct hfs_cat_file));
489 WARN_ON(fd.entrylength < sizeof(struct hfs_cat_file));
490 hfs_bnode_read(fd.bnode, &rec, fd.entryoffset,
491 sizeof(struct hfs_cat_file));
492 if (rec.type != HFS_CDR_FIL ||
493 be32_to_cpu(rec.file.FlNum) != inode->i_ino) {
496 if (inode->i_mode & S_IWUSR)
497 rec.file.Flags &= ~HFS_FIL_LOCK;
499 rec.file.Flags |= HFS_FIL_LOCK;
500 hfs_inode_write_fork(inode, rec.file.ExtRec, &rec.file.LgLen, &rec.file.PyLen);
501 rec.file.MdDat = hfs_u_to_mtime(inode->i_mtime);
503 hfs_bnode_write(fd.bnode, &rec, fd.entryoffset,
504 sizeof(struct hfs_cat_file));
511 static struct dentry *hfs_file_lookup(struct inode *dir, struct dentry *dentry,
514 struct inode *inode = NULL;
516 struct hfs_find_data fd;
519 if (HFS_IS_RSRC(dir) || strcmp(dentry->d_name.name, "rsrc"))
522 inode = HFS_I(dir)->rsrc_inode;
526 inode = new_inode(dir->i_sb);
528 return ERR_PTR(-ENOMEM);
530 res = hfs_find_init(HFS_SB(dir->i_sb)->cat_tree, &fd);
535 fd.search_key->cat = HFS_I(dir)->cat_key;
536 res = hfs_brec_read(&fd, &rec, sizeof(rec));
538 struct hfs_iget_data idata = { NULL, &rec };
539 hfs_read_inode(inode, &idata);
546 HFS_I(inode)->rsrc_inode = dir;
547 HFS_I(dir)->rsrc_inode = inode;
549 inode_fake_hash(inode);
550 mark_inode_dirty(inode);
553 return d_splice_alias(inode, dentry);
556 void hfs_evict_inode(struct inode *inode)
558 truncate_inode_pages_final(&inode->i_data);
560 if (HFS_IS_RSRC(inode) && HFS_I(inode)->rsrc_inode) {
561 HFS_I(HFS_I(inode)->rsrc_inode)->rsrc_inode = NULL;
562 iput(HFS_I(inode)->rsrc_inode);
566 static int hfs_file_open(struct inode *inode, struct file *file)
568 if (HFS_IS_RSRC(inode))
569 inode = HFS_I(inode)->rsrc_inode;
570 atomic_inc(&HFS_I(inode)->opencnt);
574 static int hfs_file_release(struct inode *inode, struct file *file)
576 //struct super_block *sb = inode->i_sb;
578 if (HFS_IS_RSRC(inode))
579 inode = HFS_I(inode)->rsrc_inode;
580 if (atomic_dec_and_test(&HFS_I(inode)->opencnt)) {
582 hfs_file_truncate(inode);
583 //if (inode->i_flags & S_DEAD) {
584 // hfs_delete_cat(inode->i_ino, HFSPLUS_SB(sb).hidden_dir, NULL);
585 // hfs_delete_inode(inode);
593 * hfs_notify_change()
595 * Based very closely on fs/msdos/inode.c by Werner Almesberger
597 * This is the notify_change() field in the super_operations structure
598 * for HFS file systems. The purpose is to take that changes made to
599 * an inode and apply then in a filesystem-dependent manner. In this
600 * case the process has a few of tasks to do:
601 * 1) prevent changes to the i_uid and i_gid fields.
602 * 2) map file permissions to the closest allowable permissions
603 * 3) Since multiple Linux files can share the same on-disk inode under
604 * HFS (for instance the data and resource forks of a file) a change
605 * to permissions must be applied to all other in-core inodes which
606 * correspond to the same HFS file.
609 int hfs_inode_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
612 struct inode *inode = d_inode(dentry);
613 struct hfs_sb_info *hsb = HFS_SB(inode->i_sb);
616 error = setattr_prepare(&nop_mnt_idmap, dentry,
617 attr); /* basic permission checks */
621 /* no uig/gid changes and limit which mode bits can be set */
622 if (((attr->ia_valid & ATTR_UID) &&
623 (!uid_eq(attr->ia_uid, hsb->s_uid))) ||
624 ((attr->ia_valid & ATTR_GID) &&
625 (!gid_eq(attr->ia_gid, hsb->s_gid))) ||
626 ((attr->ia_valid & ATTR_MODE) &&
627 ((S_ISDIR(inode->i_mode) &&
628 (attr->ia_mode != inode->i_mode)) ||
629 (attr->ia_mode & ~HFS_VALID_MODE_BITS)))) {
630 return hsb->s_quiet ? 0 : error;
633 if (attr->ia_valid & ATTR_MODE) {
634 /* Only the 'w' bits can ever change and only all together. */
635 if (attr->ia_mode & S_IWUSR)
636 attr->ia_mode = inode->i_mode | S_IWUGO;
638 attr->ia_mode = inode->i_mode & ~S_IWUGO;
639 attr->ia_mode &= S_ISDIR(inode->i_mode) ? ~hsb->s_dir_umask: ~hsb->s_file_umask;
642 if ((attr->ia_valid & ATTR_SIZE) &&
643 attr->ia_size != i_size_read(inode)) {
644 inode_dio_wait(inode);
646 error = inode_newsize_ok(inode, attr->ia_size);
650 truncate_setsize(inode, attr->ia_size);
651 hfs_file_truncate(inode);
652 inode->i_atime = inode->i_mtime = inode->i_ctime =
656 setattr_copy(&nop_mnt_idmap, inode, attr);
657 mark_inode_dirty(inode);
661 static int hfs_file_fsync(struct file *filp, loff_t start, loff_t end,
664 struct inode *inode = filp->f_mapping->host;
665 struct super_block * sb;
668 ret = file_write_and_wait_range(filp, start, end);
673 /* sync the inode to buffers */
674 ret = write_inode_now(inode, 0);
676 /* sync the superblock to buffers */
678 flush_delayed_work(&HFS_SB(sb)->mdb_work);
679 /* .. finally sync the buffers to disk */
680 err = sync_blockdev(sb->s_bdev);
687 static const struct file_operations hfs_file_operations = {
688 .llseek = generic_file_llseek,
689 .read_iter = generic_file_read_iter,
690 .write_iter = generic_file_write_iter,
691 .mmap = generic_file_mmap,
692 .splice_read = generic_file_splice_read,
693 .fsync = hfs_file_fsync,
694 .open = hfs_file_open,
695 .release = hfs_file_release,
698 static const struct inode_operations hfs_file_inode_operations = {
699 .lookup = hfs_file_lookup,
700 .setattr = hfs_inode_setattr,
701 .listxattr = generic_listxattr,