dir.c 12.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 *  linux/fs/hfsplus/dir.c
 *
 * Copyright (C) 2001
 * Brad Boyer (flar@allandria.com)
 * (C) 2003 Ardis Technologies <roman@ardistech.com>
 *
 * Handling of directories
 */

#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/random.h>

#include "hfsplus_fs.h"
#include "hfsplus_raw.h"
18
#include "xattr.h"
L
Linus Torvalds 已提交
19 20 21 22 23 24 25 26 27 28

static inline void hfsplus_instantiate(struct dentry *dentry,
				       struct inode *inode, u32 cnid)
{
	dentry->d_fsdata = (void *)(unsigned long)cnid;
	d_instantiate(dentry, inode);
}

/* Find the entry inside dir named dentry->d_name */
static struct dentry *hfsplus_lookup(struct inode *dir, struct dentry *dentry,
A
Al Viro 已提交
29
				     unsigned int flags)
L
Linus Torvalds 已提交
30 31 32 33 34 35 36 37 38 39
{
	struct inode *inode = NULL;
	struct hfs_find_data fd;
	struct super_block *sb;
	hfsplus_cat_entry entry;
	int err;
	u32 cnid, linkid = 0;
	u16 type;

	sb = dir->i_sb;
40

L
Linus Torvalds 已提交
41
	dentry->d_fsdata = NULL;
42 43 44
	err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
	if (err)
		return ERR_PTR(err);
L
Linus Torvalds 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
	hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, &dentry->d_name);
again:
	err = hfs_brec_read(&fd, &entry, sizeof(entry));
	if (err) {
		if (err == -ENOENT) {
			hfs_find_exit(&fd);
			/* No such entry */
			inode = NULL;
			goto out;
		}
		goto fail;
	}
	type = be16_to_cpu(entry.type);
	if (type == HFSPLUS_FOLDER) {
		if (fd.entrylength < sizeof(struct hfsplus_cat_folder)) {
			err = -EIO;
			goto fail;
		}
		cnid = be32_to_cpu(entry.folder.id);
		dentry->d_fsdata = (void *)(unsigned long)cnid;
	} else if (type == HFSPLUS_FILE) {
		if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
			err = -EIO;
			goto fail;
		}
		cnid = be32_to_cpu(entry.file.id);
71 72 73 74 75 76 77 78 79 80 81
		if (entry.file.user_info.fdType ==
				cpu_to_be32(HFSP_HARDLINK_TYPE) &&
				entry.file.user_info.fdCreator ==
				cpu_to_be32(HFSP_HFSPLUS_CREATOR) &&
				(entry.file.create_date ==
					HFSPLUS_I(HFSPLUS_SB(sb)->hidden_dir)->
						create_date ||
				entry.file.create_date ==
					HFSPLUS_I(sb->s_root->d_inode)->
						create_date) &&
				HFSPLUS_SB(sb)->hidden_dir) {
L
Linus Torvalds 已提交
82 83 84 85
			struct qstr str;
			char name[32];

			if (dentry->d_fsdata) {
86 87 88 89 90 91 92 93
				/*
				 * We found a link pointing to another link,
				 * so ignore it and treat it as regular file.
				 */
				cnid = (unsigned long)dentry->d_fsdata;
				linkid = 0;
			} else {
				dentry->d_fsdata = (void *)(unsigned long)cnid;
94 95
				linkid =
					be32_to_cpu(entry.file.permissions.dev);
96 97
				str.len = sprintf(name, "iNode%d", linkid);
				str.name = name;
98
				hfsplus_cat_build_key(sb, fd.search_key,
99 100
					HFSPLUS_SB(sb)->hidden_dir->i_ino,
					&str);
101
				goto again;
L
Linus Torvalds 已提交
102 103 104 105
			}
		} else if (!dentry->d_fsdata)
			dentry->d_fsdata = (void *)(unsigned long)cnid;
	} else {
106
		pr_err("invalid catalog entry type in lookup\n");
L
Linus Torvalds 已提交
107 108 109 110
		err = -EIO;
		goto fail;
	}
	hfs_find_exit(&fd);
