blob: eb68cdd41d481707d726aa5a67411d84cb2127c9 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
4 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License v.2.
8 */
9
10/*
11* Implements Extendible Hashing as described in:
12* "Extendible Hashing" by Fagin, et al in
13* __ACM Trans. on Database Systems__, Sept 1979.
14*
15*
16* Here's the layout of dirents which is essentially the same as that of ext2
17* within a single block. The field de_name_len is the number of bytes
18* actually required for the name (no null terminator). The field de_rec_len
19* is the number of bytes allocated to the dirent. The offset of the next
20* dirent in the block is (dirent + dirent->de_rec_len). When a dirent is
21* deleted, the preceding dirent inherits its allocated space, ie
22* prev->de_rec_len += deleted->de_rec_len. Since the next dirent is obtained
23* by adding de_rec_len to the current dirent, this essentially causes the
24* deleted dirent to get jumped over when iterating through all the dirents.
25*
26* When deleting the first dirent in a block, there is no previous dirent so
27* the field de_ino is set to zero to designate it as deleted. When allocating
28* a dirent, gfs2_dirent_alloc iterates through the dirents in a block. If the
29* first dirent has (de_ino == 0) and de_rec_len is large enough, this first
30* dirent is allocated. Otherwise it must go through all the 'used' dirents
31* searching for one in which the amount of total space minus the amount of
32* used space will provide enough space for the new dirent.
33*
34* There are two types of blocks in which dirents reside. In a stuffed dinode,
35* the dirents begin at offset sizeof(struct gfs2_dinode) from the beginning of
36* the block. In leaves, they begin at offset sizeof(struct gfs2_leaf) from the
37* beginning of the leaf block. The dirents reside in leaves when
38*
39* dip->i_di.di_flags & GFS2_DIF_EXHASH is true
40*
41* Otherwise, the dirents are "linear", within a single stuffed dinode block.
42*
43* When the dirents are in leaves, the actual contents of the directory file are
44* used as an array of 64-bit block pointers pointing to the leaf blocks. The
45* dirents are NOT in the directory file itself. There can be more than one block
46* pointer in the array that points to the same leaf. In fact, when a directory
47* is first converted from linear to exhash, all of the pointers point to the
48* same leaf.
49*
50* When a leaf is completely full, the size of the hash table can be
51* doubled unless it is already at the maximum size which is hard coded into
52* GFS2_DIR_MAX_DEPTH. After that, leaves are chained together in a linked list,
53* but never before the maximum hash table size has been reached.
54*/
55
56#include <linux/sched.h>
57#include <linux/slab.h>
58#include <linux/spinlock.h>
59#include <linux/completion.h>
60#include <linux/buffer_head.h>
61#include <linux/sort.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050062#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050063#include <linux/crc32.h>
Steven Whitehousefe1bded2006-04-18 10:09:15 -040064#include <linux/vmalloc.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000065#include <asm/semaphore.h>
66
67#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050068#include "lm_interface.h"
69#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000070#include "dir.h"
71#include "glock.h"
72#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000073#include "meta_io.h"
74#include "quota.h"
75#include "rgrp.h"
76#include "trans.h"
Steven Whitehousee13940b2006-01-30 13:31:50 +000077#include "bmap.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050078#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000079
80#define IS_LEAF 1 /* Hashed (leaf) directory */
81#define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
82
83#if 1
84#define gfs2_disk_hash2offset(h) (((uint64_t)(h)) >> 1)
85#define gfs2_dir_offset2hash(p) ((uint32_t)(((uint64_t)(p)) << 1))
86#else
87#define gfs2_disk_hash2offset(h) (((uint64_t)(h)))
88#define gfs2_dir_offset2hash(p) ((uint32_t)(((uint64_t)(p))))
89#endif
90
91typedef int (*leaf_call_t) (struct gfs2_inode *dip,
92 uint32_t index, uint32_t len, uint64_t leaf_no,
93 void *data);
94
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000095int gfs2_dir_get_buffer(struct gfs2_inode *ip, uint64_t block, int new,
96 struct buffer_head **bhp)
Steven Whitehousee13940b2006-01-30 13:31:50 +000097{
98 struct buffer_head *bh;
99 int error = 0;
100
101 if (new) {
102 bh = gfs2_meta_new(ip->i_gl, block);
103 gfs2_trans_add_bh(ip->i_gl, bh, 1);
104 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
105 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
106 } else {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500107 error = gfs2_meta_read(ip->i_gl, block, DIO_START | DIO_WAIT,
108 &bh);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000109 if (error)
110 return error;
111 if (gfs2_metatype_check(ip->i_sbd, bh, GFS2_METATYPE_JD)) {
112 brelse(bh);
113 return -EIO;
114 }
115 }
116
117 *bhp = bh;
118 return 0;
119}
120
121
122
123static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
124 unsigned int offset, unsigned int size)
125
126{
127 struct buffer_head *dibh;
128 int error;
129
130 error = gfs2_meta_inode_buffer(ip, &dibh);
131 if (error)
132 return error;
133
134 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehousec7526662006-03-20 12:30:04 -0500135 memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000136 if (ip->i_di.di_size < offset + size)
137 ip->i_di.di_size = offset + size;
138 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
139 gfs2_dinode_out(&ip->i_di, dibh->b_data);
140
141 brelse(dibh);
142
143 return size;
144}
145
146
147
148/**
149 * gfs2_dir_write_data - Write directory information to the inode
150 * @ip: The GFS2 inode
151 * @buf: The buffer containing information to be written
152 * @offset: The file offset to start writing at
153 * @size: The amount of data to write
154 *
155 * Returns: The number of bytes correctly written or error code
156 */
157static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
158 uint64_t offset, unsigned int size)
159{
160 struct gfs2_sbd *sdp = ip->i_sbd;
161 struct buffer_head *dibh;
162 uint64_t lblock, dblock;
163 uint32_t extlen = 0;
164 unsigned int o;
165 int copied = 0;
166 int error = 0;
167
168 if (!size)
169 return 0;
170
171 if (gfs2_is_stuffed(ip) &&
172 offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500173 return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
174 size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000175
176 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
177 return -EINVAL;
178
179 if (gfs2_is_stuffed(ip)) {
180 error = gfs2_unstuff_dinode(ip, NULL, NULL);
181 if (error)
Steven Whitehousec7526662006-03-20 12:30:04 -0500182 return error;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000183 }
184
185 lblock = offset;
186 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
187
188 while (copied < size) {
189 unsigned int amount;
190 struct buffer_head *bh;
191 int new;
192
193 amount = size - copied;
194 if (amount > sdp->sd_sb.sb_bsize - o)
195 amount = sdp->sd_sb.sb_bsize - o;
196
197 if (!extlen) {
198 new = 1;
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500199 error = gfs2_block_map(ip, lblock, &new, &dblock,
200 &extlen);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000201 if (error)
202 goto fail;
203 error = -EIO;
204 if (gfs2_assert_withdraw(sdp, dblock))
205 goto fail;
206 }
207
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500208 error = gfs2_dir_get_buffer(ip, dblock,
209 (amount == sdp->sd_jbsize) ?
210 1 : new, &bh);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000211 if (error)
212 goto fail;
213
214 gfs2_trans_add_bh(ip->i_gl, bh, 1);
215 memcpy(bh->b_data + o, buf, amount);
216 brelse(bh);
217 if (error)
218 goto fail;
219
220 copied += amount;
221 lblock++;
222 dblock++;
223 extlen--;
224
225 o = sizeof(struct gfs2_meta_header);
226 }
227
228out:
229 error = gfs2_meta_inode_buffer(ip, &dibh);
230 if (error)
231 return error;
232
233 if (ip->i_di.di_size < offset + copied)
234 ip->i_di.di_size = offset + copied;
235 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
236
237 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
238 gfs2_dinode_out(&ip->i_di, dibh->b_data);
239 brelse(dibh);
240
241 return copied;
242fail:
243 if (copied)
244 goto out;
245 return error;
246}
247
248static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf,
Steven Whitehousec7526662006-03-20 12:30:04 -0500249 unsigned int offset, unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000250{
251 struct buffer_head *dibh;
252 int error;
253
254 error = gfs2_meta_inode_buffer(ip, &dibh);
255 if (!error) {
256 offset += sizeof(struct gfs2_dinode);
257 memcpy(buf, dibh->b_data + offset, size);
258 brelse(dibh);
259 }
260
261 return (error) ? error : size;
262}
263
264
265/**
266 * gfs2_dir_read_data - Read a data from a directory inode
267 * @ip: The GFS2 Inode
268 * @buf: The buffer to place result into
269 * @offset: File offset to begin jdata_readng from
270 * @size: Amount of data to transfer
271 *
272 * Returns: The amount of data actually copied or the error
273 */
274static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf,
275 uint64_t offset, unsigned int size)
276{
277 struct gfs2_sbd *sdp = ip->i_sbd;
278 uint64_t lblock, dblock;
279 uint32_t extlen = 0;
280 unsigned int o;
281 int copied = 0;
282 int error = 0;
283
284 if (offset >= ip->i_di.di_size)
285 return 0;
286
287 if ((offset + size) > ip->i_di.di_size)
288 size = ip->i_di.di_size - offset;
289
290 if (!size)
291 return 0;
292
293 if (gfs2_is_stuffed(ip))
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500294 return gfs2_dir_read_stuffed(ip, buf, (unsigned int)offset,
295 size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000296
297 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
298 return -EINVAL;
299
300 lblock = offset;
301 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
302
303 while (copied < size) {
304 unsigned int amount;
305 struct buffer_head *bh;
306 int new;
307
308 amount = size - copied;
309 if (amount > sdp->sd_sb.sb_bsize - o)
310 amount = sdp->sd_sb.sb_bsize - o;
311
312 if (!extlen) {
313 new = 0;
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500314 error = gfs2_block_map(ip, lblock, &new, &dblock,
315 &extlen);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000316 if (error)
317 goto fail;
318 }
319
320 if (extlen > 1)
321 gfs2_meta_ra(ip->i_gl, dblock, extlen);
322
323 if (dblock) {
324 error = gfs2_dir_get_buffer(ip, dblock, new, &bh);
325 if (error)
326 goto fail;
327 dblock++;
328 extlen--;
329 } else
330 bh = NULL;
331
332 memcpy(buf, bh->b_data + o, amount);
333 brelse(bh);
334 if (error)
335 goto fail;
336
337 copied += amount;
338 lblock++;
339
340 o = sizeof(struct gfs2_meta_header);
341 }
342
343 return copied;
344fail:
345 return (copied) ? copied : error;
346}
347
Steven Whitehousec7526662006-03-20 12:30:04 -0500348typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500349 const struct qstr *name,
350 void *opaque);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000351
Steven Whitehousec7526662006-03-20 12:30:04 -0500352static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
353 const struct qstr *name, int ret)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000354{
Steven Whitehousec7526662006-03-20 12:30:04 -0500355 if (dent->de_inum.no_addr != 0 &&
356 be32_to_cpu(dent->de_hash) == name->hash &&
357 be16_to_cpu(dent->de_name_len) == name->len &&
358 memcmp((char *)(dent+1), name->name, name->len) == 0)
359 return ret;
360 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000361}
362
Steven Whitehousec7526662006-03-20 12:30:04 -0500363static int gfs2_dirent_find(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500364 const struct qstr *name,
365 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500366{
367 return __gfs2_dirent_find(dent, name, 1);
368}
369
370static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500371 const struct qstr *name,
372 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500373{
374 return __gfs2_dirent_find(dent, name, 2);
375}
376
377/*
378 * name->name holds ptr to start of block.
379 * name->len holds size of block.
380 */
381static int gfs2_dirent_last(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500382 const struct qstr *name,
383 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500384{
385 const char *start = name->name;
386 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
387 if (name->len == (end - start))
388 return 1;
389 return 0;
390}
391
392static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500393 const struct qstr *name,
394 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500395{
396 unsigned required = GFS2_DIRENT_SIZE(name->len);
397 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
398 unsigned totlen = be16_to_cpu(dent->de_rec_len);
399
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500400 if (!dent->de_inum.no_addr)
401 actual = GFS2_DIRENT_SIZE(0);
Steven Whitehousec7526662006-03-20 12:30:04 -0500402 if ((totlen - actual) >= required)
403 return 1;
404 return 0;
405}
406
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500407struct dirent_gather {
408 const struct gfs2_dirent **pdent;
409 unsigned offset;
410};
411
412static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
413 const struct qstr *name,
414 void *opaque)
415{
416 struct dirent_gather *g = opaque;
417 if (dent->de_inum.no_addr) {
418 g->pdent[g->offset++] = dent;
419 }
420 return 0;
421}
422
Steven Whitehousec7526662006-03-20 12:30:04 -0500423/*
424 * Other possible things to check:
425 * - Inode located within filesystem size (and on valid block)
426 * - Valid directory entry type
427 * Not sure how heavy-weight we want to make this... could also check
428 * hash is correct for example, but that would take a lot of extra time.
429 * For now the most important thing is to check that the various sizes
430 * are correct.
431 */
432static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
433 unsigned int size, unsigned int len, int first)
434{
435 const char *msg = "gfs2_dirent too small";
436 if (unlikely(size < sizeof(struct gfs2_dirent)))
437 goto error;
438 msg = "gfs2_dirent misaligned";
439 if (unlikely(offset & 0x7))
440 goto error;
441 msg = "gfs2_dirent points beyond end of block";
442 if (unlikely(offset + size > len))
443 goto error;
444 msg = "zero inode number";
445 if (unlikely(!first && !dent->de_inum.no_addr))
446 goto error;
447 msg = "name length is greater than space in dirent";
448 if (dent->de_inum.no_addr &&
449 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
450 size))
451 goto error;
452 return 0;
453error:
454 printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
455 first ? "first in block" : "not first in block");
456 return -EIO;
457}
458
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500459static int gfs2_dirent_offset(const void *buf)
Steven Whitehousec7526662006-03-20 12:30:04 -0500460{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500461 const struct gfs2_meta_header *h = buf;
462 int offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500463
464 BUG_ON(buf == NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500465
Steven Whitehousee3167de2006-03-30 15:46:23 -0500466 switch(be32_to_cpu(h->mh_type)) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500467 case GFS2_METATYPE_LF:
468 offset = sizeof(struct gfs2_leaf);
469 break;
470 case GFS2_METATYPE_DI:
471 offset = sizeof(struct gfs2_dinode);
472 break;
473 default:
474 goto wrong_type;
475 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500476 return offset;
477wrong_type:
478 printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
Steven Whitehousee3167de2006-03-30 15:46:23 -0500479 be32_to_cpu(h->mh_type));
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500480 return -1;
481}
Steven Whitehousec7526662006-03-20 12:30:04 -0500482
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500483static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode,
484 void *buf,
485 unsigned int len, gfs2_dscan_t scan,
486 const struct qstr *name,
487 void *opaque)
488{
489 struct gfs2_dirent *dent, *prev;
490 unsigned offset;
491 unsigned size;
492 int ret = 0;
493
494 ret = gfs2_dirent_offset(buf);
495 if (ret < 0)
496 goto consist_inode;
497
498 offset = ret;
Steven Whitehousec7526662006-03-20 12:30:04 -0500499 prev = NULL;
500 dent = (struct gfs2_dirent *)(buf + offset);
501 size = be16_to_cpu(dent->de_rec_len);
502 if (gfs2_check_dirent(dent, offset, size, len, 1))
503 goto consist_inode;
504 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500505 ret = scan(dent, name, opaque);
Steven Whitehousec7526662006-03-20 12:30:04 -0500506 if (ret)
507 break;
508 offset += size;
509 if (offset == len)
510 break;
511 prev = dent;
512 dent = (struct gfs2_dirent *)(buf + offset);
513 size = be16_to_cpu(dent->de_rec_len);
514 if (gfs2_check_dirent(dent, offset, size, len, 0))
515 goto consist_inode;
516 } while(1);
517
518 switch(ret) {
519 case 0:
520 return NULL;
521 case 1:
522 return dent;
523 case 2:
524 return prev ? prev : dent;
525 default:
526 BUG_ON(ret > 0);
527 return ERR_PTR(ret);
528 }
529
Steven Whitehousec7526662006-03-20 12:30:04 -0500530consist_inode:
531 gfs2_consist_inode(inode->u.generic_ip);
532 return ERR_PTR(-EIO);
533}
534
535
David Teiglandb3b94fa2006-01-16 16:50:04 +0000536/**
537 * dirent_first - Return the first dirent
538 * @dip: the directory
539 * @bh: The buffer
540 * @dent: Pointer to list of dirents
541 *
542 * return first dirent whether bh points to leaf or stuffed dinode
543 *
544 * Returns: IS_LEAF, IS_DINODE, or -errno
545 */
546
547static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
548 struct gfs2_dirent **dent)
549{
550 struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data;
551
Steven Whitehousee3167de2006-03-30 15:46:23 -0500552 if (be32_to_cpu(h->mh_type) == GFS2_METATYPE_LF) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000553 if (gfs2_meta_check(dip->i_sbd, bh))
554 return -EIO;
555 *dent = (struct gfs2_dirent *)(bh->b_data +
556 sizeof(struct gfs2_leaf));
557 return IS_LEAF;
558 } else {
559 if (gfs2_metatype_check(dip->i_sbd, bh, GFS2_METATYPE_DI))
560 return -EIO;
561 *dent = (struct gfs2_dirent *)(bh->b_data +
562 sizeof(struct gfs2_dinode));
563 return IS_DINODE;
564 }
565}
566
567/**
568 * dirent_next - Next dirent
569 * @dip: the directory
570 * @bh: The buffer
571 * @dent: Pointer to list of dirents
572 *
573 * Returns: 0 on success, error code otherwise
574 */
575
576static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
577 struct gfs2_dirent **dent)
578{
579 struct gfs2_dirent *tmp, *cur;
580 char *bh_end;
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000581 uint16_t cur_rec_len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000582
583 cur = *dent;
584 bh_end = bh->b_data + bh->b_size;
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000585 cur_rec_len = be16_to_cpu(cur->de_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000586
587 if ((char *)cur + cur_rec_len >= bh_end) {
588 if ((char *)cur + cur_rec_len > bh_end) {
589 gfs2_consist_inode(dip);
590 return -EIO;
591 }
592 return -ENOENT;
593 }
594
595 tmp = (struct gfs2_dirent *)((char *)cur + cur_rec_len);
596
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000597 if ((char *)tmp + be16_to_cpu(tmp->de_rec_len) > bh_end) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000598 gfs2_consist_inode(dip);
599 return -EIO;
600 }
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000601
602 if (cur_rec_len == 0) {
603 gfs2_consist_inode(dip);
604 return -EIO;
605 }
606
David Teiglandb3b94fa2006-01-16 16:50:04 +0000607 /* Only the first dent could ever have de_inum.no_addr == 0 */
608 if (!tmp->de_inum.no_addr) {
609 gfs2_consist_inode(dip);
610 return -EIO;
611 }
612
613 *dent = tmp;
614
615 return 0;
616}
617
618/**
619 * dirent_del - Delete a dirent
620 * @dip: The GFS2 inode
621 * @bh: The buffer
622 * @prev: The previous dirent
623 * @cur: The current dirent
624 *
625 */
626
627static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
628 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
629{
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000630 uint16_t cur_rec_len, prev_rec_len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000631
632 if (!cur->de_inum.no_addr) {
633 gfs2_consist_inode(dip);
634 return;
635 }
636
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000637 gfs2_trans_add_bh(dip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000638
639 /* If there is no prev entry, this is the first entry in the block.
640 The de_rec_len is already as big as it needs to be. Just zero
641 out the inode number and return. */
642
643 if (!prev) {
644 cur->de_inum.no_addr = 0; /* No endianess worries */
645 return;
646 }
647
648 /* Combine this dentry with the previous one. */
649
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000650 prev_rec_len = be16_to_cpu(prev->de_rec_len);
651 cur_rec_len = be16_to_cpu(cur->de_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000652
653 if ((char *)prev + prev_rec_len != (char *)cur)
654 gfs2_consist_inode(dip);
655 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
656 gfs2_consist_inode(dip);
657
658 prev_rec_len += cur_rec_len;
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000659 prev->de_rec_len = cpu_to_be16(prev_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000660}
661
Steven Whitehousec7526662006-03-20 12:30:04 -0500662/*
663 * Takes a dent from which to grab space as an argument. Returns the
664 * newly created dent.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000665 */
Steven Whitehousec7526662006-03-20 12:30:04 -0500666struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
667 struct gfs2_dirent *dent,
668 const struct qstr *name,
669 struct buffer_head *bh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000670{
Steven Whitehousec7526662006-03-20 12:30:04 -0500671 struct gfs2_inode *ip = inode->u.generic_ip;
672 struct gfs2_dirent *ndent;
673 unsigned offset = 0, totlen;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000674
Steven Whitehousec7526662006-03-20 12:30:04 -0500675 if (dent->de_inum.no_addr)
676 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
677 totlen = be16_to_cpu(dent->de_rec_len);
678 BUG_ON(offset + name->len > totlen);
679 gfs2_trans_add_bh(ip->i_gl, bh, 1);
680 ndent = (struct gfs2_dirent *)((char *)dent + offset);
681 dent->de_rec_len = cpu_to_be16(offset);
682 gfs2_qstr2dirent(name, totlen - offset, ndent);
683 return ndent;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000684}
685
Steven Whitehousec7526662006-03-20 12:30:04 -0500686static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
687 struct buffer_head *bh,
688 const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000689{
690 struct gfs2_dirent *dent;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500691 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
692 gfs2_dirent_find_space, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500693 if (!dent || IS_ERR(dent))
694 return dent;
695 return gfs2_init_dirent(inode, dent, name, bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000696}
697
698static int get_leaf(struct gfs2_inode *dip, uint64_t leaf_no,
699 struct buffer_head **bhp)
700{
701 int error;
702
703 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_START | DIO_WAIT, bhp);
704 if (!error && gfs2_metatype_check(dip->i_sbd, *bhp, GFS2_METATYPE_LF))
705 error = -EIO;
706
707 return error;
708}
709
710/**
711 * get_leaf_nr - Get a leaf number associated with the index
712 * @dip: The GFS2 inode
713 * @index:
714 * @leaf_out:
715 *
716 * Returns: 0 on success, error code otherwise
717 */
718
719static int get_leaf_nr(struct gfs2_inode *dip, uint32_t index,
720 uint64_t *leaf_out)
721{
722 uint64_t leaf_no;
723 int error;
724
Steven Whitehousee13940b2006-01-30 13:31:50 +0000725 error = gfs2_dir_read_data(dip, (char *)&leaf_no,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000726 index * sizeof(uint64_t),
727 sizeof(uint64_t));
728 if (error != sizeof(uint64_t))
729 return (error < 0) ? error : -EIO;
730
731 *leaf_out = be64_to_cpu(leaf_no);
732
733 return 0;
734}
735
736static int get_first_leaf(struct gfs2_inode *dip, uint32_t index,
737 struct buffer_head **bh_out)
738{
739 uint64_t leaf_no;
740 int error;
741
742 error = get_leaf_nr(dip, index, &leaf_no);
743 if (!error)
744 error = get_leaf(dip, leaf_no, bh_out);
745
746 return error;
747}
748
Steven Whitehousec7526662006-03-20 12:30:04 -0500749static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
750 const struct qstr *name,
751 gfs2_dscan_t scan,
752 struct buffer_head **pbh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000753{
Steven Whitehousec7526662006-03-20 12:30:04 -0500754 struct buffer_head *bh;
755 struct gfs2_dirent *dent;
756 struct gfs2_inode *ip = inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000757 int error;
758
Steven Whitehousec7526662006-03-20 12:30:04 -0500759 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
760 struct gfs2_leaf *leaf;
761 unsigned hsize = 1 << ip->i_di.di_depth;
762 unsigned index;
763 u64 ln;
764 if (hsize * sizeof(u64) != ip->i_di.di_size) {
765 gfs2_consist_inode(ip);
766 return ERR_PTR(-EIO);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000767 }
768
Steven Whitehousec7526662006-03-20 12:30:04 -0500769 index = name->hash >> (32 - ip->i_di.di_depth);
770 error = get_first_leaf(ip, index, &bh);
771 if (error)
772 return ERR_PTR(error);
773 do {
774 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500775 scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500776 if (dent)
777 goto got_dent;
778 leaf = (struct gfs2_leaf *)bh->b_data;
779 ln = be64_to_cpu(leaf->lf_next);
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400780 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500781 if (!ln)
782 break;
783 error = get_leaf(ip, ln, &bh);
784 } while(!error);
785
786 return error ? ERR_PTR(error) : NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000787 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000788
Steven Whitehousec7526662006-03-20 12:30:04 -0500789 error = gfs2_meta_inode_buffer(ip, &bh);
790 if (error)
791 return ERR_PTR(error);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500792 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500793got_dent:
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400794 if (unlikely(dent == NULL || IS_ERR(dent))) {
Steven Whitehouseed386502006-04-07 16:28:07 -0400795 brelse(bh);
796 bh = NULL;
797 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500798 *pbh = bh;
799 return dent;
800}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000801
Steven Whitehousec7526662006-03-20 12:30:04 -0500802static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
803{
804 struct gfs2_inode *ip = inode->u.generic_ip;
805 u64 bn = gfs2_alloc_meta(ip);
806 struct buffer_head *bh = gfs2_meta_new(ip->i_gl, bn);
807 struct gfs2_leaf *leaf;
808 struct gfs2_dirent *dent;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500809 struct qstr name = { .name = "", .len = 0, .hash = 0 };
Steven Whitehousec7526662006-03-20 12:30:04 -0500810 if (!bh)
811 return NULL;
812 gfs2_trans_add_bh(ip->i_gl, bh, 1);
813 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
814 leaf = (struct gfs2_leaf *)bh->b_data;
815 leaf->lf_depth = cpu_to_be16(depth);
816 leaf->lf_entries = cpu_to_be16(0);
817 leaf->lf_dirent_format = cpu_to_be16(GFS2_FORMAT_DE);
818 leaf->lf_next = cpu_to_be64(0);
819 memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
820 dent = (struct gfs2_dirent *)(leaf+1);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500821 gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
Steven Whitehousec7526662006-03-20 12:30:04 -0500822 *pbh = bh;
823 return leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000824}
825
826/**
827 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
828 * @dip: The GFS2 inode
829 *
830 * Returns: 0 on success, error code otherwise
831 */
832
Steven Whitehousec7526662006-03-20 12:30:04 -0500833static int dir_make_exhash(struct inode *inode)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000834{
Steven Whitehousec7526662006-03-20 12:30:04 -0500835 struct gfs2_inode *dip = inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000836 struct gfs2_sbd *sdp = dip->i_sbd;
837 struct gfs2_dirent *dent;
Steven Whitehousec7526662006-03-20 12:30:04 -0500838 struct qstr args;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000839 struct buffer_head *bh, *dibh;
840 struct gfs2_leaf *leaf;
841 int y;
842 uint32_t x;
843 uint64_t *lp, bn;
844 int error;
845
846 error = gfs2_meta_inode_buffer(dip, &dibh);
847 if (error)
848 return error;
849
David Teiglandb3b94fa2006-01-16 16:50:04 +0000850 /* Turn over a new leaf */
851
Steven Whitehousec7526662006-03-20 12:30:04 -0500852 leaf = new_leaf(inode, &bh, 0);
853 if (!leaf)
854 return -ENOSPC;
855 bn = bh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000856
857 gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000858 leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries);
859
860 /* Copy dirents */
861
862 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
863 sizeof(struct gfs2_dinode));
864
865 /* Find last entry */
866
867 x = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500868 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
869 sizeof(struct gfs2_leaf);
870 args.name = bh->b_data;
871 dent = gfs2_dirent_scan(dip->i_vnode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500872 gfs2_dirent_last, &args, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500873 if (!dent) {
874 brelse(bh);
875 brelse(dibh);
876 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000877 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500878 if (IS_ERR(dent)) {
879 brelse(bh);
880 brelse(dibh);
881 return PTR_ERR(dent);
882 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000883
884 /* Adjust the last dirent's record length
885 (Remember that dent still points to the last entry.) */
886
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000887 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000888 sizeof(struct gfs2_dinode) -
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000889 sizeof(struct gfs2_leaf));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000890
891 brelse(bh);
892
893 /* We're done with the new leaf block, now setup the new
894 hash table. */
895
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000896 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000897 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
898
899 lp = (uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode));
900
901 for (x = sdp->sd_hash_ptrs; x--; lp++)
902 *lp = cpu_to_be64(bn);
903
904 dip->i_di.di_size = sdp->sd_sb.sb_bsize / 2;
905 dip->i_di.di_blocks++;
906 dip->i_di.di_flags |= GFS2_DIF_EXHASH;
907 dip->i_di.di_payload_format = 0;
908
909 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
910 dip->i_di.di_depth = y;
911
912 gfs2_dinode_out(&dip->i_di, dibh->b_data);
913
914 brelse(dibh);
915
916 return 0;
917}
918
919/**
920 * dir_split_leaf - Split a leaf block into two
921 * @dip: The GFS2 inode
922 * @index:
923 * @leaf_no:
924 *
925 * Returns: 0 on success, error code on failure
926 */
927
Steven Whitehousec7526662006-03-20 12:30:04 -0500928static int dir_split_leaf(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000929{
Steven Whitehousec7526662006-03-20 12:30:04 -0500930 struct gfs2_inode *dip = inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000931 struct buffer_head *nbh, *obh, *dibh;
932 struct gfs2_leaf *nleaf, *oleaf;
933 struct gfs2_dirent *dent, *prev = NULL, *next = NULL, *new;
934 uint32_t start, len, half_len, divider;
Steven Whitehousec7526662006-03-20 12:30:04 -0500935 uint64_t bn, *lp, leaf_no;
936 uint32_t index;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000937 int x, moved = 0;
938 int error;
939
Steven Whitehousec7526662006-03-20 12:30:04 -0500940 index = name->hash >> (32 - dip->i_di.di_depth);
941 error = get_leaf_nr(dip, index, &leaf_no);
942 if (error)
943 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000944
945 /* Get the old leaf block */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000946 error = get_leaf(dip, leaf_no, &obh);
947 if (error)
Steven Whitehousee90deff2006-03-29 19:02:15 -0500948 return error;
949
950 oleaf = (struct gfs2_leaf *)obh->b_data;
951 if (dip->i_di.di_depth == be16_to_cpu(oleaf->lf_depth)) {
952 brelse(obh);
953 return 1; /* can't split */
954 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000955
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000956 gfs2_trans_add_bh(dip->i_gl, obh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000957
Steven Whitehousec7526662006-03-20 12:30:04 -0500958 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
959 if (!nleaf) {
960 brelse(obh);
961 return -ENOSPC;
962 }
963 bn = nbh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000964
Steven Whitehousec7526662006-03-20 12:30:04 -0500965 /* Compute the start and len of leaf pointers in the hash table. */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000966 len = 1 << (dip->i_di.di_depth - be16_to_cpu(oleaf->lf_depth));
967 half_len = len >> 1;
968 if (!half_len) {
Steven Whitehousee90deff2006-03-29 19:02:15 -0500969 printk(KERN_WARNING "di_depth %u lf_depth %u index %u\n", dip->i_di.di_depth, be16_to_cpu(oleaf->lf_depth), index);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000970 gfs2_consist_inode(dip);
971 error = -EIO;
972 goto fail_brelse;
973 }
974
975 start = (index & ~(len - 1));
976
977 /* Change the pointers.
978 Don't bother distinguishing stuffed from non-stuffed.
979 This code is complicated enough already. */
Steven Whitehousec7526662006-03-20 12:30:04 -0500980 lp = kmalloc(half_len * sizeof(uint64_t), GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000981 /* Change the pointers */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000982 for (x = 0; x < half_len; x++)
983 lp[x] = cpu_to_be64(bn);
984
Steven Whitehousee13940b2006-01-30 13:31:50 +0000985 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(uint64_t),
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500986 half_len * sizeof(uint64_t));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000987 if (error != half_len * sizeof(uint64_t)) {
988 if (error >= 0)
989 error = -EIO;
990 goto fail_lpfree;
991 }
992
993 kfree(lp);
994
995 /* Compute the divider */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000996 divider = (start + half_len) << (32 - dip->i_di.di_depth);
997
998 /* Copy the entries */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000999 dirent_first(dip, obh, &dent);
1000
1001 do {
1002 next = dent;
1003 if (dirent_next(dip, obh, &next))
1004 next = NULL;
1005
1006 if (dent->de_inum.no_addr &&
1007 be32_to_cpu(dent->de_hash) < divider) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001008 struct qstr str;
1009 str.name = (char*)(dent+1);
1010 str.len = be16_to_cpu(dent->de_name_len);
1011 str.hash = be32_to_cpu(dent->de_hash);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001012 new = gfs2_dirent_alloc(inode, nbh, &str);
Steven Whitehousec7526662006-03-20 12:30:04 -05001013 if (IS_ERR(new)) {
1014 error = PTR_ERR(new);
1015 break;
1016 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001017
1018 new->de_inum = dent->de_inum; /* No endian worries */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001019 new->de_type = dent->de_type; /* No endian worries */
Steven Whitehousec7526662006-03-20 12:30:04 -05001020 nleaf->lf_entries = cpu_to_be16(be16_to_cpu(nleaf->lf_entries)+1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001021
1022 dirent_del(dip, obh, prev, dent);
1023
1024 if (!oleaf->lf_entries)
1025 gfs2_consist_inode(dip);
Steven Whitehousec7526662006-03-20 12:30:04 -05001026 oleaf->lf_entries = cpu_to_be16(be16_to_cpu(oleaf->lf_entries)-1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001027
1028 if (!prev)
1029 prev = dent;
1030
1031 moved = 1;
Steven Whitehousec7526662006-03-20 12:30:04 -05001032 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001033 prev = dent;
Steven Whitehousec7526662006-03-20 12:30:04 -05001034 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001035 dent = next;
Steven Whitehousec7526662006-03-20 12:30:04 -05001036 } while (dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001037
Steven Whitehousec7526662006-03-20 12:30:04 -05001038 oleaf->lf_depth = nleaf->lf_depth;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001039
1040 error = gfs2_meta_inode_buffer(dip, &dibh);
1041 if (!gfs2_assert_withdraw(dip->i_sbd, !error)) {
1042 dip->i_di.di_blocks++;
1043 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1044 brelse(dibh);
1045 }
1046
1047 brelse(obh);
1048 brelse(nbh);
1049
1050 return error;
1051
Steven Whitehousee90deff2006-03-29 19:02:15 -05001052fail_lpfree:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001053 kfree(lp);
1054
Steven Whitehousee90deff2006-03-29 19:02:15 -05001055fail_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001056 brelse(obh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001057 brelse(nbh);
1058 return error;
1059}
1060
1061/**
1062 * dir_double_exhash - Double size of ExHash table
1063 * @dip: The GFS2 dinode
1064 *
1065 * Returns: 0 on success, error code on failure
1066 */
1067
1068static int dir_double_exhash(struct gfs2_inode *dip)
1069{
1070 struct gfs2_sbd *sdp = dip->i_sbd;
1071 struct buffer_head *dibh;
1072 uint32_t hsize;
1073 uint64_t *buf;
1074 uint64_t *from, *to;
1075 uint64_t block;
1076 int x;
1077 int error = 0;
1078
1079 hsize = 1 << dip->i_di.di_depth;
1080 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1081 gfs2_consist_inode(dip);
1082 return -EIO;
1083 }
1084
1085 /* Allocate both the "from" and "to" buffers in one big chunk */
1086
1087 buf = kcalloc(3, sdp->sd_hash_bsize, GFP_KERNEL | __GFP_NOFAIL);
1088
1089 for (block = dip->i_di.di_size >> sdp->sd_hash_bsize_shift; block--;) {
Steven Whitehousee13940b2006-01-30 13:31:50 +00001090 error = gfs2_dir_read_data(dip, (char *)buf,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001091 block * sdp->sd_hash_bsize,
1092 sdp->sd_hash_bsize);
1093 if (error != sdp->sd_hash_bsize) {
1094 if (error >= 0)
1095 error = -EIO;
1096 goto fail;
1097 }
1098
1099 from = buf;
1100 to = (uint64_t *)((char *)buf + sdp->sd_hash_bsize);
1101
1102 for (x = sdp->sd_hash_ptrs; x--; from++) {
1103 *to++ = *from; /* No endianess worries */
1104 *to++ = *from;
1105 }
1106
Steven Whitehousee13940b2006-01-30 13:31:50 +00001107 error = gfs2_dir_write_data(dip,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001108 (char *)buf + sdp->sd_hash_bsize,
1109 block * sdp->sd_sb.sb_bsize,
1110 sdp->sd_sb.sb_bsize);
1111 if (error != sdp->sd_sb.sb_bsize) {
1112 if (error >= 0)
1113 error = -EIO;
1114 goto fail;
1115 }
1116 }
1117
1118 kfree(buf);
1119
1120 error = gfs2_meta_inode_buffer(dip, &dibh);
1121 if (!gfs2_assert_withdraw(sdp, !error)) {
1122 dip->i_di.di_depth++;
1123 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1124 brelse(dibh);
1125 }
1126
1127 return error;
1128
1129 fail:
1130 kfree(buf);
1131
1132 return error;
1133}
1134
1135/**
1136 * compare_dents - compare directory entries by hash value
1137 * @a: first dent
1138 * @b: second dent
1139 *
1140 * When comparing the hash entries of @a to @b:
1141 * gt: returns 1
1142 * lt: returns -1
1143 * eq: returns 0
1144 */
1145
1146static int compare_dents(const void *a, const void *b)
1147{
1148 struct gfs2_dirent *dent_a, *dent_b;
1149 uint32_t hash_a, hash_b;
1150 int ret = 0;
1151
1152 dent_a = *(struct gfs2_dirent **)a;
Steven Whitehousec7526662006-03-20 12:30:04 -05001153 hash_a = be32_to_cpu(dent_a->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001154
1155 dent_b = *(struct gfs2_dirent **)b;
Steven Whitehousec7526662006-03-20 12:30:04 -05001156 hash_b = be32_to_cpu(dent_b->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001157
1158 if (hash_a > hash_b)
1159 ret = 1;
1160 else if (hash_a < hash_b)
1161 ret = -1;
1162 else {
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001163 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1164 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001165
1166 if (len_a > len_b)
1167 ret = 1;
1168 else if (len_a < len_b)
1169 ret = -1;
1170 else
1171 ret = memcmp((char *)(dent_a + 1),
1172 (char *)(dent_b + 1),
1173 len_a);
1174 }
1175
1176 return ret;
1177}
1178
1179/**
1180 * do_filldir_main - read out directory entries
1181 * @dip: The GFS2 inode
1182 * @offset: The offset in the file to read from
1183 * @opaque: opaque data to pass to filldir
1184 * @filldir: The function to pass entries to
1185 * @darr: an array of struct gfs2_dirent pointers to read
1186 * @entries: the number of entries in darr
1187 * @copied: pointer to int that's non-zero if a entry has been copied out
1188 *
1189 * Jump through some hoops to make sure that if there are hash collsions,
1190 * they are read out at the beginning of a buffer. We want to minimize
1191 * the possibility that they will fall into different readdir buffers or
1192 * that someone will want to seek to that location.
1193 *
1194 * Returns: errno, >0 on exception from filldir
1195 */
1196
1197static int do_filldir_main(struct gfs2_inode *dip, uint64_t *offset,
1198 void *opaque, gfs2_filldir_t filldir,
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001199 const struct gfs2_dirent **darr, uint32_t entries,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001200 int *copied)
1201{
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001202 const struct gfs2_dirent *dent, *dent_next;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001203 struct gfs2_inum inum;
1204 uint64_t off, off_next;
1205 unsigned int x, y;
1206 int run = 0;
1207 int error = 0;
1208
1209 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1210
1211 dent_next = darr[0];
1212 off_next = be32_to_cpu(dent_next->de_hash);
1213 off_next = gfs2_disk_hash2offset(off_next);
1214
1215 for (x = 0, y = 1; x < entries; x++, y++) {
1216 dent = dent_next;
1217 off = off_next;
1218
1219 if (y < entries) {
1220 dent_next = darr[y];
1221 off_next = be32_to_cpu(dent_next->de_hash);
1222 off_next = gfs2_disk_hash2offset(off_next);
1223
1224 if (off < *offset)
1225 continue;
1226 *offset = off;
1227
1228 if (off_next == off) {
1229 if (*copied && !run)
1230 return 1;
1231 run = 1;
1232 } else
1233 run = 0;
1234 } else {
1235 if (off < *offset)
1236 continue;
1237 *offset = off;
1238 }
1239
1240 gfs2_inum_in(&inum, (char *)&dent->de_inum);
1241
1242 error = filldir(opaque, (char *)(dent + 1),
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001243 be16_to_cpu(dent->de_name_len),
David Teiglandb3b94fa2006-01-16 16:50:04 +00001244 off, &inum,
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001245 be16_to_cpu(dent->de_type));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001246 if (error)
1247 return 1;
1248
1249 *copied = 1;
1250 }
1251
1252 /* Increment the *offset by one, so the next time we come into the
1253 do_filldir fxn, we get the next entry instead of the last one in the
1254 current leaf */
1255
1256 (*offset)++;
1257
1258 return 0;
1259}
1260
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001261static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
1262 gfs2_filldir_t filldir, int *copied,
1263 unsigned *depth, u64 leaf_no)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001264{
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001265 struct gfs2_inode *ip = inode->u.generic_ip;
1266 struct buffer_head *bh;
1267 struct gfs2_leaf *lf;
1268 unsigned entries = 0;
1269 unsigned leaves = 0;
1270 const struct gfs2_dirent **darr, *dent;
1271 struct dirent_gather g;
1272 struct buffer_head **larr;
1273 int leaf = 0;
1274 int error, i;
1275 u64 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001276
David Teiglandb3b94fa2006-01-16 16:50:04 +00001277 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001278 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001279 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001280 goto out;
1281 lf = (struct gfs2_leaf *)bh->b_data;
1282 if (leaves == 0)
1283 *depth = be16_to_cpu(lf->lf_depth);
1284 entries += be16_to_cpu(lf->lf_entries);
1285 leaves++;
1286 lfn = be64_to_cpu(lf->lf_next);
1287 brelse(bh);
1288 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001289
1290 if (!entries)
1291 return 0;
1292
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001293 error = -ENOMEM;
Steven Whitehousefe1bded2006-04-18 10:09:15 -04001294 larr = vmalloc((leaves + entries) * sizeof(void*));
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001295 if (!larr)
1296 goto out;
1297 darr = (const struct gfs2_dirent **)(larr + leaves);
1298 g.pdent = darr;
1299 g.offset = 0;
1300 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001301
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001302 do {
1303 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001304 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001305 goto out_kfree;
1306 lf = (struct gfs2_leaf *)bh->b_data;
1307 lfn = be64_to_cpu(lf->lf_next);
1308 if (lf->lf_entries) {
1309 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1310 gfs2_dirent_gather, NULL, &g);
1311 error = PTR_ERR(dent);
1312 if (IS_ERR(dent)) {
1313 goto out_kfree;
1314 }
1315 error = 0;
1316 larr[leaf++] = bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001317 } else {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001318 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001319 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001320 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001321
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001322 error = do_filldir_main(ip, offset, opaque, filldir, darr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001323 entries, copied);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001324out_kfree:
1325 for(i = 0; i < leaf; i++)
1326 brelse(larr[i]);
Steven Whitehousefe1bded2006-04-18 10:09:15 -04001327 vfree(larr);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001328out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001329 return error;
1330}
1331
1332/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001333 * dir_e_read - Reads the entries from a directory into a filldir buffer
1334 * @dip: dinode pointer
1335 * @offset: the hash of the last entry read shifted to the right once
1336 * @opaque: buffer for the filldir function to fill
1337 * @filldir: points to the filldir function to use
1338 *
1339 * Returns: errno
1340 */
1341
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001342static int dir_e_read(struct inode *inode, uint64_t *offset, void *opaque,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001343 gfs2_filldir_t filldir)
1344{
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001345 struct gfs2_inode *dip = inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001346 struct gfs2_sbd *sdp = dip->i_sbd;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001347 uint32_t hsize, len = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001348 uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
1349 uint32_t hash, index;
1350 uint64_t *lp;
1351 int copied = 0;
1352 int error = 0;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001353 unsigned depth;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001354
1355 hsize = 1 << dip->i_di.di_depth;
1356 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1357 gfs2_consist_inode(dip);
1358 return -EIO;
1359 }
1360
1361 hash = gfs2_dir_offset2hash(*offset);
1362 index = hash >> (32 - dip->i_di.di_depth);
1363
1364 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1365 if (!lp)
1366 return -ENOMEM;
1367
1368 while (index < hsize) {
1369 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1370 ht_offset = index - lp_offset;
1371
1372 if (ht_offset_cur != ht_offset) {
Steven Whitehousee13940b2006-01-30 13:31:50 +00001373 error = gfs2_dir_read_data(dip, (char *)lp,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001374 ht_offset * sizeof(uint64_t),
1375 sdp->sd_hash_bsize);
1376 if (error != sdp->sd_hash_bsize) {
1377 if (error >= 0)
1378 error = -EIO;
1379 goto out;
1380 }
1381 ht_offset_cur = ht_offset;
1382 }
1383
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001384 error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
1385 &copied, &depth,
1386 be64_to_cpu(lp[lp_offset]));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001387 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001388 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001389
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001390 len = 1 << (dip->i_di.di_depth - depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001391 index = (index & ~(len - 1)) + len;
1392 }
1393
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001394out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001395 kfree(lp);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001396 if (error > 0)
1397 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001398 return error;
1399}
1400
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001401int gfs2_dir_read(struct inode *inode, uint64_t *offset, void *opaque,
1402 gfs2_filldir_t filldir)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001403{
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001404 struct gfs2_inode *dip = inode->u.generic_ip;
1405 struct dirent_gather g;
1406 const struct gfs2_dirent **darr, *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001407 struct buffer_head *dibh;
1408 int copied = 0;
1409 int error;
1410
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001411 if (!dip->i_di.di_entries)
1412 return 0;
1413
1414 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
1415 return dir_e_read(inode, offset, opaque, filldir);
1416
David Teiglandb3b94fa2006-01-16 16:50:04 +00001417 if (!gfs2_is_stuffed(dip)) {
1418 gfs2_consist_inode(dip);
1419 return -EIO;
1420 }
1421
David Teiglandb3b94fa2006-01-16 16:50:04 +00001422 error = gfs2_meta_inode_buffer(dip, &dibh);
1423 if (error)
1424 return error;
1425
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001426 error = -ENOMEM;
1427 darr = kmalloc(dip->i_di.di_entries * sizeof(struct gfs2_dirent *),
1428 GFP_KERNEL);
1429 if (darr) {
1430 g.pdent = darr;
1431 g.offset = 0;
1432 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1433 gfs2_dirent_gather, NULL, &g);
1434 if (IS_ERR(dent)) {
1435 error = PTR_ERR(dent);
1436 goto out;
1437 }
1438 error = do_filldir_main(dip, offset, opaque, filldir, darr,
1439 dip->i_di.di_entries, &copied);
1440out:
1441 kfree(darr);
1442 }
1443
David Teiglandb3b94fa2006-01-16 16:50:04 +00001444 if (error > 0)
1445 error = 0;
1446
1447 brelse(dibh);
1448
1449 return error;
1450}
1451
David Teiglandb3b94fa2006-01-16 16:50:04 +00001452/**
1453 * gfs2_dir_search - Search a directory
1454 * @dip: The GFS2 inode
1455 * @filename:
1456 * @inode:
1457 *
1458 * This routine searches a directory for a file or another directory.
1459 * Assumes a glock is held on dip.
1460 *
1461 * Returns: errno
1462 */
1463
Steven Whitehousec7526662006-03-20 12:30:04 -05001464int gfs2_dir_search(struct inode *dir, const struct qstr *name,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001465 struct gfs2_inum *inum, unsigned int *type)
1466{
Steven Whitehousec7526662006-03-20 12:30:04 -05001467 struct buffer_head *bh;
1468 struct gfs2_dirent *dent;
1469
1470 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1471 if (dent) {
1472 if (IS_ERR(dent))
1473 return PTR_ERR(dent);
1474 if (inum)
1475 gfs2_inum_in(inum, (char *)&dent->de_inum);
1476 if (type)
1477 *type = be16_to_cpu(dent->de_type);
1478 brelse(bh);
1479 return 0;
1480 }
1481 return -ENOENT;
1482}
1483
1484static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1485{
1486 struct buffer_head *bh, *obh;
1487 struct gfs2_inode *ip = inode->u.generic_ip;
1488 struct gfs2_leaf *leaf, *oleaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001489 int error;
Steven Whitehousec7526662006-03-20 12:30:04 -05001490 u32 index;
1491 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001492
Steven Whitehousec7526662006-03-20 12:30:04 -05001493 index = name->hash >> (32 - ip->i_di.di_depth);
1494 error = get_first_leaf(ip, index, &obh);
1495 if (error)
1496 return error;
1497 do {
1498 oleaf = (struct gfs2_leaf *)obh->b_data;
1499 bn = be64_to_cpu(oleaf->lf_next);
1500 if (!bn)
1501 break;
1502 brelse(obh);
1503 error = get_leaf(ip, bn, &obh);
1504 if (error)
1505 return error;
1506 } while(1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001507
Steven Whitehousec7526662006-03-20 12:30:04 -05001508 gfs2_trans_add_bh(ip->i_gl, obh, 1);
1509
1510 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1511 if (!leaf) {
1512 brelse(obh);
1513 return -ENOSPC;
1514 }
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04001515 oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
Steven Whitehousec7526662006-03-20 12:30:04 -05001516 brelse(bh);
1517 brelse(obh);
1518
1519 error = gfs2_meta_inode_buffer(ip, &bh);
1520 if (error)
1521 return error;
1522 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1523 ip->i_di.di_blocks++;
1524 gfs2_dinode_out(&ip->i_di, bh->b_data);
1525 brelse(bh);
1526 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001527}
1528
1529/**
1530 * gfs2_dir_add - Add new filename into directory
1531 * @dip: The GFS2 inode
1532 * @filename: The new name
1533 * @inode: The inode number of the entry
1534 * @type: The type of the entry
1535 *
1536 * Returns: 0 on success, error code on failure
1537 */
1538
Steven Whitehousec7526662006-03-20 12:30:04 -05001539int gfs2_dir_add(struct inode *inode, const struct qstr *name,
1540 const struct gfs2_inum *inum, unsigned type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001541{
Steven Whitehousec7526662006-03-20 12:30:04 -05001542 struct gfs2_inode *ip = inode->u.generic_ip;
1543 struct buffer_head *bh;
1544 struct gfs2_dirent *dent;
1545 struct gfs2_leaf *leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001546 int error;
1547
Steven Whitehousec7526662006-03-20 12:30:04 -05001548 while(1) {
1549 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
1550 &bh);
1551 if (dent) {
1552 if (IS_ERR(dent))
1553 return PTR_ERR(dent);
1554 dent = gfs2_init_dirent(inode, dent, name, bh);
1555 gfs2_inum_out(inum, (char *)&dent->de_inum);
1556 dent->de_type = cpu_to_be16(type);
1557 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
1558 leaf = (struct gfs2_leaf *)bh->b_data;
1559 leaf->lf_entries = cpu_to_be16(be16_to_cpu(leaf->lf_entries) + 1);
1560 }
1561 brelse(bh);
1562 error = gfs2_meta_inode_buffer(ip, &bh);
1563 if (error)
1564 break;
1565 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1566 ip->i_di.di_entries++;
1567 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
1568 gfs2_dinode_out(&ip->i_di, bh->b_data);
1569 brelse(bh);
1570 error = 0;
1571 break;
1572 }
1573 if (!(ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
1574 error = dir_make_exhash(inode);
1575 if (error)
1576 break;
1577 continue;
1578 }
1579 error = dir_split_leaf(inode, name);
1580 if (error == 0)
1581 continue;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001582 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001583 break;
1584 if (ip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) {
1585 error = dir_double_exhash(ip);
1586 if (error)
1587 break;
1588 error = dir_split_leaf(inode, name);
Steven Whitehousee90deff2006-03-29 19:02:15 -05001589 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001590 break;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001591 if (error == 0)
1592 continue;
Steven Whitehousec7526662006-03-20 12:30:04 -05001593 }
1594 error = dir_new_leaf(inode, name);
1595 if (!error)
1596 continue;
1597 error = -ENOSPC;
1598 break;
1599 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001600 return error;
1601}
1602
Steven Whitehousec7526662006-03-20 12:30:04 -05001603
David Teiglandb3b94fa2006-01-16 16:50:04 +00001604/**
1605 * gfs2_dir_del - Delete a directory entry
1606 * @dip: The GFS2 inode
1607 * @filename: The filename
1608 *
1609 * Returns: 0 on success, error code on failure
1610 */
1611
Steven Whitehousec7526662006-03-20 12:30:04 -05001612int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001613{
Steven Whitehousec7526662006-03-20 12:30:04 -05001614 struct gfs2_dirent *dent, *prev = NULL;
1615 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001616 int error;
1617
Steven Whitehousec7526662006-03-20 12:30:04 -05001618 /* Returns _either_ the entry (if its first in block) or the
1619 previous entry otherwise */
1620 dent = gfs2_dirent_search(dip->i_vnode, name, gfs2_dirent_prev, &bh);
1621 if (!dent) {
1622 gfs2_consist_inode(dip);
1623 return -EIO;
1624 }
1625 if (IS_ERR(dent)) {
1626 gfs2_consist_inode(dip);
1627 return PTR_ERR(dent);
1628 }
1629 /* If not first in block, adjust pointers accordingly */
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001630 if (gfs2_dirent_find(dent, name, NULL) == 0) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001631 prev = dent;
1632 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1633 }
1634
1635 dirent_del(dip, bh, prev, dent);
1636 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1637 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1638 u16 entries = be16_to_cpu(leaf->lf_entries);
1639 if (!entries)
1640 gfs2_consist_inode(dip);
1641 leaf->lf_entries = cpu_to_be16(--entries);
Steven Whitehousec7526662006-03-20 12:30:04 -05001642 }
Steven Whitehouseed386502006-04-07 16:28:07 -04001643 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001644
1645 error = gfs2_meta_inode_buffer(dip, &bh);
1646 if (error)
1647 return error;
1648
1649 if (!dip->i_di.di_entries)
1650 gfs2_consist_inode(dip);
1651 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1652 dip->i_di.di_entries--;
1653 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1654 gfs2_dinode_out(&dip->i_di, bh->b_data);
1655 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001656
1657 return error;
1658}
1659
David Teiglandb3b94fa2006-01-16 16:50:04 +00001660/**
1661 * gfs2_dir_mvino - Change inode number of directory entry
1662 * @dip: The GFS2 inode
1663 * @filename:
1664 * @new_inode:
1665 *
1666 * This routine changes the inode number of a directory entry. It's used
1667 * by rename to change ".." when a directory is moved.
1668 * Assumes a glock is held on dvp.
1669 *
1670 * Returns: errno
1671 */
1672
Steven Whitehousec7526662006-03-20 12:30:04 -05001673int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001674 struct gfs2_inum *inum, unsigned int new_type)
1675{
Steven Whitehousec7526662006-03-20 12:30:04 -05001676 struct buffer_head *bh;
1677 struct gfs2_dirent *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001678 int error;
1679
Steven Whitehousec7526662006-03-20 12:30:04 -05001680 dent = gfs2_dirent_search(dip->i_vnode, filename, gfs2_dirent_find, &bh);
1681 if (!dent) {
1682 gfs2_consist_inode(dip);
1683 return -EIO;
1684 }
1685 if (IS_ERR(dent))
1686 return PTR_ERR(dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001687
Steven Whitehousec7526662006-03-20 12:30:04 -05001688 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1689 gfs2_inum_out(inum, (char *)&dent->de_inum);
1690 dent->de_type = cpu_to_be16(new_type);
1691
1692 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1693 brelse(bh);
1694 error = gfs2_meta_inode_buffer(dip, &bh);
1695 if (error)
1696 return error;
1697 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1698 }
1699
1700 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1701 gfs2_dinode_out(&dip->i_di, bh->b_data);
1702 brelse(bh);
1703 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001704}
1705
1706/**
1707 * foreach_leaf - call a function for each leaf in a directory
1708 * @dip: the directory
1709 * @lc: the function to call for each each
1710 * @data: private data to pass to it
1711 *
1712 * Returns: errno
1713 */
1714
1715static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
1716{
1717 struct gfs2_sbd *sdp = dip->i_sbd;
1718 struct buffer_head *bh;
Steven Whitehousec7526662006-03-20 12:30:04 -05001719 struct gfs2_leaf *leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001720 uint32_t hsize, len;
1721 uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
1722 uint32_t index = 0;
1723 uint64_t *lp;
1724 uint64_t leaf_no;
1725 int error = 0;
1726
1727 hsize = 1 << dip->i_di.di_depth;
1728 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1729 gfs2_consist_inode(dip);
1730 return -EIO;
1731 }
1732
1733 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1734 if (!lp)
1735 return -ENOMEM;
1736
1737 while (index < hsize) {
1738 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1739 ht_offset = index - lp_offset;
1740
1741 if (ht_offset_cur != ht_offset) {
Steven Whitehousee13940b2006-01-30 13:31:50 +00001742 error = gfs2_dir_read_data(dip, (char *)lp,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001743 ht_offset * sizeof(uint64_t),
1744 sdp->sd_hash_bsize);
1745 if (error != sdp->sd_hash_bsize) {
1746 if (error >= 0)
1747 error = -EIO;
1748 goto out;
1749 }
1750 ht_offset_cur = ht_offset;
1751 }
1752
1753 leaf_no = be64_to_cpu(lp[lp_offset]);
1754 if (leaf_no) {
1755 error = get_leaf(dip, leaf_no, &bh);
1756 if (error)
1757 goto out;
Steven Whitehousec7526662006-03-20 12:30:04 -05001758 leaf = (struct gfs2_leaf *)bh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001759 brelse(bh);
1760
Steven Whitehousec7526662006-03-20 12:30:04 -05001761 len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001762
1763 error = lc(dip, index, len, leaf_no, data);
1764 if (error)
1765 goto out;
1766
1767 index = (index & ~(len - 1)) + len;
1768 } else
1769 index++;
1770 }
1771
1772 if (index != hsize) {
1773 gfs2_consist_inode(dip);
1774 error = -EIO;
1775 }
1776
1777 out:
1778 kfree(lp);
1779
1780 return error;
1781}
1782
1783/**
1784 * leaf_dealloc - Deallocate a directory leaf
1785 * @dip: the directory
1786 * @index: the hash table offset in the directory
1787 * @len: the number of pointers to this leaf
1788 * @leaf_no: the leaf number
1789 * @data: not used
1790 *
1791 * Returns: errno
1792 */
1793
1794static int leaf_dealloc(struct gfs2_inode *dip, uint32_t index, uint32_t len,
1795 uint64_t leaf_no, void *data)
1796{
1797 struct gfs2_sbd *sdp = dip->i_sbd;
Steven Whitehousec7526662006-03-20 12:30:04 -05001798 struct gfs2_leaf *tmp_leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001799 struct gfs2_rgrp_list rlist;
1800 struct buffer_head *bh, *dibh;
Steven Whitehousec7526662006-03-20 12:30:04 -05001801 uint64_t blk, nblk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001802 unsigned int rg_blocks = 0, l_blocks = 0;
1803 char *ht;
1804 unsigned int x, size = len * sizeof(uint64_t);
1805 int error;
1806
1807 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1808
1809 ht = kzalloc(size, GFP_KERNEL);
1810 if (!ht)
1811 return -ENOMEM;
1812
1813 gfs2_alloc_get(dip);
1814
1815 error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1816 if (error)
1817 goto out;
1818
1819 error = gfs2_rindex_hold(sdp, &dip->i_alloc.al_ri_gh);
1820 if (error)
1821 goto out_qs;
1822
1823 /* Count the number of leaves */
1824
Steven Whitehousec7526662006-03-20 12:30:04 -05001825 for (blk = leaf_no; blk; blk = nblk) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001826 error = get_leaf(dip, blk, &bh);
1827 if (error)
1828 goto out_rlist;
Steven Whitehousec7526662006-03-20 12:30:04 -05001829 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1830 nblk = be64_to_cpu(tmp_leaf->lf_next);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001831 brelse(bh);
1832
1833 gfs2_rlist_add(sdp, &rlist, blk);
1834 l_blocks++;
1835 }
1836
1837 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1838
1839 for (x = 0; x < rlist.rl_rgrps; x++) {
1840 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001841 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001842 rg_blocks += rgd->rd_ri.ri_length;
1843 }
1844
1845 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1846 if (error)
1847 goto out_rlist;
1848
1849 error = gfs2_trans_begin(sdp,
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001850 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
David Teiglandb3b94fa2006-01-16 16:50:04 +00001851 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1852 if (error)
1853 goto out_rg_gunlock;
1854
Steven Whitehousec7526662006-03-20 12:30:04 -05001855 for (blk = leaf_no; blk; blk = nblk) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001856 error = get_leaf(dip, blk, &bh);
1857 if (error)
1858 goto out_end_trans;
Steven Whitehousec7526662006-03-20 12:30:04 -05001859 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1860 nblk = be64_to_cpu(tmp_leaf->lf_next);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001861 brelse(bh);
1862
1863 gfs2_free_meta(dip, blk, 1);
1864
1865 if (!dip->i_di.di_blocks)
1866 gfs2_consist_inode(dip);
1867 dip->i_di.di_blocks--;
1868 }
1869
Steven Whitehousee13940b2006-01-30 13:31:50 +00001870 error = gfs2_dir_write_data(dip, ht, index * sizeof(uint64_t), size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001871 if (error != size) {
1872 if (error >= 0)
1873 error = -EIO;
1874 goto out_end_trans;
1875 }
1876
1877 error = gfs2_meta_inode_buffer(dip, &dibh);
1878 if (error)
1879 goto out_end_trans;
1880
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001881 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001882 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1883 brelse(dibh);
1884
1885 out_end_trans:
1886 gfs2_trans_end(sdp);
1887
1888 out_rg_gunlock:
1889 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1890
1891 out_rlist:
1892 gfs2_rlist_free(&rlist);
1893 gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh);
1894
1895 out_qs:
1896 gfs2_quota_unhold(dip);
1897
1898 out:
1899 gfs2_alloc_put(dip);
1900 kfree(ht);
1901
1902 return error;
1903}
1904
1905/**
1906 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1907 * @dip: the directory
1908 *
1909 * Dealloc all on-disk directory leaves to FREEMETA state
1910 * Change on-disk inode type to "regular file"
1911 *
1912 * Returns: errno
1913 */
1914
1915int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
1916{
1917 struct gfs2_sbd *sdp = dip->i_sbd;
1918 struct buffer_head *bh;
1919 int error;
1920
1921 /* Dealloc on-disk leaves to FREEMETA state */
1922 error = foreach_leaf(dip, leaf_dealloc, NULL);
1923 if (error)
1924 return error;
1925
1926 /* Make this a regular file in case we crash.
1927 (We don't want to free these blocks a second time.) */
1928
1929 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1930 if (error)
1931 return error;
1932
1933 error = gfs2_meta_inode_buffer(dip, &bh);
1934 if (!error) {
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001935 gfs2_trans_add_bh(dip->i_gl, bh, 1);
Steven Whitehouse568f4c92006-02-27 12:00:42 -05001936 ((struct gfs2_dinode *)bh->b_data)->di_mode =
1937 cpu_to_be32(S_IFREG);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001938 brelse(bh);
1939 }
1940
1941 gfs2_trans_end(sdp);
1942
1943 return error;
1944}
1945
1946/**
1947 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
1948 * @ip: the file being written to
1949 * @filname: the filename that's going to be added
David Teiglandb3b94fa2006-01-16 16:50:04 +00001950 *
Steven Whitehousec7526662006-03-20 12:30:04 -05001951 * Returns: 1 if alloc required, 0 if not, -ve on error
David Teiglandb3b94fa2006-01-16 16:50:04 +00001952 */
1953
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04001954int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001955{
Steven Whitehousec7526662006-03-20 12:30:04 -05001956 struct gfs2_dirent *dent;
1957 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001958
Steven Whitehousec7526662006-03-20 12:30:04 -05001959 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
Steven Whitehouseed386502006-04-07 16:28:07 -04001960 if (!dent) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001961 return 1;
Steven Whitehouseed386502006-04-07 16:28:07 -04001962 }
Steven Whitehousec7526662006-03-20 12:30:04 -05001963 if (IS_ERR(dent))
1964 return PTR_ERR(dent);
1965 brelse(bh);
1966 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001967}
1968