dir.c 15.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
Linus Torvalds's avatar
Linus Torvalds committed
2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 *  linux/fs/ufs/ufs_dir.c
 *
 * Copyright (C) 1996
 * Adrian Rodriguez (adrian@franklins-tower.rutgers.edu)
 * Laboratory for Computer Science Research Computing Facility
 * Rutgers, The State University of New Jersey
 *
 * swab support by Francois-Rene Rideau <fare@tunes.org> 19970406
 *
 * 4.4BSD (FreeBSD) support added on February 1st 1998 by
 * Niels Kristian Bech Jensen <nkbj@image.dk> partially based
 * on code by Martin von Loewis <martin@mira.isdn.cs.tu-berlin.de>.
15 16 17
 *
 * Migration to usage of "page cache" on May 2006 by
 * Evgeniy Dushistov <dushistov@mail.ru> based on ext2 code base.
Linus Torvalds's avatar
Linus Torvalds committed
18 19 20 21
 */

#include <linux/time.h>
#include <linux/fs.h>
Nick Piggin's avatar
Nick Piggin committed
22
#include <linux/swap.h>
Jeff Layton's avatar
Jeff Layton committed
23
#include <linux/iversion.h>
Linus Torvalds's avatar
Linus Torvalds committed
24

25
#include "ufs_fs.h"
26
#include "ufs.h"
Linus Torvalds's avatar
Linus Torvalds committed
27 28 29 30 31 32 33 34 35
#include "swab.h"
#include "util.h"

/*
 * NOTE! unlike strncmp, ufs_match returns 1 for success, 0 for failure.
 *
 * len <= UFS_MAXNAMLEN and de != NULL are guaranteed by caller.
 */
static inline int ufs_match(struct super_block *sb, int len,
36
		const unsigned char *name, struct ufs_dir_entry *de)
Linus Torvalds's avatar
Linus Torvalds committed
37 38 39 40 41 42 43 44
{
	if (len != ufs_get_de_namlen(sb, de))
		return 0;
	if (!de->d_ino)
		return 0;
	return !memcmp(name, de->d_name, len);
}

Nick Piggin's avatar
Nick Piggin committed
45
static int ufs_commit_chunk(struct page *page, loff_t pos, unsigned len)
Linus Torvalds's avatar
Linus Torvalds committed
46
{
Nick Piggin's avatar
Nick Piggin committed
47 48
	struct address_space *mapping = page->mapping;
	struct inode *dir = mapping->host;
49
	int err = 0;
Nick Piggin's avatar
Nick Piggin committed
50

Jeff Layton's avatar
Jeff Layton committed
51
	inode_inc_iversion(dir);
Nick Piggin's avatar
Nick Piggin committed
52 53 54 55 56
	block_write_end(NULL, mapping, pos, len, len, page, NULL);
	if (pos+len > dir->i_size) {
		i_size_write(dir, pos+len);
		mark_inode_dirty(dir);
	}
57
	if (IS_DIRSYNC(dir))
58
		err = write_one_page(page);
59 60 61 62
	else
		unlock_page(page);
	return err;
}
Linus Torvalds's avatar
Linus Torvalds committed
63

64 65 66
static inline void ufs_put_page(struct page *page)
{
	kunmap(page);
67
	put_page(page);
68
}
Linus Torvalds's avatar
Linus Torvalds committed
69

70
ino_t ufs_inode_by_name(struct inode *dir, const struct qstr *qstr)
71 72 73 74 75
{
	ino_t res = 0;
	struct ufs_dir_entry *de;
	struct page *page;
	
76
	de = ufs_find_entry(dir, qstr, &page);
77 78 79
	if (de) {
		res = fs32_to_cpu(dir->i_sb, de->d_ino);
		ufs_put_page(page);
Linus Torvalds's avatar
Linus Torvalds committed
80
	}
81
	return res;
Linus Torvalds's avatar
Linus Torvalds committed
82 83 84
}


85 86
/* Releases the page */
void ufs_set_link(struct inode *dir, struct ufs_dir_entry *de,
87 88
		  struct page *page, struct inode *inode,
		  bool update_times)
