Blame view

fs/omfs/dir.c 9.62 KB
a3ab7155e   Bob Copeland   omfs: add directo...
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
  /*
   * OMFS (as used by RIO Karma) directory operations.
   * Copyright (C) 2005 Bob Copeland <me@bobcopeland.com>
   * Released under GPL v2.
   */
  
  #include <linux/fs.h>
  #include <linux/ctype.h>
  #include <linux/buffer_head.h>
  #include "omfs.h"
  
  static int omfs_hash(const char *name, int namelen, int mod)
  {
  	int i, hash = 0;
  	for (i = 0; i < namelen; i++)
  		hash ^= tolower(name[i]) << (i % 24);
  	return hash % mod;
  }
  
  /*
   * Finds the bucket for a given name and reads the containing block;
   * *ofs is set to the offset of the first list entry.
   */
  static struct buffer_head *omfs_get_bucket(struct inode *dir,
  		const char *name, int namelen, int *ofs)
  {
  	int nbuckets = (dir->i_size - OMFS_DIR_START)/8;
a3ab7155e   Bob Copeland   omfs: add directo...
28
29
30
  	int bucket = omfs_hash(name, namelen, nbuckets);
  
  	*ofs = OMFS_DIR_START + bucket * 8;
f068272cb   Bob Copeland   omfs: check bound...
31
  	return omfs_bread(dir->i_sb, dir->i_ino);
a3ab7155e   Bob Copeland   omfs: add directo...
32
33
34
35
36
37
38
39
40
41
42
43
  }
  
  static struct buffer_head *omfs_scan_list(struct inode *dir, u64 block,
  				const char *name, int namelen,
  				u64 *prev_block)
  {
  	struct buffer_head *bh;
  	struct omfs_inode *oi;
  	int err = -ENOENT;
  	*prev_block = ~0;
  
  	while (block != ~0) {
f068272cb   Bob Copeland   omfs: check bound...
44
  		bh = omfs_bread(dir->i_sb, block);
a3ab7155e   Bob Copeland   omfs: add directo...
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  		if (!bh) {
  			err = -EIO;
  			goto err;
  		}
  
  		oi = (struct omfs_inode *) bh->b_data;
  		if (omfs_is_bad(OMFS_SB(dir->i_sb), &oi->i_head, block)) {
  			brelse(bh);
  			goto err;
  		}
  
  		if (strncmp(oi->i_name, name, namelen) == 0)
  			return bh;
  
  		*prev_block = block;
  		block = be64_to_cpu(oi->i_sibling);
  		brelse(bh);
  	}
  err:
  	return ERR_PTR(err);
  }
  
  static struct buffer_head *omfs_find_entry(struct inode *dir,
  					   const char *name, int namelen)
  {
  	struct buffer_head *bh;
  	int ofs;
  	u64 block, dummy;
  
  	bh = omfs_get_bucket(dir, name, namelen, &ofs);
  	if (!bh)
  		return ERR_PTR(-EIO);
  
  	block = be64_to_cpu(*((__be64 *) &bh->b_data[ofs]));
  	brelse(bh);
  
  	return omfs_scan_list(dir, block, name, namelen, &dummy);
  }
  
  int omfs_make_empty(struct inode *inode, struct super_block *sb)
  {
  	struct omfs_sb_info *sbi = OMFS_SB(sb);
a3ab7155e   Bob Copeland   omfs: add directo...
87
88
  	struct buffer_head *bh;
  	struct omfs_inode *oi;
f068272cb   Bob Copeland   omfs: check bound...
89
  	bh = omfs_bread(sb, inode->i_ino);
a3ab7155e   Bob Copeland   omfs: add directo...
90
91
92
93
  	if (!bh)
  		return -ENOMEM;
  
  	memset(bh->b_data, 0, sizeof(struct omfs_inode));
41c96486f   Al Viro   omfs: fix (mode &...
94
  	if (S_ISDIR(inode->i_mode)) {
a3ab7155e   Bob Copeland   omfs: add directo...
95
96
97
98
99
100
101
  		memset(&bh->b_data[OMFS_DIR_START], 0xff,
  			sbi->s_sys_blocksize - OMFS_DIR_START);
  	} else
  		omfs_make_empty_table(bh, OMFS_EXTENT_START);
  
  	oi = (struct omfs_inode *) bh->b_data;
  	oi->i_head.h_self = cpu_to_be64(inode->i_ino);
d406f66dd   Harvey Harrison   omfs: sparse anno...
102
  	oi->i_sibling = ~cpu_to_be64(0ULL);
a3ab7155e   Bob Copeland   omfs: add directo...
103
104
105
106
107
108
109
110
  
  	mark_buffer_dirty(bh);
  	brelse(bh);
  	return 0;
  }
  
  static int omfs_add_link(struct dentry *dentry, struct inode *inode)
  {
2b0143b5c   David Howells   VFS: normal files...
111
  	struct inode *dir = d_inode(dentry->d_parent);
a3ab7155e   Bob Copeland   omfs: add directo...
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  	const char *name = dentry->d_name.name;
  	int namelen = dentry->d_name.len;
  	struct omfs_inode *oi;
  	struct buffer_head *bh;
  	u64 block;
  	__be64 *entry;
  	int ofs;
  
  	/* just prepend to head of queue in proper bucket */
  	bh = omfs_get_bucket(dir, name, namelen, &ofs);
  	if (!bh)
  		goto out;
  
  	entry = (__be64 *) &bh->b_data[ofs];
  	block = be64_to_cpu(*entry);
  	*entry = cpu_to_be64(inode->i_ino);
  	mark_buffer_dirty(bh);
  	brelse(bh);
  
  	/* now set the sibling and parent pointers on the new inode */
f068272cb   Bob Copeland   omfs: check bound...
132
  	bh = omfs_bread(dir->i_sb, inode->i_ino);
a3ab7155e   Bob Copeland   omfs: add directo...
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
  	if (!bh)
  		goto out;
  
  	oi = (struct omfs_inode *) bh->b_data;
  	memcpy(oi->i_name, name, namelen);
  	memset(oi->i_name + namelen, 0, OMFS_NAMELEN - namelen);
  	oi->i_sibling = cpu_to_be64(block);
  	oi->i_parent = cpu_to_be64(dir->i_ino);
  	mark_buffer_dirty(bh);
  	brelse(bh);
  
  	dir->i_ctime = CURRENT_TIME_SEC;
  
  	/* mark affected inodes dirty to rebuild checksums */
  	mark_inode_dirty(dir);
  	mark_inode_dirty(inode);
  	return 0;
  out:
  	return -ENOMEM;
  }
  
  static int omfs_delete_entry(struct dentry *dentry)
  {
2b0143b5c   David Howells   VFS: normal files...
156
  	struct inode *dir = d_inode(dentry->d_parent);
a3ab7155e   Bob Copeland   omfs: add directo...
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
  	struct inode *dirty;
  	const char *name = dentry->d_name.name;
  	int namelen = dentry->d_name.len;
  	struct omfs_inode *oi;
  	struct buffer_head *bh, *bh2;
  	__be64 *entry, next;
  	u64 block, prev;
  	int ofs;
  	int err = -ENOMEM;
  
  	/* delete the proper node in the bucket's linked list */
  	bh = omfs_get_bucket(dir, name, namelen, &ofs);
  	if (!bh)
  		goto out;
  
  	entry = (__be64 *) &bh->b_data[ofs];
  	block = be64_to_cpu(*entry);
  
  	bh2 = omfs_scan_list(dir, block, name, namelen, &prev);
  	if (IS_ERR(bh2)) {
  		err = PTR_ERR(bh2);
  		goto out_free_bh;
  	}
  
  	oi = (struct omfs_inode *) bh2->b_data;
  	next = oi->i_sibling;
  	brelse(bh2);
  
  	if (prev != ~0) {
  		/* found in middle of list, get list ptr */
  		brelse(bh);
f068272cb   Bob Copeland   omfs: check bound...
188
  		bh = omfs_bread(dir->i_sb, prev);
a3ab7155e   Bob Copeland   omfs: add directo...
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
  		if (!bh)
  			goto out;
  
  		oi = (struct omfs_inode *) bh->b_data;
  		entry = &oi->i_sibling;
  	}
  
  	*entry = next;
  	mark_buffer_dirty(bh);
  
  	if (prev != ~0) {
  		dirty = omfs_iget(dir->i_sb, prev);
  		if (!IS_ERR(dirty)) {
  			mark_inode_dirty(dirty);
  			iput(dirty);
  		}
  	}
  
  	err = 0;
  out_free_bh:
  	brelse(bh);
  out:
  	return err;
  }
  
  static int omfs_dir_is_empty(struct inode *inode)
  {
  	int nbuckets = (inode->i_size - OMFS_DIR_START) / 8;
  	struct buffer_head *bh;
  	u64 *ptr;
  	int i;
f068272cb   Bob Copeland   omfs: check bound...
220
  	bh = omfs_bread(inode->i_sb, inode->i_ino);
a3ab7155e   Bob Copeland   omfs: add directo...
221
222
223
224
225
226
227
228
229
230
231
232
233
  
  	if (!bh)
  		return 0;
  
  	ptr = (u64 *) &bh->b_data[OMFS_DIR_START];
  
  	for (i = 0; i < nbuckets; i++, ptr++)
  		if (*ptr != ~0)
  			break;
  
  	brelse(bh);
  	return *ptr != ~0;
  }
d932805b3   Al Viro   omfs: merge unlin...
234
  static int omfs_remove(struct inode *dir, struct dentry *dentry)
a3ab7155e   Bob Copeland   omfs: add directo...
235
  {
2b0143b5c   David Howells   VFS: normal files...
236
  	struct inode *inode = d_inode(dentry);
d932805b3   Al Viro   omfs: merge unlin...
237
  	int ret;
79bf7c732   Sage Weil   vfs: push dentry_...
238

8aaa0f543   Sage Weil   omfs: remove unne...
239
240
241
  	if (S_ISDIR(inode->i_mode) &&
  	    !omfs_dir_is_empty(inode))
  		return -ENOTEMPTY;
a3ab7155e   Bob Copeland   omfs: add directo...
242
243
244
  
  	ret = omfs_delete_entry(dentry);
  	if (ret)
d932805b3   Al Viro   omfs: merge unlin...
245
246
247
248
  		return ret;
  	
  	clear_nlink(inode);
  	mark_inode_dirty(inode);
a3ab7155e   Bob Copeland   omfs: add directo...
249
  	mark_inode_dirty(dir);
d932805b3   Al Viro   omfs: merge unlin...
250
  	return 0;
a3ab7155e   Bob Copeland   omfs: add directo...
251
  }
587228be4   Al Viro   omfs: propagate u...
252
  static int omfs_add_node(struct inode *dir, struct dentry *dentry, umode_t mode)
a3ab7155e   Bob Copeland   omfs: add directo...
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
  {
  	int err;
  	struct inode *inode = omfs_new_inode(dir, mode);
  
  	if (IS_ERR(inode))
  		return PTR_ERR(inode);
  
  	err = omfs_make_empty(inode, dir->i_sb);
  	if (err)
  		goto out_free_inode;
  
  	err = omfs_add_link(dentry, inode);
  	if (err)
  		goto out_free_inode;
  
  	d_instantiate(dentry, inode);
  	return 0;
  
  out_free_inode:
  	iput(inode);
  	return err;
  }
18bb1db3e   Al Viro   switch vfs_mkdir(...
275
  static int omfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
a3ab7155e   Bob Copeland   omfs: add directo...
276
277
278
  {
  	return omfs_add_node(dir, dentry, mode | S_IFDIR);
  }
4acdaf27e   Al Viro   switch ->create()...
279
  static int omfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
ebfc3b49a   Al Viro   don't pass nameid...
280
  		bool excl)
a3ab7155e   Bob Copeland   omfs: add directo...
281
282
283
284
285
  {
  	return omfs_add_node(dir, dentry, mode | S_IFREG);
  }
  
  static struct dentry *omfs_lookup(struct inode *dir, struct dentry *dentry,
00cd8dd3b   Al Viro   stop passing name...
286
  				  unsigned int flags)
a3ab7155e   Bob Copeland   omfs: add directo...
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
  {
  	struct buffer_head *bh;
  	struct inode *inode = NULL;
  
  	if (dentry->d_name.len > OMFS_NAMELEN)
  		return ERR_PTR(-ENAMETOOLONG);
  
  	bh = omfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len);
  	if (!IS_ERR(bh)) {
  		struct omfs_inode *oi = (struct omfs_inode *)bh->b_data;
  		ino_t ino = be64_to_cpu(oi->i_head.h_self);
  		brelse(bh);
  		inode = omfs_iget(dir->i_sb, ino);
  		if (IS_ERR(inode))
  			return ERR_CAST(inode);
  	}
  	d_add(dentry, inode);
  	return NULL;
  }
  
  /* sanity check block's self pointer */
  int omfs_is_bad(struct omfs_sb_info *sbi, struct omfs_header *header,
  	u64 fsblock)
  {
  	int is_bad;
  	u64 ino = be64_to_cpu(header->h_self);
  	is_bad = ((ino != fsblock) || (ino < sbi->s_root_ino) ||
  		(ino > sbi->s_num_blocks));
  
  	if (is_bad)
  		printk(KERN_WARNING "omfs: bad hash chain detected
  ");
  
  	return is_bad;
  }
9fd4d0594   Al Viro   [readdir] convert...
322
  static bool omfs_fill_chain(struct inode *dir, struct dir_context *ctx,
a3ab7155e   Bob Copeland   omfs: add directo...
323
324
  		u64 fsblock, int hindex)
  {
a3ab7155e   Bob Copeland   omfs: add directo...
325
326
  	/* follow chain in this bucket */
  	while (fsblock != ~0) {
9fd4d0594   Al Viro   [readdir] convert...
327
328
329
330
  		struct buffer_head *bh = omfs_bread(dir->i_sb, fsblock);
  		struct omfs_inode *oi;
  		u64 self;
  		unsigned char d_type;
a3ab7155e   Bob Copeland   omfs: add directo...
331
  		if (!bh)
9fd4d0594   Al Viro   [readdir] convert...
332
  			return true;
a3ab7155e   Bob Copeland   omfs: add directo...
333
334
335
336
  
  		oi = (struct omfs_inode *) bh->b_data;
  		if (omfs_is_bad(OMFS_SB(dir->i_sb), &oi->i_head, fsblock)) {
  			brelse(bh);
9fd4d0594   Al Viro   [readdir] convert...
337
  			return true;
a3ab7155e   Bob Copeland   omfs: add directo...
338
339
340
341
342
343
344
345
346
347
348
349
350
  		}
  
  		self = fsblock;
  		fsblock = be64_to_cpu(oi->i_sibling);
  
  		/* skip visited nodes */
  		if (hindex) {
  			hindex--;
  			brelse(bh);
  			continue;
  		}
  
  		d_type = (oi->i_type == OMFS_DIR) ? DT_DIR : DT_REG;
9fd4d0594   Al Viro   [readdir] convert...
351
352
353
354
355
356
  		if (!dir_emit(ctx, oi->i_name,
  			      strnlen(oi->i_name, OMFS_NAMELEN),
  			      self, d_type)) {
  			brelse(bh);
  			return false;
  		}
a3ab7155e   Bob Copeland   omfs: add directo...
357
  		brelse(bh);
9fd4d0594   Al Viro   [readdir] convert...
358
  		ctx->pos++;
a3ab7155e   Bob Copeland   omfs: add directo...
359
  	}
9fd4d0594   Al Viro   [readdir] convert...
360
  	return true;
a3ab7155e   Bob Copeland   omfs: add directo...
361
362
363
364
365
  }
  
  static int omfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  		struct inode *new_dir, struct dentry *new_dentry)
  {
2b0143b5c   David Howells   VFS: normal files...
366
367
  	struct inode *new_inode = d_inode(new_dentry);
  	struct inode *old_inode = d_inode(old_dentry);
a3ab7155e   Bob Copeland   omfs: add directo...
368
  	int err;
a3ab7155e   Bob Copeland   omfs: add directo...
369
370
  	if (new_inode) {
  		/* overwriting existing file/dir */
d932805b3   Al Viro   omfs: merge unlin...
371
  		err = omfs_remove(new_dir, new_dentry);
a3ab7155e   Bob Copeland   omfs: add directo...
372
373
374
375
376
377
  		if (err)
  			goto out;
  	}
  
  	/* since omfs locates files by name, we need to unlink _before_
  	 * adding the new link or we won't find the old one */
cdb26496d   Al Viro   omfs: stop playin...
378
379
  	err = omfs_delete_entry(old_dentry);
  	if (err)
a3ab7155e   Bob Copeland   omfs: add directo...
380
  		goto out;
a3ab7155e   Bob Copeland   omfs: add directo...
381

cdb26496d   Al Viro   omfs: stop playin...
382
  	mark_inode_dirty(old_dir);
a3ab7155e   Bob Copeland   omfs: add directo...
383
384
385
386
387
  	err = omfs_add_link(new_dentry, old_inode);
  	if (err)
  		goto out;
  
  	old_inode->i_ctime = CURRENT_TIME_SEC;
013e4f4a2   Al Viro   omfs: rename() ne...
388
  	mark_inode_dirty(old_inode);
a3ab7155e   Bob Copeland   omfs: add directo...
389
390
391
  out:
  	return err;
  }
