dir.c 11.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/*
 *  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"

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,
				     struct nameidata *nd)
{
	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;
39 40

	dentry->d_op = &hfsplus_dentry_operations;
L
Linus Torvalds 已提交
41
	dentry->d_fsdata = NULL;
42
	hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
L
Linus Torvalds 已提交
43 44 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
	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);
		if (entry.file.user_info.fdType == cpu_to_be32(HFSP_HARDLINK_TYPE) &&
70
		    entry.file.user_info.fdCreator == cpu_to_be32(HFSP_HFSPLUS_CREATOR) &&
71 72
		    (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) &&
73
		    HFSPLUS_SB(sb)->hidden_dir) {
L
Linus Torvalds 已提交
74 75 76 77
			struct qstr str;
			char name[32];

			if (dentry->d_fsdata) {
78 79 80 81 82 83 84 85 86 87 88
				/*
				 * 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;
				linkid = be32_to_cpu(entry.file.permissions.dev);
				str.len = sprintf(name, "iNode%d", linkid);
				str.name = name;
89 90
				hfsplus_cat_build_key(sb, fd.search_key,
					HFSPLUS_SB(sb)->hidden_dir->i_ino, &str);
91
				goto again;
L
Linus Torvalds 已提交
92 93 94 95
			}
		} else if (!dentry->d_fsdata)
			dentry->d_fsdata = (void *)(unsigned long)cnid;
	} else {
R
Roman Zippel 已提交
96
		printk(KERN_ERR "hfs: invalid catalog entry type in lookup\n");
L
Linus Torvalds 已提交
97 98 99 100
		err = -EIO;
		goto fail;
	}
	hfs_find_exit(&fd);
101 102 103
	inode = hfsplus_iget(dir->i_sb, cnid);
	if (IS_ERR(inode))
		return ERR_CAST(inode);
L
Linus Torvalds 已提交
104
	if (S_ISREG(inode->i_mode))
105
		HFSPLUS_I(inode)->dev = linkid;
L
Linus Torvalds 已提交
106 107 108 109 110 111 112 113 114 115
out:
	d_add(dentry, inode);
	return NULL;
fail:
	hfs_find_exit(&fd);
	return ERR_PTR(err);
}

static int hfsplus_readdir(struct file *filp, void *dirent, filldir_t filldir)
{
J
Josef Sipek 已提交
116
	struct inode *inode = filp->f_path.dentry->d_inode;
L
Linus Torvalds 已提交
117 118 119 120 121 122 123 124 125 126 127
	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;

	if (filp->f_pos >= inode->i_size)
		return 0;

128
	hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
L
Linus Torvalds 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
	hfsplus_cat_build_key(sb, fd.search_key, inode->i_ino, NULL);
	err = hfs_brec_find(&fd);
	if (err)
		goto out;

	switch ((u32)filp->f_pos) {
	case 0:
		/* This is completely artificial... */
		if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR))
			goto out;
		filp->f_pos++;
		/* fall through */
	case 1:
		hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
		if (be16_to_cpu(entry.type) != HFSPLUS_FOLDER_THREAD) {
R
Roman Zippel 已提交
144
			printk(KERN_ERR "hfs: bad catalog folder thread\n");
L
Linus Torvalds 已提交
145 146 147 148
			err = -EIO;
			goto out;
		}
		if (fd.entrylength < HFSPLUS_MIN_THREAD_SZ) {
R
Roman Zippel 已提交
149
			printk(KERN_ERR "hfs: truncated catalog thread\n");
L
Linus Torvalds 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
			err = -EIO;
			goto out;
		}
		if (filldir(dirent, "..", 2, 1,
			    be32_to_cpu(entry.thread.parentID), DT_DIR))
			goto out;
		filp->f_pos++;
		/* fall through */
	default:
		if (filp->f_pos >= inode->i_size)
			goto out;
		err = hfs_brec_goto(&fd, filp->f_pos - 1);
		if (err)
			goto out;
	}

	for (;;) {
		if (be32_to_cpu(fd.key->cat.parent) != inode->i_ino) {
R
Roman Zippel 已提交
168
			printk(KERN_ERR "hfs: walked past end of dir\n");
L
Linus Torvalds 已提交
169 170 171 172 173 174 175 176 177 178 179
			err = -EIO;
			goto out;
		}
		hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
		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) {
			if (fd.entrylength < sizeof(struct hfsplus_cat_folder)) {
R
Roman Zippel 已提交
180
				printk(KERN_ERR "hfs: small dir entry\n");
L
Linus Torvalds 已提交
181 182 183
				err = -EIO;
				goto out;
			}
184 185 186
			if (HFSPLUS_SB(sb)->hidden_dir &&
			    HFSPLUS_SB(sb)->hidden_dir->i_ino ==
					be32_to_cpu(entry.folder.id))
L
Linus Torvalds 已提交
187 188 189 190 191 192
				goto next;
			if (filldir(dirent, strbuf, len, filp->f_pos,
				    be32_to_cpu(entry.folder.id), DT_DIR))
				break;
		} else if (type == HFSPLUS_FILE) {
			if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
R
Roman Zippel 已提交
193
				printk(KERN_ERR "hfs: small file entry\n");
L
Linus Torvalds 已提交
194 195 196 197 198 199 200
				err = -EIO;
				goto out;
			}
			if (filldir(dirent, strbuf, len, filp->f_pos,
				    be32_to_cpu(entry.file.id), DT_REG))
				break;
		} else {
R
Roman Zippel 已提交
201
			printk(KERN_ERR "hfs: bad catalog entry type\n");
L
Linus Torvalds 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
			err = -EIO;
			goto out;
		}
	next:
		filp->f_pos++;
		if (filp->f_pos >= inode->i_size)
			goto out;
		err = hfs_brec_goto(&fd, 1);
		if (err)
			goto out;
	}
	rd = filp->private_data;
	if (!rd) {
		rd = kmalloc(sizeof(struct hfsplus_readdir_data), GFP_KERNEL);
		if (!rd) {
			err = -ENOMEM;
			goto out;
		}
		filp->private_data = rd;
		rd->file = filp;
222
		list_add(&rd->list, &HFSPLUS_I(inode)->open_dir_list);
L
Linus Torvalds 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	}
	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) {
		list_del(&rd->list);
		kfree(rd);
	}
	return 0;
}