Linus Torvalds's avatar
Linus Torvalds committed
89
{
Nick Piggin's avatar
Nick Piggin committed
90 91 92
	loff_t pos = page_offset(page) +
			(char *) de - (char *) page_address(page);
	unsigned len = fs16_to_cpu(dir->i_sb, de->d_reclen);
93
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
94

95
	lock_page(page);
96
	err = ufs_prepare_chunk(page, pos, len);
97
	BUG_ON(err);
Nick Piggin's avatar
Nick Piggin committed
98

99 100
	de->d_ino = cpu_to_fs32(dir->i_sb, inode->i_ino);
	ufs_set_de_type(dir->i_sb, de, inode->i_mode);
Nick Piggin's avatar
Nick Piggin committed
101 102

	err = ufs_commit_chunk(page, pos, len);
103
	ufs_put_page(page);
104
	if (update_times)
105
		dir->i_mtime = dir->i_ctime = current_time(dir);
106 107
	mark_inode_dirty(dir);
}
Linus Torvalds's avatar
Linus Torvalds committed
108 109


110
static bool ufs_check_page(struct page *page)
111 112 113 114 115
{
	struct inode *dir = page->mapping->host;
	struct super_block *sb = dir->i_sb;
	char *kaddr = page_address(page);
	unsigned offs, rec_len;
116
	unsigned limit = PAGE_SIZE;
117
	const unsigned chunk_mask = UFS_SB(sb)->s_uspi->s_dirblksize - 1;
118 119 120
	struct ufs_dir_entry *p;
	char *error;

121 122
	if ((dir->i_size >> PAGE_SHIFT) == page->index) {
		limit = dir->i_size & ~PAGE_MASK;
123
		if (limit & chunk_mask)
124 125 126
			goto Ebadsize;
		if (!limit)
			goto out;
Linus Torvalds's avatar
Linus Torvalds committed
127
	}
128 129 130 131 132 133 134 135 136 137
	for (offs = 0; offs <= limit - UFS_DIR_REC_LEN(1); offs += rec_len) {
		p = (struct ufs_dir_entry *)(kaddr + offs);
		rec_len = fs16_to_cpu(sb, p->d_reclen);

		if (rec_len < UFS_DIR_REC_LEN(1))
			goto Eshort;
		if (rec_len & 3)
			goto Ealign;
		if (rec_len < UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, p)))
			goto Enamelen;
138
		if (((offs + rec_len - 1) ^ offs) & ~chunk_mask)
139 140 141 142 143 144 145 146 147
			goto Espan;
		if (fs32_to_cpu(sb, p->d_ino) > (UFS_SB(sb)->s_uspi->s_ipg *
						  UFS_SB(sb)->s_uspi->s_ncg))
			goto Einumber;
	}
	if (offs != limit)
		goto Eend;
out:
	SetPageChecked(page);
148
	return true;
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

	/* Too bad, we had an error */

Ebadsize:
	ufs_error(sb, "ufs_check_page",
		  "size of directory #%lu is not a multiple of chunk size",
		  dir->i_ino
	);
	goto fail;
Eshort:
	error = "rec_len is smaller than minimal";
	goto bad_entry;
Ealign:
	error = "unaligned directory entry";
	goto bad_entry;
Enamelen:
	error = "rec_len is too small for name_len";
	goto bad_entry;
Espan:
	error = "directory entry across blocks";
	goto bad_entry;
Einumber:
	error = "inode out of bounds";
bad_entry:
	ufs_error (sb, "ufs_check_page", "bad entry in directory #%lu: %s - "
		   "offset=%lu, rec_len=%d, name_len=%d",
175
		   dir->i_ino, error, (page->index<<PAGE_SHIFT)+offs,
176 177 178 179
		   rec_len, ufs_get_de_namlen(sb, p));
	goto fail;
Eend:
	p = (struct ufs_dir_entry *)(kaddr + offs);