9fd4d0594   Al Viro   [readdir] convert...
392
  static int omfs_readdir(struct file *file, struct dir_context *ctx)
a3ab7155e   Bob Copeland   omfs: add directo...
393
  {
9fd4d0594   Al Viro   [readdir] convert...
394
  	struct inode *dir = file_inode(file);
a3ab7155e   Bob Copeland   omfs: add directo...
395
  	struct buffer_head *bh;
9fd4d0594   Al Viro   [readdir] convert...
396
  	__be64 *p;
a3ab7155e   Bob Copeland   omfs: add directo...
397
398
  	unsigned int hchain, hindex;
  	int nbuckets;
9fd4d0594   Al Viro   [readdir] convert...
399
400
401
402
403
404
405
406
  
  	if (ctx->pos >> 32)
  		return -EINVAL;
  
  	if (ctx->pos < 1 << 20) {
  		if (!dir_emit_dots(file, ctx))
  			return 0;
  		ctx->pos = 1 << 20;
a3ab7155e   Bob Copeland   omfs: add directo...
407
408
409
410
411
  	}
  
  	nbuckets = (dir->i_size - OMFS_DIR_START) / 8;
  
  	/* high 12 bits store bucket + 1 and low 20 bits store hash index */
9fd4d0594   Al Viro   [readdir] convert...
412
413
  	hchain = (ctx->pos >> 20) - 1;
  	hindex = ctx->pos & 0xfffff;
a3ab7155e   Bob Copeland   omfs: add directo...
414

f068272cb   Bob Copeland   omfs: check bound...
415
  	bh = omfs_bread(dir->i_sb, dir->i_ino);
a3ab7155e   Bob Copeland   omfs: add directo...
416
  	if (!bh)
9fd4d0594   Al Viro   [readdir] convert...
417
  		return -EINVAL;
a3ab7155e   Bob Copeland   omfs: add directo...
418

9fd4d0594   Al Viro   [readdir] convert...
419
  	p = (__be64 *)(bh->b_data + OMFS_DIR_START) + hchain;
a3ab7155e   Bob Copeland   omfs: add directo...
420

9fd4d0594   Al Viro   [readdir] convert...
421
422
423
  	for (; hchain < nbuckets; hchain++) {
  		__u64 fsblock = be64_to_cpu(*p++);
  		if (!omfs_fill_chain(dir, ctx, fsblock, hindex))
a3ab7155e   Bob Copeland   omfs: add directo...
424
  			break;
9fd4d0594   Al Viro   [readdir] convert...
425
426
  		hindex = 0;
  		ctx->pos = (hchain+2) << 20;
a3ab7155e   Bob Copeland   omfs: add directo...
427
428
  	}
  	brelse(bh);
9fd4d0594   Al Viro   [readdir] convert...
429
  	return 0;
a3ab7155e   Bob Copeland   omfs: add directo...
430
  }
6e1d5dcc2   Alexey Dobriyan   const: mark remai...
431
  const struct inode_operations omfs_dir_inops = {
a3ab7155e   Bob Copeland   omfs: add directo...
432
433
434
435
  	.lookup = omfs_lookup,
  	.mkdir = omfs_mkdir,
  	.rename = omfs_rename,
  	.create = omfs_create,
d932805b3   Al Viro   omfs: merge unlin...
436
437
  	.unlink = omfs_remove,
  	.rmdir = omfs_remove,
a3ab7155e   Bob Copeland   omfs: add directo...
438
  };
828c09509   Alexey Dobriyan   const: constify r...
439
  const struct file_operations omfs_dir_operations = {
a3ab7155e   Bob Copeland   omfs: add directo...
440
  	.read = generic_read_dir,
9fd4d0594   Al Viro   [readdir] convert...
441
  	.iterate = omfs_readdir,
3222a3e55   Christoph Hellwig   [PATCH] fix ->lls...
442
  	.llseek = generic_file_llseek,
a3ab7155e   Bob Copeland   omfs: add directo...
443
  };