blob: 527f6e46cbe8128f1ea01b8ecd072c850bccf236 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/hfs/dir.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 directory-related functions independent of which
9 * scheme is being used to represent forks.
10 *
11 * Based on the minix file system code, (C) 1991, 1992 by Linus Torvalds
12 */
13
14#include "hfs_fs.h"
15#include "btree.h"
16
17/*
18 * hfs_lookup()
19 */
20static struct dentry *hfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -040021 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
23 hfs_cat_rec rec;
24 struct hfs_find_data fd;
25 struct inode *inode = NULL;
26 int res;
27
Alexey Khoroshilov9509f172013-04-30 15:27:52 -070028 res = hfs_find_init(HFS_SB(dir->i_sb)->cat_tree, &fd);
29 if (res)
30 return ERR_PTR(res);
Roman Zippel328b9222005-09-06 15:18:49 -070031 hfs_cat_build_key(dir->i_sb, fd.search_key, dir->i_ino, &dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 res = hfs_brec_read(&fd, &rec, sizeof(rec));
33 if (res) {
Al Viro6b9ccee2018-04-30 19:51:43 -040034 if (res != -ENOENT)
35 inode = ERR_PTR(res);
36 } else {
37 inode = hfs_iget(dir->i_sb, &fd.search_key->cat, &rec);
38 if (!inode)
39 inode = ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 hfs_find_exit(&fd);
Al Viro6b9ccee2018-04-30 19:51:43 -040042 return d_splice_alias(inode, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
44
45/*
46 * hfs_readdir
47 */
Al Viro002f8be2013-05-22 14:29:35 -040048static int hfs_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Al Viro002f8be2013-05-22 14:29:35 -040050 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 struct super_block *sb = inode->i_sb;
52 int len, err;
Roman Zippel328b9222005-09-06 15:18:49 -070053 char strbuf[HFS_MAX_NAMELEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 union hfs_cat_rec entry;
55 struct hfs_find_data fd;
56 struct hfs_readdir_data *rd;
57 u16 type;
58
Al Viro002f8be2013-05-22 14:29:35 -040059 if (ctx->pos >= inode->i_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return 0;
61
Alexey Khoroshilov9509f172013-04-30 15:27:52 -070062 err = hfs_find_init(HFS_SB(sb)->cat_tree, &fd);
63 if (err)
64 return err;
Roman Zippel328b9222005-09-06 15:18:49 -070065 hfs_cat_build_key(sb, fd.search_key, inode->i_ino, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 err = hfs_brec_find(&fd);
67 if (err)
68 goto out;
69
Al Viro002f8be2013-05-22 14:29:35 -040070 if (ctx->pos == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 /* This is completely artificial... */
Al Viro002f8be2013-05-22 14:29:35 -040072 if (!dir_emit_dot(file, ctx))
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 goto out;
Al Viro002f8be2013-05-22 14:29:35 -040074 ctx->pos = 1;
75 }
76 if (ctx->pos == 1) {
Amerigo Wangec81aec2009-12-14 17:57:37 -080077 if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
78 err = -EIO;
79 goto out;
80 }
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
83 if (entry.type != HFS_CDR_THD) {
Joe Perchesd6142672013-04-30 15:27:55 -070084 pr_err("bad catalog folder thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 err = -EIO;
86 goto out;
87 }
88 //if (fd.entrylength < HFS_MIN_THREAD_SZ) {
Joe Perchesd6142672013-04-30 15:27:55 -070089 // pr_err("truncated catalog thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 // err = -EIO;
91 // goto out;
92 //}
Al Viro002f8be2013-05-22 14:29:35 -040093 if (!dir_emit(ctx, "..", 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 be32_to_cpu(entry.thread.ParID), DT_DIR))
95 goto out;
Al Viro002f8be2013-05-22 14:29:35 -040096 ctx->pos = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
Al Viro002f8be2013-05-22 14:29:35 -040098 if (ctx->pos >= inode->i_size)
99 goto out;
100 err = hfs_brec_goto(&fd, ctx->pos - 1);
101 if (err)
102 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 for (;;) {
105 if (be32_to_cpu(fd.key->cat.ParID) != inode->i_ino) {
Joe Perchesd6142672013-04-30 15:27:55 -0700106 pr_err("walked past end of dir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 err = -EIO;
108 goto out;
109 }
Amerigo Wangec81aec2009-12-14 17:57:37 -0800110
111 if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
112 err = -EIO;
113 goto out;
114 }
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
117 type = entry.type;
Roman Zippel328b9222005-09-06 15:18:49 -0700118 len = hfs_mac2asc(sb, strbuf, &fd.key->cat.CName);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (type == HFS_CDR_DIR) {
120 if (fd.entrylength < sizeof(struct hfs_cat_dir)) {
Joe Perchesd6142672013-04-30 15:27:55 -0700121 pr_err("small dir entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 err = -EIO;
123 goto out;
124 }
Al Viro002f8be2013-05-22 14:29:35 -0400125 if (!dir_emit(ctx, strbuf, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 be32_to_cpu(entry.dir.DirID), DT_DIR))
127 break;
128 } else if (type == HFS_CDR_FIL) {
129 if (fd.entrylength < sizeof(struct hfs_cat_file)) {
Joe Perchesd6142672013-04-30 15:27:55 -0700130 pr_err("small file entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 err = -EIO;
132 goto out;
133 }
Al Viro002f8be2013-05-22 14:29:35 -0400134 if (!dir_emit(ctx, strbuf, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 be32_to_cpu(entry.file.FlNum), DT_REG))
136 break;
137 } else {
Joe Perchesd6142672013-04-30 15:27:55 -0700138 pr_err("bad catalog entry type %d\n", type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 err = -EIO;
140 goto out;
141 }
Al Viro002f8be2013-05-22 14:29:35 -0400142 ctx->pos++;
143 if (ctx->pos >= inode->i_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 goto out;
145 err = hfs_brec_goto(&fd, 1);
146 if (err)
147 goto out;
148 }
Al Viro002f8be2013-05-22 14:29:35 -0400149 rd = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (!rd) {
151 rd = kmalloc(sizeof(struct hfs_readdir_data), GFP_KERNEL);
152 if (!rd) {
153 err = -ENOMEM;
154 goto out;
155 }
Al Viro002f8be2013-05-22 14:29:35 -0400156 file->private_data = rd;
157 rd->file = file;
Al Viro9717a912016-05-12 20:13:50 -0400158 spin_lock(&HFS_I(inode)->open_dir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 list_add(&rd->list, &HFS_I(inode)->open_dir_list);
Al Viro9717a912016-05-12 20:13:50 -0400160 spin_unlock(&HFS_I(inode)->open_dir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
Al Viro9717a912016-05-12 20:13:50 -0400162 /*
163 * Can be done after the list insertion; exclusion with
164 * hfs_delete_cat() is provided by directory lock.
165 */
Dan Carpentereec11532017-01-18 14:13:20 +0300166 memcpy(&rd->key, &fd.key->cat, sizeof(struct hfs_cat_key));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167out:
168 hfs_find_exit(&fd);
169 return err;
170}
171
172static int hfs_dir_release(struct inode *inode, struct file *file)
173{
174 struct hfs_readdir_data *rd = file->private_data;
175 if (rd) {
Al Viro9717a912016-05-12 20:13:50 -0400176 spin_lock(&HFS_I(inode)->open_dir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 list_del(&rd->list);
Al Viro9717a912016-05-12 20:13:50 -0400178 spin_unlock(&HFS_I(inode)->open_dir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 kfree(rd);
180 }
181 return 0;
182}
183
184/*
185 * hfs_create()
186 *
187 * This is the create() entry in the inode_operations structure for
188 * regular HFS directories. The purpose is to create a new file in
189 * a directory and return a corresponding inode, given the inode for
190 * the directory and the name (and its length) of the new file.
191 */
Christian Brauner549c7292021-01-21 14:19:43 +0100192static int hfs_create(struct user_namespace *mnt_userns, struct inode *dir,
193 struct dentry *dentry, umode_t mode, bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 struct inode *inode;
196 int res;
197
198 inode = hfs_new_inode(dir, &dentry->d_name, mode);
199 if (!inode)
Chengyu Song13f24482015-04-16 12:46:53 -0700200 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 res = hfs_cat_create(inode->i_ino, dir, &dentry->d_name, inode);
203 if (res) {
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200204 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 hfs_delete_inode(inode);
206 iput(inode);
207 return res;
208 }
209 d_instantiate(dentry, inode);
210 mark_inode_dirty(inode);
211 return 0;
212}
213
214/*
215 * hfs_mkdir()
216 *
217 * This is the mkdir() entry in the inode_operations structure for
218 * regular HFS directories. The purpose is to create a new directory
219 * in a directory, given the inode for the parent directory and the
220 * name (and its length) of the new directory.
221 */
Christian Brauner549c7292021-01-21 14:19:43 +0100222static int hfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
223 struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 struct inode *inode;
226 int res;
227
228 inode = hfs_new_inode(dir, &dentry->d_name, S_IFDIR | mode);
229 if (!inode)
Chengyu Song13f24482015-04-16 12:46:53 -0700230 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 res = hfs_cat_create(inode->i_ino, dir, &dentry->d_name, inode);
233 if (res) {
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200234 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 hfs_delete_inode(inode);
236 iput(inode);
237 return res;
238 }
239 d_instantiate(dentry, inode);
240 mark_inode_dirty(inode);
241 return 0;
242}
243
244/*
Al Viro69102e92011-03-02 23:46:51 -0500245 * hfs_remove()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 *
Al Viro69102e92011-03-02 23:46:51 -0500247 * This serves as both unlink() and rmdir() in the inode_operations
248 * structure for regular HFS directories. The purpose is to delete
249 * an existing child, given the inode for the parent directory and
250 * the name (and its length) of the existing directory.
251 *
252 * HFS does not have hardlinks, so both rmdir and unlink set the
253 * link count to 0. The only difference is the emptiness check.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 */
Al Viro69102e92011-03-02 23:46:51 -0500255static int hfs_remove(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
David Howells2b0143b2015-03-17 22:25:59 +0000257 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 int res;
259
Al Viro69102e92011-03-02 23:46:51 -0500260 if (S_ISDIR(inode->i_mode) && inode->i_size != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return -ENOTEMPTY;
262 res = hfs_cat_delete(inode->i_ino, dir, &dentry->d_name);
263 if (res)
264 return res;
Dave Hansence71ec32006-09-30 23:29:06 -0700265 clear_nlink(inode);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700266 inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 hfs_delete_inode(inode);
268 mark_inode_dirty(inode);
269 return 0;
270}
271
272/*
273 * hfs_rename()
274 *
275 * This is the rename() entry in the inode_operations structure for
276 * regular HFS directories. The purpose is to rename an existing
277 * file or directory, given the inode for the current directory and
278 * the name (and its length) of the existing file/directory and the
279 * inode for the new directory and the name (and its length) of the
280 * new file/directory.
281 * XXX: how do you handle must_be dir?
282 */
Christian Brauner549c7292021-01-21 14:19:43 +0100283static int hfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
284 struct dentry *old_dentry, struct inode *new_dir,
285 struct dentry *new_dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 int res;
288
Miklos Szeredif03b8ad2016-09-27 11:03:57 +0200289 if (flags & ~RENAME_NOREPLACE)
290 return -EINVAL;
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 /* Unlink destination if it already exists */
David Howells2b0143b2015-03-17 22:25:59 +0000293 if (d_really_is_positive(new_dentry)) {
Al Viro69102e92011-03-02 23:46:51 -0500294 res = hfs_remove(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (res)
296 return res;
297 }
298
David Howells2b0143b2015-03-17 22:25:59 +0000299 res = hfs_cat_move(d_inode(old_dentry)->i_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 old_dir, &old_dentry->d_name,
301 new_dir, &new_dentry->d_name);
302 if (!res)
Roman Zippel328b9222005-09-06 15:18:49 -0700303 hfs_cat_build_key(old_dir->i_sb,
David Howells2b0143b2015-03-17 22:25:59 +0000304 (btree_key *)&HFS_I(d_inode(old_dentry))->cat_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 new_dir->i_ino, &new_dentry->d_name);
306 return res;
307}
308
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800309const struct file_operations hfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 .read = generic_read_dir,
Al Viro9717a912016-05-12 20:13:50 -0400311 .iterate_shared = hfs_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 .llseek = generic_file_llseek,
313 .release = hfs_dir_release,
314};
315
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800316const struct inode_operations hfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 .create = hfs_create,
318 .lookup = hfs_lookup,
Al Viro69102e92011-03-02 23:46:51 -0500319 .unlink = hfs_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 .mkdir = hfs_mkdir,
Al Viro69102e92011-03-02 23:46:51 -0500321 .rmdir = hfs_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 .rename = hfs_rename,
323 .setattr = hfs_inode_setattr,
324};