180
	ufs_error(sb, __func__,
181 182
		   "entry in directory #%lu spans the page boundary"
		   "offset=%lu",
183
		   dir->i_ino, (page->index<<PAGE_SHIFT)+offs);
184 185
fail:
	SetPageError(page);
186
	return false;
187
}
Linus Torvalds's avatar
Linus Torvalds committed
188

189 190 191
static struct page *ufs_get_page(struct inode *dir, unsigned long n)
{
	struct address_space *mapping = dir->i_mapping;
192
	struct page *page = read_mapping_page(mapping, n, NULL);
193 194
	if (!IS_ERR(page)) {
		kmap(page);
195 196 197 198
		if (unlikely(!PageChecked(page))) {
			if (PageError(page) || !ufs_check_page(page))
				goto fail;
		}
Linus Torvalds's avatar
Linus Torvalds committed
199
	}
200
	return page;
Linus Torvalds's avatar
Linus Torvalds committed
201

202 203 204
fail:
	ufs_put_page(page);
	return ERR_PTR(-EIO);
Linus Torvalds's avatar
Linus Torvalds committed
205 206
}

207 208 209 210 211 212
/*
 * Return the offset into page `page_nr' of the last valid
 * byte in that page, plus one.
 */
static unsigned
ufs_last_byte(struct inode *inode, unsigned long page_nr)
Linus Torvalds's avatar
Linus Torvalds committed
213
{
214 215
	unsigned last_byte = inode->i_size;

216 217 218
	last_byte -= page_nr << PAGE_SHIFT;
	if (last_byte > PAGE_SIZE)
		last_byte = PAGE_SIZE;
219
	return last_byte;
Linus Torvalds's avatar
Linus Torvalds committed
220 221
}

222 223
static inline struct ufs_dir_entry *
ufs_next_entry(struct super_block *sb, struct ufs_dir_entry *p)
Linus Torvalds's avatar
Linus Torvalds committed
224
{
225 226
	return (struct ufs_dir_entry *)((char *)p +
					fs16_to_cpu(sb, p->d_reclen));
Linus Torvalds's avatar
Linus Torvalds committed
227
}
228 229

struct ufs_dir_entry *ufs_dotdot(struct inode *dir, struct page **p)
Linus Torvalds's avatar
Linus Torvalds committed
230
{
231 232
	struct page *page = ufs_get_page(dir, 0);
	struct ufs_dir_entry *de = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
233

234 235 236 237
	if (!IS_ERR(page)) {
		de = ufs_next_entry(dir->i_sb,
				    (struct ufs_dir_entry *)page_address(page));
		*p = page;
Linus Torvalds's avatar
Linus Torvalds committed
238
	}
239
	return de;
Linus Torvalds's avatar
Linus Torvalds committed
240 241
}

242 243 244 245 246 247 248 249
/*
 *	ufs_find_entry()
 *
 * finds an entry in the specified directory with the wanted name. It
 * returns the page in which the entry was found, and the entry itself
 * (as a parameter - res_dir). Page is returned mapped and unlocked.
 * Entry is guaranteed to be valid.
 */
250
struct ufs_dir_entry *ufs_find_entry(struct inode *dir, const struct qstr *qstr,
251
				     struct page **res_page)
Linus Torvalds's avatar
Linus Torvalds committed
252
{
253
	struct super_block *sb = dir->i_sb;
254
	const unsigned char *name = qstr->name;
255
	int namelen = qstr->len;
256 257
	unsigned reclen = UFS_DIR_REC_LEN(namelen);
	unsigned long start, n;
258
	unsigned long npages = dir_pages(dir);
259
	struct page *page = NULL;
260
	struct ufs_inode_info *ui = UFS_I(dir);
261 262
	struct ufs_dir_entry *de;

263
	UFSD("ENTER, dir_ino %lu, name %s, namlen %u\n", dir->i_ino, name, namelen);
264 265 266 267 268 269 270

	if (npages == 0 || namelen > UFS_MAXNAMLEN)
		goto out;

	/* OFFSET_CACHE */
	*res_page = NULL;

271 272
	start = ui->i_dir_start_lookup;

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
	if (start >= npages)
		start = 0;
	n = start;
	do {
		char *kaddr;
		page = ufs_get_page(dir, n);
		if (!IS_ERR(page)) {
			kaddr = page_address(page);
			de = (struct ufs_dir_entry *) kaddr;
			kaddr += ufs_last_byte(dir, n) - reclen;
			while ((char *) de <= kaddr) {
				if (ufs_match(sb, namelen, name, de))
					goto found;
				de = ufs_next_entry(sb, de);
			}
			ufs_put_page(page);
		}
		if (++n >= npages)
			n = 0;
	} while (n != start);
out:
	return NULL;

found:
	*res_page = page;
298
	ui->i_dir_start_lookup = n;
299
	return de;
Linus Torvalds's avatar
Linus Torvalds committed
300 301 302
}