111 112 113
	inode = hfsplus_iget(dir->i_sb, cnid);
	if (IS_ERR(inode))
		return ERR_CAST(inode);
L
Linus Torvalds 已提交
114
	if (S_ISREG(inode->i_mode))
C
Christoph Hellwig 已提交
115
		HFSPLUS_I(inode)->linkid = linkid;
L
Linus Torvalds 已提交
116 117 118 119 120 121 122 123
out:
	d_add(dentry, inode);
	return NULL;
fail:
	hfs_find_exit(&fd);
	return ERR_PTR(err);
}

A
Al Viro 已提交
124
static int hfsplus_readdir(struct file *file, struct dir_context *ctx)
L
Linus Torvalds 已提交
125
{
A
Al Viro 已提交
126
	struct inode *inode = file_inode(file);
L
Linus Torvalds 已提交
127 128 129 130 131 132 133 134
	struct super_block *sb = inode->i_sb;
	int len, err;
	char strbuf[HFSPLUS_MAX_STRLEN + 1];
	hfsplus_cat_entry entry;
	struct hfs_find_data fd;
	struct hfsplus_readdir_data *rd;
	u16 type;

A
Al Viro 已提交
135
	if (file->f_pos >= inode->i_size)
L
Linus Torvalds 已提交
136 137
		return 0;

138 139 140
	err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
	if (err)
		return err;
L
Linus Torvalds 已提交
141
	hfsplus_cat_build_key(sb, fd.search_key, inode->i_ino, NULL);
142
	err = hfs_brec_find(&fd, hfs_find_rec_by_key);
L
Linus Torvalds 已提交
143 144 145
	if (err)
		goto out;

A
Al Viro 已提交
146
	if (ctx->pos == 0) {
L
Linus Torvalds 已提交
147
		/* This is completely artificial... */
A
Al Viro 已提交
148
		if (!dir_emit_dot(file, ctx))
L
Linus Torvalds 已提交
149
			goto out;
A
Al Viro 已提交
150 151 152
		ctx->pos = 1;
	}
	if (ctx->pos == 1) {
153 154 155 156 157
		if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
			err = -EIO;
			goto out;
		}

158 159
		hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
			fd.entrylength);
L
Linus Torvalds 已提交
160
		if (be16_to_cpu(entry.type) != HFSPLUS_FOLDER_THREAD) {
161
			pr_err("bad catalog folder thread\n");
L
Linus Torvalds 已提交
162 163 164 165
			err = -EIO;
			goto out;
		}
		if (fd.entrylength < HFSPLUS_MIN_THREAD_SZ) {
166
			pr_err("truncated catalog thread\n");
L
Linus Torvalds 已提交
167 168 169
			err = -EIO;
			goto out;
		}
A
Al Viro 已提交
170
		if (!dir_emit(ctx, "..", 2,
L
Linus Torvalds 已提交
171 172
			    be32_to_cpu(entry.thread.parentID), DT_DIR))
			goto out;
A
Al Viro 已提交
173
		ctx->pos = 2;
L
Linus Torvalds 已提交
174
	}
A
Al Viro 已提交
175 176 177 178 179
	if (ctx->pos >= inode->i_size)
		goto out;
	err = hfs_brec_goto(&fd, ctx->pos - 1);
	if (err)
		goto out;
L
Linus Torvalds 已提交
180 181
	for (;;) {
		if (be32_to_cpu(fd.key->cat.parent) != inode->i_ino) {
182
			pr_err("walked past end of dir\n");
L
Linus Torvalds 已提交
183 184 185
			err = -EIO;
			goto out;
		}
186 187 188 189 190 191

		if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
			err = -EIO;
			goto out;
		}