static int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir,
			struct dentry *dst_dentry)
{
243
	struct hfsplus_sb_info *sbi = HFSPLUS_SB(dst_dir->i_sb);
L
Linus Torvalds 已提交
244 245 246 247 248 249 250 251 252 253
	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;

254
	mutex_lock(&sbi->vh_mutex);
L
Linus Torvalds 已提交
255 256 257 258 259 260 261 262
	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,
263
						 sbi->hidden_dir, &str);
L
Linus Torvalds 已提交
264 265 266
			if (!res)
				break;
			if (res != -EEXIST)
267
				goto out;
L
Linus Torvalds 已提交
268
		}
269
		HFSPLUS_I(inode)->dev = id;
270
		cnid = sbi->next_cnid++;
L
Linus Torvalds 已提交
271 272 273 274
		src_dentry->d_fsdata = (void *)(unsigned long)cnid;
		res = hfsplus_create_cat(cnid, src_dir, &src_dentry->d_name, inode);
		if (res)
			/* panic? */
275
			goto out;
276
		sbi->file_count++;
L
Linus Torvalds 已提交
277
	}
278
	cnid = sbi->next_cnid++;
L
Linus Torvalds 已提交
279 280
	res = hfsplus_create_cat(cnid, dst_dir, &dst_dentry->d_name, inode);
	if (res)
281
		goto out;
L
Linus Torvalds 已提交
282

283
	inc_nlink(inode);
L
Linus Torvalds 已提交
284 285 286 287
	hfsplus_instantiate(dst_dentry, inode, cnid);
	atomic_inc(&inode->i_count);
	inode->i_ctime = CURRENT_TIME_SEC;
	mark_inode_dirty(inode);
288 289
	sbi->file_count++;
	dst_dir->i_sb->s_dirt = 1;
290 291 292
out:
	mutex_unlock(&sbi->vh_mutex);
	return res;
L
Linus Torvalds 已提交
293 294 295 296
}

static int hfsplus_unlink(struct inode *dir, struct dentry *dentry)
{
297
	struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
L
Linus Torvalds 已提交
298 299 300 301 302 303 304 305 306
	struct inode *inode = dentry->d_inode;
	struct qstr str;
	char name[32];
	u32 cnid;
	int res;

	if (HFSPLUS_IS_RSRC(inode))
		return -EPERM;

307
	mutex_lock(&sbi->vh_mutex);
L
Linus Torvalds 已提交
308 309
	cnid = (u32)(unsigned long)dentry->d_fsdata;
	if (inode->i_ino == cnid &&
310
	    atomic_read(&HFSPLUS_I(inode)->opencnt)) {
L
Linus Torvalds 已提交
311 312 313 314
		str.name = name;
		str.len = sprintf(name, "temp%lu", inode->i_ino);
		res = hfsplus_rename_cat(inode->i_ino,
					 dir, &dentry->d_name,
315
					 sbi->hidden_dir, &str);
L
Linus Torvalds 已提交
316 317
		if (!res)
			inode->i_flags |= S_DEAD;
318
		goto out;
L
Linus Torvalds 已提交
319 320 321
	}
	res = hfsplus_delete_cat(cnid, dir, &dentry->d_name);
	if (res)
322
		goto out;
L
Linus Torvalds 已提交
323

324
	if (inode->i_nlink > 0)
325
		drop_nlink(inode);
R
Roman Zippel 已提交
326 327 328 329
	if (inode->i_ino == cnid)
		clear_nlink(inode);
	if (!inode->i_nlink) {
		if (inode->i_ino != cnid) {
330
			sbi->file_count--;
331
			if (!atomic_read(&HFSPLUS_I(inode)->opencnt)) {
R
Roman Zippel 已提交
332
				res = hfsplus_delete_cat(inode->i_ino,
333
							 sbi->hidden_dir,
R
Roman Zippel 已提交
334 335 336 337 338
							 NULL);
				if (!res)
					hfsplus_delete_inode(inode);
			} else
				inode->i_flags |= S_DEAD;
L
Linus Torvalds 已提交
339
		} else
R
Roman Zippel 已提交
340
			hfsplus_delete_inode(inode);
341
	} else
342
		sbi->file_count--;
L
Linus Torvalds 已提交
343 344
	inode->i_ctime = CURRENT_TIME_SEC;
	mark_inode_dirty(inode);
345 346
out:
	mutex_unlock(&sbi->vh_mutex);
L
Linus Torvalds 已提交
347 348 349 350 351
	return res;
}