/*
303
 *	Parent is locked.
Linus Torvalds's avatar
Linus Torvalds committed
304 305 306
 */
int ufs_add_link(struct dentry *dentry, struct inode *inode)
{
307
	struct inode *dir = d_inode(dentry->d_parent);
308
	const unsigned char *name = dentry->d_name.name;
Linus Torvalds's avatar
Linus Torvalds committed
309
	int namelen = dentry->d_name.len;
310 311
	struct super_block *sb = dir->i_sb;
	unsigned reclen = UFS_DIR_REC_LEN(namelen);
312
	const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize;
313 314 315
	unsigned short rec_len, name_len;
	struct page *page = NULL;
	struct ufs_dir_entry *de;
316
	unsigned long npages = dir_pages(dir);
317 318
	unsigned long n;
	char *kaddr;
Nick Piggin's avatar
Nick Piggin committed
319
	loff_t pos;
Linus Torvalds's avatar
Linus Torvalds committed
320 321
	int err;

322
	UFSD("ENTER, name %s, namelen %u\n", name, namelen);
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339

	/*
	 * We take care of directory expansion in the same loop.
	 * This code plays outside i_size, so it locks the page
	 * to protect that region.
	 */
	for (n = 0; n <= npages; n++) {
		char *dir_end;

		page = ufs_get_page(dir, n);
		err = PTR_ERR(page);
		if (IS_ERR(page))
			goto out;
		lock_page(page);
		kaddr = page_address(page);
		dir_end = kaddr + ufs_last_byte(dir, n);
		de = (struct ufs_dir_entry *)kaddr;
340
		kaddr += PAGE_SIZE - reclen;
341 342 343 344
		while ((char *)de <= kaddr) {
			if ((char *)de == dir_end) {
				/* We hit i_size */
				name_len = 0;
345 346
				rec_len = chunk_size;
				de->d_reclen = cpu_to_fs16(sb, chunk_size);
347 348
				de->d_ino = 0;
				goto got_it;
Linus Torvalds's avatar
Linus Torvalds committed
349
			}
350
			if (de->d_reclen == 0) {
351
				ufs_error(dir->i_sb, __func__,
352 353 354 355 356 357 358 359 360 361 362 363 364 365
					  "zero-length directory entry");
				err = -EIO;
				goto out_unlock;
			}
			err = -EEXIST;
			if (ufs_match(sb, namelen, name, de))
				goto out_unlock;
			name_len = UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, de));
			rec_len = fs16_to_cpu(sb, de->d_reclen);
			if (!de->d_ino && rec_len >= reclen)
				goto got_it;
			if (rec_len >= name_len + reclen)
				goto got_it;
			de = (struct ufs_dir_entry *) ((char *) de + rec_len);
Linus Torvalds's avatar
Linus Torvalds committed
366
		}
367 368
		unlock_page(page);
		ufs_put_page(page);
Linus Torvalds's avatar
Linus Torvalds committed
369
	}
370 371 372 373
	BUG();
	return -EINVAL;

got_it:
Nick Piggin's avatar
Nick Piggin committed
374 375
	pos = page_offset(page) +
			(char*)de - (char*)page_address(page);
376
	err = ufs_prepare_chunk(page, pos, rec_len);