192 193
		hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
			fd.entrylength);
L
Linus Torvalds 已提交
194 195 196 197 198 199
		type = be16_to_cpu(entry.type);
		len = HFSPLUS_MAX_STRLEN;
		err = hfsplus_uni2asc(sb, &fd.key->cat.name, strbuf, &len);
		if (err)
			goto out;
		if (type == HFSPLUS_FOLDER) {
200 201
			if (fd.entrylength <
					sizeof(struct hfsplus_cat_folder)) {
202
				pr_err("small dir entry\n");
L
Linus Torvalds 已提交
203 204 205
				err = -EIO;
				goto out;
			}
206 207 208
			if (HFSPLUS_SB(sb)->hidden_dir &&
			    HFSPLUS_SB(sb)->hidden_dir->i_ino ==
					be32_to_cpu(entry.folder.id))
L
Linus Torvalds 已提交
209
				goto next;
A
Al Viro 已提交
210
			if (!dir_emit(ctx, strbuf, len,
L
Linus Torvalds 已提交
211 212 213 214
				    be32_to_cpu(entry.folder.id), DT_DIR))
				break;
		} else if (type == HFSPLUS_FILE) {
			if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
215
				pr_err("small file entry\n");
L
Linus Torvalds 已提交
216 217 218
				err = -EIO;
				goto out;
			}
A
Al Viro 已提交
219
			if (!dir_emit(ctx, strbuf, len,
L
Linus Torvalds 已提交
220 221 222
				    be32_to_cpu(entry.file.id), DT_REG))
				break;
		} else {
223
			pr_err("bad catalog entry type\n");
L
Linus Torvalds 已提交
224 225 226
			err = -EIO;
			goto out;
		}
227
next:
A
Al Viro 已提交
228 229
		ctx->pos++;
		if (ctx->pos >= inode->i_size)
L
Linus Torvalds 已提交
230 231 232 233 234
			goto out;
		err = hfs_brec_goto(&fd, 1);
		if (err)
			goto out;
	}
A
Al Viro 已提交
235
	rd = file->private_data;
L
Linus Torvalds 已提交
236 237 238 239 240 241
	if (!rd) {
		rd = kmalloc(sizeof(struct hfsplus_readdir_data), GFP_KERNEL);
		if (!rd) {
			err = -ENOMEM;
			goto out;
		}
A
Al Viro 已提交
242 243
		file->private_data = rd;
		rd->file = file;
244
		list_add(&rd->list, &HFSPLUS_I(inode)->open_dir_list);
L
Linus Torvalds 已提交
245 246 247 248 249 250 251 252 253 254 255
	}
	memcpy(&rd->key, fd.key, sizeof(struct hfsplus_cat_key));
out:
	hfs_find_exit(&fd);
	return err;
}

static int hfsplus_dir_release(struct inode *inode, struct file *file)
{
	struct hfsplus_readdir_data *rd = file->private_data;
	if (rd) {
256
		mutex_lock(&inode->i_mutex);
L
Linus Torvalds 已提交
257
		list_del(&rd->list);
258
		mutex_unlock(&inode->i_mutex);
L
Linus Torvalds 已提交
259 260 261 262 263 264 265 266
		kfree(rd);
	}
	return 0;
}

static int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir,
			struct dentry *dst_dentry)
{
267
	struct hfsplus_sb_info *sbi = HFSPLUS_SB(dst_dir->i_sb);
L
Linus Torvalds 已提交
268 269 270 271 272 273 274 275 276
	struct inode *inode = src_dentry->d_inode;
	struct inode *src_dir = src_dentry->d_parent->d_inode;
	struct qstr str;
	char name[32];
	u32 cnid, id;
	int res;