static int hfsplus_rmdir(struct inode *dir, struct dentry *dentry)
{
352 353
	struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
	struct inode *inode = dentry->d_inode;
L
Linus Torvalds 已提交
354 355 356 357
	int res;

	if (inode->i_size != 2)
		return -ENOTEMPTY;
358 359

	mutex_lock(&sbi->vh_mutex);
L
Linus Torvalds 已提交
360 361
	res = hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
	if (res)
362
		goto out;
363
	clear_nlink(inode);
L
Linus Torvalds 已提交
364 365 366
	inode->i_ctime = CURRENT_TIME_SEC;
	hfsplus_delete_inode(inode);
	mark_inode_dirty(inode);
367 368 369
out:
	mutex_unlock(&sbi->vh_mutex);
	return res;
L
Linus Torvalds 已提交
370 371 372 373 374
}

static int hfsplus_symlink(struct inode *dir, struct dentry *dentry,
			   const char *symname)
{
375
	struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
L
Linus Torvalds 已提交
376
	struct inode *inode;
377
	int res = -ENOSPC;
L
Linus Torvalds 已提交
378

379
	mutex_lock(&sbi->vh_mutex);
380
	inode = hfsplus_new_inode(dir->i_sb, S_IFLNK | S_IRWXUGO);
L
Linus Torvalds 已提交
381
	if (!inode)
382
		goto out;
L
Linus Torvalds 已提交
383 384

	res = page_symlink(inode, symname, strlen(symname) + 1);
385 386
	if (res)
		goto out_err;
L
Linus Torvalds 已提交
387 388

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

392 393
	hfsplus_instantiate(dentry, inode, inode->i_ino);
	mark_inode_dirty(inode);
394
	goto out;
L
Linus Torvalds 已提交
395

396 397 398 399
out_err:
	inode->i_nlink = 0;
	hfsplus_delete_inode(inode);
	iput(inode);
400 401
out:
	mutex_unlock(&sbi->vh_mutex);
L
Linus Torvalds 已提交
402 403 404 405 406 407
	return res;
}

static int hfsplus_mknod(struct inode *dir, struct dentry *dentry,
			 int mode, dev_t rdev)
{
408
	struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
L
Linus Torvalds 已提交
409
	struct inode *inode;
410
	int res = -ENOSPC;
L
Linus Torvalds 已提交
411

412
	mutex_lock(&sbi->vh_mutex);
413
	inode = hfsplus_new_inode(dir->i_sb, mode);
L
Linus Torvalds 已提交
414
	if (!inode)
415
		goto out;
L
Linus Torvalds 已提交
416 417 418 419 420 421

	res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
	if (res) {
		inode->i_nlink = 0;
		hfsplus_delete_inode(inode);
		iput(inode);
422
		goto out;
L
Linus Torvalds 已提交
423
	}
424 425 426 427

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

L
Linus Torvalds 已提交
428 429
	hfsplus_instantiate(dentry, inode, inode->i_ino);
	mark_inode_dirty(inode);
430 431 432
out:
	mutex_unlock(&sbi->vh_mutex);
	return res;
L
Linus Torvalds 已提交
433 434
}

435 436 437 438 439 440 441 442 443 444 445
static int hfsplus_create(struct inode *dir, struct dentry *dentry, int mode,
			  struct nameidata *nd)
{
	return hfsplus_mknod(dir, dentry, mode, 0);
}

static int hfsplus_mkdir(struct inode *dir, struct dentry *dentry, int mode)
{
	return hfsplus_mknod(dir, dentry, mode | S_IFDIR, 0);
}

L
Linus Torvalds 已提交
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
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) {
		res = hfsplus_unlink(new_dir, new_dentry);
		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;
}

466
const struct inode_operations hfsplus_dir_inode_operations = {
L
Linus Torvalds 已提交
467 468 469 470 471 472 473 474 475 476 477
	.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,
};

478
const struct file_operations hfsplus_dir_operations = {
L
Linus Torvalds 已提交
479 480
	.read		= generic_read_dir,
	.readdir	= hfsplus_readdir,
481
	.unlocked_ioctl = hfsplus_ioctl,
L
Linus Torvalds 已提交
482 483 484
	.llseek		= generic_file_llseek,
	.release	= hfsplus_dir_release,
};