377 378
	if (err)
		goto out_unlock;
Linus Torvalds's avatar
Linus Torvalds committed
379
	if (de->d_ino) {
380 381 382 383 384
		struct ufs_dir_entry *de1 =
			(struct ufs_dir_entry *) ((char *) de + name_len);
		de1->d_reclen = cpu_to_fs16(sb, rec_len - name_len);
		de->d_reclen = cpu_to_fs16(sb, name_len);

Linus Torvalds's avatar
Linus Torvalds committed
385 386
		de = de1;
	}
387

Linus Torvalds's avatar
Linus Torvalds committed
388
	ufs_set_de_namlen(sb, de, namelen);
389
	memcpy(de->d_name, name, namelen + 1);
Linus Torvalds's avatar
Linus Torvalds committed
390 391
	de->d_ino = cpu_to_fs32(sb, inode->i_ino);
	ufs_set_de_type(sb, de, inode->i_mode);
392

Nick Piggin's avatar
Nick Piggin committed
393
	err = ufs_commit_chunk(page, pos, rec_len);
394
	dir->i_mtime = dir->i_ctime = current_time(dir);
395

Linus Torvalds's avatar
Linus Torvalds committed
396
	mark_inode_dirty(dir);
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
	/* OFFSET_CACHE */
out_put:
	ufs_put_page(page);
out:
	return err;
out_unlock:
	unlock_page(page);
	goto out_put;
}

static inline unsigned
ufs_validate_entry(struct super_block *sb, char *base,
		   unsigned offset, unsigned mask)
{
	struct ufs_dir_entry *de = (struct ufs_dir_entry*)(base + offset);
	struct ufs_dir_entry *p = (struct ufs_dir_entry*)(base + (offset&mask));
413
	while ((char*)p < (char*)de)
414 415 416
		p = ufs_next_entry(sb, p);
	return (char *)p - base;
}
Linus Torvalds's avatar
Linus Torvalds committed
417

418 419 420 421 422

/*
 * This is blatantly stolen from ext2fs
 */
static int
423
ufs_readdir(struct file *file, struct dir_context *ctx)
424
{
425 426
	loff_t pos = ctx->pos;
	struct inode *inode = file_inode(file);
427
	struct super_block *sb = inode->i_sb;
428 429
	unsigned int offset = pos & ~PAGE_MASK;
	unsigned long n = pos >> PAGE_SHIFT;
430
	unsigned long npages = dir_pages(inode);
431
	unsigned chunk_mask = ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1);
432
	bool need_revalidate = !inode_eq_iversion(inode, file->f_version);
433 434
	unsigned flags = UFS_SB(sb)->s_flags;

435
	UFSD("BEGIN\n");
436 437 438 439 440 441 442 443 444 445 446

	if (pos > inode->i_size - UFS_DIR_REC_LEN(1))
		return 0;

	for ( ; n < npages; n++, offset = 0) {
		char *kaddr, *limit;
		struct ufs_dir_entry *de;

		struct page *page = ufs_get_page(inode, n);

		if (IS_ERR(page)) {
447
			ufs_error(sb, __func__,
448 449
				  "bad page in #%lu",
				  inode->i_ino);
450
			ctx->pos += PAGE_SIZE - offset;
451 452 453 454 455 456
			return -EIO;
		}
		kaddr = page_address(page);
		if (unlikely(need_revalidate)) {
			if (offset) {
				offset = ufs_validate_entry(sb, kaddr, offset, chunk_mask);
457
				ctx->pos = (n<<PAGE_SHIFT) + offset;
458
			}
Jeff Layton's avatar
Jeff Layton committed
459 460
			file->f_version = inode_query_iversion(inode);
			need_revalidate = false;
461 462 463 464 465 466 467
		}
		de = (struct ufs_dir_entry *)(kaddr+offset);
		limit = kaddr + ufs_last_byte(inode, n) - UFS_DIR_REC_LEN(1);
		for ( ;(char*)de <= limit; de = ufs_next_entry(sb, de)) {
			if (de->d_ino) {
				unsigned char d_type = DT_UNKNOWN;

468 469 470
				UFSD("filldir(%s,%u)\n", de->d_name,
				      fs32_to_cpu(sb, de->d_ino));
				UFSD("namlen %u\n", ufs_get_de_namlen(sb, de));
471 472 473 474

				if ((flags & UFS_DE_MASK) == UFS_DE_44BSD)
					d_type = de->d_u.d_44.d_type;

475
				if (!dir_emit(ctx, de->d_name,
476
					       ufs_get_de_namlen(sb, de),
477 478
					       fs32_to_cpu(sb, de->d_ino),
					       d_type)) {
479 480 481 482
					ufs_put_page(page);
					return 0;
				}
			}
483
			ctx->pos += fs16_to_cpu(sb, de->d_reclen);
484 485 486
		}
		ufs_put_page(page);
	}