	if (HFSPLUS_IS_RSRC(inode))
		return -EPERM;
C
Christoph Hellwig 已提交
277 278
	if (!S_ISREG(inode->i_mode))
		return -EPERM;
L
Linus Torvalds 已提交
279

280
	mutex_lock(&sbi->vh_mutex);
L
Linus Torvalds 已提交
281 282 283 284 285 286 287 288
	if (inode->i_ino == (u32)(unsigned long)src_dentry->d_fsdata) {
		for (;;) {
			get_random_bytes(&id, sizeof(cnid));
			id &= 0x3fffffff;
			str.name = name;
			str.len = sprintf(name, "iNode%d", id);
			res = hfsplus_rename_cat(inode->i_ino,
						 src_dir, &src_dentry->d_name,
289
						 sbi->hidden_dir, &str);
L
Linus Torvalds 已提交
290 291 292
			if (!res)
				break;
			if (res != -EEXIST)
293
				goto out;
L
Linus Torvalds 已提交
294
		}
C
Christoph Hellwig 已提交
295
		HFSPLUS_I(inode)->linkid = id;
296
		cnid = sbi->next_cnid++;
L
Linus Torvalds 已提交
297
		src_dentry->d_fsdata = (void *)(unsigned long)cnid;
298 299
		res = hfsplus_create_cat(cnid, src_dir,
			&src_dentry->d_name, inode);
L
Linus Torvalds 已提交
300 301
		if (res)
			/* panic? */
302
			goto out;
303
		sbi->file_count++;
L
Linus Torvalds 已提交
304
	}
305
	cnid = sbi->next_cnid++;
L
Linus Torvalds 已提交
306 307
	res = hfsplus_create_cat(cnid, dst_dir, &dst_dentry->d_name, inode);
	if (res)
308
		goto out;
L
Linus Torvalds 已提交
309

310
	inc_nlink(inode);
L
Linus Torvalds 已提交
311
	hfsplus_instantiate(dst_dentry, inode, cnid);
A
Al Viro 已提交
312
	ihold(inode);
L
Linus Torvalds 已提交
313 314
	inode->i_ctime = CURRENT_TIME_SEC;
	mark_inode_dirty(inode);
315
	sbi->file_count++;
316
	hfsplus_mark_mdb_dirty(dst_dir->i_sb);
317 318 319
out:
	mutex_unlock(&sbi->vh_mutex);
	return res;
L
Linus Torvalds 已提交
320 321 322 323
}

static int hfsplus_unlink(struct inode *dir, struct dentry *dentry)
{
324
	struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
L
Linus Torvalds 已提交
325 326 327 328 329 330 331 332 333
	struct inode *inode = dentry->d_inode;
	struct qstr str;
	char name[32];
	u32 cnid;
	int res;

	if (HFSPLUS_IS_RSRC(inode))
		return -EPERM;

334
	mutex_lock(&sbi->vh_mutex);
L
Linus Torvalds 已提交
335 336
	cnid = (u32)(unsigned long)dentry->d_fsdata;
	if (inode->i_ino == cnid &&
337
	    atomic_read(&HFSPLUS_I(inode)->opencnt)) {
L
Linus Torvalds 已提交
338 339 340 341
		str.name = name;
		str.len = sprintf(name, "temp%lu", inode->i_ino);
		res = hfsplus_rename_cat(inode->i_ino,
					 dir, &dentry->d_name,
342
					 sbi->hidden_dir, &str);
343
		if (!res) {
L
Linus Torvalds 已提交
344
			inode->i_flags |= S_DEAD;
345 346
			drop_nlink(inode);
		}
347
		goto out;
L
Linus Torvalds 已提交
348 349 350
	}
	res = hfsplus_delete_cat(cnid, dir, &dentry->d_name);
	if (res)
351
		goto out;
L
Linus Torvalds 已提交
352

353
	if (inode->i_nlink > 0)
354
		drop_nlink(inode);
R
Roman Zippel 已提交
355 356 357 358
	if (inode->i_ino == cnid)
		clear_nlink(inode);
	if (!inode->i_nlink) {
		if (inode->i_ino != cnid) {
359
			sbi->file_count--;
360
			if (!atomic_read(&HFSPLUS_I(inode)->opencnt)) {
R
Roman Zippel 已提交
361
				res = hfsplus_delete_cat(inode->i_ino,
362
							 sbi->hidden_dir,
R
Roman Zippel 已提交
363 364 365 366 367
							 NULL);
				if (!res)
					hfsplus_delete_inode(inode);
			} else
				inode->i_flags |= S_DEAD;
L
Linus Torvalds 已提交
368
		} else
R
Roman Zippel 已提交
369
			hfsplus_delete_inode(inode);
370
	} else
371
		sbi->file_count--;
L
Linus Torvalds 已提交
372 373
	inode->i_ctime = CURRENT_TIME_SEC;
	mark_inode_dirty(inode);
374 375
out:
	mutex_unlock(&sbi->vh_mutex);
L
Linus Torvalds 已提交
376 377 378 379 380
	return res;
}

