blob: ead7df066853b8aecbb497e4a8efbf70d828c5c7 [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;
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,
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;
232 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
233
234 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
235 gfs2_dinode_out(&ip->i_di, dibh->b_data);
236 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);
318 }
319 if (!bh) {
320 error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, &bh);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000321 if (error)
322 goto fail;
323 }
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400324 error = gfs2_metatype_check(sdp, bh, GFS2_METATYPE_JD);
325 if (error) {
326 brelse(bh);
327 goto fail;
328 }
329 dblock++;
330 extlen--;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000331 memcpy(buf, bh->b_data + o, amount);
332 brelse(bh);
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400333 bh = NULL;
Steven Whitehouse899bb262006-08-01 15:28:57 -0400334 buf += amount;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000335 copied += amount;
336 lblock++;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000337 o = sizeof(struct gfs2_meta_header);
338 }
339
340 return copied;
341fail:
342 return (copied) ? copied : error;
343}
344
Steven Whitehousec7526662006-03-20 12:30:04 -0500345static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
346 const struct qstr *name, int ret)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000347{
Steven Whitehousec7526662006-03-20 12:30:04 -0500348 if (dent->de_inum.no_addr != 0 &&
349 be32_to_cpu(dent->de_hash) == name->hash &&
350 be16_to_cpu(dent->de_name_len) == name->len &&
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400351 memcmp(dent+1, name->name, name->len) == 0)
Steven Whitehousec7526662006-03-20 12:30:04 -0500352 return ret;
353 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000354}
355
Steven Whitehousec7526662006-03-20 12:30:04 -0500356static int gfs2_dirent_find(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500357 const struct qstr *name,
358 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500359{
360 return __gfs2_dirent_find(dent, name, 1);
361}
362
363static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500364 const struct qstr *name,
365 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500366{
367 return __gfs2_dirent_find(dent, name, 2);
368}
369
370/*
371 * name->name holds ptr to start of block.
372 * name->len holds size of block.
373 */
374static int gfs2_dirent_last(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500375 const struct qstr *name,
376 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500377{
378 const char *start = name->name;
379 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
380 if (name->len == (end - start))
381 return 1;
382 return 0;
383}
384
385static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500386 const struct qstr *name,
387 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500388{
389 unsigned required = GFS2_DIRENT_SIZE(name->len);
390 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
391 unsigned totlen = be16_to_cpu(dent->de_rec_len);
392
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500393 if (!dent->de_inum.no_addr)
394 actual = GFS2_DIRENT_SIZE(0);
Jan Engelhardtc5392122006-09-05 14:30:40 +0200395 if (totlen - actual >= required)
Steven Whitehousec7526662006-03-20 12:30:04 -0500396 return 1;
397 return 0;
398}
399
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500400struct dirent_gather {
401 const struct gfs2_dirent **pdent;
402 unsigned offset;
403};
404
405static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
406 const struct qstr *name,
407 void *opaque)
408{
409 struct dirent_gather *g = opaque;
410 if (dent->de_inum.no_addr) {
411 g->pdent[g->offset++] = dent;
412 }
413 return 0;
414}
415
Steven Whitehousec7526662006-03-20 12:30:04 -0500416/*
417 * Other possible things to check:
418 * - Inode located within filesystem size (and on valid block)
419 * - Valid directory entry type
420 * Not sure how heavy-weight we want to make this... could also check
421 * hash is correct for example, but that would take a lot of extra time.
422 * For now the most important thing is to check that the various sizes
423 * are correct.
424 */
425static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
426 unsigned int size, unsigned int len, int first)
427{
428 const char *msg = "gfs2_dirent too small";
429 if (unlikely(size < sizeof(struct gfs2_dirent)))
430 goto error;
431 msg = "gfs2_dirent misaligned";
432 if (unlikely(offset & 0x7))
433 goto error;
434 msg = "gfs2_dirent points beyond end of block";
435 if (unlikely(offset + size > len))
436 goto error;
437 msg = "zero inode number";
438 if (unlikely(!first && !dent->de_inum.no_addr))
439 goto error;
440 msg = "name length is greater than space in dirent";
441 if (dent->de_inum.no_addr &&
442 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
443 size))
444 goto error;
445 return 0;
446error:
447 printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
448 first ? "first in block" : "not first in block");
449 return -EIO;
450}
451
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500452static int gfs2_dirent_offset(const void *buf)
Steven Whitehousec7526662006-03-20 12:30:04 -0500453{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500454 const struct gfs2_meta_header *h = buf;
455 int offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500456
457 BUG_ON(buf == NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500458
Steven Whitehousee3167de2006-03-30 15:46:23 -0500459 switch(be32_to_cpu(h->mh_type)) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500460 case GFS2_METATYPE_LF:
461 offset = sizeof(struct gfs2_leaf);
462 break;
463 case GFS2_METATYPE_DI:
464 offset = sizeof(struct gfs2_dinode);
465 break;
466 default:
467 goto wrong_type;
468 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500469 return offset;
470wrong_type:
471 printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
Steven Whitehousee3167de2006-03-30 15:46:23 -0500472 be32_to_cpu(h->mh_type));
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500473 return -1;
474}
Steven Whitehousec7526662006-03-20 12:30:04 -0500475
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400476static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500477 unsigned int len, gfs2_dscan_t scan,
478 const struct qstr *name,
479 void *opaque)
480{
481 struct gfs2_dirent *dent, *prev;
482 unsigned offset;
483 unsigned size;
484 int ret = 0;
485
486 ret = gfs2_dirent_offset(buf);
487 if (ret < 0)
488 goto consist_inode;
489
490 offset = ret;
Steven Whitehousec7526662006-03-20 12:30:04 -0500491 prev = NULL;
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400492 dent = buf + offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500493 size = be16_to_cpu(dent->de_rec_len);
494 if (gfs2_check_dirent(dent, offset, size, len, 1))
495 goto consist_inode;
496 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500497 ret = scan(dent, name, opaque);
Steven Whitehousec7526662006-03-20 12:30:04 -0500498 if (ret)
499 break;
500 offset += size;
501 if (offset == len)
502 break;
503 prev = dent;
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400504 dent = buf + offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500505 size = be16_to_cpu(dent->de_rec_len);
506 if (gfs2_check_dirent(dent, offset, size, len, 0))
507 goto consist_inode;
508 } while(1);
509
510 switch(ret) {
511 case 0:
512 return NULL;
513 case 1:
514 return dent;
515 case 2:
516 return prev ? prev : dent;
517 default:
518 BUG_ON(ret > 0);
519 return ERR_PTR(ret);
520 }
521
Steven Whitehousec7526662006-03-20 12:30:04 -0500522consist_inode:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400523 gfs2_consist_inode(GFS2_I(inode));
Steven Whitehousec7526662006-03-20 12:30:04 -0500524 return ERR_PTR(-EIO);
525}
526
527
David Teiglandb3b94fa2006-01-16 16:50:04 +0000528/**
529 * dirent_first - Return the first dirent
530 * @dip: the directory
531 * @bh: The buffer
532 * @dent: Pointer to list of dirents
533 *
534 * return first dirent whether bh points to leaf or stuffed dinode
535 *
536 * Returns: IS_LEAF, IS_DINODE, or -errno
537 */
538
539static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
540 struct gfs2_dirent **dent)
541{
542 struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data;
543
Steven Whitehousee3167de2006-03-30 15:46:23 -0500544 if (be32_to_cpu(h->mh_type) == GFS2_METATYPE_LF) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400545 if (gfs2_meta_check(GFS2_SB(&dip->i_inode), bh))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000546 return -EIO;
547 *dent = (struct gfs2_dirent *)(bh->b_data +
548 sizeof(struct gfs2_leaf));
549 return IS_LEAF;
550 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400551 if (gfs2_metatype_check(GFS2_SB(&dip->i_inode), bh, GFS2_METATYPE_DI))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000552 return -EIO;
553 *dent = (struct gfs2_dirent *)(bh->b_data +
554 sizeof(struct gfs2_dinode));
555 return IS_DINODE;
556 }
557}
558
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400559static int dirent_check_reclen(struct gfs2_inode *dip,
560 const struct gfs2_dirent *d, const void *end_p)
561{
562 const void *ptr = d;
563 u16 rec_len = be16_to_cpu(d->de_rec_len);
564
565 if (unlikely(rec_len < sizeof(struct gfs2_dirent)))
566 goto broken;
567 ptr += rec_len;
568 if (ptr < end_p)
569 return rec_len;
570 if (ptr == end_p)
571 return -ENOENT;
572broken:
573 gfs2_consist_inode(dip);
574 return -EIO;
575}
576
David Teiglandb3b94fa2006-01-16 16:50:04 +0000577/**
578 * dirent_next - Next dirent
579 * @dip: the directory
580 * @bh: The buffer
581 * @dent: Pointer to list of dirents
582 *
583 * Returns: 0 on success, error code otherwise
584 */
585
586static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
587 struct gfs2_dirent **dent)
588{
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400589 struct gfs2_dirent *cur = *dent, *tmp;
590 char *bh_end = bh->b_data + bh->b_size;
591 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000592
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400593 ret = dirent_check_reclen(dip, cur, bh_end);
594 if (ret < 0)
595 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000596
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400597 tmp = (void *)cur + ret;
598 ret = dirent_check_reclen(dip, tmp, bh_end);
599 if (ret == -EIO)
600 return ret;
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000601
David Teiglandb3b94fa2006-01-16 16:50:04 +0000602 /* Only the first dent could ever have de_inum.no_addr == 0 */
603 if (!tmp->de_inum.no_addr) {
604 gfs2_consist_inode(dip);
605 return -EIO;
606 }
607
608 *dent = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000609 return 0;
610}
611
612/**
613 * dirent_del - Delete a dirent
614 * @dip: The GFS2 inode
615 * @bh: The buffer
616 * @prev: The previous dirent
617 * @cur: The current dirent
618 *
619 */
620
621static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
622 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
623{
Steven Whitehousecd915492006-09-04 12:49:07 -0400624 u16 cur_rec_len, prev_rec_len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000625
626 if (!cur->de_inum.no_addr) {
627 gfs2_consist_inode(dip);
628 return;
629 }
630
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000631 gfs2_trans_add_bh(dip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000632
633 /* If there is no prev entry, this is the first entry in the block.
634 The de_rec_len is already as big as it needs to be. Just zero
635 out the inode number and return. */
636
637 if (!prev) {
638 cur->de_inum.no_addr = 0; /* No endianess worries */
639 return;
640 }
641
642 /* Combine this dentry with the previous one. */
643
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000644 prev_rec_len = be16_to_cpu(prev->de_rec_len);
645 cur_rec_len = be16_to_cpu(cur->de_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000646
647 if ((char *)prev + prev_rec_len != (char *)cur)
648 gfs2_consist_inode(dip);
649 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
650 gfs2_consist_inode(dip);
651
652 prev_rec_len += cur_rec_len;
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000653 prev->de_rec_len = cpu_to_be16(prev_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000654}
655
Steven Whitehousec7526662006-03-20 12:30:04 -0500656/*
657 * Takes a dent from which to grab space as an argument. Returns the
658 * newly created dent.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000659 */
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400660static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
661 struct gfs2_dirent *dent,
662 const struct qstr *name,
663 struct buffer_head *bh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000664{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400665 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -0500666 struct gfs2_dirent *ndent;
667 unsigned offset = 0, totlen;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000668
Steven Whitehousec7526662006-03-20 12:30:04 -0500669 if (dent->de_inum.no_addr)
670 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
671 totlen = be16_to_cpu(dent->de_rec_len);
672 BUG_ON(offset + name->len > totlen);
673 gfs2_trans_add_bh(ip->i_gl, bh, 1);
674 ndent = (struct gfs2_dirent *)((char *)dent + offset);
675 dent->de_rec_len = cpu_to_be16(offset);
676 gfs2_qstr2dirent(name, totlen - offset, ndent);
677 return ndent;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000678}
679
Steven Whitehousec7526662006-03-20 12:30:04 -0500680static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
681 struct buffer_head *bh,
682 const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000683{
684 struct gfs2_dirent *dent;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400685 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500686 gfs2_dirent_find_space, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500687 if (!dent || IS_ERR(dent))
688 return dent;
689 return gfs2_init_dirent(inode, dent, name, bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000690}
691
Steven Whitehousecd915492006-09-04 12:49:07 -0400692static int get_leaf(struct gfs2_inode *dip, u64 leaf_no,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000693 struct buffer_head **bhp)
694{
695 int error;
696
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400697 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, bhp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400698 if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
699 /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000700 error = -EIO;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400701 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000702
703 return error;
704}
705
706/**
707 * get_leaf_nr - Get a leaf number associated with the index
708 * @dip: The GFS2 inode
709 * @index:
710 * @leaf_out:
711 *
712 * Returns: 0 on success, error code otherwise
713 */
714
Steven Whitehousecd915492006-09-04 12:49:07 -0400715static int get_leaf_nr(struct gfs2_inode *dip, u32 index,
716 u64 *leaf_out)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000717{
Steven Whitehousecd915492006-09-04 12:49:07 -0400718 u64 leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000719 int error;
720
Steven Whitehousee13940b2006-01-30 13:31:50 +0000721 error = gfs2_dir_read_data(dip, (char *)&leaf_no,
Steven Whitehousecd915492006-09-04 12:49:07 -0400722 index * sizeof(u64),
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400723 sizeof(u64), 0);
Steven Whitehousecd915492006-09-04 12:49:07 -0400724 if (error != sizeof(u64))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000725 return (error < 0) ? error : -EIO;
726
727 *leaf_out = be64_to_cpu(leaf_no);
728
729 return 0;
730}
731
Steven Whitehousecd915492006-09-04 12:49:07 -0400732static int get_first_leaf(struct gfs2_inode *dip, u32 index,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000733 struct buffer_head **bh_out)
734{
Steven Whitehousecd915492006-09-04 12:49:07 -0400735 u64 leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000736 int error;
737
738 error = get_leaf_nr(dip, index, &leaf_no);
739 if (!error)
740 error = get_leaf(dip, leaf_no, bh_out);
741
742 return error;
743}
744
Steven Whitehousec7526662006-03-20 12:30:04 -0500745static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
746 const struct qstr *name,
747 gfs2_dscan_t scan,
748 struct buffer_head **pbh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000749{
Steven Whitehousec7526662006-03-20 12:30:04 -0500750 struct buffer_head *bh;
751 struct gfs2_dirent *dent;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400752 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000753 int error;
754
Steven Whitehousec7526662006-03-20 12:30:04 -0500755 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
756 struct gfs2_leaf *leaf;
757 unsigned hsize = 1 << ip->i_di.di_depth;
758 unsigned index;
759 u64 ln;
760 if (hsize * sizeof(u64) != ip->i_di.di_size) {
761 gfs2_consist_inode(ip);
762 return ERR_PTR(-EIO);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000763 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400764
Steven Whitehousec7526662006-03-20 12:30:04 -0500765 index = name->hash >> (32 - ip->i_di.di_depth);
766 error = get_first_leaf(ip, index, &bh);
767 if (error)
768 return ERR_PTR(error);
769 do {
770 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500771 scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500772 if (dent)
773 goto got_dent;
774 leaf = (struct gfs2_leaf *)bh->b_data;
775 ln = be64_to_cpu(leaf->lf_next);
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400776 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500777 if (!ln)
778 break;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400779
Steven Whitehousec7526662006-03-20 12:30:04 -0500780 error = get_leaf(ip, ln, &bh);
781 } while(!error);
782
783 return error ? ERR_PTR(error) : NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000784 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000785
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400786
Steven Whitehousec7526662006-03-20 12:30:04 -0500787 error = gfs2_meta_inode_buffer(ip, &bh);
788 if (error)
789 return ERR_PTR(error);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500790 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500791got_dent:
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400792 if (unlikely(dent == NULL || IS_ERR(dent))) {
Steven Whitehouseed386502006-04-07 16:28:07 -0400793 brelse(bh);
794 bh = NULL;
795 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500796 *pbh = bh;
797 return dent;
798}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000799
Steven Whitehousec7526662006-03-20 12:30:04 -0500800static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
801{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400802 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -0500803 u64 bn = gfs2_alloc_meta(ip);
804 struct buffer_head *bh = gfs2_meta_new(ip->i_gl, bn);
805 struct gfs2_leaf *leaf;
806 struct gfs2_dirent *dent;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500807 struct qstr name = { .name = "", .len = 0, .hash = 0 };
Steven Whitehousec7526662006-03-20 12:30:04 -0500808 if (!bh)
809 return NULL;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400810
Steven Whitehousec7526662006-03-20 12:30:04 -0500811 gfs2_trans_add_bh(ip->i_gl, bh, 1);
812 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
813 leaf = (struct gfs2_leaf *)bh->b_data;
814 leaf->lf_depth = cpu_to_be16(depth);
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400815 leaf->lf_entries = 0;
Al Viroa2d7d022006-10-14 16:49:30 +0100816 leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400817 leaf->lf_next = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500818 memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
819 dent = (struct gfs2_dirent *)(leaf+1);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500820 gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
Steven Whitehousec7526662006-03-20 12:30:04 -0500821 *pbh = bh;
822 return leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000823}
824
825/**
826 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
827 * @dip: The GFS2 inode
828 *
829 * Returns: 0 on success, error code otherwise
830 */
831
Steven Whitehousec7526662006-03-20 12:30:04 -0500832static int dir_make_exhash(struct inode *inode)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000833{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400834 struct gfs2_inode *dip = GFS2_I(inode);
835 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000836 struct gfs2_dirent *dent;
Steven Whitehousec7526662006-03-20 12:30:04 -0500837 struct qstr args;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000838 struct buffer_head *bh, *dibh;
839 struct gfs2_leaf *leaf;
840 int y;
Steven Whitehousecd915492006-09-04 12:49:07 -0400841 u32 x;
842 u64 *lp, bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000843 int error;
844
845 error = gfs2_meta_inode_buffer(dip, &dibh);
846 if (error)
847 return error;
848
David Teiglandb3b94fa2006-01-16 16:50:04 +0000849 /* Turn over a new leaf */
850
Steven Whitehousec7526662006-03-20 12:30:04 -0500851 leaf = new_leaf(inode, &bh, 0);
852 if (!leaf)
853 return -ENOSPC;
854 bn = bh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000855
856 gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000857 leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries);
858
859 /* Copy dirents */
860
861 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
862 sizeof(struct gfs2_dinode));
863
864 /* Find last entry */
865
866 x = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500867 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
868 sizeof(struct gfs2_leaf);
869 args.name = bh->b_data;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400870 dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500871 gfs2_dirent_last, &args, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500872 if (!dent) {
873 brelse(bh);
874 brelse(dibh);
875 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000876 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500877 if (IS_ERR(dent)) {
878 brelse(bh);
879 brelse(dibh);
880 return PTR_ERR(dent);
881 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000882
883 /* Adjust the last dirent's record length
884 (Remember that dent still points to the last entry.) */
885
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000886 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000887 sizeof(struct gfs2_dinode) -
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000888 sizeof(struct gfs2_leaf));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000889
890 brelse(bh);
891
892 /* We're done with the new leaf block, now setup the new
893 hash table. */
894
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000895 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000896 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
897
Steven Whitehousecd915492006-09-04 12:49:07 -0400898 lp = (u64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000899
900 for (x = sdp->sd_hash_ptrs; x--; lp++)
901 *lp = cpu_to_be64(bn);
902
903 dip->i_di.di_size = sdp->sd_sb.sb_bsize / 2;
904 dip->i_di.di_blocks++;
905 dip->i_di.di_flags |= GFS2_DIF_EXHASH;
906 dip->i_di.di_payload_format = 0;
907
908 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
909 dip->i_di.di_depth = y;
910
911 gfs2_dinode_out(&dip->i_di, dibh->b_data);
912
913 brelse(dibh);
914
915 return 0;
916}
917
918/**
919 * dir_split_leaf - Split a leaf block into two
920 * @dip: The GFS2 inode
921 * @index:
922 * @leaf_no:
923 *
924 * Returns: 0 on success, error code on failure
925 */
926
Steven Whitehousec7526662006-03-20 12:30:04 -0500927static int dir_split_leaf(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000928{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400929 struct gfs2_inode *dip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000930 struct buffer_head *nbh, *obh, *dibh;
931 struct gfs2_leaf *nleaf, *oleaf;
Steven Whitehouse4da3c642006-07-11 13:19:13 -0400932 struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
Steven Whitehousecd915492006-09-04 12:49:07 -0400933 u32 start, len, half_len, divider;
934 u64 bn, *lp, leaf_no;
935 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. */
Steven Whitehousecd915492006-09-04 12:49:07 -0400979 lp = kmalloc(half_len * sizeof(u64), 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++;
1042 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1043 brelse(dibh);
1044 }
1045
1046 brelse(obh);
1047 brelse(nbh);
1048
1049 return error;
1050
Steven Whitehousee90deff2006-03-29 19:02:15 -05001051fail_lpfree:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001052 kfree(lp);
1053
Steven Whitehousee90deff2006-03-29 19:02:15 -05001054fail_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001055 brelse(obh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001056 brelse(nbh);
1057 return error;
1058}
1059
1060/**
1061 * dir_double_exhash - Double size of ExHash table
1062 * @dip: The GFS2 dinode
1063 *
1064 * Returns: 0 on success, error code on failure
1065 */
1066
1067static int dir_double_exhash(struct gfs2_inode *dip)
1068{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001069 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001070 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001071 u32 hsize;
1072 u64 *buf;
1073 u64 *from, *to;
1074 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001075 int x;
1076 int error = 0;
1077
1078 hsize = 1 << dip->i_di.di_depth;
Steven Whitehousecd915492006-09-04 12:49:07 -04001079 if (hsize * sizeof(u64) != dip->i_di.di_size) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001080 gfs2_consist_inode(dip);
1081 return -EIO;
1082 }
1083
1084 /* Allocate both the "from" and "to" buffers in one big chunk */
1085
1086 buf = kcalloc(3, sdp->sd_hash_bsize, GFP_KERNEL | __GFP_NOFAIL);
1087
1088 for (block = dip->i_di.di_size >> sdp->sd_hash_bsize_shift; block--;) {
Steven Whitehousee13940b2006-01-30 13:31:50 +00001089 error = gfs2_dir_read_data(dip, (char *)buf,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001090 block * sdp->sd_hash_bsize,
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001091 sdp->sd_hash_bsize, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001092 if (error != sdp->sd_hash_bsize) {
1093 if (error >= 0)
1094 error = -EIO;
1095 goto fail;
1096 }
1097
1098 from = buf;
Steven Whitehousecd915492006-09-04 12:49:07 -04001099 to = (u64 *)((char *)buf + sdp->sd_hash_bsize);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001100
1101 for (x = sdp->sd_hash_ptrs; x--; from++) {
1102 *to++ = *from; /* No endianess worries */
1103 *to++ = *from;
1104 }
1105
Steven Whitehousee13940b2006-01-30 13:31:50 +00001106 error = gfs2_dir_write_data(dip,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001107 (char *)buf + sdp->sd_hash_bsize,
1108 block * sdp->sd_sb.sb_bsize,
1109 sdp->sd_sb.sb_bsize);
1110 if (error != sdp->sd_sb.sb_bsize) {
1111 if (error >= 0)
1112 error = -EIO;
1113 goto fail;
1114 }
1115 }
1116
1117 kfree(buf);
1118
1119 error = gfs2_meta_inode_buffer(dip, &dibh);
1120 if (!gfs2_assert_withdraw(sdp, !error)) {
1121 dip->i_di.di_depth++;
1122 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1123 brelse(dibh);
1124 }
1125
1126 return error;
1127
Steven Whitehousea91ea692006-09-04 12:04:26 -04001128fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001129 kfree(buf);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001130 return error;
1131}
1132
1133/**
1134 * compare_dents - compare directory entries by hash value
1135 * @a: first dent
1136 * @b: second dent
1137 *
1138 * When comparing the hash entries of @a to @b:
1139 * gt: returns 1
1140 * lt: returns -1
1141 * eq: returns 0
1142 */
1143
1144static int compare_dents(const void *a, const void *b)
1145{
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001146 const struct gfs2_dirent *dent_a, *dent_b;
Steven Whitehousecd915492006-09-04 12:49:07 -04001147 u32 hash_a, hash_b;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001148 int ret = 0;
1149
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001150 dent_a = *(const struct gfs2_dirent **)a;
Steven Whitehousec7526662006-03-20 12:30:04 -05001151 hash_a = be32_to_cpu(dent_a->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001152
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001153 dent_b = *(const struct gfs2_dirent **)b;
Steven Whitehousec7526662006-03-20 12:30:04 -05001154 hash_b = be32_to_cpu(dent_b->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001155
1156 if (hash_a > hash_b)
1157 ret = 1;
1158 else if (hash_a < hash_b)
1159 ret = -1;
1160 else {
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001161 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1162 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001163
1164 if (len_a > len_b)
1165 ret = 1;
1166 else if (len_a < len_b)
1167 ret = -1;
1168 else
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001169 ret = memcmp(dent_a + 1, dent_b + 1, len_a);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001170 }
1171
1172 return ret;
1173}
1174
1175/**
1176 * do_filldir_main - read out directory entries
1177 * @dip: The GFS2 inode
1178 * @offset: The offset in the file to read from
1179 * @opaque: opaque data to pass to filldir
1180 * @filldir: The function to pass entries to
1181 * @darr: an array of struct gfs2_dirent pointers to read
1182 * @entries: the number of entries in darr
1183 * @copied: pointer to int that's non-zero if a entry has been copied out
1184 *
1185 * Jump through some hoops to make sure that if there are hash collsions,
1186 * they are read out at the beginning of a buffer. We want to minimize
1187 * the possibility that they will fall into different readdir buffers or
1188 * that someone will want to seek to that location.
1189 *
1190 * Returns: errno, >0 on exception from filldir
1191 */
1192
Steven Whitehousecd915492006-09-04 12:49:07 -04001193static int do_filldir_main(struct gfs2_inode *dip, u64 *offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001194 void *opaque, gfs2_filldir_t filldir,
Steven Whitehousecd915492006-09-04 12:49:07 -04001195 const struct gfs2_dirent **darr, u32 entries,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001196 int *copied)
1197{
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001198 const struct gfs2_dirent *dent, *dent_next;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001199 struct gfs2_inum inum;
Steven Whitehousecd915492006-09-04 12:49:07 -04001200 u64 off, off_next;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001201 unsigned int x, y;
1202 int run = 0;
1203 int error = 0;
1204
1205 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1206
1207 dent_next = darr[0];
1208 off_next = be32_to_cpu(dent_next->de_hash);
1209 off_next = gfs2_disk_hash2offset(off_next);
1210
1211 for (x = 0, y = 1; x < entries; x++, y++) {
1212 dent = dent_next;
1213 off = off_next;
1214
1215 if (y < entries) {
1216 dent_next = darr[y];
1217 off_next = be32_to_cpu(dent_next->de_hash);
1218 off_next = gfs2_disk_hash2offset(off_next);
1219
1220 if (off < *offset)
1221 continue;
1222 *offset = off;
1223
1224 if (off_next == off) {
1225 if (*copied && !run)
1226 return 1;
1227 run = 1;
1228 } else
1229 run = 0;
1230 } else {
1231 if (off < *offset)
1232 continue;
1233 *offset = off;
1234 }
1235
1236 gfs2_inum_in(&inum, (char *)&dent->de_inum);
1237
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001238 error = filldir(opaque, (const char *)(dent + 1),
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001239 be16_to_cpu(dent->de_name_len),
David Teiglandb3b94fa2006-01-16 16:50:04 +00001240 off, &inum,
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001241 be16_to_cpu(dent->de_type));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001242 if (error)
1243 return 1;
1244
1245 *copied = 1;
1246 }
1247
1248 /* Increment the *offset by one, so the next time we come into the
1249 do_filldir fxn, we get the next entry instead of the last one in the
1250 current leaf */
1251
1252 (*offset)++;
1253
1254 return 0;
1255}
1256
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001257static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
1258 gfs2_filldir_t filldir, int *copied,
1259 unsigned *depth, u64 leaf_no)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001260{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001261 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001262 struct buffer_head *bh;
1263 struct gfs2_leaf *lf;
1264 unsigned entries = 0;
1265 unsigned leaves = 0;
1266 const struct gfs2_dirent **darr, *dent;
1267 struct dirent_gather g;
1268 struct buffer_head **larr;
1269 int leaf = 0;
1270 int error, i;
1271 u64 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001272
David Teiglandb3b94fa2006-01-16 16:50:04 +00001273 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001274 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001275 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001276 goto out;
1277 lf = (struct gfs2_leaf *)bh->b_data;
1278 if (leaves == 0)
1279 *depth = be16_to_cpu(lf->lf_depth);
1280 entries += be16_to_cpu(lf->lf_entries);
1281 leaves++;
1282 lfn = be64_to_cpu(lf->lf_next);
1283 brelse(bh);
1284 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001285
1286 if (!entries)
1287 return 0;
1288
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001289 error = -ENOMEM;
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001290 larr = vmalloc((leaves + entries) * sizeof(void *));
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001291 if (!larr)
1292 goto out;
1293 darr = (const struct gfs2_dirent **)(larr + leaves);
1294 g.pdent = darr;
1295 g.offset = 0;
1296 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001297
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001298 do {
1299 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001300 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001301 goto out_kfree;
1302 lf = (struct gfs2_leaf *)bh->b_data;
1303 lfn = be64_to_cpu(lf->lf_next);
1304 if (lf->lf_entries) {
1305 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1306 gfs2_dirent_gather, NULL, &g);
1307 error = PTR_ERR(dent);
1308 if (IS_ERR(dent)) {
1309 goto out_kfree;
1310 }
1311 error = 0;
1312 larr[leaf++] = bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001313 } else {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001314 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001315 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001316 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001317
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001318 error = do_filldir_main(ip, offset, opaque, filldir, darr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001319 entries, copied);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001320out_kfree:
1321 for(i = 0; i < leaf; i++)
1322 brelse(larr[i]);
Steven Whitehousefe1bded2006-04-18 10:09:15 -04001323 vfree(larr);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001324out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001325 return error;
1326}
1327
1328/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001329 * dir_e_read - Reads the entries from a directory into a filldir buffer
1330 * @dip: dinode pointer
1331 * @offset: the hash of the last entry read shifted to the right once
1332 * @opaque: buffer for the filldir function to fill
1333 * @filldir: points to the filldir function to use
1334 *
1335 * Returns: errno
1336 */
1337
Steven Whitehousecd915492006-09-04 12:49:07 -04001338static int dir_e_read(struct inode *inode, u64 *offset, void *opaque,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001339 gfs2_filldir_t filldir)
1340{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001341 struct gfs2_inode *dip = GFS2_I(inode);
1342 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehousecd915492006-09-04 12:49:07 -04001343 u32 hsize, len = 0;
1344 u32 ht_offset, lp_offset, ht_offset_cur = -1;
1345 u32 hash, index;
1346 u64 *lp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001347 int copied = 0;
1348 int error = 0;
Steven Whitehouse4da3c642006-07-11 13:19:13 -04001349 unsigned depth = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001350
1351 hsize = 1 << dip->i_di.di_depth;
Steven Whitehousecd915492006-09-04 12:49:07 -04001352 if (hsize * sizeof(u64) != dip->i_di.di_size) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001353 gfs2_consist_inode(dip);
1354 return -EIO;
1355 }
1356
1357 hash = gfs2_dir_offset2hash(*offset);
1358 index = hash >> (32 - dip->i_di.di_depth);
1359
1360 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1361 if (!lp)
1362 return -ENOMEM;
1363
1364 while (index < hsize) {
1365 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1366 ht_offset = index - lp_offset;
1367
1368 if (ht_offset_cur != ht_offset) {
Steven Whitehousee13940b2006-01-30 13:31:50 +00001369 error = gfs2_dir_read_data(dip, (char *)lp,
Steven Whitehousecd915492006-09-04 12:49:07 -04001370 ht_offset * sizeof(u64),
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001371 sdp->sd_hash_bsize, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001372 if (error != sdp->sd_hash_bsize) {
1373 if (error >= 0)
1374 error = -EIO;
1375 goto out;
1376 }
1377 ht_offset_cur = ht_offset;
1378 }
1379
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001380 error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
1381 &copied, &depth,
1382 be64_to_cpu(lp[lp_offset]));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001383 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001384 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001385
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001386 len = 1 << (dip->i_di.di_depth - depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001387 index = (index & ~(len - 1)) + len;
1388 }
1389
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001390out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001391 kfree(lp);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001392 if (error > 0)
1393 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001394 return error;
1395}
1396
Steven Whitehousecd915492006-09-04 12:49:07 -04001397int gfs2_dir_read(struct inode *inode, u64 *offset, void *opaque,
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001398 gfs2_filldir_t filldir)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001399{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001400 struct gfs2_inode *dip = GFS2_I(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001401 struct dirent_gather g;
1402 const struct gfs2_dirent **darr, *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001403 struct buffer_head *dibh;
1404 int copied = 0;
1405 int error;
1406
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001407 if (!dip->i_di.di_entries)
1408 return 0;
1409
1410 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
1411 return dir_e_read(inode, offset, opaque, filldir);
1412
David Teiglandb3b94fa2006-01-16 16:50:04 +00001413 if (!gfs2_is_stuffed(dip)) {
1414 gfs2_consist_inode(dip);
1415 return -EIO;
1416 }
1417
David Teiglandb3b94fa2006-01-16 16:50:04 +00001418 error = gfs2_meta_inode_buffer(dip, &dibh);
1419 if (error)
1420 return error;
1421
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001422 error = -ENOMEM;
1423 darr = kmalloc(dip->i_di.di_entries * sizeof(struct gfs2_dirent *),
1424 GFP_KERNEL);
1425 if (darr) {
1426 g.pdent = darr;
1427 g.offset = 0;
1428 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1429 gfs2_dirent_gather, NULL, &g);
1430 if (IS_ERR(dent)) {
1431 error = PTR_ERR(dent);
1432 goto out;
1433 }
1434 error = do_filldir_main(dip, offset, opaque, filldir, darr,
1435 dip->i_di.di_entries, &copied);
1436out:
1437 kfree(darr);
1438 }
1439
David Teiglandb3b94fa2006-01-16 16:50:04 +00001440 if (error > 0)
1441 error = 0;
1442
1443 brelse(dibh);
1444
1445 return error;
1446}
1447
David Teiglandb3b94fa2006-01-16 16:50:04 +00001448/**
1449 * gfs2_dir_search - Search a directory
1450 * @dip: The GFS2 inode
1451 * @filename:
1452 * @inode:
1453 *
1454 * This routine searches a directory for a file or another directory.
1455 * Assumes a glock is held on dip.
1456 *
1457 * Returns: errno
1458 */
1459
Steven Whitehousec7526662006-03-20 12:30:04 -05001460int gfs2_dir_search(struct inode *dir, const struct qstr *name,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001461 struct gfs2_inum *inum, unsigned int *type)
1462{
Steven Whitehousec7526662006-03-20 12:30:04 -05001463 struct buffer_head *bh;
1464 struct gfs2_dirent *dent;
1465
1466 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1467 if (dent) {
1468 if (IS_ERR(dent))
1469 return PTR_ERR(dent);
1470 if (inum)
1471 gfs2_inum_in(inum, (char *)&dent->de_inum);
1472 if (type)
1473 *type = be16_to_cpu(dent->de_type);
1474 brelse(bh);
1475 return 0;
1476 }
1477 return -ENOENT;
1478}
1479
1480static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1481{
1482 struct buffer_head *bh, *obh;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001483 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001484 struct gfs2_leaf *leaf, *oleaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001485 int error;
Steven Whitehousec7526662006-03-20 12:30:04 -05001486 u32 index;
1487 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001488
Steven Whitehousec7526662006-03-20 12:30:04 -05001489 index = name->hash >> (32 - ip->i_di.di_depth);
1490 error = get_first_leaf(ip, index, &obh);
1491 if (error)
1492 return error;
1493 do {
1494 oleaf = (struct gfs2_leaf *)obh->b_data;
1495 bn = be64_to_cpu(oleaf->lf_next);
1496 if (!bn)
1497 break;
1498 brelse(obh);
1499 error = get_leaf(ip, bn, &obh);
1500 if (error)
1501 return error;
1502 } while(1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001503
Steven Whitehousec7526662006-03-20 12:30:04 -05001504 gfs2_trans_add_bh(ip->i_gl, obh, 1);
1505
1506 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1507 if (!leaf) {
1508 brelse(obh);
1509 return -ENOSPC;
1510 }
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04001511 oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
Steven Whitehousec7526662006-03-20 12:30:04 -05001512 brelse(bh);
1513 brelse(obh);
1514
1515 error = gfs2_meta_inode_buffer(ip, &bh);
1516 if (error)
1517 return error;
1518 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1519 ip->i_di.di_blocks++;
1520 gfs2_dinode_out(&ip->i_di, bh->b_data);
1521 brelse(bh);
1522 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001523}
1524
1525/**
1526 * gfs2_dir_add - Add new filename into directory
1527 * @dip: The GFS2 inode
1528 * @filename: The new name
1529 * @inode: The inode number of the entry
1530 * @type: The type of the entry
1531 *
1532 * Returns: 0 on success, error code on failure
1533 */
1534
Steven Whitehousec7526662006-03-20 12:30:04 -05001535int gfs2_dir_add(struct inode *inode, const struct qstr *name,
1536 const struct gfs2_inum *inum, unsigned type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001537{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001538 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001539 struct buffer_head *bh;
1540 struct gfs2_dirent *dent;
1541 struct gfs2_leaf *leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001542 int error;
1543
Steven Whitehousec7526662006-03-20 12:30:04 -05001544 while(1) {
1545 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
1546 &bh);
1547 if (dent) {
1548 if (IS_ERR(dent))
1549 return PTR_ERR(dent);
1550 dent = gfs2_init_dirent(inode, dent, name, bh);
1551 gfs2_inum_out(inum, (char *)&dent->de_inum);
1552 dent->de_type = cpu_to_be16(type);
1553 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
1554 leaf = (struct gfs2_leaf *)bh->b_data;
1555 leaf->lf_entries = cpu_to_be16(be16_to_cpu(leaf->lf_entries) + 1);
1556 }
1557 brelse(bh);
1558 error = gfs2_meta_inode_buffer(ip, &bh);
1559 if (error)
1560 break;
1561 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1562 ip->i_di.di_entries++;
1563 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
1564 gfs2_dinode_out(&ip->i_di, bh->b_data);
1565 brelse(bh);
1566 error = 0;
1567 break;
1568 }
1569 if (!(ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
1570 error = dir_make_exhash(inode);
1571 if (error)
1572 break;
1573 continue;
1574 }
1575 error = dir_split_leaf(inode, name);
1576 if (error == 0)
1577 continue;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001578 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001579 break;
1580 if (ip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) {
1581 error = dir_double_exhash(ip);
1582 if (error)
1583 break;
1584 error = dir_split_leaf(inode, name);
Steven Whitehousee90deff2006-03-29 19:02:15 -05001585 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001586 break;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001587 if (error == 0)
1588 continue;
Steven Whitehousec7526662006-03-20 12:30:04 -05001589 }
1590 error = dir_new_leaf(inode, name);
1591 if (!error)
1592 continue;
1593 error = -ENOSPC;
1594 break;
1595 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001596 return error;
1597}
1598
Steven Whitehousec7526662006-03-20 12:30:04 -05001599
David Teiglandb3b94fa2006-01-16 16:50:04 +00001600/**
1601 * gfs2_dir_del - Delete a directory entry
1602 * @dip: The GFS2 inode
1603 * @filename: The filename
1604 *
1605 * Returns: 0 on success, error code on failure
1606 */
1607
Steven Whitehousec7526662006-03-20 12:30:04 -05001608int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001609{
Steven Whitehousec7526662006-03-20 12:30:04 -05001610 struct gfs2_dirent *dent, *prev = NULL;
1611 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001612 int error;
1613
Steven Whitehousec7526662006-03-20 12:30:04 -05001614 /* Returns _either_ the entry (if its first in block) or the
1615 previous entry otherwise */
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001616 dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001617 if (!dent) {
1618 gfs2_consist_inode(dip);
1619 return -EIO;
1620 }
1621 if (IS_ERR(dent)) {
1622 gfs2_consist_inode(dip);
1623 return PTR_ERR(dent);
1624 }
1625 /* If not first in block, adjust pointers accordingly */
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001626 if (gfs2_dirent_find(dent, name, NULL) == 0) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001627 prev = dent;
1628 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1629 }
1630
1631 dirent_del(dip, bh, prev, dent);
1632 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1633 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1634 u16 entries = be16_to_cpu(leaf->lf_entries);
1635 if (!entries)
1636 gfs2_consist_inode(dip);
1637 leaf->lf_entries = cpu_to_be16(--entries);
Steven Whitehousec7526662006-03-20 12:30:04 -05001638 }
Steven Whitehouseed386502006-04-07 16:28:07 -04001639 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001640
1641 error = gfs2_meta_inode_buffer(dip, &bh);
1642 if (error)
1643 return error;
1644
1645 if (!dip->i_di.di_entries)
1646 gfs2_consist_inode(dip);
1647 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1648 dip->i_di.di_entries--;
1649 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1650 gfs2_dinode_out(&dip->i_di, bh->b_data);
1651 brelse(bh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001652 mark_inode_dirty(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001653
1654 return error;
1655}
1656
David Teiglandb3b94fa2006-01-16 16:50:04 +00001657/**
1658 * gfs2_dir_mvino - Change inode number of directory entry
1659 * @dip: The GFS2 inode
1660 * @filename:
1661 * @new_inode:
1662 *
1663 * This routine changes the inode number of a directory entry. It's used
1664 * by rename to change ".." when a directory is moved.
1665 * Assumes a glock is held on dvp.
1666 *
1667 * Returns: errno
1668 */
1669
Steven Whitehousec7526662006-03-20 12:30:04 -05001670int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001671 struct gfs2_inum *inum, unsigned int new_type)
1672{
Steven Whitehousec7526662006-03-20 12:30:04 -05001673 struct buffer_head *bh;
1674 struct gfs2_dirent *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001675 int error;
1676
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001677 dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001678 if (!dent) {
1679 gfs2_consist_inode(dip);
1680 return -EIO;
1681 }
1682 if (IS_ERR(dent))
1683 return PTR_ERR(dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001684
Steven Whitehousec7526662006-03-20 12:30:04 -05001685 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1686 gfs2_inum_out(inum, (char *)&dent->de_inum);
1687 dent->de_type = cpu_to_be16(new_type);
1688
1689 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1690 brelse(bh);
1691 error = gfs2_meta_inode_buffer(dip, &bh);
1692 if (error)
1693 return error;
1694 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1695 }
1696
1697 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1698 gfs2_dinode_out(&dip->i_di, bh->b_data);
1699 brelse(bh);
1700 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001701}
1702
1703/**
1704 * foreach_leaf - call a function for each leaf in a directory
1705 * @dip: the directory
1706 * @lc: the function to call for each each
1707 * @data: private data to pass to it
1708 *
1709 * Returns: errno
1710 */
1711
1712static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
1713{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001714 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001715 struct buffer_head *bh;
Steven Whitehousec7526662006-03-20 12:30:04 -05001716 struct gfs2_leaf *leaf;
Steven Whitehousecd915492006-09-04 12:49:07 -04001717 u32 hsize, len;
1718 u32 ht_offset, lp_offset, ht_offset_cur = -1;
1719 u32 index = 0;
1720 u64 *lp;
1721 u64 leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001722 int error = 0;
1723
1724 hsize = 1 << dip->i_di.di_depth;
Steven Whitehousecd915492006-09-04 12:49:07 -04001725 if (hsize * sizeof(u64) != dip->i_di.di_size) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001726 gfs2_consist_inode(dip);
1727 return -EIO;
1728 }
1729
1730 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1731 if (!lp)
1732 return -ENOMEM;
1733
1734 while (index < hsize) {
1735 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1736 ht_offset = index - lp_offset;
1737
1738 if (ht_offset_cur != ht_offset) {
Steven Whitehousee13940b2006-01-30 13:31:50 +00001739 error = gfs2_dir_read_data(dip, (char *)lp,
Steven Whitehousecd915492006-09-04 12:49:07 -04001740 ht_offset * sizeof(u64),
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001741 sdp->sd_hash_bsize, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001742 if (error != sdp->sd_hash_bsize) {
1743 if (error >= 0)
1744 error = -EIO;
1745 goto out;
1746 }
1747 ht_offset_cur = ht_offset;
1748 }
1749
1750 leaf_no = be64_to_cpu(lp[lp_offset]);
1751 if (leaf_no) {
1752 error = get_leaf(dip, leaf_no, &bh);
1753 if (error)
1754 goto out;
Steven Whitehousec7526662006-03-20 12:30:04 -05001755 leaf = (struct gfs2_leaf *)bh->b_data;
Steven Whitehousec7526662006-03-20 12:30:04 -05001756 len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth));
Steven Whitehouse634ee0b2006-07-17 09:32:37 -04001757 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001758
1759 error = lc(dip, index, len, leaf_no, data);
1760 if (error)
1761 goto out;
1762
1763 index = (index & ~(len - 1)) + len;
1764 } else
1765 index++;
1766 }
1767
1768 if (index != hsize) {
1769 gfs2_consist_inode(dip);
1770 error = -EIO;
1771 }
1772
Steven Whitehouse634ee0b2006-07-17 09:32:37 -04001773out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001774 kfree(lp);
1775
1776 return error;
1777}
1778
1779/**
1780 * leaf_dealloc - Deallocate a directory leaf
1781 * @dip: the directory
1782 * @index: the hash table offset in the directory
1783 * @len: the number of pointers to this leaf
1784 * @leaf_no: the leaf number
1785 * @data: not used
1786 *
1787 * Returns: errno
1788 */
1789
Steven Whitehousecd915492006-09-04 12:49:07 -04001790static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
1791 u64 leaf_no, void *data)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001792{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001793 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001794 struct gfs2_leaf *tmp_leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001795 struct gfs2_rgrp_list rlist;
1796 struct buffer_head *bh, *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001797 u64 blk, nblk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001798 unsigned int rg_blocks = 0, l_blocks = 0;
1799 char *ht;
Steven Whitehousecd915492006-09-04 12:49:07 -04001800 unsigned int x, size = len * sizeof(u64);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001801 int error;
1802
1803 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1804
1805 ht = kzalloc(size, GFP_KERNEL);
1806 if (!ht)
1807 return -ENOMEM;
1808
1809 gfs2_alloc_get(dip);
1810
1811 error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1812 if (error)
1813 goto out;
1814
1815 error = gfs2_rindex_hold(sdp, &dip->i_alloc.al_ri_gh);
1816 if (error)
1817 goto out_qs;
1818
1819 /* Count the number of leaves */
1820
Steven Whitehousec7526662006-03-20 12:30:04 -05001821 for (blk = leaf_no; blk; blk = nblk) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001822 error = get_leaf(dip, blk, &bh);
1823 if (error)
1824 goto out_rlist;
Steven Whitehousec7526662006-03-20 12:30:04 -05001825 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1826 nblk = be64_to_cpu(tmp_leaf->lf_next);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001827 brelse(bh);
1828
1829 gfs2_rlist_add(sdp, &rlist, blk);
1830 l_blocks++;
1831 }
1832
1833 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1834
1835 for (x = 0; x < rlist.rl_rgrps; x++) {
1836 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001837 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001838 rg_blocks += rgd->rd_ri.ri_length;
1839 }
1840
1841 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1842 if (error)
1843 goto out_rlist;
1844
1845 error = gfs2_trans_begin(sdp,
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001846 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
David Teiglandb3b94fa2006-01-16 16:50:04 +00001847 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1848 if (error)
1849 goto out_rg_gunlock;
1850
Steven Whitehousec7526662006-03-20 12:30:04 -05001851 for (blk = leaf_no; blk; blk = nblk) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001852 error = get_leaf(dip, blk, &bh);
1853 if (error)
1854 goto out_end_trans;
Steven Whitehousec7526662006-03-20 12:30:04 -05001855 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1856 nblk = be64_to_cpu(tmp_leaf->lf_next);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001857 brelse(bh);
1858
1859 gfs2_free_meta(dip, blk, 1);
1860
1861 if (!dip->i_di.di_blocks)
1862 gfs2_consist_inode(dip);
1863 dip->i_di.di_blocks--;
1864 }
1865
Steven Whitehousecd915492006-09-04 12:49:07 -04001866 error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001867 if (error != size) {
1868 if (error >= 0)
1869 error = -EIO;
1870 goto out_end_trans;
1871 }
1872
1873 error = gfs2_meta_inode_buffer(dip, &dibh);
1874 if (error)
1875 goto out_end_trans;
1876
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001877 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001878 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1879 brelse(dibh);
1880
Steven Whitehousea91ea692006-09-04 12:04:26 -04001881out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001882 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001883out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001884 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001885out_rlist:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001886 gfs2_rlist_free(&rlist);
1887 gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001888out_qs:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001889 gfs2_quota_unhold(dip);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001890out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001891 gfs2_alloc_put(dip);
1892 kfree(ht);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001893 return error;
1894}
1895
1896/**
1897 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1898 * @dip: the directory
1899 *
1900 * Dealloc all on-disk directory leaves to FREEMETA state
1901 * Change on-disk inode type to "regular file"
1902 *
1903 * Returns: errno
1904 */
1905
1906int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
1907{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001908 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001909 struct buffer_head *bh;
1910 int error;
1911
1912 /* Dealloc on-disk leaves to FREEMETA state */
1913 error = foreach_leaf(dip, leaf_dealloc, NULL);
1914 if (error)
1915 return error;
1916
1917 /* Make this a regular file in case we crash.
1918 (We don't want to free these blocks a second time.) */
1919
1920 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1921 if (error)
1922 return error;
1923
1924 error = gfs2_meta_inode_buffer(dip, &bh);
1925 if (!error) {
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001926 gfs2_trans_add_bh(dip->i_gl, bh, 1);
Steven Whitehouse568f4c92006-02-27 12:00:42 -05001927 ((struct gfs2_dinode *)bh->b_data)->di_mode =
1928 cpu_to_be32(S_IFREG);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001929 brelse(bh);
1930 }
1931
1932 gfs2_trans_end(sdp);
1933
1934 return error;
1935}
1936
1937/**
1938 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
1939 * @ip: the file being written to
1940 * @filname: the filename that's going to be added
David Teiglandb3b94fa2006-01-16 16:50:04 +00001941 *
Steven Whitehousec7526662006-03-20 12:30:04 -05001942 * Returns: 1 if alloc required, 0 if not, -ve on error
David Teiglandb3b94fa2006-01-16 16:50:04 +00001943 */
1944
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04001945int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001946{
Steven Whitehousec7526662006-03-20 12:30:04 -05001947 struct gfs2_dirent *dent;
1948 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001949
Steven Whitehousec7526662006-03-20 12:30:04 -05001950 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
Steven Whitehouseed386502006-04-07 16:28:07 -04001951 if (!dent) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001952 return 1;
Steven Whitehouseed386502006-04-07 16:28:07 -04001953 }
Steven Whitehousec7526662006-03-20 12:30:04 -05001954 if (IS_ERR(dent))
1955 return PTR_ERR(dent);
1956 brelse(bh);
1957 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001958}
1959