Linus Torvalds's avatar
Linus Torvalds committed
487 488 489
	return 0;
}

490

Linus Torvalds's avatar
Linus Torvalds committed
491 492 493 494
/*
 * ufs_delete_entry deletes a directory entry by merging it with the
 * previous entry.
 */
495 496
int ufs_delete_entry(struct inode *inode, struct ufs_dir_entry *dir,
		     struct page * page)
Linus Torvalds's avatar
Linus Torvalds committed
497
{
498 499
	struct super_block *sb = inode->i_sb;
	char *kaddr = page_address(page);
500
	unsigned from = ((char*)dir - kaddr) & ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1);
501
	unsigned to = ((char*)dir - kaddr) + fs16_to_cpu(sb, dir->d_reclen);
Nick Piggin's avatar
Nick Piggin committed
502
	loff_t pos;
503 504 505
	struct ufs_dir_entry *pde = NULL;
	struct ufs_dir_entry *de = (struct ufs_dir_entry *) (kaddr + from);
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
506

507
	UFSD("ENTER\n");
Linus Torvalds's avatar
Linus Torvalds committed
508

509
	UFSD("ino %u, reclen %u, namlen %u, name %s\n",
510 511
	      fs32_to_cpu(sb, de->d_ino),
	      fs16_to_cpu(sb, de->d_reclen),
512
	      ufs_get_de_namlen(sb, de), de->d_name);
513 514 515

	while ((char*)de < (char*)dir) {
		if (de->d_reclen == 0) {
516
			ufs_error(inode->i_sb, __func__,
517 518 519
				  "zero-length directory entry");
			err = -EIO;
			goto out;
Linus Torvalds's avatar
Linus Torvalds committed
520
		}
521 522
		pde = de;
		de = ufs_next_entry(sb, de);
Linus Torvalds's avatar
Linus Torvalds committed
523
	}
524 525
	if (pde)
		from = (char*)pde - (char*)page_address(page);
Nick Piggin's avatar
Nick Piggin committed
526 527

	pos = page_offset(page) + from;
528
	lock_page(page);
529
	err = ufs_prepare_chunk(page, pos, to - from);
530 531
	BUG_ON(err);
	if (pde)
Nick Piggin's avatar
Nick Piggin committed
532
		pde->d_reclen = cpu_to_fs16(sb, to - from);
533
	dir->d_ino = 0;
Nick Piggin's avatar
Nick Piggin committed
534
	err = ufs_commit_chunk(page, pos, to - from);
535
	inode->i_ctime = inode->i_mtime = current_time(inode);
536 537 538
	mark_inode_dirty(inode);
out:
	ufs_put_page(page);
539
	UFSD("EXIT\n");
540
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
541 542 543 544 545
}