static int hfsplus_rmdir(struct inode *dir, struct dentry *dentry)
{
381 382
	struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
	struct inode *inode = dentry->d_inode;
L
Linus Torvalds 已提交
383 384 385 386
	int res;

	if (inode->i_size != 2)
		return -ENOTEMPTY;
387 388

	mutex_lock(&sbi->vh_mutex);
L
Linus Torvalds 已提交
389 390
	res = hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
	if (res)
391
		goto out;
392
	clear_nlink(inode);
L
Linus Torvalds 已提交
393 394 395
	inode->i_ctime = CURRENT_TIME_SEC;
	hfsplus_delete_inode(inode);
	mark_inode_dirty(inode);
396 397 398
out:
	mutex_unlock(&sbi->vh_mutex);
	return res;
L
Linus Torvalds 已提交
399 400 401 402 403
}

static int hfsplus_symlink(struct inode *dir, struct dentry *dentry,
			   const char *symname)
{
404
	struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
L
Linus Torvalds 已提交
405
	struct inode *inode;
406
	int res = -ENOSPC;
L
Linus Torvalds 已提交
407

408
	mutex_lock(&sbi->vh_mutex);
409
	inode = hfsplus_new_inode(dir->i_sb, S_IFLNK | S_IRWXUGO);
L
Linus Torvalds 已提交
410
	if (!inode)
411
		goto out;
L
Linus Torvalds 已提交
412 413

	res = page_symlink(inode, symname, strlen(symname) + 1);
414 415
	if (res)
		goto out_err;
L
Linus Torvalds 已提交
416 417

	res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
418 419
	if (res)
		goto out_err;
L
Linus Torvalds 已提交
420

421 422 423 424 425 426 427 428 429
	res = hfsplus_init_inode_security(inode, dir, &dentry->d_name);
	if (res == -EOPNOTSUPP)
		res = 0; /* Operation is not supported. */
	else if (res) {
		/* Try to delete anyway without error analysis. */
		hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
		goto out_err;
	}

430 431
	hfsplus_instantiate(dentry, inode, inode->i_ino);
	mark_inode_dirty(inode);
432
	goto out;
L
Linus Torvalds 已提交
433

434
out_err:
435
	clear_nlink(inode);
436 437
	hfsplus_delete_inode(inode);
	iput(inode);
438 439
out:
	mutex_unlock(&sbi->vh_mutex);
L
Linus Torvalds 已提交
440 441 442 443
	return res;
}

static int hfsplus_mknod(struct inode *dir, struct dentry *dentry,
A
Al Viro 已提交
444
			 umode_t mode, dev_t rdev)
