blob: a2923fb91bba0306cf961f3f8450ce527a18f67f [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
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
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>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020064#include <linux/lm_interface.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000065
66#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050067#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
Steven Whitehousecd915492006-09-04 12:49:07 -040081#define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
82#define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
David Teiglandb3b94fa2006-01-16 16:50:04 +000083
Steven Whitehouse907b9bc2006-09-25 09:26:04 -040084typedef int (*leaf_call_t) (struct gfs2_inode *dip, u32 index, u32 len,
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -040085 u64 leaf_no, void *data);
86typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
87 const struct qstr *name, void *opaque);
David Teiglandb3b94fa2006-01-16 16:50:04 +000088
Steven Whitehouse61e085a2006-04-24 10:07:13 -040089
Steven Whitehousecd915492006-09-04 12:49:07 -040090int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
Steven Whitehouse61e085a2006-04-24 10:07:13 -040091 struct buffer_head **bhp)
Steven Whitehousee13940b2006-01-30 13:31:50 +000092{
93 struct buffer_head *bh;
Steven Whitehousee13940b2006-01-30 13:31:50 +000094
Steven Whitehouse61e085a2006-04-24 10:07:13 -040095 bh = gfs2_meta_new(ip->i_gl, block);
96 gfs2_trans_add_bh(ip->i_gl, bh, 1);
97 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
98 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
Steven Whitehousee13940b2006-01-30 13:31:50 +000099 *bhp = bh;
100 return 0;
101}
102
Steven Whitehousecd915492006-09-04 12:49:07 -0400103static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, u64 block,
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400104 struct buffer_head **bhp)
105{
106 struct buffer_head *bh;
107 int error;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000108
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400109 error = gfs2_meta_read(ip->i_gl, block, DIO_WAIT, &bh);
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400110 if (error)
111 return error;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400112 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) {
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400113 brelse(bh);
114 return -EIO;
115 }
116 *bhp = bh;
117 return 0;
118}
Steven Whitehousee13940b2006-01-30 13:31:50 +0000119
120static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
121 unsigned int offset, unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000122{
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;
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -0500134 ip->i_inode.i_mtime.tv_sec = ip->i_inode.i_ctime.tv_sec = get_seconds();
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500135 gfs2_dinode_out(ip, dibh->b_data);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000136
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,
Steven Whitehousecd915492006-09-04 12:49:07 -0400154 u64 offset, unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000155{
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;
Steven Whitehousecd915492006-09-04 12:49:07 -0400158 u64 lblock, dblock;
159 u32 extlen = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000160 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)) {
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400176 error = gfs2_unstuff_dinode(ip, NULL);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000177 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;
Adrian Bunk348acd42006-10-19 15:20:04 +0200187 int new = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000188
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);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000215
Steven Whitehouse899bb262006-08-01 15:28:57 -0400216 buf += amount;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000217 copied += amount;
218 lblock++;
219 dblock++;
220 extlen--;
221
222 o = sizeof(struct gfs2_meta_header);
223 }
224
225out:
226 error = gfs2_meta_inode_buffer(ip, &dibh);
227 if (error)
228 return error;
229
230 if (ip->i_di.di_size < offset + copied)
231 ip->i_di.di_size = offset + copied;
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -0500232 ip->i_inode.i_mtime.tv_sec = ip->i_inode.i_ctime.tv_sec = get_seconds();
Steven Whitehousee13940b2006-01-30 13:31:50 +0000233
234 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500235 gfs2_dinode_out(ip, dibh->b_data);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000236 brelse(dibh);
237
238 return copied;
239fail:
240 if (copied)
241 goto out;
242 return error;
243}
244
245static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf,
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400246 u64 offset, unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000247{
248 struct buffer_head *dibh;
249 int error;
250
251 error = gfs2_meta_inode_buffer(ip, &dibh);
252 if (!error) {
253 offset += sizeof(struct gfs2_dinode);
254 memcpy(buf, dibh->b_data + offset, size);
255 brelse(dibh);
256 }
257
258 return (error) ? error : size;
259}
260
261
262/**
263 * gfs2_dir_read_data - Read a data from a directory inode
264 * @ip: The GFS2 Inode
265 * @buf: The buffer to place result into
266 * @offset: File offset to begin jdata_readng from
267 * @size: Amount of data to transfer
268 *
269 * Returns: The amount of data actually copied or the error
270 */
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400271static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf, u64 offset,
272 unsigned int size, unsigned ra)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000273{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400274 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehousecd915492006-09-04 12:49:07 -0400275 u64 lblock, dblock;
276 u32 extlen = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000277 unsigned int o;
278 int copied = 0;
279 int error = 0;
280
281 if (offset >= ip->i_di.di_size)
282 return 0;
283
Jan Engelhardtc5392122006-09-05 14:30:40 +0200284 if (offset + size > ip->i_di.di_size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000285 size = ip->i_di.di_size - offset;
286
287 if (!size)
288 return 0;
289
290 if (gfs2_is_stuffed(ip))
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400291 return gfs2_dir_read_stuffed(ip, buf, offset, size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000292
293 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
294 return -EINVAL;
295
296 lblock = offset;
297 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
298
299 while (copied < size) {
300 unsigned int amount;
301 struct buffer_head *bh;
302 int new;
303
304 amount = size - copied;
305 if (amount > sdp->sd_sb.sb_bsize - o)
306 amount = sdp->sd_sb.sb_bsize - o;
307
308 if (!extlen) {
309 new = 0;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400310 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
Steven Whitehousefd88de562006-05-05 16:59:11 -0400311 &dblock, &extlen);
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400312 if (error || !dblock)
313 goto fail;
314 BUG_ON(extlen < 1);
315 if (!ra)
316 extlen = 1;
317 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
Adrian Bunkb7d8ac32006-10-19 16:02:07 +0200318 } else {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400319 error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, &bh);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000320 if (error)
321 goto fail;
322 }
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400323 error = gfs2_metatype_check(sdp, bh, GFS2_METATYPE_JD);
324 if (error) {
325 brelse(bh);
326 goto fail;
327 }
328 dblock++;
329 extlen--;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000330 memcpy(buf, bh->b_data + o, amount);
331 brelse(bh);
Steven Whitehouse899bb262006-08-01 15:28:57 -0400332 buf += amount;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000333 copied += amount;
334 lblock++;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000335 o = sizeof(struct gfs2_meta_header);
336 }
337
338 return copied;
339fail:
340 return (copied) ? copied : error;
341}
342
Steven Whitehousec7526662006-03-20 12:30:04 -0500343static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
344 const struct qstr *name, int ret)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000345{
Steven Whitehousec7526662006-03-20 12:30:04 -0500346 if (dent->de_inum.no_addr != 0 &&
347 be32_to_cpu(dent->de_hash) == name->hash &&
348 be16_to_cpu(dent->de_name_len) == name->len &&
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400349 memcmp(dent+1, name->name, name->len) == 0)
Steven Whitehousec7526662006-03-20 12:30:04 -0500350 return ret;
351 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000352}
353
Steven Whitehousec7526662006-03-20 12:30:04 -0500354static int gfs2_dirent_find(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500355 const struct qstr *name,
356 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500357{
358 return __gfs2_dirent_find(dent, name, 1);
359}
360
361static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500362 const struct qstr *name,
363 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500364{
365 return __gfs2_dirent_find(dent, name, 2);
366}
367
368/*
369 * name->name holds ptr to start of block.
370 * name->len holds size of block.
371 */
372static int gfs2_dirent_last(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500373 const struct qstr *name,
374 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500375{
376 const char *start = name->name;
377 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
378 if (name->len == (end - start))
379 return 1;
380 return 0;
381}
382
383static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500384 const struct qstr *name,
385 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500386{
387 unsigned required = GFS2_DIRENT_SIZE(name->len);
388 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
389 unsigned totlen = be16_to_cpu(dent->de_rec_len);
390
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500391 if (!dent->de_inum.no_addr)
392 actual = GFS2_DIRENT_SIZE(0);
Jan Engelhardtc5392122006-09-05 14:30:40 +0200393 if (totlen - actual >= required)
Steven Whitehousec7526662006-03-20 12:30:04 -0500394 return 1;
395 return 0;
396}
397
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500398struct dirent_gather {
399 const struct gfs2_dirent **pdent;
400 unsigned offset;
401};
402
403static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
404 const struct qstr *name,
405 void *opaque)
406{
407 struct dirent_gather *g = opaque;
408 if (dent->de_inum.no_addr) {
409 g->pdent[g->offset++] = dent;
410 }
411 return 0;
412}
413
Steven Whitehousec7526662006-03-20 12:30:04 -0500414/*
415 * Other possible things to check:
416 * - Inode located within filesystem size (and on valid block)
417 * - Valid directory entry type
418 * Not sure how heavy-weight we want to make this... could also check
419 * hash is correct for example, but that would take a lot of extra time.
420 * For now the most important thing is to check that the various sizes
421 * are correct.
422 */
423static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
424 unsigned int size, unsigned int len, int first)
425{
426 const char *msg = "gfs2_dirent too small";
427 if (unlikely(size < sizeof(struct gfs2_dirent)))
428 goto error;
429 msg = "gfs2_dirent misaligned";
430 if (unlikely(offset & 0x7))
431 goto error;
432 msg = "gfs2_dirent points beyond end of block";
433 if (unlikely(offset + size > len))
434 goto error;
435 msg = "zero inode number";
436 if (unlikely(!first && !dent->de_inum.no_addr))
437 goto error;
438 msg = "name length is greater than space in dirent";
439 if (dent->de_inum.no_addr &&
440 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
441 size))
442 goto error;
443 return 0;
444error:
445 printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
446 first ? "first in block" : "not first in block");
447 return -EIO;
448}
449
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500450static int gfs2_dirent_offset(const void *buf)
Steven Whitehousec7526662006-03-20 12:30:04 -0500451{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500452 const struct gfs2_meta_header *h = buf;
453 int offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500454
455 BUG_ON(buf == NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500456
Steven Whitehousee3167de2006-03-30 15:46:23 -0500457 switch(be32_to_cpu(h->mh_type)) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500458 case GFS2_METATYPE_LF:
459 offset = sizeof(struct gfs2_leaf);
460 break;
461 case GFS2_METATYPE_DI:
462 offset = sizeof(struct gfs2_dinode);
463 break;
464 default:
465 goto wrong_type;
466 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500467 return offset;
468wrong_type:
469 printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
Steven Whitehousee3167de2006-03-30 15:46:23 -0500470 be32_to_cpu(h->mh_type));
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500471 return -1;
472}
Steven Whitehousec7526662006-03-20 12:30:04 -0500473
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400474static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500475 unsigned int len, gfs2_dscan_t scan,
476 const struct qstr *name,
477 void *opaque)
478{
479 struct gfs2_dirent *dent, *prev;
480 unsigned offset;
481 unsigned size;
482 int ret = 0;
483
484 ret = gfs2_dirent_offset(buf);
485 if (ret < 0)
486 goto consist_inode;
487
488 offset = ret;
Steven Whitehousec7526662006-03-20 12:30:04 -0500489 prev = NULL;
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400490 dent = buf + offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500491 size = be16_to_cpu(dent->de_rec_len);
492 if (gfs2_check_dirent(dent, offset, size, len, 1))
493 goto consist_inode;
494 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500495 ret = scan(dent, name, opaque);
Steven Whitehousec7526662006-03-20 12:30:04 -0500496 if (ret)
497 break;
498 offset += size;
499 if (offset == len)
500 break;
501 prev = dent;
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400502 dent = buf + offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500503 size = be16_to_cpu(dent->de_rec_len);
504 if (gfs2_check_dirent(dent, offset, size, len, 0))
505 goto consist_inode;
506 } while(1);
507
508 switch(ret) {
509 case 0:
510 return NULL;
511 case 1:
512 return dent;
513 case 2:
514 return prev ? prev : dent;
515 default:
516 BUG_ON(ret > 0);
517 return ERR_PTR(ret);
518 }
519
Steven Whitehousec7526662006-03-20 12:30:04 -0500520consist_inode:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400521 gfs2_consist_inode(GFS2_I(inode));
Steven Whitehousec7526662006-03-20 12:30:04 -0500522 return ERR_PTR(-EIO);
523}
524
525
David Teiglandb3b94fa2006-01-16 16:50:04 +0000526/**
527 * dirent_first - Return the first dirent
528 * @dip: the directory
529 * @bh: The buffer
530 * @dent: Pointer to list of dirents
531 *
532 * return first dirent whether bh points to leaf or stuffed dinode
533 *
534 * Returns: IS_LEAF, IS_DINODE, or -errno
535 */
536
537static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
538 struct gfs2_dirent **dent)
539{
540 struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data;
541
Steven Whitehousee3167de2006-03-30 15:46:23 -0500542 if (be32_to_cpu(h->mh_type) == GFS2_METATYPE_LF) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400543 if (gfs2_meta_check(GFS2_SB(&dip->i_inode), bh))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000544 return -EIO;
545 *dent = (struct gfs2_dirent *)(bh->b_data +
546 sizeof(struct gfs2_leaf));
547 return IS_LEAF;
548 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400549 if (gfs2_metatype_check(GFS2_SB(&dip->i_inode), bh, GFS2_METATYPE_DI))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000550 return -EIO;
551 *dent = (struct gfs2_dirent *)(bh->b_data +
552 sizeof(struct gfs2_dinode));
553 return IS_DINODE;
554 }
555}
556
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400557static int dirent_check_reclen(struct gfs2_inode *dip,
558 const struct gfs2_dirent *d, const void *end_p)
559{
560 const void *ptr = d;
561 u16 rec_len = be16_to_cpu(d->de_rec_len);
562
563 if (unlikely(rec_len < sizeof(struct gfs2_dirent)))
564 goto broken;
565 ptr += rec_len;
566 if (ptr < end_p)
567 return rec_len;
568 if (ptr == end_p)
569 return -ENOENT;
570broken:
571 gfs2_consist_inode(dip);
572 return -EIO;
573}
574
David Teiglandb3b94fa2006-01-16 16:50:04 +0000575/**
576 * dirent_next - Next dirent
577 * @dip: the directory
578 * @bh: The buffer
579 * @dent: Pointer to list of dirents
580 *
581 * Returns: 0 on success, error code otherwise
582 */
583
584static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
585 struct gfs2_dirent **dent)
586{
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400587 struct gfs2_dirent *cur = *dent, *tmp;
588 char *bh_end = bh->b_data + bh->b_size;
589 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000590
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400591 ret = dirent_check_reclen(dip, cur, bh_end);
592 if (ret < 0)
593 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000594
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400595 tmp = (void *)cur + ret;
596 ret = dirent_check_reclen(dip, tmp, bh_end);
597 if (ret == -EIO)
598 return ret;
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000599
David Teiglandb3b94fa2006-01-16 16:50:04 +0000600 /* Only the first dent could ever have de_inum.no_addr == 0 */
601 if (!tmp->de_inum.no_addr) {
602 gfs2_consist_inode(dip);
603 return -EIO;
604 }
605
606 *dent = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000607 return 0;
608}
609
610/**
611 * dirent_del - Delete a dirent
612 * @dip: The GFS2 inode
613 * @bh: The buffer
614 * @prev: The previous dirent
615 * @cur: The current dirent
616 *
617 */
618
619static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
620 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
621{
Steven Whitehousecd915492006-09-04 12:49:07 -0400622 u16 cur_rec_len, prev_rec_len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000623
624 if (!cur->de_inum.no_addr) {
625 gfs2_consist_inode(dip);
626 return;
627 }
628
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000629 gfs2_trans_add_bh(dip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000630
631 /* If there is no prev entry, this is the first entry in the block.
632 The de_rec_len is already as big as it needs to be. Just zero
633 out the inode number and return. */
634
635 if (!prev) {
636 cur->de_inum.no_addr = 0; /* No endianess worries */
637 return;
638 }
639
640 /* Combine this dentry with the previous one. */
641
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000642 prev_rec_len = be16_to_cpu(prev->de_rec_len);
643 cur_rec_len = be16_to_cpu(cur->de_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000644
645 if ((char *)prev + prev_rec_len != (char *)cur)
646 gfs2_consist_inode(dip);
647 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
648 gfs2_consist_inode(dip);
649
650 prev_rec_len += cur_rec_len;
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000651 prev->de_rec_len = cpu_to_be16(prev_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000652}
653
Steven Whitehousec7526662006-03-20 12:30:04 -0500654/*
655 * Takes a dent from which to grab space as an argument. Returns the
656 * newly created dent.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000657 */
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400658static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
659 struct gfs2_dirent *dent,
660 const struct qstr *name,
661 struct buffer_head *bh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000662{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400663 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -0500664 struct gfs2_dirent *ndent;
665 unsigned offset = 0, totlen;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000666
Steven Whitehousec7526662006-03-20 12:30:04 -0500667 if (dent->de_inum.no_addr)
668 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
669 totlen = be16_to_cpu(dent->de_rec_len);
670 BUG_ON(offset + name->len > totlen);
671 gfs2_trans_add_bh(ip->i_gl, bh, 1);
672 ndent = (struct gfs2_dirent *)((char *)dent + offset);
673 dent->de_rec_len = cpu_to_be16(offset);
674 gfs2_qstr2dirent(name, totlen - offset, ndent);
675 return ndent;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000676}
677
Steven Whitehousec7526662006-03-20 12:30:04 -0500678static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
679 struct buffer_head *bh,
680 const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000681{
682 struct gfs2_dirent *dent;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400683 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500684 gfs2_dirent_find_space, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500685 if (!dent || IS_ERR(dent))
686 return dent;
687 return gfs2_init_dirent(inode, dent, name, bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000688}
689
Steven Whitehousecd915492006-09-04 12:49:07 -0400690static int get_leaf(struct gfs2_inode *dip, u64 leaf_no,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000691 struct buffer_head **bhp)
692{
693 int error;
694
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400695 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, bhp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400696 if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
697 /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000698 error = -EIO;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400699 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000700
701 return error;
702}
703
704/**
705 * get_leaf_nr - Get a leaf number associated with the index
706 * @dip: The GFS2 inode
707 * @index:
708 * @leaf_out:
709 *
710 * Returns: 0 on success, error code otherwise
711 */
712
Steven Whitehousecd915492006-09-04 12:49:07 -0400713static int get_leaf_nr(struct gfs2_inode *dip, u32 index,
714 u64 *leaf_out)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000715{
Al Virob44b84d2006-10-14 10:46:30 -0400716 __be64 leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000717 int error;
718
Steven Whitehousee13940b2006-01-30 13:31:50 +0000719 error = gfs2_dir_read_data(dip, (char *)&leaf_no,
Al Virob44b84d2006-10-14 10:46:30 -0400720 index * sizeof(__be64),
721 sizeof(__be64), 0);
Steven Whitehousecd915492006-09-04 12:49:07 -0400722 if (error != sizeof(u64))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000723 return (error < 0) ? error : -EIO;
724
725 *leaf_out = be64_to_cpu(leaf_no);
726
727 return 0;
728}
729
Steven Whitehousecd915492006-09-04 12:49:07 -0400730static int get_first_leaf(struct gfs2_inode *dip, u32 index,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000731 struct buffer_head **bh_out)
732{
Steven Whitehousecd915492006-09-04 12:49:07 -0400733 u64 leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000734 int error;
735
736 error = get_leaf_nr(dip, index, &leaf_no);
737 if (!error)
738 error = get_leaf(dip, leaf_no, bh_out);
739
740 return error;
741}
742
Steven Whitehousec7526662006-03-20 12:30:04 -0500743static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
744 const struct qstr *name,
745 gfs2_dscan_t scan,
746 struct buffer_head **pbh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000747{
Steven Whitehousec7526662006-03-20 12:30:04 -0500748 struct buffer_head *bh;
749 struct gfs2_dirent *dent;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400750 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000751 int error;
752
Steven Whitehousec7526662006-03-20 12:30:04 -0500753 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
754 struct gfs2_leaf *leaf;
755 unsigned hsize = 1 << ip->i_di.di_depth;
756 unsigned index;
757 u64 ln;
758 if (hsize * sizeof(u64) != ip->i_di.di_size) {
759 gfs2_consist_inode(ip);
760 return ERR_PTR(-EIO);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000761 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400762
Steven Whitehousec7526662006-03-20 12:30:04 -0500763 index = name->hash >> (32 - ip->i_di.di_depth);
764 error = get_first_leaf(ip, index, &bh);
765 if (error)
766 return ERR_PTR(error);
767 do {
768 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500769 scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500770 if (dent)
771 goto got_dent;
772 leaf = (struct gfs2_leaf *)bh->b_data;
773 ln = be64_to_cpu(leaf->lf_next);
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400774 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500775 if (!ln)
776 break;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400777
Steven Whitehousec7526662006-03-20 12:30:04 -0500778 error = get_leaf(ip, ln, &bh);
779 } while(!error);
780
781 return error ? ERR_PTR(error) : NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000782 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000783
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400784
Steven Whitehousec7526662006-03-20 12:30:04 -0500785 error = gfs2_meta_inode_buffer(ip, &bh);
786 if (error)
787 return ERR_PTR(error);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500788 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500789got_dent:
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400790 if (unlikely(dent == NULL || IS_ERR(dent))) {
Steven Whitehouseed386502006-04-07 16:28:07 -0400791 brelse(bh);
792 bh = NULL;
793 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500794 *pbh = bh;
795 return dent;
796}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000797
Steven Whitehousec7526662006-03-20 12:30:04 -0500798static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
799{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400800 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -0500801 u64 bn = gfs2_alloc_meta(ip);
802 struct buffer_head *bh = gfs2_meta_new(ip->i_gl, bn);
803 struct gfs2_leaf *leaf;
804 struct gfs2_dirent *dent;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500805 struct qstr name = { .name = "", .len = 0, .hash = 0 };
Steven Whitehousec7526662006-03-20 12:30:04 -0500806 if (!bh)
807 return NULL;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400808
Steven Whitehousec7526662006-03-20 12:30:04 -0500809 gfs2_trans_add_bh(ip->i_gl, bh, 1);
810 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
811 leaf = (struct gfs2_leaf *)bh->b_data;
812 leaf->lf_depth = cpu_to_be16(depth);
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400813 leaf->lf_entries = 0;
Al Viroa2d7d022006-10-14 16:49:30 +0100814 leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400815 leaf->lf_next = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500816 memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
817 dent = (struct gfs2_dirent *)(leaf+1);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500818 gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
Steven Whitehousec7526662006-03-20 12:30:04 -0500819 *pbh = bh;
820 return leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000821}
822
823/**
824 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
825 * @dip: The GFS2 inode
826 *
827 * Returns: 0 on success, error code otherwise
828 */
829
Steven Whitehousec7526662006-03-20 12:30:04 -0500830static int dir_make_exhash(struct inode *inode)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000831{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400832 struct gfs2_inode *dip = GFS2_I(inode);
833 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000834 struct gfs2_dirent *dent;
Steven Whitehousec7526662006-03-20 12:30:04 -0500835 struct qstr args;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000836 struct buffer_head *bh, *dibh;
837 struct gfs2_leaf *leaf;
838 int y;
Steven Whitehousecd915492006-09-04 12:49:07 -0400839 u32 x;
Al Virob44b84d2006-10-14 10:46:30 -0400840 __be64 *lp;
841 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000842 int error;
843
844 error = gfs2_meta_inode_buffer(dip, &dibh);
845 if (error)
846 return error;
847
David Teiglandb3b94fa2006-01-16 16:50:04 +0000848 /* Turn over a new leaf */
849
Steven Whitehousec7526662006-03-20 12:30:04 -0500850 leaf = new_leaf(inode, &bh, 0);
851 if (!leaf)
852 return -ENOSPC;
853 bn = bh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000854
855 gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000856 leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries);
857
858 /* Copy dirents */
859
860 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
861 sizeof(struct gfs2_dinode));
862
863 /* Find last entry */
864
865 x = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500866 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
867 sizeof(struct gfs2_leaf);
868 args.name = bh->b_data;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400869 dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500870 gfs2_dirent_last, &args, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500871 if (!dent) {
872 brelse(bh);
873 brelse(dibh);
874 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000875 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500876 if (IS_ERR(dent)) {
877 brelse(bh);
878 brelse(dibh);
879 return PTR_ERR(dent);
880 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000881
882 /* Adjust the last dirent's record length
883 (Remember that dent still points to the last entry.) */
884
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000885 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000886 sizeof(struct gfs2_dinode) -
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000887 sizeof(struct gfs2_leaf));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000888
889 brelse(bh);
890
891 /* We're done with the new leaf block, now setup the new
892 hash table. */
893
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000894 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000895 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
896
Al Virob44b84d2006-10-14 10:46:30 -0400897 lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000898
899 for (x = sdp->sd_hash_ptrs; x--; lp++)
900 *lp = cpu_to_be64(bn);
901
902 dip->i_di.di_size = sdp->sd_sb.sb_bsize / 2;
903 dip->i_di.di_blocks++;
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -0500904 gfs2_set_inode_blocks(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000905 dip->i_di.di_flags |= GFS2_DIF_EXHASH;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000906
907 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
908 dip->i_di.di_depth = y;
909
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500910 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000911
912 brelse(dibh);
913
914 return 0;
915}
916
917/**
918 * dir_split_leaf - Split a leaf block into two
919 * @dip: The GFS2 inode
920 * @index:
921 * @leaf_no:
922 *
923 * Returns: 0 on success, error code on failure
924 */
925
Steven Whitehousec7526662006-03-20 12:30:04 -0500926static int dir_split_leaf(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000927{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400928 struct gfs2_inode *dip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000929 struct buffer_head *nbh, *obh, *dibh;
930 struct gfs2_leaf *nleaf, *oleaf;
Steven Whitehouse4da3c642006-07-11 13:19:13 -0400931 struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
Steven Whitehousecd915492006-09-04 12:49:07 -0400932 u32 start, len, half_len, divider;
Al Virob44b84d2006-10-14 10:46:30 -0400933 u64 bn, leaf_no;
934 __be64 *lp;
Steven Whitehousecd915492006-09-04 12:49:07 -0400935 u32 index;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000936 int x, moved = 0;
937 int error;
938
Steven Whitehousec7526662006-03-20 12:30:04 -0500939 index = name->hash >> (32 - dip->i_di.di_depth);
940 error = get_leaf_nr(dip, index, &leaf_no);
941 if (error)
942 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000943
944 /* Get the old leaf block */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000945 error = get_leaf(dip, leaf_no, &obh);
946 if (error)
Steven Whitehousee90deff2006-03-29 19:02:15 -0500947 return error;
948
949 oleaf = (struct gfs2_leaf *)obh->b_data;
950 if (dip->i_di.di_depth == be16_to_cpu(oleaf->lf_depth)) {
951 brelse(obh);
952 return 1; /* can't split */
953 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000954
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000955 gfs2_trans_add_bh(dip->i_gl, obh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000956
Steven Whitehousec7526662006-03-20 12:30:04 -0500957 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
958 if (!nleaf) {
959 brelse(obh);
960 return -ENOSPC;
961 }
962 bn = nbh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000963
Steven Whitehousec7526662006-03-20 12:30:04 -0500964 /* Compute the start and len of leaf pointers in the hash table. */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000965 len = 1 << (dip->i_di.di_depth - be16_to_cpu(oleaf->lf_depth));
966 half_len = len >> 1;
967 if (!half_len) {
Steven Whitehousee90deff2006-03-29 19:02:15 -0500968 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 +0000969 gfs2_consist_inode(dip);
970 error = -EIO;
971 goto fail_brelse;
972 }
973
974 start = (index & ~(len - 1));
975
976 /* Change the pointers.
977 Don't bother distinguishing stuffed from non-stuffed.
978 This code is complicated enough already. */
Al Virob44b84d2006-10-14 10:46:30 -0400979 lp = kmalloc(half_len * sizeof(__be64), GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000980 /* Change the pointers */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000981 for (x = 0; x < half_len; x++)
982 lp[x] = cpu_to_be64(bn);
983
Steven Whitehousecd915492006-09-04 12:49:07 -0400984 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
985 half_len * sizeof(u64));
986 if (error != half_len * sizeof(u64)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000987 if (error >= 0)
988 error = -EIO;
989 goto fail_lpfree;
990 }
991
992 kfree(lp);
993
994 /* Compute the divider */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000995 divider = (start + half_len) << (32 - dip->i_di.di_depth);
996
997 /* Copy the entries */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000998 dirent_first(dip, obh, &dent);
999
1000 do {
1001 next = dent;
1002 if (dirent_next(dip, obh, &next))
1003 next = NULL;
1004
1005 if (dent->de_inum.no_addr &&
1006 be32_to_cpu(dent->de_hash) < divider) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001007 struct qstr str;
1008 str.name = (char*)(dent+1);
1009 str.len = be16_to_cpu(dent->de_name_len);
1010 str.hash = be32_to_cpu(dent->de_hash);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001011 new = gfs2_dirent_alloc(inode, nbh, &str);
Steven Whitehousec7526662006-03-20 12:30:04 -05001012 if (IS_ERR(new)) {
1013 error = PTR_ERR(new);
1014 break;
1015 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001016
1017 new->de_inum = dent->de_inum; /* No endian worries */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001018 new->de_type = dent->de_type; /* No endian worries */
Steven Whitehousec7526662006-03-20 12:30:04 -05001019 nleaf->lf_entries = cpu_to_be16(be16_to_cpu(nleaf->lf_entries)+1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001020
1021 dirent_del(dip, obh, prev, dent);
1022
1023 if (!oleaf->lf_entries)
1024 gfs2_consist_inode(dip);
Steven Whitehousec7526662006-03-20 12:30:04 -05001025 oleaf->lf_entries = cpu_to_be16(be16_to_cpu(oleaf->lf_entries)-1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001026
1027 if (!prev)
1028 prev = dent;
1029
1030 moved = 1;
Steven Whitehousec7526662006-03-20 12:30:04 -05001031 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001032 prev = dent;
Steven Whitehousec7526662006-03-20 12:30:04 -05001033 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001034 dent = next;
Steven Whitehousec7526662006-03-20 12:30:04 -05001035 } while (dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001036
Steven Whitehousec7526662006-03-20 12:30:04 -05001037 oleaf->lf_depth = nleaf->lf_depth;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001038
1039 error = gfs2_meta_inode_buffer(dip, &dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001040 if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001041 dip->i_di.di_blocks++;
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -05001042 gfs2_set_inode_blocks(&dip->i_inode);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001043 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001044 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{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001070 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001071 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001072 u32 hsize;
1073 u64 *buf;
1074 u64 *from, *to;
1075 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001076 int x;
1077 int error = 0;
1078
1079 hsize = 1 << dip->i_di.di_depth;
Steven Whitehousecd915492006-09-04 12:49:07 -04001080 if (hsize * sizeof(u64) != dip->i_di.di_size) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001081 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,
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001092 sdp->sd_hash_bsize, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001093 if (error != sdp->sd_hash_bsize) {
1094 if (error >= 0)
1095 error = -EIO;
1096 goto fail;
1097 }
1098
1099 from = buf;
Steven Whitehousecd915492006-09-04 12:49:07 -04001100 to = (u64 *)((char *)buf + sdp->sd_hash_bsize);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001101
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++;
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001123 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001124 brelse(dibh);
1125 }
1126
1127 return error;
1128
Steven Whitehousea91ea692006-09-04 12:04:26 -04001129fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001130 kfree(buf);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001131 return error;
1132}
1133
1134/**
1135 * compare_dents - compare directory entries by hash value
1136 * @a: first dent
1137 * @b: second dent
1138 *
1139 * When comparing the hash entries of @a to @b:
1140 * gt: returns 1
1141 * lt: returns -1
1142 * eq: returns 0
1143 */
1144
1145static int compare_dents(const void *a, const void *b)
1146{
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001147 const struct gfs2_dirent *dent_a, *dent_b;
Steven Whitehousecd915492006-09-04 12:49:07 -04001148 u32 hash_a, hash_b;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001149 int ret = 0;
1150
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001151 dent_a = *(const struct gfs2_dirent **)a;
Steven Whitehousec7526662006-03-20 12:30:04 -05001152 hash_a = be32_to_cpu(dent_a->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001153
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001154 dent_b = *(const struct gfs2_dirent **)b;
Steven Whitehousec7526662006-03-20 12:30:04 -05001155 hash_b = be32_to_cpu(dent_b->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001156
1157 if (hash_a > hash_b)
1158 ret = 1;
1159 else if (hash_a < hash_b)
1160 ret = -1;
1161 else {
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001162 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1163 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001164
1165 if (len_a > len_b)
1166 ret = 1;
1167 else if (len_a < len_b)
1168 ret = -1;
1169 else
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001170 ret = memcmp(dent_a + 1, dent_b + 1, len_a);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001171 }
1172
1173 return ret;
1174}
1175
1176/**
1177 * do_filldir_main - read out directory entries
1178 * @dip: The GFS2 inode
1179 * @offset: The offset in the file to read from
1180 * @opaque: opaque data to pass to filldir
1181 * @filldir: The function to pass entries to
1182 * @darr: an array of struct gfs2_dirent pointers to read
1183 * @entries: the number of entries in darr
1184 * @copied: pointer to int that's non-zero if a entry has been copied out
1185 *
1186 * Jump through some hoops to make sure that if there are hash collsions,
1187 * they are read out at the beginning of a buffer. We want to minimize
1188 * the possibility that they will fall into different readdir buffers or
1189 * that someone will want to seek to that location.
1190 *
1191 * Returns: errno, >0 on exception from filldir
1192 */
1193
Steven Whitehousecd915492006-09-04 12:49:07 -04001194static int do_filldir_main(struct gfs2_inode *dip, u64 *offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001195 void *opaque, gfs2_filldir_t filldir,
Steven Whitehousecd915492006-09-04 12:49:07 -04001196 const struct gfs2_dirent **darr, u32 entries,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001197 int *copied)
1198{
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001199 const struct gfs2_dirent *dent, *dent_next;
Al Viro629a21e2006-10-13 22:51:24 -04001200 struct gfs2_inum_host inum;
Steven Whitehousecd915492006-09-04 12:49:07 -04001201 u64 off, off_next;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001202 unsigned int x, y;
1203 int run = 0;
1204 int error = 0;
1205
1206 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1207
1208 dent_next = darr[0];
1209 off_next = be32_to_cpu(dent_next->de_hash);
1210 off_next = gfs2_disk_hash2offset(off_next);
1211
1212 for (x = 0, y = 1; x < entries; x++, y++) {
1213 dent = dent_next;
1214 off = off_next;
1215
1216 if (y < entries) {
1217 dent_next = darr[y];
1218 off_next = be32_to_cpu(dent_next->de_hash);
1219 off_next = gfs2_disk_hash2offset(off_next);
1220
1221 if (off < *offset)
1222 continue;
1223 *offset = off;
1224
1225 if (off_next == off) {
1226 if (*copied && !run)
1227 return 1;
1228 run = 1;
1229 } else
1230 run = 0;
1231 } else {
1232 if (off < *offset)
1233 continue;
1234 *offset = off;
1235 }
1236
1237 gfs2_inum_in(&inum, (char *)&dent->de_inum);
1238
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001239 error = filldir(opaque, (const char *)(dent + 1),
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001240 be16_to_cpu(dent->de_name_len),
David Teiglandb3b94fa2006-01-16 16:50:04 +00001241 off, &inum,
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001242 be16_to_cpu(dent->de_type));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001243 if (error)
1244 return 1;
1245
1246 *copied = 1;
1247 }
1248
1249 /* Increment the *offset by one, so the next time we come into the
1250 do_filldir fxn, we get the next entry instead of the last one in the
1251 current leaf */
1252
1253 (*offset)++;
1254
1255 return 0;
1256}
1257
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001258static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
1259 gfs2_filldir_t filldir, int *copied,
1260 unsigned *depth, u64 leaf_no)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001261{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001262 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001263 struct buffer_head *bh;
1264 struct gfs2_leaf *lf;
1265 unsigned entries = 0;
1266 unsigned leaves = 0;
1267 const struct gfs2_dirent **darr, *dent;
1268 struct dirent_gather g;
1269 struct buffer_head **larr;
1270 int leaf = 0;
1271 int error, i;
1272 u64 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001273
David Teiglandb3b94fa2006-01-16 16:50:04 +00001274 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001275 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001276 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001277 goto out;
1278 lf = (struct gfs2_leaf *)bh->b_data;
1279 if (leaves == 0)
1280 *depth = be16_to_cpu(lf->lf_depth);
1281 entries += be16_to_cpu(lf->lf_entries);
1282 leaves++;
1283 lfn = be64_to_cpu(lf->lf_next);
1284 brelse(bh);
1285 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001286
1287 if (!entries)
1288 return 0;
1289
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001290 error = -ENOMEM;
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001291 larr = vmalloc((leaves + entries) * sizeof(void *));
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001292 if (!larr)
1293 goto out;
1294 darr = (const struct gfs2_dirent **)(larr + leaves);
1295 g.pdent = darr;
1296 g.offset = 0;
1297 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001298
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001299 do {
1300 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001301 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001302 goto out_kfree;
1303 lf = (struct gfs2_leaf *)bh->b_data;
1304 lfn = be64_to_cpu(lf->lf_next);
1305 if (lf->lf_entries) {
1306 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1307 gfs2_dirent_gather, NULL, &g);
1308 error = PTR_ERR(dent);
1309 if (IS_ERR(dent)) {
1310 goto out_kfree;
1311 }
1312 error = 0;
1313 larr[leaf++] = bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001314 } else {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001315 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001316 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001317 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001318
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001319 error = do_filldir_main(ip, offset, opaque, filldir, darr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001320 entries, copied);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001321out_kfree:
1322 for(i = 0; i < leaf; i++)
1323 brelse(larr[i]);
Steven Whitehousefe1bded2006-04-18 10:09:15 -04001324 vfree(larr);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001325out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001326 return error;
1327}
1328
1329/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001330 * dir_e_read - Reads the entries from a directory into a filldir buffer
1331 * @dip: dinode pointer
1332 * @offset: the hash of the last entry read shifted to the right once
1333 * @opaque: buffer for the filldir function to fill
1334 * @filldir: points to the filldir function to use
1335 *
1336 * Returns: errno
1337 */
1338
Steven Whitehousecd915492006-09-04 12:49:07 -04001339static int dir_e_read(struct inode *inode, u64 *offset, void *opaque,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001340 gfs2_filldir_t filldir)
1341{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001342 struct gfs2_inode *dip = GFS2_I(inode);
1343 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehousecd915492006-09-04 12:49:07 -04001344 u32 hsize, len = 0;
1345 u32 ht_offset, lp_offset, ht_offset_cur = -1;
1346 u32 hash, index;
Al Virob44b84d2006-10-14 10:46:30 -04001347 __be64 *lp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001348 int copied = 0;
1349 int error = 0;
Steven Whitehouse4da3c642006-07-11 13:19:13 -04001350 unsigned depth = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001351
1352 hsize = 1 << dip->i_di.di_depth;
Steven Whitehousecd915492006-09-04 12:49:07 -04001353 if (hsize * sizeof(u64) != dip->i_di.di_size) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001354 gfs2_consist_inode(dip);
1355 return -EIO;
1356 }
1357
1358 hash = gfs2_dir_offset2hash(*offset);
1359 index = hash >> (32 - dip->i_di.di_depth);
1360
1361 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1362 if (!lp)
1363 return -ENOMEM;
1364
1365 while (index < hsize) {
1366 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1367 ht_offset = index - lp_offset;
1368
1369 if (ht_offset_cur != ht_offset) {
Steven Whitehousee13940b2006-01-30 13:31:50 +00001370 error = gfs2_dir_read_data(dip, (char *)lp,
Al Virob44b84d2006-10-14 10:46:30 -04001371 ht_offset * sizeof(__be64),
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001372 sdp->sd_hash_bsize, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001373 if (error != sdp->sd_hash_bsize) {
1374 if (error >= 0)
1375 error = -EIO;
1376 goto out;
1377 }
1378 ht_offset_cur = ht_offset;
1379 }
1380
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001381 error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
1382 &copied, &depth,
1383 be64_to_cpu(lp[lp_offset]));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001384 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001385 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001386
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001387 len = 1 << (dip->i_di.di_depth - depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001388 index = (index & ~(len - 1)) + len;
1389 }
1390
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001391out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001392 kfree(lp);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001393 if (error > 0)
1394 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001395 return error;
1396}
1397
Steven Whitehousecd915492006-09-04 12:49:07 -04001398int gfs2_dir_read(struct inode *inode, u64 *offset, void *opaque,
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001399 gfs2_filldir_t filldir)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001400{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001401 struct gfs2_inode *dip = GFS2_I(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001402 struct dirent_gather g;
1403 const struct gfs2_dirent **darr, *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001404 struct buffer_head *dibh;
1405 int copied = 0;
1406 int error;
1407
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001408 if (!dip->i_di.di_entries)
1409 return 0;
1410
1411 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
1412 return dir_e_read(inode, offset, opaque, filldir);
1413
David Teiglandb3b94fa2006-01-16 16:50:04 +00001414 if (!gfs2_is_stuffed(dip)) {
1415 gfs2_consist_inode(dip);
1416 return -EIO;
1417 }
1418
David Teiglandb3b94fa2006-01-16 16:50:04 +00001419 error = gfs2_meta_inode_buffer(dip, &dibh);
1420 if (error)
1421 return error;
1422
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001423 error = -ENOMEM;
1424 darr = kmalloc(dip->i_di.di_entries * sizeof(struct gfs2_dirent *),
1425 GFP_KERNEL);
1426 if (darr) {
1427 g.pdent = darr;
1428 g.offset = 0;
1429 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1430 gfs2_dirent_gather, NULL, &g);
1431 if (IS_ERR(dent)) {
1432 error = PTR_ERR(dent);
1433 goto out;
1434 }
1435 error = do_filldir_main(dip, offset, opaque, filldir, darr,
1436 dip->i_di.di_entries, &copied);
1437out:
1438 kfree(darr);
1439 }
1440
David Teiglandb3b94fa2006-01-16 16:50:04 +00001441 if (error > 0)
1442 error = 0;
1443
1444 brelse(dibh);
1445
1446 return error;
1447}
1448
David Teiglandb3b94fa2006-01-16 16:50:04 +00001449/**
1450 * gfs2_dir_search - Search a directory
1451 * @dip: The GFS2 inode
1452 * @filename:
1453 * @inode:
1454 *
1455 * This routine searches a directory for a file or another directory.
1456 * Assumes a glock is held on dip.
1457 *
1458 * Returns: errno
1459 */
1460
Steven Whitehousec7526662006-03-20 12:30:04 -05001461int gfs2_dir_search(struct inode *dir, const struct qstr *name,
Al Viro629a21e2006-10-13 22:51:24 -04001462 struct gfs2_inum_host *inum, unsigned int *type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001463{
Steven Whitehousec7526662006-03-20 12:30:04 -05001464 struct buffer_head *bh;
1465 struct gfs2_dirent *dent;
1466
1467 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1468 if (dent) {
1469 if (IS_ERR(dent))
1470 return PTR_ERR(dent);
1471 if (inum)
1472 gfs2_inum_in(inum, (char *)&dent->de_inum);
1473 if (type)
1474 *type = be16_to_cpu(dent->de_type);
1475 brelse(bh);
1476 return 0;
1477 }
1478 return -ENOENT;
1479}
1480
1481static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1482{
1483 struct buffer_head *bh, *obh;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001484 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001485 struct gfs2_leaf *leaf, *oleaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001486 int error;
Steven Whitehousec7526662006-03-20 12:30:04 -05001487 u32 index;
1488 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001489
Steven Whitehousec7526662006-03-20 12:30:04 -05001490 index = name->hash >> (32 - ip->i_di.di_depth);
1491 error = get_first_leaf(ip, index, &obh);
1492 if (error)
1493 return error;
1494 do {
1495 oleaf = (struct gfs2_leaf *)obh->b_data;
1496 bn = be64_to_cpu(oleaf->lf_next);
1497 if (!bn)
1498 break;
1499 brelse(obh);
1500 error = get_leaf(ip, bn, &obh);
1501 if (error)
1502 return error;
1503 } while(1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001504
Steven Whitehousec7526662006-03-20 12:30:04 -05001505 gfs2_trans_add_bh(ip->i_gl, obh, 1);
1506
1507 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1508 if (!leaf) {
1509 brelse(obh);
1510 return -ENOSPC;
1511 }
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04001512 oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
Steven Whitehousec7526662006-03-20 12:30:04 -05001513 brelse(bh);
1514 brelse(obh);
1515
1516 error = gfs2_meta_inode_buffer(ip, &bh);
1517 if (error)
1518 return error;
1519 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1520 ip->i_di.di_blocks++;
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -05001521 gfs2_set_inode_blocks(&ip->i_inode);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001522 gfs2_dinode_out(ip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001523 brelse(bh);
1524 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001525}
1526
1527/**
1528 * gfs2_dir_add - Add new filename into directory
1529 * @dip: The GFS2 inode
1530 * @filename: The new name
1531 * @inode: The inode number of the entry
1532 * @type: The type of the entry
1533 *
1534 * Returns: 0 on success, error code on failure
1535 */
1536
Steven Whitehousec7526662006-03-20 12:30:04 -05001537int gfs2_dir_add(struct inode *inode, const struct qstr *name,
Al Viro629a21e2006-10-13 22:51:24 -04001538 const struct gfs2_inum_host *inum, unsigned type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001539{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001540 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001541 struct buffer_head *bh;
1542 struct gfs2_dirent *dent;
1543 struct gfs2_leaf *leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001544 int error;
1545
Steven Whitehousec7526662006-03-20 12:30:04 -05001546 while(1) {
1547 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
1548 &bh);
1549 if (dent) {
1550 if (IS_ERR(dent))
1551 return PTR_ERR(dent);
1552 dent = gfs2_init_dirent(inode, dent, name, bh);
1553 gfs2_inum_out(inum, (char *)&dent->de_inum);
1554 dent->de_type = cpu_to_be16(type);
1555 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
1556 leaf = (struct gfs2_leaf *)bh->b_data;
1557 leaf->lf_entries = cpu_to_be16(be16_to_cpu(leaf->lf_entries) + 1);
1558 }
1559 brelse(bh);
1560 error = gfs2_meta_inode_buffer(ip, &bh);
1561 if (error)
1562 break;
1563 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1564 ip->i_di.di_entries++;
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -05001565 ip->i_inode.i_mtime.tv_sec = ip->i_inode.i_ctime.tv_sec = get_seconds();
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001566 gfs2_dinode_out(ip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001567 brelse(bh);
1568 error = 0;
1569 break;
1570 }
1571 if (!(ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
1572 error = dir_make_exhash(inode);
1573 if (error)
1574 break;
1575 continue;
1576 }
1577 error = dir_split_leaf(inode, name);
1578 if (error == 0)
1579 continue;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001580 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001581 break;
1582 if (ip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) {
1583 error = dir_double_exhash(ip);
1584 if (error)
1585 break;
1586 error = dir_split_leaf(inode, name);
Steven Whitehousee90deff2006-03-29 19:02:15 -05001587 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001588 break;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001589 if (error == 0)
1590 continue;
Steven Whitehousec7526662006-03-20 12:30:04 -05001591 }
1592 error = dir_new_leaf(inode, name);
1593 if (!error)
1594 continue;
1595 error = -ENOSPC;
1596 break;
1597 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001598 return error;
1599}
1600
Steven Whitehousec7526662006-03-20 12:30:04 -05001601
David Teiglandb3b94fa2006-01-16 16:50:04 +00001602/**
1603 * gfs2_dir_del - Delete a directory entry
1604 * @dip: The GFS2 inode
1605 * @filename: The filename
1606 *
1607 * Returns: 0 on success, error code on failure
1608 */
1609
Steven Whitehousec7526662006-03-20 12:30:04 -05001610int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001611{
Steven Whitehousec7526662006-03-20 12:30:04 -05001612 struct gfs2_dirent *dent, *prev = NULL;
1613 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001614 int error;
1615
Steven Whitehousec7526662006-03-20 12:30:04 -05001616 /* Returns _either_ the entry (if its first in block) or the
1617 previous entry otherwise */
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001618 dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001619 if (!dent) {
1620 gfs2_consist_inode(dip);
1621 return -EIO;
1622 }
1623 if (IS_ERR(dent)) {
1624 gfs2_consist_inode(dip);
1625 return PTR_ERR(dent);
1626 }
1627 /* If not first in block, adjust pointers accordingly */
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001628 if (gfs2_dirent_find(dent, name, NULL) == 0) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001629 prev = dent;
1630 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1631 }
1632
1633 dirent_del(dip, bh, prev, dent);
1634 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1635 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1636 u16 entries = be16_to_cpu(leaf->lf_entries);
1637 if (!entries)
1638 gfs2_consist_inode(dip);
1639 leaf->lf_entries = cpu_to_be16(--entries);
Steven Whitehousec7526662006-03-20 12:30:04 -05001640 }
Steven Whitehouseed386502006-04-07 16:28:07 -04001641 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001642
1643 error = gfs2_meta_inode_buffer(dip, &bh);
1644 if (error)
1645 return error;
1646
1647 if (!dip->i_di.di_entries)
1648 gfs2_consist_inode(dip);
1649 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1650 dip->i_di.di_entries--;
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -05001651 dip->i_inode.i_mtime.tv_sec = dip->i_inode.i_ctime.tv_sec = get_seconds();
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001652 gfs2_dinode_out(dip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001653 brelse(bh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001654 mark_inode_dirty(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001655
1656 return error;
1657}
1658
David Teiglandb3b94fa2006-01-16 16:50:04 +00001659/**
1660 * gfs2_dir_mvino - Change inode number of directory entry
1661 * @dip: The GFS2 inode
1662 * @filename:
1663 * @new_inode:
1664 *
1665 * This routine changes the inode number of a directory entry. It's used
1666 * by rename to change ".." when a directory is moved.
1667 * Assumes a glock is held on dvp.
1668 *
1669 * Returns: errno
1670 */
1671
Steven Whitehousec7526662006-03-20 12:30:04 -05001672int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
Al Viro629a21e2006-10-13 22:51:24 -04001673 struct gfs2_inum_host *inum, unsigned int new_type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001674{
Steven Whitehousec7526662006-03-20 12:30:04 -05001675 struct buffer_head *bh;
1676 struct gfs2_dirent *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001677 int error;
1678
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001679 dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001680 if (!dent) {
1681 gfs2_consist_inode(dip);
1682 return -EIO;
1683 }
1684 if (IS_ERR(dent))
1685 return PTR_ERR(dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001686
Steven Whitehousec7526662006-03-20 12:30:04 -05001687 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1688 gfs2_inum_out(inum, (char *)&dent->de_inum);
1689 dent->de_type = cpu_to_be16(new_type);
1690
1691 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1692 brelse(bh);
1693 error = gfs2_meta_inode_buffer(dip, &bh);
1694 if (error)
1695 return error;
1696 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1697 }
1698
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -05001699 dip->i_inode.i_mtime.tv_sec = dip->i_inode.i_ctime.tv_sec = get_seconds();
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001700 gfs2_dinode_out(dip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001701 brelse(bh);
1702 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001703}
1704
1705/**
1706 * foreach_leaf - call a function for each leaf in a directory
1707 * @dip: the directory
1708 * @lc: the function to call for each each
1709 * @data: private data to pass to it
1710 *
1711 * Returns: errno
1712 */
1713
1714static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
1715{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001716 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001717 struct buffer_head *bh;
Steven Whitehousec7526662006-03-20 12:30:04 -05001718 struct gfs2_leaf *leaf;
Steven Whitehousecd915492006-09-04 12:49:07 -04001719 u32 hsize, len;
1720 u32 ht_offset, lp_offset, ht_offset_cur = -1;
1721 u32 index = 0;
Al Virob44b84d2006-10-14 10:46:30 -04001722 __be64 *lp;
Steven Whitehousecd915492006-09-04 12:49:07 -04001723 u64 leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001724 int error = 0;
1725
1726 hsize = 1 << dip->i_di.di_depth;
Steven Whitehousecd915492006-09-04 12:49:07 -04001727 if (hsize * sizeof(u64) != dip->i_di.di_size) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001728 gfs2_consist_inode(dip);
1729 return -EIO;
1730 }
1731
1732 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1733 if (!lp)
1734 return -ENOMEM;
1735
1736 while (index < hsize) {
1737 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1738 ht_offset = index - lp_offset;
1739
1740 if (ht_offset_cur != ht_offset) {
Steven Whitehousee13940b2006-01-30 13:31:50 +00001741 error = gfs2_dir_read_data(dip, (char *)lp,
Al Virob44b84d2006-10-14 10:46:30 -04001742 ht_offset * sizeof(__be64),
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001743 sdp->sd_hash_bsize, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001744 if (error != sdp->sd_hash_bsize) {
1745 if (error >= 0)
1746 error = -EIO;
1747 goto out;
1748 }
1749 ht_offset_cur = ht_offset;
1750 }
1751
1752 leaf_no = be64_to_cpu(lp[lp_offset]);
1753 if (leaf_no) {
1754 error = get_leaf(dip, leaf_no, &bh);
1755 if (error)
1756 goto out;
Steven Whitehousec7526662006-03-20 12:30:04 -05001757 leaf = (struct gfs2_leaf *)bh->b_data;
Steven Whitehousec7526662006-03-20 12:30:04 -05001758 len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth));
Steven Whitehouse634ee0b2006-07-17 09:32:37 -04001759 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001760
1761 error = lc(dip, index, len, leaf_no, data);
1762 if (error)
1763 goto out;
1764
1765 index = (index & ~(len - 1)) + len;
1766 } else
1767 index++;
1768 }
1769
1770 if (index != hsize) {
1771 gfs2_consist_inode(dip);
1772 error = -EIO;
1773 }
1774
Steven Whitehouse634ee0b2006-07-17 09:32:37 -04001775out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001776 kfree(lp);
1777
1778 return error;
1779}
1780
1781/**
1782 * leaf_dealloc - Deallocate a directory leaf
1783 * @dip: the directory
1784 * @index: the hash table offset in the directory
1785 * @len: the number of pointers to this leaf
1786 * @leaf_no: the leaf number
1787 * @data: not used
1788 *
1789 * Returns: errno
1790 */
1791
Steven Whitehousecd915492006-09-04 12:49:07 -04001792static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
1793 u64 leaf_no, void *data)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001794{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001795 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001796 struct gfs2_leaf *tmp_leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001797 struct gfs2_rgrp_list rlist;
1798 struct buffer_head *bh, *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001799 u64 blk, nblk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001800 unsigned int rg_blocks = 0, l_blocks = 0;
1801 char *ht;
Steven Whitehousecd915492006-09-04 12:49:07 -04001802 unsigned int x, size = len * sizeof(u64);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001803 int error;
1804
1805 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1806
1807 ht = kzalloc(size, GFP_KERNEL);
1808 if (!ht)
1809 return -ENOMEM;
1810
1811 gfs2_alloc_get(dip);
1812
1813 error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1814 if (error)
1815 goto out;
1816
1817 error = gfs2_rindex_hold(sdp, &dip->i_alloc.al_ri_gh);
1818 if (error)
1819 goto out_qs;
1820
1821 /* Count the number of leaves */
1822
Steven Whitehousec7526662006-03-20 12:30:04 -05001823 for (blk = leaf_no; blk; blk = nblk) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001824 error = get_leaf(dip, blk, &bh);
1825 if (error)
1826 goto out_rlist;
Steven Whitehousec7526662006-03-20 12:30:04 -05001827 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1828 nblk = be64_to_cpu(tmp_leaf->lf_next);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001829 brelse(bh);
1830
1831 gfs2_rlist_add(sdp, &rlist, blk);
1832 l_blocks++;
1833 }
1834
1835 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1836
1837 for (x = 0; x < rlist.rl_rgrps; x++) {
1838 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001839 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001840 rg_blocks += rgd->rd_ri.ri_length;
1841 }
1842
1843 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1844 if (error)
1845 goto out_rlist;
1846
1847 error = gfs2_trans_begin(sdp,
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001848 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
David Teiglandb3b94fa2006-01-16 16:50:04 +00001849 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1850 if (error)
1851 goto out_rg_gunlock;
1852
Steven Whitehousec7526662006-03-20 12:30:04 -05001853 for (blk = leaf_no; blk; blk = nblk) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001854 error = get_leaf(dip, blk, &bh);
1855 if (error)
1856 goto out_end_trans;
Steven Whitehousec7526662006-03-20 12:30:04 -05001857 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1858 nblk = be64_to_cpu(tmp_leaf->lf_next);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001859 brelse(bh);
1860
1861 gfs2_free_meta(dip, blk, 1);
1862
1863 if (!dip->i_di.di_blocks)
1864 gfs2_consist_inode(dip);
1865 dip->i_di.di_blocks--;
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -05001866 gfs2_set_inode_blocks(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001867 }
1868
Steven Whitehousecd915492006-09-04 12:49:07 -04001869 error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001870 if (error != size) {
1871 if (error >= 0)
1872 error = -EIO;
1873 goto out_end_trans;
1874 }
1875
1876 error = gfs2_meta_inode_buffer(dip, &dibh);
1877 if (error)
1878 goto out_end_trans;
1879
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001880 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001881 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001882 brelse(dibh);
1883
Steven Whitehousea91ea692006-09-04 12:04:26 -04001884out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001885 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001886out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001887 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001888out_rlist:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001889 gfs2_rlist_free(&rlist);
1890 gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001891out_qs:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001892 gfs2_quota_unhold(dip);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001893out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001894 gfs2_alloc_put(dip);
1895 kfree(ht);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001896 return error;
1897}
1898
1899/**
1900 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1901 * @dip: the directory
1902 *
1903 * Dealloc all on-disk directory leaves to FREEMETA state
1904 * Change on-disk inode type to "regular file"
1905 *
1906 * Returns: errno
1907 */
1908
1909int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
1910{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001911 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001912 struct buffer_head *bh;
1913 int error;
1914
1915 /* Dealloc on-disk leaves to FREEMETA state */
1916 error = foreach_leaf(dip, leaf_dealloc, NULL);
1917 if (error)
1918 return error;
1919
1920 /* Make this a regular file in case we crash.
1921 (We don't want to free these blocks a second time.) */
1922
1923 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1924 if (error)
1925 return error;
1926
1927 error = gfs2_meta_inode_buffer(dip, &bh);
1928 if (!error) {
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001929 gfs2_trans_add_bh(dip->i_gl, bh, 1);
Steven Whitehouse568f4c92006-02-27 12:00:42 -05001930 ((struct gfs2_dinode *)bh->b_data)->di_mode =
1931 cpu_to_be32(S_IFREG);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001932 brelse(bh);
1933 }
1934
1935 gfs2_trans_end(sdp);
1936
1937 return error;
1938}
1939
1940/**
1941 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
1942 * @ip: the file being written to
1943 * @filname: the filename that's going to be added
David Teiglandb3b94fa2006-01-16 16:50:04 +00001944 *
Steven Whitehousec7526662006-03-20 12:30:04 -05001945 * Returns: 1 if alloc required, 0 if not, -ve on error
David Teiglandb3b94fa2006-01-16 16:50:04 +00001946 */
1947
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04001948int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001949{
Steven Whitehousec7526662006-03-20 12:30:04 -05001950 struct gfs2_dirent *dent;
1951 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001952
Steven Whitehousec7526662006-03-20 12:30:04 -05001953 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
Steven Whitehouseed386502006-04-07 16:28:07 -04001954 if (!dent) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001955 return 1;
Steven Whitehouseed386502006-04-07 16:28:07 -04001956 }
Steven Whitehousec7526662006-03-20 12:30:04 -05001957 if (IS_ERR(dent))
1958 return PTR_ERR(dent);
1959 brelse(bh);
1960 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001961}
1962