int ufs_make_empty(struct inode * inode, struct inode *dir)
{
	struct super_block * sb = dir->i_sb;
546 547
	struct address_space *mapping = inode->i_mapping;
	struct page *page = grab_cache_page(mapping, 0);
548
	const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize;
Linus Torvalds's avatar
Linus Torvalds committed
549
	struct ufs_dir_entry * de;
550
	char *base;
Linus Torvalds's avatar
Linus Torvalds committed
551 552
	int err;

553 554
	if (!page)
		return -ENOMEM;
Nick Piggin's avatar
Nick Piggin committed
555

556
	err = ufs_prepare_chunk(page, 0, chunk_size);
557 558 559 560 561
	if (err) {
		unlock_page(page);
		goto fail;
	}

Nick Piggin's avatar
Nick Piggin committed
562
	kmap(page);
563
	base = (char*)page_address(page);
564
	memset(base, 0, PAGE_SIZE);
565 566

	de = (struct ufs_dir_entry *) base;
Linus Torvalds's avatar
Linus Torvalds committed
567 568 569 570 571 572 573 574 575 576

	de->d_ino = cpu_to_fs32(sb, inode->i_ino);
	ufs_set_de_type(sb, de, inode->i_mode);
	ufs_set_de_namlen(sb, de, 1);
	de->d_reclen = cpu_to_fs16(sb, UFS_DIR_REC_LEN(1));
	strcpy (de->d_name, ".");
	de = (struct ufs_dir_entry *)
		((char *)de + fs16_to_cpu(sb, de->d_reclen));
	de->d_ino = cpu_to_fs32(sb, dir->i_ino);
	ufs_set_de_type(sb, de, dir->i_mode);
577
	de->d_reclen = cpu_to_fs16(sb, chunk_size - UFS_DIR_REC_LEN(1));
Linus Torvalds's avatar
Linus Torvalds committed
578 579
	ufs_set_de_namlen(sb, de, 2);
	strcpy (de->d_name, "..");
Nick Piggin's avatar
Nick Piggin committed
580
	kunmap(page);
581

582
	err = ufs_commit_chunk(page, 0, chunk_size);
583
fail:
584
	put_page(page);
585
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
586 587 588 589 590
}

/*
 * routine to check that the specified directory is empty (for rmdir)
 */
591
int ufs_empty_dir(struct inode * inode)
Linus Torvalds's avatar
Linus Torvalds committed
592
{
593 594
	struct super_block *sb = inode->i_sb;
	struct page *page = NULL;
595
	unsigned long i, npages = dir_pages(inode);
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610

	for (i = 0; i < npages; i++) {
		char *kaddr;
		struct ufs_dir_entry *de;
		page = ufs_get_page(inode, i);

		if (IS_ERR(page))
			continue;

		kaddr = page_address(page);
		de = (struct ufs_dir_entry *)kaddr;
		kaddr += ufs_last_byte(inode, i) - UFS_DIR_REC_LEN(1);

		while ((char *)de <= kaddr) {
			if (de->d_reclen == 0) {
611
				ufs_error(inode->i_sb, __func__,
612 613 614
					"zero-length directory entry: "
					"kaddr=%p, de=%p\n", kaddr, de);
				goto not_empty;
Linus Torvalds's avatar
Linus Torvalds committed
615
			}
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
			if (de->d_ino) {
				u16 namelen=ufs_get_de_namlen(sb, de);
				/* check for . and .. */
				if (de->d_name[0] != '.')
					goto not_empty;
				if (namelen > 2)
					goto not_empty;
				if (namelen < 2) {
					if (inode->i_ino !=
					    fs32_to_cpu(sb, de->d_ino))
						goto not_empty;
				} else if (de->d_name[1] != '.')
					goto not_empty;
			}
			de = ufs_next_entry(sb, de);
Linus Torvalds's avatar
Linus Torvalds committed
631
		}
632
		ufs_put_page(page);
Linus Torvalds's avatar
Linus Torvalds committed
633 634
	}
	return 1;
635 636 637 638

not_empty:
	ufs_put_page(page);
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
639 640
}

641
const struct file_operations ufs_dir_operations = {
Linus Torvalds's avatar
Linus Torvalds committed
642
	.read		= generic_read_dir,
643
	.iterate_shared	= ufs_readdir,
644
	.fsync		= generic_file_fsync,
645
	.llseek		= generic_file_llseek,
Linus Torvalds's avatar
Linus Torvalds committed
646
};