blob: f62223b9e53d048add9a5446dba6e2b4263672b0 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
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/*
Steven Whitehouse61e085a2006-04-24 10:07:13 -040011 * 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
46 * block pointer in the array that points to the same leaf. In fact, when a
47 * directory is first converted from linear to exhash, all of the pointers
48 * point to the 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 */
David Teiglandb3b94fa2006-01-16 16:50:04 +000055
56#include <linux/sched.h>
57#include <linux/slab.h>
58#include <linux/spinlock.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000059#include <linux/buffer_head.h>
60#include <linux/sort.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050061#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050062#include <linux/crc32.h>
Steven Whitehousefe1bded2006-04-18 10:09:15 -040063#include <linux/vmalloc.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000064
65#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050066#include "lm_interface.h"
67#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000068#include "dir.h"
69#include "glock.h"
70#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000071#include "meta_io.h"
72#include "quota.h"
73#include "rgrp.h"
74#include "trans.h"
Steven Whitehousee13940b2006-01-30 13:31:50 +000075#include "bmap.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050076#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000077
78#define IS_LEAF 1 /* Hashed (leaf) directory */
79#define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
80
David Teiglandb3b94fa2006-01-16 16:50:04 +000081#define gfs2_disk_hash2offset(h) (((uint64_t)(h)) >> 1)
82#define gfs2_dir_offset2hash(p) ((uint32_t)(((uint64_t)(p)) << 1))
David Teiglandb3b94fa2006-01-16 16:50:04 +000083
84typedef int (*leaf_call_t) (struct gfs2_inode *dip,
85 uint32_t index, uint32_t len, uint64_t leaf_no,
86 void *data);
87
Steven Whitehouse61e085a2006-04-24 10:07:13 -040088
89int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, uint64_t block,
90 struct buffer_head **bhp)
Steven Whitehousee13940b2006-01-30 13:31:50 +000091{
92 struct buffer_head *bh;
Steven Whitehousee13940b2006-01-30 13:31:50 +000093
Steven Whitehouse61e085a2006-04-24 10:07:13 -040094 bh = gfs2_meta_new(ip->i_gl, block);
95 gfs2_trans_add_bh(ip->i_gl, bh, 1);
96 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
97 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
Steven Whitehousee13940b2006-01-30 13:31:50 +000098 *bhp = bh;
99 return 0;
100}
101
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400102static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, uint64_t block,
103 struct buffer_head **bhp)
104{
105 struct buffer_head *bh;
106 int error;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000107
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400108 error = gfs2_meta_read(ip->i_gl, block, DIO_START | DIO_WAIT, &bh);
109 if (error)
110 return error;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400111 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) {
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400112 brelse(bh);
113 return -EIO;
114 }
115 *bhp = bh;
116 return 0;
117}
Steven Whitehousee13940b2006-01-30 13:31:50 +0000118
119static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
120 unsigned int offset, unsigned int size)
121
122{
123 struct buffer_head *dibh;
124 int error;
125
126 error = gfs2_meta_inode_buffer(ip, &dibh);
127 if (error)
128 return error;
129
130 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehousec7526662006-03-20 12:30:04 -0500131 memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000132 if (ip->i_di.di_size < offset + size)
133 ip->i_di.di_size = offset + size;
134 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
135 gfs2_dinode_out(&ip->i_di, dibh->b_data);
136
137 brelse(dibh);
138
139 return size;
140}
141
142
143
144/**
145 * gfs2_dir_write_data - Write directory information to the inode
146 * @ip: The GFS2 inode
147 * @buf: The buffer containing information to be written
148 * @offset: The file offset to start writing at
149 * @size: The amount of data to write
150 *
151 * Returns: The number of bytes correctly written or error code
152 */
153static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
154 uint64_t offset, unsigned int size)
155{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400156 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000157 struct buffer_head *dibh;
158 uint64_t lblock, dblock;
159 uint32_t extlen = 0;
160 unsigned int o;
161 int copied = 0;
162 int error = 0;
163
164 if (!size)
165 return 0;
166
167 if (gfs2_is_stuffed(ip) &&
168 offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500169 return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
170 size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000171
172 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
173 return -EINVAL;
174
175 if (gfs2_is_stuffed(ip)) {
176 error = gfs2_unstuff_dinode(ip, NULL, NULL);
177 if (error)
Steven Whitehousec7526662006-03-20 12:30:04 -0500178 return error;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000179 }
180
181 lblock = offset;
182 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
183
184 while (copied < size) {
185 unsigned int amount;
186 struct buffer_head *bh;
187 int new;
188
189 amount = size - copied;
190 if (amount > sdp->sd_sb.sb_bsize - o)
191 amount = sdp->sd_sb.sb_bsize - o;
192
193 if (!extlen) {
194 new = 1;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400195 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
Steven Whitehousefd88de562006-05-05 16:59:11 -0400196 &dblock, &extlen);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000197 if (error)
198 goto fail;
199 error = -EIO;
200 if (gfs2_assert_withdraw(sdp, dblock))
201 goto fail;
202 }
203
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400204 if (amount == sdp->sd_jbsize || new)
205 error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
206 else
207 error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
208
Steven Whitehousee13940b2006-01-30 13:31:50 +0000209 if (error)
210 goto fail;
211
212 gfs2_trans_add_bh(ip->i_gl, bh, 1);
213 memcpy(bh->b_data + o, buf, amount);
214 brelse(bh);
215 if (error)
216 goto fail;
217
218 copied += amount;
219 lblock++;
220 dblock++;
221 extlen--;
222
223 o = sizeof(struct gfs2_meta_header);
224 }
225
226out:
227 error = gfs2_meta_inode_buffer(ip, &dibh);
228 if (error)
229 return error;
230
231 if (ip->i_di.di_size < offset + copied)
232 ip->i_di.di_size = offset + copied;
233 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
234
235 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
236 gfs2_dinode_out(&ip->i_di, dibh->b_data);
237 brelse(dibh);
238
239 return copied;
240fail:
241 if (copied)
242 goto out;
243 return error;
244}
245
246static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf,
Steven Whitehousec7526662006-03-20 12:30:04 -0500247 unsigned int offset, unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000248{
249 struct buffer_head *dibh;
250 int error;
251
252 error = gfs2_meta_inode_buffer(ip, &dibh);
253 if (!error) {
254 offset += sizeof(struct gfs2_dinode);
255 memcpy(buf, dibh->b_data + offset, size);
256 brelse(dibh);
257 }
258
259 return (error) ? error : size;
260}
261
262
263/**
264 * gfs2_dir_read_data - Read a data from a directory inode
265 * @ip: The GFS2 Inode
266 * @buf: The buffer to place result into
267 * @offset: File offset to begin jdata_readng from
268 * @size: Amount of data to transfer
269 *
270 * Returns: The amount of data actually copied or the error
271 */
272static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf,
273 uint64_t offset, unsigned int size)
274{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400275 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000276 uint64_t lblock, dblock;
277 uint32_t extlen = 0;
278 unsigned int o;
279 int copied = 0;
280 int error = 0;
281
282 if (offset >= ip->i_di.di_size)
283 return 0;
284
285 if ((offset + size) > ip->i_di.di_size)
286 size = ip->i_di.di_size - offset;
287
288 if (!size)
289 return 0;
290
291 if (gfs2_is_stuffed(ip))
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500292 return gfs2_dir_read_stuffed(ip, buf, (unsigned int)offset,
293 size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000294
295 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
296 return -EINVAL;
297
298 lblock = offset;
299 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
300
301 while (copied < size) {
302 unsigned int amount;
303 struct buffer_head *bh;
304 int new;
305
306 amount = size - copied;
307 if (amount > sdp->sd_sb.sb_bsize - o)
308 amount = sdp->sd_sb.sb_bsize - o;
309
310 if (!extlen) {
311 new = 0;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400312 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
Steven Whitehousefd88de562006-05-05 16:59:11 -0400313 &dblock, &extlen);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000314 if (error)
315 goto fail;
316 }
317
318 if (extlen > 1)
319 gfs2_meta_ra(ip->i_gl, dblock, extlen);
320
321 if (dblock) {
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400322 if (new)
323 error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
324 else
325 error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000326 if (error)
327 goto fail;
328 dblock++;
329 extlen--;
330 } else
331 bh = NULL;
332
333 memcpy(buf, bh->b_data + o, amount);
334 brelse(bh);
335 if (error)
336 goto fail;
337
338 copied += amount;
339 lblock++;
340
341 o = sizeof(struct gfs2_meta_header);
342 }
343
344 return copied;
345fail:
346 return (copied) ? copied : error;
347}
348
Steven Whitehousec7526662006-03-20 12:30:04 -0500349typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500350 const struct qstr *name,
351 void *opaque);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000352
Steven Whitehousec7526662006-03-20 12:30:04 -0500353static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
354 const struct qstr *name, int ret)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000355{
Steven Whitehousec7526662006-03-20 12:30:04 -0500356 if (dent->de_inum.no_addr != 0 &&
357 be32_to_cpu(dent->de_hash) == name->hash &&
358 be16_to_cpu(dent->de_name_len) == name->len &&
359 memcmp((char *)(dent+1), name->name, name->len) == 0)
360 return ret;
361 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000362}
363
Steven Whitehousec7526662006-03-20 12:30:04 -0500364static int gfs2_dirent_find(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500365 const struct qstr *name,
366 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500367{
368 return __gfs2_dirent_find(dent, name, 1);
369}
370
371static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500372 const struct qstr *name,
373 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500374{
375 return __gfs2_dirent_find(dent, name, 2);
376}
377
378/*
379 * name->name holds ptr to start of block.
380 * name->len holds size of block.
381 */
382static int gfs2_dirent_last(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500383 const struct qstr *name,
384 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500385{
386 const char *start = name->name;
387 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
388 if (name->len == (end - start))
389 return 1;
390 return 0;
391}
392
393static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500394 const struct qstr *name,
395 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500396{
397 unsigned required = GFS2_DIRENT_SIZE(name->len);
398 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
399 unsigned totlen = be16_to_cpu(dent->de_rec_len);
400
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500401 if (!dent->de_inum.no_addr)
402 actual = GFS2_DIRENT_SIZE(0);
Steven Whitehousec7526662006-03-20 12:30:04 -0500403 if ((totlen - actual) >= required)
404 return 1;
405 return 0;
406}
407
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500408struct dirent_gather {
409 const struct gfs2_dirent **pdent;
410 unsigned offset;
411};
412
413static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
414 const struct qstr *name,
415 void *opaque)
416{
417 struct dirent_gather *g = opaque;
418 if (dent->de_inum.no_addr) {
419 g->pdent[g->offset++] = dent;
420 }
421 return 0;
422}
423
Steven Whitehousec7526662006-03-20 12:30:04 -0500424/*
425 * Other possible things to check:
426 * - Inode located within filesystem size (and on valid block)
427 * - Valid directory entry type
428 * Not sure how heavy-weight we want to make this... could also check
429 * hash is correct for example, but that would take a lot of extra time.
430 * For now the most important thing is to check that the various sizes
431 * are correct.
432 */
433static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
434 unsigned int size, unsigned int len, int first)
435{
436 const char *msg = "gfs2_dirent too small";
437 if (unlikely(size < sizeof(struct gfs2_dirent)))
438 goto error;
439 msg = "gfs2_dirent misaligned";
440 if (unlikely(offset & 0x7))
441 goto error;
442 msg = "gfs2_dirent points beyond end of block";
443 if (unlikely(offset + size > len))
444 goto error;
445 msg = "zero inode number";
446 if (unlikely(!first && !dent->de_inum.no_addr))
447 goto error;
448 msg = "name length is greater than space in dirent";
449 if (dent->de_inum.no_addr &&
450 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
451 size))
452 goto error;
453 return 0;
454error:
455 printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
456 first ? "first in block" : "not first in block");
457 return -EIO;
458}
459
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500460static int gfs2_dirent_offset(const void *buf)
Steven Whitehousec7526662006-03-20 12:30:04 -0500461{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500462 const struct gfs2_meta_header *h = buf;
463 int offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500464
465 BUG_ON(buf == NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500466
Steven Whitehousee3167de2006-03-30 15:46:23 -0500467 switch(be32_to_cpu(h->mh_type)) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500468 case GFS2_METATYPE_LF:
469 offset = sizeof(struct gfs2_leaf);
470 break;
471 case GFS2_METATYPE_DI:
472 offset = sizeof(struct gfs2_dinode);
473 break;
474 default:
475 goto wrong_type;
476 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500477 return offset;
478wrong_type:
479 printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
Steven Whitehousee3167de2006-03-30 15:46:23 -0500480 be32_to_cpu(h->mh_type));
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500481 return -1;
482}
Steven Whitehousec7526662006-03-20 12:30:04 -0500483
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500484static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode,
485 void *buf,
486 unsigned int len, gfs2_dscan_t scan,
487 const struct qstr *name,
488 void *opaque)
489{
490 struct gfs2_dirent *dent, *prev;
491 unsigned offset;
492 unsigned size;
493 int ret = 0;
494
495 ret = gfs2_dirent_offset(buf);
496 if (ret < 0)
497 goto consist_inode;
498
499 offset = ret;
Steven Whitehousec7526662006-03-20 12:30:04 -0500500 prev = NULL;
501 dent = (struct gfs2_dirent *)(buf + offset);
502 size = be16_to_cpu(dent->de_rec_len);
503 if (gfs2_check_dirent(dent, offset, size, len, 1))
504 goto consist_inode;
505 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500506 ret = scan(dent, name, opaque);
Steven Whitehousec7526662006-03-20 12:30:04 -0500507 if (ret)
508 break;
509 offset += size;
510 if (offset == len)
511 break;
512 prev = dent;
513 dent = (struct gfs2_dirent *)(buf + offset);
514 size = be16_to_cpu(dent->de_rec_len);
515 if (gfs2_check_dirent(dent, offset, size, len, 0))
516 goto consist_inode;
517 } while(1);
518
519 switch(ret) {
520 case 0:
521 return NULL;
522 case 1:
523 return dent;
524 case 2:
525 return prev ? prev : dent;
526 default:
527 BUG_ON(ret > 0);
528 return ERR_PTR(ret);
529 }
530
Steven Whitehousec7526662006-03-20 12:30:04 -0500531consist_inode:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400532 gfs2_consist_inode(GFS2_I(inode));
Steven Whitehousec7526662006-03-20 12:30:04 -0500533 return ERR_PTR(-EIO);
534}
535
536
David Teiglandb3b94fa2006-01-16 16:50:04 +0000537/**
538 * dirent_first - Return the first dirent
539 * @dip: the directory
540 * @bh: The buffer
541 * @dent: Pointer to list of dirents
542 *
543 * return first dirent whether bh points to leaf or stuffed dinode
544 *
545 * Returns: IS_LEAF, IS_DINODE, or -errno
546 */
547
548static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
549 struct gfs2_dirent **dent)
550{
551 struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data;
552
Steven Whitehousee3167de2006-03-30 15:46:23 -0500553 if (be32_to_cpu(h->mh_type) == GFS2_METATYPE_LF) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400554 if (gfs2_meta_check(GFS2_SB(&dip->i_inode), bh))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000555 return -EIO;
556 *dent = (struct gfs2_dirent *)(bh->b_data +
557 sizeof(struct gfs2_leaf));
558 return IS_LEAF;
559 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400560 if (gfs2_metatype_check(GFS2_SB(&dip->i_inode), bh, GFS2_METATYPE_DI))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000561 return -EIO;
562 *dent = (struct gfs2_dirent *)(bh->b_data +
563 sizeof(struct gfs2_dinode));
564 return IS_DINODE;
565 }
566}
567
568/**
569 * dirent_next - Next dirent
570 * @dip: the directory
571 * @bh: The buffer
572 * @dent: Pointer to list of dirents
573 *
574 * Returns: 0 on success, error code otherwise
575 */
576
577static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
578 struct gfs2_dirent **dent)
579{
580 struct gfs2_dirent *tmp, *cur;
581 char *bh_end;
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000582 uint16_t cur_rec_len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000583
584 cur = *dent;
585 bh_end = bh->b_data + bh->b_size;
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000586 cur_rec_len = be16_to_cpu(cur->de_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000587
588 if ((char *)cur + cur_rec_len >= bh_end) {
589 if ((char *)cur + cur_rec_len > bh_end) {
590 gfs2_consist_inode(dip);
591 return -EIO;
592 }
593 return -ENOENT;
594 }
595
596 tmp = (struct gfs2_dirent *)((char *)cur + cur_rec_len);
597
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000598 if ((char *)tmp + be16_to_cpu(tmp->de_rec_len) > bh_end) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000599 gfs2_consist_inode(dip);
600 return -EIO;
601 }
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000602
603 if (cur_rec_len == 0) {
604 gfs2_consist_inode(dip);
605 return -EIO;
606 }
607
David Teiglandb3b94fa2006-01-16 16:50:04 +0000608 /* Only the first dent could ever have de_inum.no_addr == 0 */
609 if (!tmp->de_inum.no_addr) {
610 gfs2_consist_inode(dip);
611 return -EIO;
612 }
613
614 *dent = tmp;
615
616 return 0;
617}
618
619/**
620 * dirent_del - Delete a dirent
621 * @dip: The GFS2 inode
622 * @bh: The buffer
623 * @prev: The previous dirent
624 * @cur: The current dirent
625 *
626 */
627
628static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
629 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
630{
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000631 uint16_t cur_rec_len, prev_rec_len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000632
633 if (!cur->de_inum.no_addr) {
634 gfs2_consist_inode(dip);
635 return;
636 }
637
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000638 gfs2_trans_add_bh(dip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000639
640 /* If there is no prev entry, this is the first entry in the block.
641 The de_rec_len is already as big as it needs to be. Just zero
642 out the inode number and return. */
643
644 if (!prev) {
645 cur->de_inum.no_addr = 0; /* No endianess worries */
646 return;
647 }
648
649 /* Combine this dentry with the previous one. */
650
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000651 prev_rec_len = be16_to_cpu(prev->de_rec_len);
652 cur_rec_len = be16_to_cpu(cur->de_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000653
654 if ((char *)prev + prev_rec_len != (char *)cur)
655 gfs2_consist_inode(dip);
656 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
657 gfs2_consist_inode(dip);
658
659 prev_rec_len += cur_rec_len;
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000660 prev->de_rec_len = cpu_to_be16(prev_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000661}
662
Steven Whitehousec7526662006-03-20 12:30:04 -0500663/*
664 * Takes a dent from which to grab space as an argument. Returns the
665 * newly created dent.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000666 */
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400667static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
668 struct gfs2_dirent *dent,
669 const struct qstr *name,
670 struct buffer_head *bh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000671{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400672 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -0500673 struct gfs2_dirent *ndent;
674 unsigned offset = 0, totlen;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000675
Steven Whitehousec7526662006-03-20 12:30:04 -0500676 if (dent->de_inum.no_addr)
677 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
678 totlen = be16_to_cpu(dent->de_rec_len);
679 BUG_ON(offset + name->len > totlen);
680 gfs2_trans_add_bh(ip->i_gl, bh, 1);
681 ndent = (struct gfs2_dirent *)((char *)dent + offset);
682 dent->de_rec_len = cpu_to_be16(offset);
683 gfs2_qstr2dirent(name, totlen - offset, ndent);
684 return ndent;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000685}
686
Steven Whitehousec7526662006-03-20 12:30:04 -0500687static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
688 struct buffer_head *bh,
689 const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000690{
691 struct gfs2_dirent *dent;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500692 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
693 gfs2_dirent_find_space, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500694 if (!dent || IS_ERR(dent))
695 return dent;
696 return gfs2_init_dirent(inode, dent, name, bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000697}
698
699static int get_leaf(struct gfs2_inode *dip, uint64_t leaf_no,
700 struct buffer_head **bhp)
701{
702 int error;
703
704 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_START | DIO_WAIT, bhp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400705 if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
706 /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000707 error = -EIO;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400708 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000709
710 return error;
711}
712
713/**
714 * get_leaf_nr - Get a leaf number associated with the index
715 * @dip: The GFS2 inode
716 * @index:
717 * @leaf_out:
718 *
719 * Returns: 0 on success, error code otherwise
720 */
721
722static int get_leaf_nr(struct gfs2_inode *dip, uint32_t index,
723 uint64_t *leaf_out)
724{
725 uint64_t leaf_no;
726 int error;
727
Steven Whitehousee13940b2006-01-30 13:31:50 +0000728 error = gfs2_dir_read_data(dip, (char *)&leaf_no,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000729 index * sizeof(uint64_t),
730 sizeof(uint64_t));
731 if (error != sizeof(uint64_t))
732 return (error < 0) ? error : -EIO;
733
734 *leaf_out = be64_to_cpu(leaf_no);
735
736 return 0;
737}
738
739static int get_first_leaf(struct gfs2_inode *dip, uint32_t index,
740 struct buffer_head **bh_out)
741{
742 uint64_t leaf_no;
743 int error;
744
745 error = get_leaf_nr(dip, index, &leaf_no);
746 if (!error)
747 error = get_leaf(dip, leaf_no, bh_out);
748
749 return error;
750}
751
Steven Whitehousec7526662006-03-20 12:30:04 -0500752static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
753 const struct qstr *name,
754 gfs2_dscan_t scan,
755 struct buffer_head **pbh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000756{
Steven Whitehousec7526662006-03-20 12:30:04 -0500757 struct buffer_head *bh;
758 struct gfs2_dirent *dent;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400759 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000760 int error;
761
Steven Whitehousec7526662006-03-20 12:30:04 -0500762 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
763 struct gfs2_leaf *leaf;
764 unsigned hsize = 1 << ip->i_di.di_depth;
765 unsigned index;
766 u64 ln;
767 if (hsize * sizeof(u64) != ip->i_di.di_size) {
768 gfs2_consist_inode(ip);
769 return ERR_PTR(-EIO);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000770 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400771
Steven Whitehousec7526662006-03-20 12:30:04 -0500772 index = name->hash >> (32 - ip->i_di.di_depth);
773 error = get_first_leaf(ip, index, &bh);
774 if (error)
775 return ERR_PTR(error);
776 do {
777 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500778 scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500779 if (dent)
780 goto got_dent;
781 leaf = (struct gfs2_leaf *)bh->b_data;
782 ln = be64_to_cpu(leaf->lf_next);
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400783 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500784 if (!ln)
785 break;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400786
Steven Whitehousec7526662006-03-20 12:30:04 -0500787 error = get_leaf(ip, ln, &bh);
788 } while(!error);
789
790 return error ? ERR_PTR(error) : NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000791 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000792
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400793
Steven Whitehousec7526662006-03-20 12:30:04 -0500794 error = gfs2_meta_inode_buffer(ip, &bh);
795 if (error)
796 return ERR_PTR(error);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500797 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500798got_dent:
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400799 if (unlikely(dent == NULL || IS_ERR(dent))) {
Steven Whitehouseed386502006-04-07 16:28:07 -0400800 brelse(bh);
801 bh = NULL;
802 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500803 *pbh = bh;
804 return dent;
805}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000806
Steven Whitehousec7526662006-03-20 12:30:04 -0500807static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
808{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400809 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -0500810 u64 bn = gfs2_alloc_meta(ip);
811 struct buffer_head *bh = gfs2_meta_new(ip->i_gl, bn);
812 struct gfs2_leaf *leaf;
813 struct gfs2_dirent *dent;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500814 struct qstr name = { .name = "", .len = 0, .hash = 0 };
Steven Whitehousec7526662006-03-20 12:30:04 -0500815 if (!bh)
816 return NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400817
Steven Whitehousec7526662006-03-20 12:30:04 -0500818 gfs2_trans_add_bh(ip->i_gl, bh, 1);
819 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
820 leaf = (struct gfs2_leaf *)bh->b_data;
821 leaf->lf_depth = cpu_to_be16(depth);
822 leaf->lf_entries = cpu_to_be16(0);
823 leaf->lf_dirent_format = cpu_to_be16(GFS2_FORMAT_DE);
824 leaf->lf_next = cpu_to_be64(0);
825 memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
826 dent = (struct gfs2_dirent *)(leaf+1);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500827 gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
Steven Whitehousec7526662006-03-20 12:30:04 -0500828 *pbh = bh;
829 return leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000830}
831
832/**
833 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
834 * @dip: The GFS2 inode
835 *
836 * Returns: 0 on success, error code otherwise
837 */
838
Steven Whitehousec7526662006-03-20 12:30:04 -0500839static int dir_make_exhash(struct inode *inode)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000840{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400841 struct gfs2_inode *dip = GFS2_I(inode);
842 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000843 struct gfs2_dirent *dent;
Steven Whitehousec7526662006-03-20 12:30:04 -0500844 struct qstr args;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000845 struct buffer_head *bh, *dibh;
846 struct gfs2_leaf *leaf;
847 int y;
848 uint32_t x;
849 uint64_t *lp, bn;
850 int error;
851
852 error = gfs2_meta_inode_buffer(dip, &dibh);
853 if (error)
854 return error;
855
David Teiglandb3b94fa2006-01-16 16:50:04 +0000856 /* Turn over a new leaf */
857
Steven Whitehousec7526662006-03-20 12:30:04 -0500858 leaf = new_leaf(inode, &bh, 0);
859 if (!leaf)
860 return -ENOSPC;
861 bn = bh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000862
863 gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000864 leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries);
865
866 /* Copy dirents */
867
868 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
869 sizeof(struct gfs2_dinode));
870
871 /* Find last entry */
872
873 x = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500874 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
875 sizeof(struct gfs2_leaf);
876 args.name = bh->b_data;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400877 dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500878 gfs2_dirent_last, &args, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500879 if (!dent) {
880 brelse(bh);
881 brelse(dibh);
882 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000883 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500884 if (IS_ERR(dent)) {
885 brelse(bh);
886 brelse(dibh);
887 return PTR_ERR(dent);
888 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000889
890 /* Adjust the last dirent's record length
891 (Remember that dent still points to the last entry.) */
892
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000893 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000894 sizeof(struct gfs2_dinode) -
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000895 sizeof(struct gfs2_leaf));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000896
897 brelse(bh);
898
899 /* We're done with the new leaf block, now setup the new
900 hash table. */
901
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000902 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000903 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
904
905 lp = (uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode));
906
907 for (x = sdp->sd_hash_ptrs; x--; lp++)
908 *lp = cpu_to_be64(bn);
909
910 dip->i_di.di_size = sdp->sd_sb.sb_bsize / 2;
911 dip->i_di.di_blocks++;
912 dip->i_di.di_flags |= GFS2_DIF_EXHASH;
913 dip->i_di.di_payload_format = 0;
914
915 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
916 dip->i_di.di_depth = y;
917
918 gfs2_dinode_out(&dip->i_di, dibh->b_data);
919
920 brelse(dibh);
921
922 return 0;
923}
924
925/**
926 * dir_split_leaf - Split a leaf block into two
927 * @dip: The GFS2 inode
928 * @index:
929 * @leaf_no:
930 *
931 * Returns: 0 on success, error code on failure
932 */
933
Steven Whitehousec7526662006-03-20 12:30:04 -0500934static int dir_split_leaf(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000935{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400936 struct gfs2_inode *dip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000937 struct buffer_head *nbh, *obh, *dibh;
938 struct gfs2_leaf *nleaf, *oleaf;
Steven Whitehouse4da3c642006-07-11 13:19:13 -0400939 struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000940 uint32_t start, len, half_len, divider;
Steven Whitehousec7526662006-03-20 12:30:04 -0500941 uint64_t bn, *lp, leaf_no;
942 uint32_t index;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000943 int x, moved = 0;
944 int error;
945
Steven Whitehousec7526662006-03-20 12:30:04 -0500946 index = name->hash >> (32 - dip->i_di.di_depth);
947 error = get_leaf_nr(dip, index, &leaf_no);
948 if (error)
949 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000950
951 /* Get the old leaf block */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000952 error = get_leaf(dip, leaf_no, &obh);
953 if (error)
Steven Whitehousee90deff2006-03-29 19:02:15 -0500954 return error;
955
956 oleaf = (struct gfs2_leaf *)obh->b_data;
957 if (dip->i_di.di_depth == be16_to_cpu(oleaf->lf_depth)) {
958 brelse(obh);
959 return 1; /* can't split */
960 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000961
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000962 gfs2_trans_add_bh(dip->i_gl, obh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000963
Steven Whitehousec7526662006-03-20 12:30:04 -0500964 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
965 if (!nleaf) {
966 brelse(obh);
967 return -ENOSPC;
968 }
969 bn = nbh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000970
Steven Whitehousec7526662006-03-20 12:30:04 -0500971 /* Compute the start and len of leaf pointers in the hash table. */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000972 len = 1 << (dip->i_di.di_depth - be16_to_cpu(oleaf->lf_depth));
973 half_len = len >> 1;
974 if (!half_len) {
Steven Whitehousee90deff2006-03-29 19:02:15 -0500975 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 +0000976 gfs2_consist_inode(dip);
977 error = -EIO;
978 goto fail_brelse;
979 }
980
981 start = (index & ~(len - 1));
982
983 /* Change the pointers.
984 Don't bother distinguishing stuffed from non-stuffed.
985 This code is complicated enough already. */
Steven Whitehousec7526662006-03-20 12:30:04 -0500986 lp = kmalloc(half_len * sizeof(uint64_t), GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000987 /* Change the pointers */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000988 for (x = 0; x < half_len; x++)
989 lp[x] = cpu_to_be64(bn);
990
Steven Whitehousee13940b2006-01-30 13:31:50 +0000991 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(uint64_t),
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500992 half_len * sizeof(uint64_t));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000993 if (error != half_len * sizeof(uint64_t)) {
994 if (error >= 0)
995 error = -EIO;
996 goto fail_lpfree;
997 }
998
999 kfree(lp);
1000
1001 /* Compute the divider */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001002 divider = (start + half_len) << (32 - dip->i_di.di_depth);
1003
1004 /* Copy the entries */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001005 dirent_first(dip, obh, &dent);
1006
1007 do {
1008 next = dent;
1009 if (dirent_next(dip, obh, &next))
1010 next = NULL;
1011
1012 if (dent->de_inum.no_addr &&
1013 be32_to_cpu(dent->de_hash) < divider) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001014 struct qstr str;
1015 str.name = (char*)(dent+1);
1016 str.len = be16_to_cpu(dent->de_name_len);
1017 str.hash = be32_to_cpu(dent->de_hash);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001018 new = gfs2_dirent_alloc(inode, nbh, &str);
Steven Whitehousec7526662006-03-20 12:30:04 -05001019 if (IS_ERR(new)) {
1020 error = PTR_ERR(new);
1021 break;
1022 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001023
1024 new->de_inum = dent->de_inum; /* No endian worries */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001025 new->de_type = dent->de_type; /* No endian worries */
Steven Whitehousec7526662006-03-20 12:30:04 -05001026 nleaf->lf_entries = cpu_to_be16(be16_to_cpu(nleaf->lf_entries)+1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001027
1028 dirent_del(dip, obh, prev, dent);
1029
1030 if (!oleaf->lf_entries)
1031 gfs2_consist_inode(dip);
Steven Whitehousec7526662006-03-20 12:30:04 -05001032 oleaf->lf_entries = cpu_to_be16(be16_to_cpu(oleaf->lf_entries)-1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001033
1034 if (!prev)
1035 prev = dent;
1036
1037 moved = 1;
Steven Whitehousec7526662006-03-20 12:30:04 -05001038 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001039 prev = dent;
Steven Whitehousec7526662006-03-20 12:30:04 -05001040 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001041 dent = next;
Steven Whitehousec7526662006-03-20 12:30:04 -05001042 } while (dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001043
Steven Whitehousec7526662006-03-20 12:30:04 -05001044 oleaf->lf_depth = nleaf->lf_depth;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001045
1046 error = gfs2_meta_inode_buffer(dip, &dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001047 if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001048 dip->i_di.di_blocks++;
1049 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1050 brelse(dibh);
1051 }
1052
1053 brelse(obh);
1054 brelse(nbh);
1055
1056 return error;
1057
Steven Whitehousee90deff2006-03-29 19:02:15 -05001058fail_lpfree:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001059 kfree(lp);
1060
Steven Whitehousee90deff2006-03-29 19:02:15 -05001061fail_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001062 brelse(obh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001063 brelse(nbh);
1064 return error;
1065}
1066
1067/**
1068 * dir_double_exhash - Double size of ExHash table
1069 * @dip: The GFS2 dinode
1070 *
1071 * Returns: 0 on success, error code on failure
1072 */
1073
1074static int dir_double_exhash(struct gfs2_inode *dip)
1075{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001076 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001077 struct buffer_head *dibh;
1078 uint32_t hsize;
1079 uint64_t *buf;
1080 uint64_t *from, *to;
1081 uint64_t block;
1082 int x;
1083 int error = 0;
1084
1085 hsize = 1 << dip->i_di.di_depth;
1086 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1087 gfs2_consist_inode(dip);
1088 return -EIO;
1089 }
1090
1091 /* Allocate both the "from" and "to" buffers in one big chunk */
1092
1093 buf = kcalloc(3, sdp->sd_hash_bsize, GFP_KERNEL | __GFP_NOFAIL);
1094
1095 for (block = dip->i_di.di_size >> sdp->sd_hash_bsize_shift; block--;) {
Steven Whitehousee13940b2006-01-30 13:31:50 +00001096 error = gfs2_dir_read_data(dip, (char *)buf,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001097 block * sdp->sd_hash_bsize,
1098 sdp->sd_hash_bsize);
1099 if (error != sdp->sd_hash_bsize) {
1100 if (error >= 0)
1101 error = -EIO;
1102 goto fail;
1103 }
1104
1105 from = buf;
1106 to = (uint64_t *)((char *)buf + sdp->sd_hash_bsize);
1107
1108 for (x = sdp->sd_hash_ptrs; x--; from++) {
1109 *to++ = *from; /* No endianess worries */
1110 *to++ = *from;
1111 }
1112
Steven Whitehousee13940b2006-01-30 13:31:50 +00001113 error = gfs2_dir_write_data(dip,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001114 (char *)buf + sdp->sd_hash_bsize,
1115 block * sdp->sd_sb.sb_bsize,
1116 sdp->sd_sb.sb_bsize);
1117 if (error != sdp->sd_sb.sb_bsize) {
1118 if (error >= 0)
1119 error = -EIO;
1120 goto fail;
1121 }
1122 }
1123
1124 kfree(buf);
1125
1126 error = gfs2_meta_inode_buffer(dip, &dibh);
1127 if (!gfs2_assert_withdraw(sdp, !error)) {
1128 dip->i_di.di_depth++;
1129 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1130 brelse(dibh);
1131 }
1132
1133 return error;
1134
1135 fail:
1136 kfree(buf);
1137
1138 return error;
1139}
1140
1141/**
1142 * compare_dents - compare directory entries by hash value
1143 * @a: first dent
1144 * @b: second dent
1145 *
1146 * When comparing the hash entries of @a to @b:
1147 * gt: returns 1
1148 * lt: returns -1
1149 * eq: returns 0
1150 */
1151
1152static int compare_dents(const void *a, const void *b)
1153{
1154 struct gfs2_dirent *dent_a, *dent_b;
1155 uint32_t hash_a, hash_b;
1156 int ret = 0;
1157
1158 dent_a = *(struct gfs2_dirent **)a;
Steven Whitehousec7526662006-03-20 12:30:04 -05001159 hash_a = be32_to_cpu(dent_a->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001160
1161 dent_b = *(struct gfs2_dirent **)b;
Steven Whitehousec7526662006-03-20 12:30:04 -05001162 hash_b = be32_to_cpu(dent_b->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001163
1164 if (hash_a > hash_b)
1165 ret = 1;
1166 else if (hash_a < hash_b)
1167 ret = -1;
1168 else {
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001169 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1170 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001171
1172 if (len_a > len_b)
1173 ret = 1;
1174 else if (len_a < len_b)
1175 ret = -1;
1176 else
1177 ret = memcmp((char *)(dent_a + 1),
1178 (char *)(dent_b + 1),
1179 len_a);
1180 }
1181
1182 return ret;
1183}
1184
1185/**
1186 * do_filldir_main - read out directory entries
1187 * @dip: The GFS2 inode
1188 * @offset: The offset in the file to read from
1189 * @opaque: opaque data to pass to filldir
1190 * @filldir: The function to pass entries to
1191 * @darr: an array of struct gfs2_dirent pointers to read
1192 * @entries: the number of entries in darr
1193 * @copied: pointer to int that's non-zero if a entry has been copied out
1194 *
1195 * Jump through some hoops to make sure that if there are hash collsions,
1196 * they are read out at the beginning of a buffer. We want to minimize
1197 * the possibility that they will fall into different readdir buffers or
1198 * that someone will want to seek to that location.
1199 *
1200 * Returns: errno, >0 on exception from filldir
1201 */
1202
1203static int do_filldir_main(struct gfs2_inode *dip, uint64_t *offset,
1204 void *opaque, gfs2_filldir_t filldir,
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001205 const struct gfs2_dirent **darr, uint32_t entries,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001206 int *copied)
1207{
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001208 const struct gfs2_dirent *dent, *dent_next;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001209 struct gfs2_inum inum;
1210 uint64_t off, off_next;
1211 unsigned int x, y;
1212 int run = 0;
1213 int error = 0;
1214
1215 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1216
1217 dent_next = darr[0];
1218 off_next = be32_to_cpu(dent_next->de_hash);
1219 off_next = gfs2_disk_hash2offset(off_next);
1220
1221 for (x = 0, y = 1; x < entries; x++, y++) {
1222 dent = dent_next;
1223 off = off_next;
1224
1225 if (y < entries) {
1226 dent_next = darr[y];
1227 off_next = be32_to_cpu(dent_next->de_hash);
1228 off_next = gfs2_disk_hash2offset(off_next);
1229
1230 if (off < *offset)
1231 continue;
1232 *offset = off;
1233
1234 if (off_next == off) {
1235 if (*copied && !run)
1236 return 1;
1237 run = 1;
1238 } else
1239 run = 0;
1240 } else {
1241 if (off < *offset)
1242 continue;
1243 *offset = off;
1244 }
1245
1246 gfs2_inum_in(&inum, (char *)&dent->de_inum);
1247
1248 error = filldir(opaque, (char *)(dent + 1),
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001249 be16_to_cpu(dent->de_name_len),
David Teiglandb3b94fa2006-01-16 16:50:04 +00001250 off, &inum,
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001251 be16_to_cpu(dent->de_type));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001252 if (error)
1253 return 1;
1254
1255 *copied = 1;
1256 }
1257
1258 /* Increment the *offset by one, so the next time we come into the
1259 do_filldir fxn, we get the next entry instead of the last one in the
1260 current leaf */
1261
1262 (*offset)++;
1263
1264 return 0;
1265}
1266
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001267static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
1268 gfs2_filldir_t filldir, int *copied,
1269 unsigned *depth, u64 leaf_no)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001270{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001271 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001272 struct buffer_head *bh;
1273 struct gfs2_leaf *lf;
1274 unsigned entries = 0;
1275 unsigned leaves = 0;
1276 const struct gfs2_dirent **darr, *dent;
1277 struct dirent_gather g;
1278 struct buffer_head **larr;
1279 int leaf = 0;
1280 int error, i;
1281 u64 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001282
David Teiglandb3b94fa2006-01-16 16:50:04 +00001283 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001284 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001285 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001286 goto out;
1287 lf = (struct gfs2_leaf *)bh->b_data;
1288 if (leaves == 0)
1289 *depth = be16_to_cpu(lf->lf_depth);
1290 entries += be16_to_cpu(lf->lf_entries);
1291 leaves++;
1292 lfn = be64_to_cpu(lf->lf_next);
1293 brelse(bh);
1294 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001295
1296 if (!entries)
1297 return 0;
1298
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001299 error = -ENOMEM;
Steven Whitehousefe1bded2006-04-18 10:09:15 -04001300 larr = vmalloc((leaves + entries) * sizeof(void*));
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001301 if (!larr)
1302 goto out;
1303 darr = (const struct gfs2_dirent **)(larr + leaves);
1304 g.pdent = darr;
1305 g.offset = 0;
1306 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001307
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001308 do {
1309 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001310 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001311 goto out_kfree;
1312 lf = (struct gfs2_leaf *)bh->b_data;
1313 lfn = be64_to_cpu(lf->lf_next);
1314 if (lf->lf_entries) {
1315 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1316 gfs2_dirent_gather, NULL, &g);
1317 error = PTR_ERR(dent);
1318 if (IS_ERR(dent)) {
1319 goto out_kfree;
1320 }
1321 error = 0;
1322 larr[leaf++] = bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001323 } else {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001324 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001325 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001326 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001327
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001328 error = do_filldir_main(ip, offset, opaque, filldir, darr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001329 entries, copied);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001330out_kfree:
1331 for(i = 0; i < leaf; i++)
1332 brelse(larr[i]);
Steven Whitehousefe1bded2006-04-18 10:09:15 -04001333 vfree(larr);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001334out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001335 return error;
1336}
1337
1338/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001339 * dir_e_read - Reads the entries from a directory into a filldir buffer
1340 * @dip: dinode pointer
1341 * @offset: the hash of the last entry read shifted to the right once
1342 * @opaque: buffer for the filldir function to fill
1343 * @filldir: points to the filldir function to use
1344 *
1345 * Returns: errno
1346 */
1347
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001348static int dir_e_read(struct inode *inode, uint64_t *offset, void *opaque,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001349 gfs2_filldir_t filldir)
1350{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001351 struct gfs2_inode *dip = GFS2_I(inode);
1352 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001353 uint32_t hsize, len = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001354 uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
1355 uint32_t hash, index;
1356 uint64_t *lp;
1357 int copied = 0;
1358 int error = 0;
Steven Whitehouse4da3c642006-07-11 13:19:13 -04001359 unsigned depth = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001360
1361 hsize = 1 << dip->i_di.di_depth;
1362 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1363 gfs2_consist_inode(dip);
1364 return -EIO;
1365 }
1366
1367 hash = gfs2_dir_offset2hash(*offset);
1368 index = hash >> (32 - dip->i_di.di_depth);
1369
1370 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1371 if (!lp)
1372 return -ENOMEM;
1373
1374 while (index < hsize) {
1375 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1376 ht_offset = index - lp_offset;
1377
1378 if (ht_offset_cur != ht_offset) {
Steven Whitehousee13940b2006-01-30 13:31:50 +00001379 error = gfs2_dir_read_data(dip, (char *)lp,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001380 ht_offset * sizeof(uint64_t),
1381 sdp->sd_hash_bsize);
1382 if (error != sdp->sd_hash_bsize) {
1383 if (error >= 0)
1384 error = -EIO;
1385 goto out;
1386 }
1387 ht_offset_cur = ht_offset;
1388 }
1389
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001390 error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
1391 &copied, &depth,
1392 be64_to_cpu(lp[lp_offset]));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001393 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001394 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001395
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001396 len = 1 << (dip->i_di.di_depth - depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001397 index = (index & ~(len - 1)) + len;
1398 }
1399
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001400out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001401 kfree(lp);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001402 if (error > 0)
1403 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001404 return error;
1405}
1406
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001407int gfs2_dir_read(struct inode *inode, uint64_t *offset, void *opaque,
1408 gfs2_filldir_t filldir)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001409{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001410 struct gfs2_inode *dip = GFS2_I(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001411 struct dirent_gather g;
1412 const struct gfs2_dirent **darr, *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001413 struct buffer_head *dibh;
1414 int copied = 0;
1415 int error;
1416
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001417 if (!dip->i_di.di_entries)
1418 return 0;
1419
1420 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
1421 return dir_e_read(inode, offset, opaque, filldir);
1422
David Teiglandb3b94fa2006-01-16 16:50:04 +00001423 if (!gfs2_is_stuffed(dip)) {
1424 gfs2_consist_inode(dip);
1425 return -EIO;
1426 }
1427
David Teiglandb3b94fa2006-01-16 16:50:04 +00001428 error = gfs2_meta_inode_buffer(dip, &dibh);
1429 if (error)
1430 return error;
1431
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001432 error = -ENOMEM;
1433 darr = kmalloc(dip->i_di.di_entries * sizeof(struct gfs2_dirent *),
1434 GFP_KERNEL);
1435 if (darr) {
1436 g.pdent = darr;
1437 g.offset = 0;
1438 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1439 gfs2_dirent_gather, NULL, &g);
1440 if (IS_ERR(dent)) {
1441 error = PTR_ERR(dent);
1442 goto out;
1443 }
1444 error = do_filldir_main(dip, offset, opaque, filldir, darr,
1445 dip->i_di.di_entries, &copied);
1446out:
1447 kfree(darr);
1448 }
1449
David Teiglandb3b94fa2006-01-16 16:50:04 +00001450 if (error > 0)
1451 error = 0;
1452
1453 brelse(dibh);
1454
1455 return error;
1456}
1457
David Teiglandb3b94fa2006-01-16 16:50:04 +00001458/**
1459 * gfs2_dir_search - Search a directory
1460 * @dip: The GFS2 inode
1461 * @filename:
1462 * @inode:
1463 *
1464 * This routine searches a directory for a file or another directory.
1465 * Assumes a glock is held on dip.
1466 *
1467 * Returns: errno
1468 */
1469
Steven Whitehousec7526662006-03-20 12:30:04 -05001470int gfs2_dir_search(struct inode *dir, const struct qstr *name,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001471 struct gfs2_inum *inum, unsigned int *type)
1472{
Steven Whitehousec7526662006-03-20 12:30:04 -05001473 struct buffer_head *bh;
1474 struct gfs2_dirent *dent;
1475
1476 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1477 if (dent) {
1478 if (IS_ERR(dent))
1479 return PTR_ERR(dent);
1480 if (inum)
1481 gfs2_inum_in(inum, (char *)&dent->de_inum);
1482 if (type)
1483 *type = be16_to_cpu(dent->de_type);
1484 brelse(bh);
1485 return 0;
1486 }
1487 return -ENOENT;
1488}
1489
1490static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1491{
1492 struct buffer_head *bh, *obh;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001493 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001494 struct gfs2_leaf *leaf, *oleaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001495 int error;
Steven Whitehousec7526662006-03-20 12:30:04 -05001496 u32 index;
1497 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001498
Steven Whitehousec7526662006-03-20 12:30:04 -05001499 index = name->hash >> (32 - ip->i_di.di_depth);
1500 error = get_first_leaf(ip, index, &obh);
1501 if (error)
1502 return error;
1503 do {
1504 oleaf = (struct gfs2_leaf *)obh->b_data;
1505 bn = be64_to_cpu(oleaf->lf_next);
1506 if (!bn)
1507 break;
1508 brelse(obh);
1509 error = get_leaf(ip, bn, &obh);
1510 if (error)
1511 return error;
1512 } while(1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001513
Steven Whitehousec7526662006-03-20 12:30:04 -05001514 gfs2_trans_add_bh(ip->i_gl, obh, 1);
1515
1516 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1517 if (!leaf) {
1518 brelse(obh);
1519 return -ENOSPC;
1520 }
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04001521 oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
Steven Whitehousec7526662006-03-20 12:30:04 -05001522 brelse(bh);
1523 brelse(obh);
1524
1525 error = gfs2_meta_inode_buffer(ip, &bh);
1526 if (error)
1527 return error;
1528 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1529 ip->i_di.di_blocks++;
1530 gfs2_dinode_out(&ip->i_di, bh->b_data);
1531 brelse(bh);
1532 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001533}
1534
1535/**
1536 * gfs2_dir_add - Add new filename into directory
1537 * @dip: The GFS2 inode
1538 * @filename: The new name
1539 * @inode: The inode number of the entry
1540 * @type: The type of the entry
1541 *
1542 * Returns: 0 on success, error code on failure
1543 */
1544
Steven Whitehousec7526662006-03-20 12:30:04 -05001545int gfs2_dir_add(struct inode *inode, const struct qstr *name,
1546 const struct gfs2_inum *inum, unsigned type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001547{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001548 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001549 struct buffer_head *bh;
1550 struct gfs2_dirent *dent;
1551 struct gfs2_leaf *leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001552 int error;
1553
Steven Whitehousec7526662006-03-20 12:30:04 -05001554 while(1) {
1555 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
1556 &bh);
1557 if (dent) {
1558 if (IS_ERR(dent))
1559 return PTR_ERR(dent);
1560 dent = gfs2_init_dirent(inode, dent, name, bh);
1561 gfs2_inum_out(inum, (char *)&dent->de_inum);
1562 dent->de_type = cpu_to_be16(type);
1563 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
1564 leaf = (struct gfs2_leaf *)bh->b_data;
1565 leaf->lf_entries = cpu_to_be16(be16_to_cpu(leaf->lf_entries) + 1);
1566 }
1567 brelse(bh);
1568 error = gfs2_meta_inode_buffer(ip, &bh);
1569 if (error)
1570 break;
1571 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1572 ip->i_di.di_entries++;
1573 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
1574 gfs2_dinode_out(&ip->i_di, bh->b_data);
1575 brelse(bh);
1576 error = 0;
1577 break;
1578 }
1579 if (!(ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
1580 error = dir_make_exhash(inode);
1581 if (error)
1582 break;
1583 continue;
1584 }
1585 error = dir_split_leaf(inode, name);
1586 if (error == 0)
1587 continue;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001588 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001589 break;
1590 if (ip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) {
1591 error = dir_double_exhash(ip);
1592 if (error)
1593 break;
1594 error = dir_split_leaf(inode, name);
Steven Whitehousee90deff2006-03-29 19:02:15 -05001595 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001596 break;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001597 if (error == 0)
1598 continue;
Steven Whitehousec7526662006-03-20 12:30:04 -05001599 }
1600 error = dir_new_leaf(inode, name);
1601 if (!error)
1602 continue;
1603 error = -ENOSPC;
1604 break;
1605 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001606 return error;
1607}
1608
Steven Whitehousec7526662006-03-20 12:30:04 -05001609
David Teiglandb3b94fa2006-01-16 16:50:04 +00001610/**
1611 * gfs2_dir_del - Delete a directory entry
1612 * @dip: The GFS2 inode
1613 * @filename: The filename
1614 *
1615 * Returns: 0 on success, error code on failure
1616 */
1617
Steven Whitehousec7526662006-03-20 12:30:04 -05001618int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001619{
Steven Whitehousec7526662006-03-20 12:30:04 -05001620 struct gfs2_dirent *dent, *prev = NULL;
1621 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001622 int error;
1623
Steven Whitehousec7526662006-03-20 12:30:04 -05001624 /* Returns _either_ the entry (if its first in block) or the
1625 previous entry otherwise */
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001626 dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001627 if (!dent) {
1628 gfs2_consist_inode(dip);
1629 return -EIO;
1630 }
1631 if (IS_ERR(dent)) {
1632 gfs2_consist_inode(dip);
1633 return PTR_ERR(dent);
1634 }
1635 /* If not first in block, adjust pointers accordingly */
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001636 if (gfs2_dirent_find(dent, name, NULL) == 0) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001637 prev = dent;
1638 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1639 }
1640
1641 dirent_del(dip, bh, prev, dent);
1642 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1643 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1644 u16 entries = be16_to_cpu(leaf->lf_entries);
1645 if (!entries)
1646 gfs2_consist_inode(dip);
1647 leaf->lf_entries = cpu_to_be16(--entries);
Steven Whitehousec7526662006-03-20 12:30:04 -05001648 }
Steven Whitehouseed386502006-04-07 16:28:07 -04001649 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001650
1651 error = gfs2_meta_inode_buffer(dip, &bh);
1652 if (error)
1653 return error;
1654
1655 if (!dip->i_di.di_entries)
1656 gfs2_consist_inode(dip);
1657 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1658 dip->i_di.di_entries--;
1659 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1660 gfs2_dinode_out(&dip->i_di, bh->b_data);
1661 brelse(bh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001662 mark_inode_dirty(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001663
1664 return error;
1665}
1666
David Teiglandb3b94fa2006-01-16 16:50:04 +00001667/**
1668 * gfs2_dir_mvino - Change inode number of directory entry
1669 * @dip: The GFS2 inode
1670 * @filename:
1671 * @new_inode:
1672 *
1673 * This routine changes the inode number of a directory entry. It's used
1674 * by rename to change ".." when a directory is moved.
1675 * Assumes a glock is held on dvp.
1676 *
1677 * Returns: errno
1678 */
1679
Steven Whitehousec7526662006-03-20 12:30:04 -05001680int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001681 struct gfs2_inum *inum, unsigned int new_type)
1682{
Steven Whitehousec7526662006-03-20 12:30:04 -05001683 struct buffer_head *bh;
1684 struct gfs2_dirent *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001685 int error;
1686
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001687 dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001688 if (!dent) {
1689 gfs2_consist_inode(dip);
1690 return -EIO;
1691 }
1692 if (IS_ERR(dent))
1693 return PTR_ERR(dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001694
Steven Whitehousec7526662006-03-20 12:30:04 -05001695 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1696 gfs2_inum_out(inum, (char *)&dent->de_inum);
1697 dent->de_type = cpu_to_be16(new_type);
1698
1699 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1700 brelse(bh);
1701 error = gfs2_meta_inode_buffer(dip, &bh);
1702 if (error)
1703 return error;
1704 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1705 }
1706
1707 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1708 gfs2_dinode_out(&dip->i_di, bh->b_data);
1709 brelse(bh);
1710 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001711}
1712
1713/**
1714 * foreach_leaf - call a function for each leaf in a directory
1715 * @dip: the directory
1716 * @lc: the function to call for each each
1717 * @data: private data to pass to it
1718 *
1719 * Returns: errno
1720 */
1721
1722static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
1723{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001724 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001725 struct buffer_head *bh;
Steven Whitehousec7526662006-03-20 12:30:04 -05001726 struct gfs2_leaf *leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001727 uint32_t hsize, len;
1728 uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
1729 uint32_t index = 0;
1730 uint64_t *lp;
1731 uint64_t leaf_no;
1732 int error = 0;
1733
1734 hsize = 1 << dip->i_di.di_depth;
1735 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1736 gfs2_consist_inode(dip);
1737 return -EIO;
1738 }
1739
1740 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1741 if (!lp)
1742 return -ENOMEM;
1743
1744 while (index < hsize) {
1745 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1746 ht_offset = index - lp_offset;
1747
1748 if (ht_offset_cur != ht_offset) {
Steven Whitehousee13940b2006-01-30 13:31:50 +00001749 error = gfs2_dir_read_data(dip, (char *)lp,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001750 ht_offset * sizeof(uint64_t),
1751 sdp->sd_hash_bsize);
1752 if (error != sdp->sd_hash_bsize) {
1753 if (error >= 0)
1754 error = -EIO;
1755 goto out;
1756 }
1757 ht_offset_cur = ht_offset;
1758 }
1759
1760 leaf_no = be64_to_cpu(lp[lp_offset]);
1761 if (leaf_no) {
1762 error = get_leaf(dip, leaf_no, &bh);
1763 if (error)
1764 goto out;
Steven Whitehousec7526662006-03-20 12:30:04 -05001765 leaf = (struct gfs2_leaf *)bh->b_data;
Steven Whitehousec7526662006-03-20 12:30:04 -05001766 len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth));
Steven Whitehouse634ee0b2006-07-17 09:32:37 -04001767 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001768
1769 error = lc(dip, index, len, leaf_no, data);
1770 if (error)
1771 goto out;
1772
1773 index = (index & ~(len - 1)) + len;
1774 } else
1775 index++;
1776 }
1777
1778 if (index != hsize) {
1779 gfs2_consist_inode(dip);
1780 error = -EIO;
1781 }
1782
Steven Whitehouse634ee0b2006-07-17 09:32:37 -04001783out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001784 kfree(lp);
1785
1786 return error;
1787}
1788
1789/**
1790 * leaf_dealloc - Deallocate a directory leaf
1791 * @dip: the directory
1792 * @index: the hash table offset in the directory
1793 * @len: the number of pointers to this leaf
1794 * @leaf_no: the leaf number
1795 * @data: not used
1796 *
1797 * Returns: errno
1798 */
1799
1800static int leaf_dealloc(struct gfs2_inode *dip, uint32_t index, uint32_t len,
1801 uint64_t leaf_no, void *data)
1802{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001803 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001804 struct gfs2_leaf *tmp_leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001805 struct gfs2_rgrp_list rlist;
1806 struct buffer_head *bh, *dibh;
Steven Whitehousec7526662006-03-20 12:30:04 -05001807 uint64_t blk, nblk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001808 unsigned int rg_blocks = 0, l_blocks = 0;
1809 char *ht;
1810 unsigned int x, size = len * sizeof(uint64_t);
1811 int error;
1812
1813 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1814
1815 ht = kzalloc(size, GFP_KERNEL);
1816 if (!ht)
1817 return -ENOMEM;
1818
1819 gfs2_alloc_get(dip);
1820
1821 error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1822 if (error)
1823 goto out;
1824
1825 error = gfs2_rindex_hold(sdp, &dip->i_alloc.al_ri_gh);
1826 if (error)
1827 goto out_qs;
1828
1829 /* Count the number of leaves */
1830
Steven Whitehousec7526662006-03-20 12:30:04 -05001831 for (blk = leaf_no; blk; blk = nblk) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001832 error = get_leaf(dip, blk, &bh);
1833 if (error)
1834 goto out_rlist;
Steven Whitehousec7526662006-03-20 12:30:04 -05001835 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1836 nblk = be64_to_cpu(tmp_leaf->lf_next);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001837 brelse(bh);
1838
1839 gfs2_rlist_add(sdp, &rlist, blk);
1840 l_blocks++;
1841 }
1842
1843 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1844
1845 for (x = 0; x < rlist.rl_rgrps; x++) {
1846 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001847 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001848 rg_blocks += rgd->rd_ri.ri_length;
1849 }
1850
1851 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1852 if (error)
1853 goto out_rlist;
1854
1855 error = gfs2_trans_begin(sdp,
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001856 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
David Teiglandb3b94fa2006-01-16 16:50:04 +00001857 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1858 if (error)
1859 goto out_rg_gunlock;
1860
Steven Whitehousec7526662006-03-20 12:30:04 -05001861 for (blk = leaf_no; blk; blk = nblk) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001862 error = get_leaf(dip, blk, &bh);
1863 if (error)
1864 goto out_end_trans;
Steven Whitehousec7526662006-03-20 12:30:04 -05001865 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1866 nblk = be64_to_cpu(tmp_leaf->lf_next);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001867 brelse(bh);
1868
1869 gfs2_free_meta(dip, blk, 1);
1870
1871 if (!dip->i_di.di_blocks)
1872 gfs2_consist_inode(dip);
1873 dip->i_di.di_blocks--;
1874 }
1875
Steven Whitehousee13940b2006-01-30 13:31:50 +00001876 error = gfs2_dir_write_data(dip, ht, index * sizeof(uint64_t), size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001877 if (error != size) {
1878 if (error >= 0)
1879 error = -EIO;
1880 goto out_end_trans;
1881 }
1882
1883 error = gfs2_meta_inode_buffer(dip, &dibh);
1884 if (error)
1885 goto out_end_trans;
1886
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001887 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001888 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1889 brelse(dibh);
1890
1891 out_end_trans:
1892 gfs2_trans_end(sdp);
1893
1894 out_rg_gunlock:
1895 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1896
1897 out_rlist:
1898 gfs2_rlist_free(&rlist);
1899 gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh);
1900
1901 out_qs:
1902 gfs2_quota_unhold(dip);
1903
1904 out:
1905 gfs2_alloc_put(dip);
1906 kfree(ht);
1907
1908 return error;
1909}
1910
1911/**
1912 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1913 * @dip: the directory
1914 *
1915 * Dealloc all on-disk directory leaves to FREEMETA state
1916 * Change on-disk inode type to "regular file"
1917 *
1918 * Returns: errno
1919 */
1920
1921int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
1922{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001923 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001924 struct buffer_head *bh;
1925 int error;
1926
1927 /* Dealloc on-disk leaves to FREEMETA state */
1928 error = foreach_leaf(dip, leaf_dealloc, NULL);
1929 if (error)
1930 return error;
1931
1932 /* Make this a regular file in case we crash.
1933 (We don't want to free these blocks a second time.) */
1934
1935 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1936 if (error)
1937 return error;
1938
1939 error = gfs2_meta_inode_buffer(dip, &bh);
1940 if (!error) {
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001941 gfs2_trans_add_bh(dip->i_gl, bh, 1);
Steven Whitehouse568f4c92006-02-27 12:00:42 -05001942 ((struct gfs2_dinode *)bh->b_data)->di_mode =
1943 cpu_to_be32(S_IFREG);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001944 brelse(bh);
1945 }
1946
1947 gfs2_trans_end(sdp);
1948
1949 return error;
1950}
1951
1952/**
1953 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
1954 * @ip: the file being written to
1955 * @filname: the filename that's going to be added
David Teiglandb3b94fa2006-01-16 16:50:04 +00001956 *
Steven Whitehousec7526662006-03-20 12:30:04 -05001957 * Returns: 1 if alloc required, 0 if not, -ve on error
David Teiglandb3b94fa2006-01-16 16:50:04 +00001958 */
1959
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04001960int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001961{
Steven Whitehousec7526662006-03-20 12:30:04 -05001962 struct gfs2_dirent *dent;
1963 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001964
Steven Whitehousec7526662006-03-20 12:30:04 -05001965 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
Steven Whitehouseed386502006-04-07 16:28:07 -04001966 if (!dent) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001967 return 1;
Steven Whitehouseed386502006-04-07 16:28:07 -04001968 }
Steven Whitehousec7526662006-03-20 12:30:04 -05001969 if (IS_ERR(dent))
1970 return PTR_ERR(dent);
1971 brelse(bh);
1972 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001973}
1974