L
Linus Torvalds 已提交
445
{
446
	struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
L
Linus Torvalds 已提交
447
	struct inode *inode;
448
	int res = -ENOSPC;
L
Linus Torvalds 已提交
449

450
	mutex_lock(&sbi->vh_mutex);
451
	inode = hfsplus_new_inode(dir->i_sb, mode);
L
Linus Torvalds 已提交
452
	if (!inode)
453
		goto out;
L
Linus Torvalds 已提交
454

455 456 457
	if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode))
		init_special_inode(inode, mode, rdev);

L
Linus Torvalds 已提交
458
	res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
459 460 461 462 463 464 465 466 467 468
	if (res)
		goto failed_mknod;

	res = hfsplus_init_inode_security(inode, dir, &dentry->d_name);
	if (res == -EOPNOTSUPP)
		res = 0; /* Operation is not supported. */
	else if (res) {
		/* Try to delete anyway without error analysis. */
		hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
		goto failed_mknod;
L
Linus Torvalds 已提交
469
	}
470

L
Linus Torvalds 已提交
471 472
	hfsplus_instantiate(dentry, inode, inode->i_ino);
	mark_inode_dirty(inode);
473 474 475 476 477 478
	goto out;

failed_mknod:
	clear_nlink(inode);
	hfsplus_delete_inode(inode);
	iput(inode);
479 480 481
out:
	mutex_unlock(&sbi->vh_mutex);
	return res;
L
Linus Torvalds 已提交
482 483
}

A
Al Viro 已提交
484
static int hfsplus_create(struct inode *dir, struct dentry *dentry, umode_t mode,
A
Al Viro 已提交
485
			  bool excl)
486 487 488 489
{
	return hfsplus_mknod(dir, dentry, mode, 0);
}

490
static int hfsplus_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
491 492 493 494
{
	return hfsplus_mknod(dir, dentry, mode | S_IFDIR, 0);
}

L
Linus Torvalds 已提交
495 496 497 498 499 500 501
static int hfsplus_rename(struct inode *old_dir, struct dentry *old_dentry,
			  struct inode *new_dir, struct dentry *new_dentry)
{
	int res;

	/* Unlink destination if it already exists */
	if (new_dentry->d_inode) {
502
		if (S_ISDIR(new_dentry->d_inode->i_mode))
503
			res = hfsplus_rmdir(new_dir, new_dentry);
504
		else
505
			res = hfsplus_unlink(new_dir, new_dentry);
L
Linus Torvalds 已提交
506 507 508 509 510 511 512 513 514 515 516 517
		if (res)
			return res;
	}

	res = hfsplus_rename_cat((u32)(unsigned long)old_dentry->d_fsdata,
				 old_dir, &old_dentry->d_name,
				 new_dir, &new_dentry->d_name);
	if (!res)
		new_dentry->d_fsdata = old_dentry->d_fsdata;
	return res;
}

518
const struct inode_operations hfsplus_dir_inode_operations = {
519 520 521 522 523 524 525 526 527 528 529 530 531
	.lookup			= hfsplus_lookup,
	.create			= hfsplus_create,
	.link			= hfsplus_link,
	.unlink			= hfsplus_unlink,
	.mkdir			= hfsplus_mkdir,
	.rmdir			= hfsplus_rmdir,
	.symlink		= hfsplus_symlink,
	.mknod			= hfsplus_mknod,
	.rename			= hfsplus_rename,
	.setxattr		= generic_setxattr,
	.getxattr		= generic_getxattr,
	.listxattr		= hfsplus_listxattr,
	.removexattr		= hfsplus_removexattr,
L
Linus Torvalds 已提交
532 533
};

534
const struct file_operations hfsplus_dir_operations = {
535
	.fsync		= hfsplus_file_fsync,
L
Linus Torvalds 已提交
536
	.read		= generic_read_dir,
A
Al Viro 已提交
537
	.iterate	= hfsplus_readdir,
538
	.unlocked_ioctl = hfsplus_ioctl,
L
Linus Torvalds 已提交
539 540 541
	.llseek		= generic_file_llseek,
	.release	= hfsplus_dir_release,
};