blob: 0fdcb7713cd9378c1fe723b61b6eda9e7a1711ee [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 Engelhardtc53921242006-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 Whitehouse5e7d65c2006-11-17 12:27:44 -0500343static inline int gfs2_dirent_sentinel(const struct gfs2_dirent *dent)
344{
345 return dent->de_inum.no_addr == 0 || dent->de_inum.no_formal_ino == 0;
346}
347
Steven Whitehousec7526662006-03-20 12:30:04 -0500348static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
349 const struct qstr *name, int ret)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000350{
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500351 if (!gfs2_dirent_sentinel(dent) &&
Steven Whitehousec7526662006-03-20 12:30:04 -0500352 be32_to_cpu(dent->de_hash) == name->hash &&
353 be16_to_cpu(dent->de_name_len) == name->len &&
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400354 memcmp(dent+1, name->name, name->len) == 0)
Steven Whitehousec7526662006-03-20 12:30:04 -0500355 return ret;
356 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000357}
358
Steven Whitehousec7526662006-03-20 12:30:04 -0500359static int gfs2_dirent_find(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500360 const struct qstr *name,
361 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500362{
363 return __gfs2_dirent_find(dent, name, 1);
364}
365
366static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500367 const struct qstr *name,
368 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500369{
370 return __gfs2_dirent_find(dent, name, 2);
371}
372
373/*
374 * name->name holds ptr to start of block.
375 * name->len holds size of block.
376 */
377static int gfs2_dirent_last(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500378 const struct qstr *name,
379 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500380{
381 const char *start = name->name;
382 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
383 if (name->len == (end - start))
384 return 1;
385 return 0;
386}
387
388static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500389 const struct qstr *name,
390 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500391{
392 unsigned required = GFS2_DIRENT_SIZE(name->len);
393 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
394 unsigned totlen = be16_to_cpu(dent->de_rec_len);
395
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500396 if (gfs2_dirent_sentinel(dent))
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500397 actual = GFS2_DIRENT_SIZE(0);
Jan Engelhardtc53921242006-09-05 14:30:40 +0200398 if (totlen - actual >= required)
Steven Whitehousec7526662006-03-20 12:30:04 -0500399 return 1;
400 return 0;
401}
402
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500403struct dirent_gather {
404 const struct gfs2_dirent **pdent;
405 unsigned offset;
406};
407
408static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
409 const struct qstr *name,
410 void *opaque)
411{
412 struct dirent_gather *g = opaque;
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500413 if (!gfs2_dirent_sentinel(dent)) {
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500414 g->pdent[g->offset++] = dent;
415 }
416 return 0;
417}
418
Steven Whitehousec7526662006-03-20 12:30:04 -0500419/*
420 * Other possible things to check:
421 * - Inode located within filesystem size (and on valid block)
422 * - Valid directory entry type
423 * Not sure how heavy-weight we want to make this... could also check
424 * hash is correct for example, but that would take a lot of extra time.
425 * For now the most important thing is to check that the various sizes
426 * are correct.
427 */
428static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
429 unsigned int size, unsigned int len, int first)
430{
431 const char *msg = "gfs2_dirent too small";
432 if (unlikely(size < sizeof(struct gfs2_dirent)))
433 goto error;
434 msg = "gfs2_dirent misaligned";
435 if (unlikely(offset & 0x7))
436 goto error;
437 msg = "gfs2_dirent points beyond end of block";
438 if (unlikely(offset + size > len))
439 goto error;
440 msg = "zero inode number";
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500441 if (unlikely(!first && gfs2_dirent_sentinel(dent)))
Steven Whitehousec7526662006-03-20 12:30:04 -0500442 goto error;
443 msg = "name length is greater than space in dirent";
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500444 if (!gfs2_dirent_sentinel(dent) &&
Steven Whitehousec7526662006-03-20 12:30:04 -0500445 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
446 size))
447 goto error;
448 return 0;
449error:
450 printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
451 first ? "first in block" : "not first in block");
452 return -EIO;
453}
454
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500455static int gfs2_dirent_offset(const void *buf)
Steven Whitehousec7526662006-03-20 12:30:04 -0500456{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500457 const struct gfs2_meta_header *h = buf;
458 int offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500459
460 BUG_ON(buf == NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500461
Steven Whitehousee3167de2006-03-30 15:46:23 -0500462 switch(be32_to_cpu(h->mh_type)) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500463 case GFS2_METATYPE_LF:
464 offset = sizeof(struct gfs2_leaf);
465 break;
466 case GFS2_METATYPE_DI:
467 offset = sizeof(struct gfs2_dinode);
468 break;
469 default:
470 goto wrong_type;
471 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500472 return offset;
473wrong_type:
474 printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
Steven Whitehousee3167de2006-03-30 15:46:23 -0500475 be32_to_cpu(h->mh_type));
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500476 return -1;
477}
Steven Whitehousec7526662006-03-20 12:30:04 -0500478
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400479static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500480 unsigned int len, gfs2_dscan_t scan,
481 const struct qstr *name,
482 void *opaque)
483{
484 struct gfs2_dirent *dent, *prev;
485 unsigned offset;
486 unsigned size;
487 int ret = 0;
488
489 ret = gfs2_dirent_offset(buf);
490 if (ret < 0)
491 goto consist_inode;
492
493 offset = ret;
Steven Whitehousec7526662006-03-20 12:30:04 -0500494 prev = NULL;
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400495 dent = buf + offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500496 size = be16_to_cpu(dent->de_rec_len);
497 if (gfs2_check_dirent(dent, offset, size, len, 1))
498 goto consist_inode;
499 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500500 ret = scan(dent, name, opaque);
Steven Whitehousec7526662006-03-20 12:30:04 -0500501 if (ret)
502 break;
503 offset += size;
504 if (offset == len)
505 break;
506 prev = dent;
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400507 dent = buf + offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500508 size = be16_to_cpu(dent->de_rec_len);
509 if (gfs2_check_dirent(dent, offset, size, len, 0))
510 goto consist_inode;
511 } while(1);
512
513 switch(ret) {
514 case 0:
515 return NULL;
516 case 1:
517 return dent;
518 case 2:
519 return prev ? prev : dent;
520 default:
521 BUG_ON(ret > 0);
522 return ERR_PTR(ret);
523 }
524
Steven Whitehousec7526662006-03-20 12:30:04 -0500525consist_inode:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400526 gfs2_consist_inode(GFS2_I(inode));
Steven Whitehousec7526662006-03-20 12:30:04 -0500527 return ERR_PTR(-EIO);
528}
529
530
David Teiglandb3b94fa2006-01-16 16:50:04 +0000531/**
532 * dirent_first - Return the first dirent
533 * @dip: the directory
534 * @bh: The buffer
535 * @dent: Pointer to list of dirents
536 *
537 * return first dirent whether bh points to leaf or stuffed dinode
538 *
539 * Returns: IS_LEAF, IS_DINODE, or -errno
540 */
541
542static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
543 struct gfs2_dirent **dent)
544{
545 struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data;
546
Steven Whitehousee3167de2006-03-30 15:46:23 -0500547 if (be32_to_cpu(h->mh_type) == GFS2_METATYPE_LF) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400548 if (gfs2_meta_check(GFS2_SB(&dip->i_inode), bh))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000549 return -EIO;
550 *dent = (struct gfs2_dirent *)(bh->b_data +
551 sizeof(struct gfs2_leaf));
552 return IS_LEAF;
553 } else {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400554 if (gfs2_metatype_check(GFS2_SB(&dip->i_inode), bh, GFS2_METATYPE_DI))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000555 return -EIO;
556 *dent = (struct gfs2_dirent *)(bh->b_data +
557 sizeof(struct gfs2_dinode));
558 return IS_DINODE;
559 }
560}
561
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400562static int dirent_check_reclen(struct gfs2_inode *dip,
563 const struct gfs2_dirent *d, const void *end_p)
564{
565 const void *ptr = d;
566 u16 rec_len = be16_to_cpu(d->de_rec_len);
567
568 if (unlikely(rec_len < sizeof(struct gfs2_dirent)))
569 goto broken;
570 ptr += rec_len;
571 if (ptr < end_p)
572 return rec_len;
573 if (ptr == end_p)
574 return -ENOENT;
575broken:
576 gfs2_consist_inode(dip);
577 return -EIO;
578}
579
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580/**
581 * dirent_next - Next dirent
582 * @dip: the directory
583 * @bh: The buffer
584 * @dent: Pointer to list of dirents
585 *
586 * Returns: 0 on success, error code otherwise
587 */
588
589static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
590 struct gfs2_dirent **dent)
591{
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400592 struct gfs2_dirent *cur = *dent, *tmp;
593 char *bh_end = bh->b_data + bh->b_size;
594 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000595
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400596 ret = dirent_check_reclen(dip, cur, bh_end);
597 if (ret < 0)
598 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000599
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400600 tmp = (void *)cur + ret;
601 ret = dirent_check_reclen(dip, tmp, bh_end);
602 if (ret == -EIO)
603 return ret;
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000604
David Teiglandb3b94fa2006-01-16 16:50:04 +0000605 /* Only the first dent could ever have de_inum.no_addr == 0 */
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500606 if (gfs2_dirent_sentinel(tmp)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000607 gfs2_consist_inode(dip);
608 return -EIO;
609 }
610
611 *dent = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000612 return 0;
613}
614
615/**
616 * dirent_del - Delete a dirent
617 * @dip: The GFS2 inode
618 * @bh: The buffer
619 * @prev: The previous dirent
620 * @cur: The current dirent
621 *
622 */
623
624static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
625 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
626{
Steven Whitehousecd915492006-09-04 12:49:07 -0400627 u16 cur_rec_len, prev_rec_len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000628
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500629 if (gfs2_dirent_sentinel(cur)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000630 gfs2_consist_inode(dip);
631 return;
632 }
633
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000634 gfs2_trans_add_bh(dip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000635
636 /* If there is no prev entry, this is the first entry in the block.
637 The de_rec_len is already as big as it needs to be. Just zero
638 out the inode number and return. */
639
640 if (!prev) {
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500641 cur->de_inum.no_addr = 0;
642 cur->de_inum.no_formal_ino = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000643 return;
644 }
645
646 /* Combine this dentry with the previous one. */
647
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000648 prev_rec_len = be16_to_cpu(prev->de_rec_len);
649 cur_rec_len = be16_to_cpu(cur->de_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000650
651 if ((char *)prev + prev_rec_len != (char *)cur)
652 gfs2_consist_inode(dip);
653 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
654 gfs2_consist_inode(dip);
655
656 prev_rec_len += cur_rec_len;
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000657 prev->de_rec_len = cpu_to_be16(prev_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000658}
659
Steven Whitehousec7526662006-03-20 12:30:04 -0500660/*
661 * Takes a dent from which to grab space as an argument. Returns the
662 * newly created dent.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000663 */
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400664static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
665 struct gfs2_dirent *dent,
666 const struct qstr *name,
667 struct buffer_head *bh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000668{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400669 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -0500670 struct gfs2_dirent *ndent;
671 unsigned offset = 0, totlen;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000672
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500673 if (!gfs2_dirent_sentinel(dent))
Steven Whitehousec7526662006-03-20 12:30:04 -0500674 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
675 totlen = be16_to_cpu(dent->de_rec_len);
676 BUG_ON(offset + name->len > totlen);
677 gfs2_trans_add_bh(ip->i_gl, bh, 1);
678 ndent = (struct gfs2_dirent *)((char *)dent + offset);
679 dent->de_rec_len = cpu_to_be16(offset);
680 gfs2_qstr2dirent(name, totlen - offset, ndent);
681 return ndent;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000682}
683
Steven Whitehousec7526662006-03-20 12:30:04 -0500684static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
685 struct buffer_head *bh,
686 const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000687{
688 struct gfs2_dirent *dent;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400689 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500690 gfs2_dirent_find_space, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500691 if (!dent || IS_ERR(dent))
692 return dent;
693 return gfs2_init_dirent(inode, dent, name, bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000694}
695
Steven Whitehousecd915492006-09-04 12:49:07 -0400696static int get_leaf(struct gfs2_inode *dip, u64 leaf_no,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000697 struct buffer_head **bhp)
698{
699 int error;
700
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400701 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, bhp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400702 if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
703 /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000704 error = -EIO;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400705 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000706
707 return error;
708}
709
710/**
711 * get_leaf_nr - Get a leaf number associated with the index
712 * @dip: The GFS2 inode
713 * @index:
714 * @leaf_out:
715 *
716 * Returns: 0 on success, error code otherwise
717 */
718
Steven Whitehousecd915492006-09-04 12:49:07 -0400719static int get_leaf_nr(struct gfs2_inode *dip, u32 index,
720 u64 *leaf_out)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000721{
Al Virob44b84d2006-10-14 10:46:30 -0400722 __be64 leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000723 int error;
724
Steven Whitehousee13940b2006-01-30 13:31:50 +0000725 error = gfs2_dir_read_data(dip, (char *)&leaf_no,
Al Virob44b84d2006-10-14 10:46:30 -0400726 index * sizeof(__be64),
727 sizeof(__be64), 0);
Steven Whitehousecd915492006-09-04 12:49:07 -0400728 if (error != sizeof(u64))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000729 return (error < 0) ? error : -EIO;
730
731 *leaf_out = be64_to_cpu(leaf_no);
732
733 return 0;
734}
735
Steven Whitehousecd915492006-09-04 12:49:07 -0400736static int get_first_leaf(struct gfs2_inode *dip, u32 index,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000737 struct buffer_head **bh_out)
738{
Steven Whitehousecd915492006-09-04 12:49:07 -0400739 u64 leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000740 int error;
741
742 error = get_leaf_nr(dip, index, &leaf_no);
743 if (!error)
744 error = get_leaf(dip, leaf_no, bh_out);
745
746 return error;
747}
748
Steven Whitehousec7526662006-03-20 12:30:04 -0500749static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
750 const struct qstr *name,
751 gfs2_dscan_t scan,
752 struct buffer_head **pbh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000753{
Steven Whitehousec7526662006-03-20 12:30:04 -0500754 struct buffer_head *bh;
755 struct gfs2_dirent *dent;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400756 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000757 int error;
758
Steven Whitehousec7526662006-03-20 12:30:04 -0500759 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
760 struct gfs2_leaf *leaf;
761 unsigned hsize = 1 << ip->i_di.di_depth;
762 unsigned index;
763 u64 ln;
764 if (hsize * sizeof(u64) != ip->i_di.di_size) {
765 gfs2_consist_inode(ip);
766 return ERR_PTR(-EIO);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000767 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400768
Steven Whitehousec7526662006-03-20 12:30:04 -0500769 index = name->hash >> (32 - ip->i_di.di_depth);
770 error = get_first_leaf(ip, index, &bh);
771 if (error)
772 return ERR_PTR(error);
773 do {
774 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500775 scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500776 if (dent)
777 goto got_dent;
778 leaf = (struct gfs2_leaf *)bh->b_data;
779 ln = be64_to_cpu(leaf->lf_next);
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400780 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500781 if (!ln)
782 break;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400783
Steven Whitehousec7526662006-03-20 12:30:04 -0500784 error = get_leaf(ip, ln, &bh);
785 } while(!error);
786
787 return error ? ERR_PTR(error) : NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000788 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000789
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400790
Steven Whitehousec7526662006-03-20 12:30:04 -0500791 error = gfs2_meta_inode_buffer(ip, &bh);
792 if (error)
793 return ERR_PTR(error);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500794 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500795got_dent:
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400796 if (unlikely(dent == NULL || IS_ERR(dent))) {
Steven Whitehouseed386502006-04-07 16:28:07 -0400797 brelse(bh);
798 bh = NULL;
799 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500800 *pbh = bh;
801 return dent;
802}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000803
Steven Whitehousec7526662006-03-20 12:30:04 -0500804static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
805{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400806 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -0500807 u64 bn = gfs2_alloc_meta(ip);
808 struct buffer_head *bh = gfs2_meta_new(ip->i_gl, bn);
809 struct gfs2_leaf *leaf;
810 struct gfs2_dirent *dent;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500811 struct qstr name = { .name = "", .len = 0, .hash = 0 };
Steven Whitehousec7526662006-03-20 12:30:04 -0500812 if (!bh)
813 return NULL;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400814
Steven Whitehousec7526662006-03-20 12:30:04 -0500815 gfs2_trans_add_bh(ip->i_gl, bh, 1);
816 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
817 leaf = (struct gfs2_leaf *)bh->b_data;
818 leaf->lf_depth = cpu_to_be16(depth);
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400819 leaf->lf_entries = 0;
Al Viroa2d7d022006-10-14 16:49:30 +0100820 leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400821 leaf->lf_next = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500822 memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
823 dent = (struct gfs2_dirent *)(leaf+1);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500824 gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
Steven Whitehousec7526662006-03-20 12:30:04 -0500825 *pbh = bh;
826 return leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000827}
828
829/**
830 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
831 * @dip: The GFS2 inode
832 *
833 * Returns: 0 on success, error code otherwise
834 */
835
Steven Whitehousec7526662006-03-20 12:30:04 -0500836static int dir_make_exhash(struct inode *inode)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000837{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400838 struct gfs2_inode *dip = GFS2_I(inode);
839 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000840 struct gfs2_dirent *dent;
Steven Whitehousec7526662006-03-20 12:30:04 -0500841 struct qstr args;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000842 struct buffer_head *bh, *dibh;
843 struct gfs2_leaf *leaf;
844 int y;
Steven Whitehousecd915492006-09-04 12:49:07 -0400845 u32 x;
Al Virob44b84d2006-10-14 10:46:30 -0400846 __be64 *lp;
847 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000848 int error;
849
850 error = gfs2_meta_inode_buffer(dip, &dibh);
851 if (error)
852 return error;
853
David Teiglandb3b94fa2006-01-16 16:50:04 +0000854 /* Turn over a new leaf */
855
Steven Whitehousec7526662006-03-20 12:30:04 -0500856 leaf = new_leaf(inode, &bh, 0);
857 if (!leaf)
858 return -ENOSPC;
859 bn = bh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000860
861 gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000862 leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries);
863
864 /* Copy dirents */
865
866 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
867 sizeof(struct gfs2_dinode));
868
869 /* Find last entry */
870
871 x = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500872 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
873 sizeof(struct gfs2_leaf);
874 args.name = bh->b_data;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400875 dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500876 gfs2_dirent_last, &args, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500877 if (!dent) {
878 brelse(bh);
879 brelse(dibh);
880 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000881 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500882 if (IS_ERR(dent)) {
883 brelse(bh);
884 brelse(dibh);
885 return PTR_ERR(dent);
886 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000887
888 /* Adjust the last dirent's record length
889 (Remember that dent still points to the last entry.) */
890
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000891 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000892 sizeof(struct gfs2_dinode) -
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000893 sizeof(struct gfs2_leaf));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000894
895 brelse(bh);
896
897 /* We're done with the new leaf block, now setup the new
898 hash table. */
899
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000900 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000901 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
902
Al Virob44b84d2006-10-14 10:46:30 -0400903 lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000904
905 for (x = sdp->sd_hash_ptrs; x--; lp++)
906 *lp = cpu_to_be64(bn);
907
908 dip->i_di.di_size = sdp->sd_sb.sb_bsize / 2;
909 dip->i_di.di_blocks++;
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -0500910 gfs2_set_inode_blocks(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000911 dip->i_di.di_flags |= GFS2_DIF_EXHASH;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000912
913 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
914 dip->i_di.di_depth = y;
915
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500916 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000917
918 brelse(dibh);
919
920 return 0;
921}
922
923/**
924 * dir_split_leaf - Split a leaf block into two
925 * @dip: The GFS2 inode
926 * @index:
927 * @leaf_no:
928 *
929 * Returns: 0 on success, error code on failure
930 */
931
Steven Whitehousec7526662006-03-20 12:30:04 -0500932static int dir_split_leaf(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000933{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400934 struct gfs2_inode *dip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000935 struct buffer_head *nbh, *obh, *dibh;
936 struct gfs2_leaf *nleaf, *oleaf;
Steven Whitehouse4da3c642006-07-11 13:19:13 -0400937 struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
Steven Whitehousecd915492006-09-04 12:49:07 -0400938 u32 start, len, half_len, divider;
Al Virob44b84d2006-10-14 10:46:30 -0400939 u64 bn, leaf_no;
940 __be64 *lp;
Steven Whitehousecd915492006-09-04 12:49:07 -0400941 u32 index;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000942 int x, moved = 0;
943 int error;
944
Steven Whitehousec7526662006-03-20 12:30:04 -0500945 index = name->hash >> (32 - dip->i_di.di_depth);
946 error = get_leaf_nr(dip, index, &leaf_no);
947 if (error)
948 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000949
950 /* Get the old leaf block */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000951 error = get_leaf(dip, leaf_no, &obh);
952 if (error)
Steven Whitehousee90deff2006-03-29 19:02:15 -0500953 return error;
954
955 oleaf = (struct gfs2_leaf *)obh->b_data;
956 if (dip->i_di.di_depth == be16_to_cpu(oleaf->lf_depth)) {
957 brelse(obh);
958 return 1; /* can't split */
959 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000960
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000961 gfs2_trans_add_bh(dip->i_gl, obh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000962
Steven Whitehousec7526662006-03-20 12:30:04 -0500963 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
964 if (!nleaf) {
965 brelse(obh);
966 return -ENOSPC;
967 }
968 bn = nbh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000969
Steven Whitehousec7526662006-03-20 12:30:04 -0500970 /* Compute the start and len of leaf pointers in the hash table. */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000971 len = 1 << (dip->i_di.di_depth - be16_to_cpu(oleaf->lf_depth));
972 half_len = len >> 1;
973 if (!half_len) {
Steven Whitehousee90deff2006-03-29 19:02:15 -0500974 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 +0000975 gfs2_consist_inode(dip);
976 error = -EIO;
977 goto fail_brelse;
978 }
979
980 start = (index & ~(len - 1));
981
982 /* Change the pointers.
983 Don't bother distinguishing stuffed from non-stuffed.
984 This code is complicated enough already. */
Al Virob44b84d2006-10-14 10:46:30 -0400985 lp = kmalloc(half_len * sizeof(__be64), GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000986 /* Change the pointers */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000987 for (x = 0; x < half_len; x++)
988 lp[x] = cpu_to_be64(bn);
989
Steven Whitehousecd915492006-09-04 12:49:07 -0400990 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
991 half_len * sizeof(u64));
992 if (error != half_len * sizeof(u64)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000993 if (error >= 0)
994 error = -EIO;
995 goto fail_lpfree;
996 }
997
998 kfree(lp);
999
1000 /* Compute the divider */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001001 divider = (start + half_len) << (32 - dip->i_di.di_depth);
1002
1003 /* Copy the entries */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001004 dirent_first(dip, obh, &dent);
1005
1006 do {
1007 next = dent;
1008 if (dirent_next(dip, obh, &next))
1009 next = NULL;
1010
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -05001011 if (!gfs2_dirent_sentinel(dent) &&
David Teiglandb3b94fa2006-01-16 16:50:04 +00001012 be32_to_cpu(dent->de_hash) < divider) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001013 struct qstr str;
1014 str.name = (char*)(dent+1);
1015 str.len = be16_to_cpu(dent->de_name_len);
1016 str.hash = be32_to_cpu(dent->de_hash);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001017 new = gfs2_dirent_alloc(inode, nbh, &str);
Steven Whitehousec7526662006-03-20 12:30:04 -05001018 if (IS_ERR(new)) {
1019 error = PTR_ERR(new);
1020 break;
1021 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001022
1023 new->de_inum = dent->de_inum; /* No endian worries */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001024 new->de_type = dent->de_type; /* No endian worries */
Steven Whitehousec7526662006-03-20 12:30:04 -05001025 nleaf->lf_entries = cpu_to_be16(be16_to_cpu(nleaf->lf_entries)+1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001026
1027 dirent_del(dip, obh, prev, dent);
1028
1029 if (!oleaf->lf_entries)
1030 gfs2_consist_inode(dip);
Steven Whitehousec7526662006-03-20 12:30:04 -05001031 oleaf->lf_entries = cpu_to_be16(be16_to_cpu(oleaf->lf_entries)-1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001032
1033 if (!prev)
1034 prev = dent;
1035
1036 moved = 1;
Steven Whitehousec7526662006-03-20 12:30:04 -05001037 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001038 prev = dent;
Steven Whitehousec7526662006-03-20 12:30:04 -05001039 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001040 dent = next;
Steven Whitehousec7526662006-03-20 12:30:04 -05001041 } while (dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001042
Steven Whitehousec7526662006-03-20 12:30:04 -05001043 oleaf->lf_depth = nleaf->lf_depth;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001044
1045 error = gfs2_meta_inode_buffer(dip, &dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001046 if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001047 dip->i_di.di_blocks++;
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -05001048 gfs2_set_inode_blocks(&dip->i_inode);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001049 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001050 brelse(dibh);
1051 }
1052
1053 brelse(obh);
1054 brelse(nbh);
1055
1056 return error;
1057
Steven Whitehousee90deff2006-03-29 19:02:15 -05001058fail_lpfree:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001059 kfree(lp);
1060
Steven Whitehousee90deff2006-03-29 19:02:15 -05001061fail_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001062 brelse(obh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001063 brelse(nbh);
1064 return error;
1065}
1066
1067/**
1068 * dir_double_exhash - Double size of ExHash table
1069 * @dip: The GFS2 dinode
1070 *
1071 * Returns: 0 on success, error code on failure
1072 */
1073
1074static int dir_double_exhash(struct gfs2_inode *dip)
1075{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001076 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001077 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001078 u32 hsize;
1079 u64 *buf;
1080 u64 *from, *to;
1081 u64 block;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001082 int x;
1083 int error = 0;
1084
1085 hsize = 1 << dip->i_di.di_depth;
Steven Whitehousecd915492006-09-04 12:49:07 -04001086 if (hsize * sizeof(u64) != dip->i_di.di_size) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001087 gfs2_consist_inode(dip);
1088 return -EIO;
1089 }
1090
1091 /* Allocate both the "from" and "to" buffers in one big chunk */
1092
1093 buf = kcalloc(3, sdp->sd_hash_bsize, GFP_KERNEL | __GFP_NOFAIL);
1094
1095 for (block = dip->i_di.di_size >> sdp->sd_hash_bsize_shift; block--;) {
Steven Whitehousee13940b2006-01-30 13:31:50 +00001096 error = gfs2_dir_read_data(dip, (char *)buf,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001097 block * sdp->sd_hash_bsize,
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001098 sdp->sd_hash_bsize, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001099 if (error != sdp->sd_hash_bsize) {
1100 if (error >= 0)
1101 error = -EIO;
1102 goto fail;
1103 }
1104
1105 from = buf;
Steven Whitehousecd915492006-09-04 12:49:07 -04001106 to = (u64 *)((char *)buf + sdp->sd_hash_bsize);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001107
1108 for (x = sdp->sd_hash_ptrs; x--; from++) {
1109 *to++ = *from; /* No endianess worries */
1110 *to++ = *from;
1111 }
1112
Steven Whitehousee13940b2006-01-30 13:31:50 +00001113 error = gfs2_dir_write_data(dip,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001114 (char *)buf + sdp->sd_hash_bsize,
1115 block * sdp->sd_sb.sb_bsize,
1116 sdp->sd_sb.sb_bsize);
1117 if (error != sdp->sd_sb.sb_bsize) {
1118 if (error >= 0)
1119 error = -EIO;
1120 goto fail;
1121 }
1122 }
1123
1124 kfree(buf);
1125
1126 error = gfs2_meta_inode_buffer(dip, &dibh);
1127 if (!gfs2_assert_withdraw(sdp, !error)) {
1128 dip->i_di.di_depth++;
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001129 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001130 brelse(dibh);
1131 }
1132
1133 return error;
1134
Steven Whitehousea91ea692006-09-04 12:04:26 -04001135fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001136 kfree(buf);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001137 return error;
1138}
1139
1140/**
1141 * compare_dents - compare directory entries by hash value
1142 * @a: first dent
1143 * @b: second dent
1144 *
1145 * When comparing the hash entries of @a to @b:
1146 * gt: returns 1
1147 * lt: returns -1
1148 * eq: returns 0
1149 */
1150
1151static int compare_dents(const void *a, const void *b)
1152{
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001153 const struct gfs2_dirent *dent_a, *dent_b;
Steven Whitehousecd915492006-09-04 12:49:07 -04001154 u32 hash_a, hash_b;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001155 int ret = 0;
1156
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001157 dent_a = *(const struct gfs2_dirent **)a;
Steven Whitehousec7526662006-03-20 12:30:04 -05001158 hash_a = be32_to_cpu(dent_a->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001159
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001160 dent_b = *(const struct gfs2_dirent **)b;
Steven Whitehousec7526662006-03-20 12:30:04 -05001161 hash_b = be32_to_cpu(dent_b->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001162
1163 if (hash_a > hash_b)
1164 ret = 1;
1165 else if (hash_a < hash_b)
1166 ret = -1;
1167 else {
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001168 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1169 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001170
1171 if (len_a > len_b)
1172 ret = 1;
1173 else if (len_a < len_b)
1174 ret = -1;
1175 else
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001176 ret = memcmp(dent_a + 1, dent_b + 1, len_a);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001177 }
1178
1179 return ret;
1180}
1181
1182/**
1183 * do_filldir_main - read out directory entries
1184 * @dip: The GFS2 inode
1185 * @offset: The offset in the file to read from
1186 * @opaque: opaque data to pass to filldir
1187 * @filldir: The function to pass entries to
1188 * @darr: an array of struct gfs2_dirent pointers to read
1189 * @entries: the number of entries in darr
1190 * @copied: pointer to int that's non-zero if a entry has been copied out
1191 *
1192 * Jump through some hoops to make sure that if there are hash collsions,
1193 * they are read out at the beginning of a buffer. We want to minimize
1194 * the possibility that they will fall into different readdir buffers or
1195 * that someone will want to seek to that location.
1196 *
1197 * Returns: errno, >0 on exception from filldir
1198 */
1199
Steven Whitehousecd915492006-09-04 12:49:07 -04001200static int do_filldir_main(struct gfs2_inode *dip, u64 *offset,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001201 void *opaque, gfs2_filldir_t filldir,
Steven Whitehousecd915492006-09-04 12:49:07 -04001202 const struct gfs2_dirent **darr, u32 entries,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001203 int *copied)
1204{
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001205 const struct gfs2_dirent *dent, *dent_next;
Al Viro629a21e2006-10-13 22:51:24 -04001206 struct gfs2_inum_host inum;
Steven Whitehousecd915492006-09-04 12:49:07 -04001207 u64 off, off_next;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001208 unsigned int x, y;
1209 int run = 0;
1210 int error = 0;
1211
1212 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1213
1214 dent_next = darr[0];
1215 off_next = be32_to_cpu(dent_next->de_hash);
1216 off_next = gfs2_disk_hash2offset(off_next);
1217
1218 for (x = 0, y = 1; x < entries; x++, y++) {
1219 dent = dent_next;
1220 off = off_next;
1221
1222 if (y < entries) {
1223 dent_next = darr[y];
1224 off_next = be32_to_cpu(dent_next->de_hash);
1225 off_next = gfs2_disk_hash2offset(off_next);
1226
1227 if (off < *offset)
1228 continue;
1229 *offset = off;
1230
1231 if (off_next == off) {
1232 if (*copied && !run)
1233 return 1;
1234 run = 1;
1235 } else
1236 run = 0;
1237 } else {
1238 if (off < *offset)
1239 continue;
1240 *offset = off;
1241 }
1242
1243 gfs2_inum_in(&inum, (char *)&dent->de_inum);
1244
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001245 error = filldir(opaque, (const char *)(dent + 1),
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001246 be16_to_cpu(dent->de_name_len),
David Teiglandb3b94fa2006-01-16 16:50:04 +00001247 off, &inum,
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001248 be16_to_cpu(dent->de_type));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001249 if (error)
1250 return 1;
1251
1252 *copied = 1;
1253 }
1254
1255 /* Increment the *offset by one, so the next time we come into the
1256 do_filldir fxn, we get the next entry instead of the last one in the
1257 current leaf */
1258
1259 (*offset)++;
1260
1261 return 0;
1262}
1263
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001264static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
1265 gfs2_filldir_t filldir, int *copied,
1266 unsigned *depth, u64 leaf_no)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001267{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001268 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001269 struct buffer_head *bh;
1270 struct gfs2_leaf *lf;
1271 unsigned entries = 0;
1272 unsigned leaves = 0;
1273 const struct gfs2_dirent **darr, *dent;
1274 struct dirent_gather g;
1275 struct buffer_head **larr;
1276 int leaf = 0;
1277 int error, i;
1278 u64 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001279
David Teiglandb3b94fa2006-01-16 16:50:04 +00001280 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001281 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001282 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001283 goto out;
1284 lf = (struct gfs2_leaf *)bh->b_data;
1285 if (leaves == 0)
1286 *depth = be16_to_cpu(lf->lf_depth);
1287 entries += be16_to_cpu(lf->lf_entries);
1288 leaves++;
1289 lfn = be64_to_cpu(lf->lf_next);
1290 brelse(bh);
1291 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001292
1293 if (!entries)
1294 return 0;
1295
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001296 error = -ENOMEM;
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001297 larr = vmalloc((leaves + entries) * sizeof(void *));
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001298 if (!larr)
1299 goto out;
1300 darr = (const struct gfs2_dirent **)(larr + leaves);
1301 g.pdent = darr;
1302 g.offset = 0;
1303 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001304
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001305 do {
1306 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001307 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001308 goto out_kfree;
1309 lf = (struct gfs2_leaf *)bh->b_data;
1310 lfn = be64_to_cpu(lf->lf_next);
1311 if (lf->lf_entries) {
1312 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1313 gfs2_dirent_gather, NULL, &g);
1314 error = PTR_ERR(dent);
1315 if (IS_ERR(dent)) {
1316 goto out_kfree;
1317 }
1318 error = 0;
1319 larr[leaf++] = bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001320 } else {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001321 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001322 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001323 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001324
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001325 error = do_filldir_main(ip, offset, opaque, filldir, darr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001326 entries, copied);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001327out_kfree:
1328 for(i = 0; i < leaf; i++)
1329 brelse(larr[i]);
Steven Whitehousefe1bded2006-04-18 10:09:15 -04001330 vfree(larr);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001331out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001332 return error;
1333}
1334
1335/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001336 * dir_e_read - Reads the entries from a directory into a filldir buffer
1337 * @dip: dinode pointer
1338 * @offset: the hash of the last entry read shifted to the right once
1339 * @opaque: buffer for the filldir function to fill
1340 * @filldir: points to the filldir function to use
1341 *
1342 * Returns: errno
1343 */
1344
Steven Whitehousecd915492006-09-04 12:49:07 -04001345static int dir_e_read(struct inode *inode, u64 *offset, void *opaque,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001346 gfs2_filldir_t filldir)
1347{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001348 struct gfs2_inode *dip = GFS2_I(inode);
1349 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehousecd915492006-09-04 12:49:07 -04001350 u32 hsize, len = 0;
1351 u32 ht_offset, lp_offset, ht_offset_cur = -1;
1352 u32 hash, index;
Al Virob44b84d2006-10-14 10:46:30 -04001353 __be64 *lp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001354 int copied = 0;
1355 int error = 0;
Steven Whitehouse4da3c642006-07-11 13:19:13 -04001356 unsigned depth = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001357
1358 hsize = 1 << dip->i_di.di_depth;
Steven Whitehousecd915492006-09-04 12:49:07 -04001359 if (hsize * sizeof(u64) != dip->i_di.di_size) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001360 gfs2_consist_inode(dip);
1361 return -EIO;
1362 }
1363
1364 hash = gfs2_dir_offset2hash(*offset);
1365 index = hash >> (32 - dip->i_di.di_depth);
1366
1367 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1368 if (!lp)
1369 return -ENOMEM;
1370
1371 while (index < hsize) {
1372 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1373 ht_offset = index - lp_offset;
1374
1375 if (ht_offset_cur != ht_offset) {
Steven Whitehousee13940b2006-01-30 13:31:50 +00001376 error = gfs2_dir_read_data(dip, (char *)lp,
Al Virob44b84d2006-10-14 10:46:30 -04001377 ht_offset * sizeof(__be64),
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001378 sdp->sd_hash_bsize, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001379 if (error != sdp->sd_hash_bsize) {
1380 if (error >= 0)
1381 error = -EIO;
1382 goto out;
1383 }
1384 ht_offset_cur = ht_offset;
1385 }
1386
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001387 error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
1388 &copied, &depth,
1389 be64_to_cpu(lp[lp_offset]));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001390 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001391 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001392
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001393 len = 1 << (dip->i_di.di_depth - depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001394 index = (index & ~(len - 1)) + len;
1395 }
1396
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001397out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001398 kfree(lp);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001399 if (error > 0)
1400 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001401 return error;
1402}
1403
Steven Whitehousecd915492006-09-04 12:49:07 -04001404int gfs2_dir_read(struct inode *inode, u64 *offset, void *opaque,
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001405 gfs2_filldir_t filldir)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001406{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001407 struct gfs2_inode *dip = GFS2_I(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001408 struct dirent_gather g;
1409 const struct gfs2_dirent **darr, *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001410 struct buffer_head *dibh;
1411 int copied = 0;
1412 int error;
1413
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001414 if (!dip->i_di.di_entries)
1415 return 0;
1416
1417 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
1418 return dir_e_read(inode, offset, opaque, filldir);
1419
David Teiglandb3b94fa2006-01-16 16:50:04 +00001420 if (!gfs2_is_stuffed(dip)) {
1421 gfs2_consist_inode(dip);
1422 return -EIO;
1423 }
1424
David Teiglandb3b94fa2006-01-16 16:50:04 +00001425 error = gfs2_meta_inode_buffer(dip, &dibh);
1426 if (error)
1427 return error;
1428
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001429 error = -ENOMEM;
1430 darr = kmalloc(dip->i_di.di_entries * sizeof(struct gfs2_dirent *),
1431 GFP_KERNEL);
1432 if (darr) {
1433 g.pdent = darr;
1434 g.offset = 0;
1435 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1436 gfs2_dirent_gather, NULL, &g);
1437 if (IS_ERR(dent)) {
1438 error = PTR_ERR(dent);
1439 goto out;
1440 }
1441 error = do_filldir_main(dip, offset, opaque, filldir, darr,
1442 dip->i_di.di_entries, &copied);
1443out:
1444 kfree(darr);
1445 }
1446
David Teiglandb3b94fa2006-01-16 16:50:04 +00001447 if (error > 0)
1448 error = 0;
1449
1450 brelse(dibh);
1451
1452 return error;
1453}
1454
David Teiglandb3b94fa2006-01-16 16:50:04 +00001455/**
1456 * gfs2_dir_search - Search a directory
1457 * @dip: The GFS2 inode
1458 * @filename:
1459 * @inode:
1460 *
1461 * This routine searches a directory for a file or another directory.
1462 * Assumes a glock is held on dip.
1463 *
1464 * Returns: errno
1465 */
1466
Steven Whitehousec7526662006-03-20 12:30:04 -05001467int gfs2_dir_search(struct inode *dir, const struct qstr *name,
Al Viro629a21e2006-10-13 22:51:24 -04001468 struct gfs2_inum_host *inum, unsigned int *type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001469{
Steven Whitehousec7526662006-03-20 12:30:04 -05001470 struct buffer_head *bh;
1471 struct gfs2_dirent *dent;
1472
1473 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1474 if (dent) {
1475 if (IS_ERR(dent))
1476 return PTR_ERR(dent);
1477 if (inum)
1478 gfs2_inum_in(inum, (char *)&dent->de_inum);
1479 if (type)
1480 *type = be16_to_cpu(dent->de_type);
1481 brelse(bh);
1482 return 0;
1483 }
1484 return -ENOENT;
1485}
1486
1487static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1488{
1489 struct buffer_head *bh, *obh;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001490 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001491 struct gfs2_leaf *leaf, *oleaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001492 int error;
Steven Whitehousec7526662006-03-20 12:30:04 -05001493 u32 index;
1494 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001495
Steven Whitehousec7526662006-03-20 12:30:04 -05001496 index = name->hash >> (32 - ip->i_di.di_depth);
1497 error = get_first_leaf(ip, index, &obh);
1498 if (error)
1499 return error;
1500 do {
1501 oleaf = (struct gfs2_leaf *)obh->b_data;
1502 bn = be64_to_cpu(oleaf->lf_next);
1503 if (!bn)
1504 break;
1505 brelse(obh);
1506 error = get_leaf(ip, bn, &obh);
1507 if (error)
1508 return error;
1509 } while(1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001510
Steven Whitehousec7526662006-03-20 12:30:04 -05001511 gfs2_trans_add_bh(ip->i_gl, obh, 1);
1512
1513 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1514 if (!leaf) {
1515 brelse(obh);
1516 return -ENOSPC;
1517 }
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04001518 oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
Steven Whitehousec7526662006-03-20 12:30:04 -05001519 brelse(bh);
1520 brelse(obh);
1521
1522 error = gfs2_meta_inode_buffer(ip, &bh);
1523 if (error)
1524 return error;
1525 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1526 ip->i_di.di_blocks++;
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -05001527 gfs2_set_inode_blocks(&ip->i_inode);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001528 gfs2_dinode_out(ip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001529 brelse(bh);
1530 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001531}
1532
1533/**
1534 * gfs2_dir_add - Add new filename into directory
1535 * @dip: The GFS2 inode
1536 * @filename: The new name
1537 * @inode: The inode number of the entry
1538 * @type: The type of the entry
1539 *
1540 * Returns: 0 on success, error code on failure
1541 */
1542
Steven Whitehousec7526662006-03-20 12:30:04 -05001543int gfs2_dir_add(struct inode *inode, const struct qstr *name,
Al Viro629a21e2006-10-13 22:51:24 -04001544 const struct gfs2_inum_host *inum, unsigned type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001545{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001546 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001547 struct buffer_head *bh;
1548 struct gfs2_dirent *dent;
1549 struct gfs2_leaf *leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001550 int error;
1551
Steven Whitehousec7526662006-03-20 12:30:04 -05001552 while(1) {
1553 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
1554 &bh);
1555 if (dent) {
1556 if (IS_ERR(dent))
1557 return PTR_ERR(dent);
1558 dent = gfs2_init_dirent(inode, dent, name, bh);
1559 gfs2_inum_out(inum, (char *)&dent->de_inum);
1560 dent->de_type = cpu_to_be16(type);
1561 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
1562 leaf = (struct gfs2_leaf *)bh->b_data;
1563 leaf->lf_entries = cpu_to_be16(be16_to_cpu(leaf->lf_entries) + 1);
1564 }
1565 brelse(bh);
1566 error = gfs2_meta_inode_buffer(ip, &bh);
1567 if (error)
1568 break;
1569 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1570 ip->i_di.di_entries++;
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -05001571 ip->i_inode.i_mtime.tv_sec = ip->i_inode.i_ctime.tv_sec = get_seconds();
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001572 gfs2_dinode_out(ip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001573 brelse(bh);
1574 error = 0;
1575 break;
1576 }
1577 if (!(ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
1578 error = dir_make_exhash(inode);
1579 if (error)
1580 break;
1581 continue;
1582 }
1583 error = dir_split_leaf(inode, name);
1584 if (error == 0)
1585 continue;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001586 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001587 break;
1588 if (ip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) {
1589 error = dir_double_exhash(ip);
1590 if (error)
1591 break;
1592 error = dir_split_leaf(inode, name);
Steven Whitehousee90deff2006-03-29 19:02:15 -05001593 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001594 break;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001595 if (error == 0)
1596 continue;
Steven Whitehousec7526662006-03-20 12:30:04 -05001597 }
1598 error = dir_new_leaf(inode, name);
1599 if (!error)
1600 continue;
1601 error = -ENOSPC;
1602 break;
1603 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001604 return error;
1605}
1606
Steven Whitehousec7526662006-03-20 12:30:04 -05001607
David Teiglandb3b94fa2006-01-16 16:50:04 +00001608/**
1609 * gfs2_dir_del - Delete a directory entry
1610 * @dip: The GFS2 inode
1611 * @filename: The filename
1612 *
1613 * Returns: 0 on success, error code on failure
1614 */
1615
Steven Whitehousec7526662006-03-20 12:30:04 -05001616int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001617{
Steven Whitehousec7526662006-03-20 12:30:04 -05001618 struct gfs2_dirent *dent, *prev = NULL;
1619 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001620 int error;
1621
Steven Whitehousec7526662006-03-20 12:30:04 -05001622 /* Returns _either_ the entry (if its first in block) or the
1623 previous entry otherwise */
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001624 dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001625 if (!dent) {
1626 gfs2_consist_inode(dip);
1627 return -EIO;
1628 }
1629 if (IS_ERR(dent)) {
1630 gfs2_consist_inode(dip);
1631 return PTR_ERR(dent);
1632 }
1633 /* If not first in block, adjust pointers accordingly */
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001634 if (gfs2_dirent_find(dent, name, NULL) == 0) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001635 prev = dent;
1636 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1637 }
1638
1639 dirent_del(dip, bh, prev, dent);
1640 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1641 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1642 u16 entries = be16_to_cpu(leaf->lf_entries);
1643 if (!entries)
1644 gfs2_consist_inode(dip);
1645 leaf->lf_entries = cpu_to_be16(--entries);
Steven Whitehousec7526662006-03-20 12:30:04 -05001646 }
Steven Whitehouseed386502006-04-07 16:28:07 -04001647 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001648
1649 error = gfs2_meta_inode_buffer(dip, &bh);
1650 if (error)
1651 return error;
1652
1653 if (!dip->i_di.di_entries)
1654 gfs2_consist_inode(dip);
1655 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1656 dip->i_di.di_entries--;
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -05001657 dip->i_inode.i_mtime.tv_sec = dip->i_inode.i_ctime.tv_sec = get_seconds();
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001658 gfs2_dinode_out(dip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001659 brelse(bh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001660 mark_inode_dirty(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001661
1662 return error;
1663}
1664
David Teiglandb3b94fa2006-01-16 16:50:04 +00001665/**
1666 * gfs2_dir_mvino - Change inode number of directory entry
1667 * @dip: The GFS2 inode
1668 * @filename:
1669 * @new_inode:
1670 *
1671 * This routine changes the inode number of a directory entry. It's used
1672 * by rename to change ".." when a directory is moved.
1673 * Assumes a glock is held on dvp.
1674 *
1675 * Returns: errno
1676 */
1677
Steven Whitehousec7526662006-03-20 12:30:04 -05001678int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
Al Viro629a21e2006-10-13 22:51:24 -04001679 struct gfs2_inum_host *inum, unsigned int new_type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001680{
Steven Whitehousec7526662006-03-20 12:30:04 -05001681 struct buffer_head *bh;
1682 struct gfs2_dirent *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001683 int error;
1684
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001685 dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001686 if (!dent) {
1687 gfs2_consist_inode(dip);
1688 return -EIO;
1689 }
1690 if (IS_ERR(dent))
1691 return PTR_ERR(dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001692
Steven Whitehousec7526662006-03-20 12:30:04 -05001693 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1694 gfs2_inum_out(inum, (char *)&dent->de_inum);
1695 dent->de_type = cpu_to_be16(new_type);
1696
1697 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1698 brelse(bh);
1699 error = gfs2_meta_inode_buffer(dip, &bh);
1700 if (error)
1701 return error;
1702 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1703 }
1704
Steven Whitehouse1a7b1ee2006-11-01 14:35:17 -05001705 dip->i_inode.i_mtime.tv_sec = dip->i_inode.i_ctime.tv_sec = get_seconds();
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001706 gfs2_dinode_out(dip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001707 brelse(bh);
1708 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001709}
1710
1711/**
1712 * foreach_leaf - call a function for each leaf in a directory
1713 * @dip: the directory
1714 * @lc: the function to call for each each
1715 * @data: private data to pass to it
1716 *
1717 * Returns: errno
1718 */
1719
1720static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
1721{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001722 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001723 struct buffer_head *bh;
Steven Whitehousec7526662006-03-20 12:30:04 -05001724 struct gfs2_leaf *leaf;
Steven Whitehousecd915492006-09-04 12:49:07 -04001725 u32 hsize, len;
1726 u32 ht_offset, lp_offset, ht_offset_cur = -1;
1727 u32 index = 0;
Al Virob44b84d2006-10-14 10:46:30 -04001728 __be64 *lp;
Steven Whitehousecd915492006-09-04 12:49:07 -04001729 u64 leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001730 int error = 0;
1731
1732 hsize = 1 << dip->i_di.di_depth;
Steven Whitehousecd915492006-09-04 12:49:07 -04001733 if (hsize * sizeof(u64) != dip->i_di.di_size) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001734 gfs2_consist_inode(dip);
1735 return -EIO;
1736 }
1737
1738 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1739 if (!lp)
1740 return -ENOMEM;
1741
1742 while (index < hsize) {
1743 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1744 ht_offset = index - lp_offset;
1745
1746 if (ht_offset_cur != ht_offset) {
Steven Whitehousee13940b2006-01-30 13:31:50 +00001747 error = gfs2_dir_read_data(dip, (char *)lp,
Al Virob44b84d2006-10-14 10:46:30 -04001748 ht_offset * sizeof(__be64),
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001749 sdp->sd_hash_bsize, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001750 if (error != sdp->sd_hash_bsize) {
1751 if (error >= 0)
1752 error = -EIO;
1753 goto out;
1754 }
1755 ht_offset_cur = ht_offset;
1756 }
1757
1758 leaf_no = be64_to_cpu(lp[lp_offset]);
1759 if (leaf_no) {
1760 error = get_leaf(dip, leaf_no, &bh);
1761 if (error)
1762 goto out;
Steven Whitehousec7526662006-03-20 12:30:04 -05001763 leaf = (struct gfs2_leaf *)bh->b_data;
Steven Whitehousec7526662006-03-20 12:30:04 -05001764 len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth));
Steven Whitehouse634ee0b2006-07-17 09:32:37 -04001765 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001766
1767 error = lc(dip, index, len, leaf_no, data);
1768 if (error)
1769 goto out;
1770
1771 index = (index & ~(len - 1)) + len;
1772 } else
1773 index++;
1774 }
1775
1776 if (index != hsize) {
1777 gfs2_consist_inode(dip);
1778 error = -EIO;
1779 }
1780
Steven Whitehouse634ee0b2006-07-17 09:32:37 -04001781out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001782 kfree(lp);
1783
1784 return error;
1785}
1786
1787/**
1788 * leaf_dealloc - Deallocate a directory leaf
1789 * @dip: the directory
1790 * @index: the hash table offset in the directory
1791 * @len: the number of pointers to this leaf
1792 * @leaf_no: the leaf number
1793 * @data: not used
1794 *
1795 * Returns: errno
1796 */
1797
Steven Whitehousecd915492006-09-04 12:49:07 -04001798static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
1799 u64 leaf_no, void *data)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001800{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001801 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001802 struct gfs2_leaf *tmp_leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001803 struct gfs2_rgrp_list rlist;
1804 struct buffer_head *bh, *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001805 u64 blk, nblk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001806 unsigned int rg_blocks = 0, l_blocks = 0;
1807 char *ht;
Steven Whitehousecd915492006-09-04 12:49:07 -04001808 unsigned int x, size = len * sizeof(u64);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001809 int error;
1810
1811 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1812
1813 ht = kzalloc(size, GFP_KERNEL);
1814 if (!ht)
1815 return -ENOMEM;
1816
1817 gfs2_alloc_get(dip);
1818
1819 error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1820 if (error)
1821 goto out;
1822
1823 error = gfs2_rindex_hold(sdp, &dip->i_alloc.al_ri_gh);
1824 if (error)
1825 goto out_qs;
1826
1827 /* Count the number of leaves */
1828
Steven Whitehousec7526662006-03-20 12:30:04 -05001829 for (blk = leaf_no; blk; blk = nblk) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001830 error = get_leaf(dip, blk, &bh);
1831 if (error)
1832 goto out_rlist;
Steven Whitehousec7526662006-03-20 12:30:04 -05001833 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1834 nblk = be64_to_cpu(tmp_leaf->lf_next);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001835 brelse(bh);
1836
1837 gfs2_rlist_add(sdp, &rlist, blk);
1838 l_blocks++;
1839 }
1840
1841 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1842
1843 for (x = 0; x < rlist.rl_rgrps; x++) {
1844 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001845 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001846 rg_blocks += rgd->rd_ri.ri_length;
1847 }
1848
1849 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1850 if (error)
1851 goto out_rlist;
1852
1853 error = gfs2_trans_begin(sdp,
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001854 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
David Teiglandb3b94fa2006-01-16 16:50:04 +00001855 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1856 if (error)
1857 goto out_rg_gunlock;
1858
Steven Whitehousec7526662006-03-20 12:30:04 -05001859 for (blk = leaf_no; blk; blk = nblk) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001860 error = get_leaf(dip, blk, &bh);
1861 if (error)
1862 goto out_end_trans;
Steven Whitehousec7526662006-03-20 12:30:04 -05001863 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1864 nblk = be64_to_cpu(tmp_leaf->lf_next);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001865 brelse(bh);
1866
1867 gfs2_free_meta(dip, blk, 1);
1868
1869 if (!dip->i_di.di_blocks)
1870 gfs2_consist_inode(dip);
1871 dip->i_di.di_blocks--;
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -05001872 gfs2_set_inode_blocks(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001873 }
1874
Steven Whitehousecd915492006-09-04 12:49:07 -04001875 error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001876 if (error != size) {
1877 if (error >= 0)
1878 error = -EIO;
1879 goto out_end_trans;
1880 }
1881
1882 error = gfs2_meta_inode_buffer(dip, &dibh);
1883 if (error)
1884 goto out_end_trans;
1885
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001886 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001887 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001888 brelse(dibh);
1889
Steven Whitehousea91ea692006-09-04 12:04:26 -04001890out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001891 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001892out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001893 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001894out_rlist:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001895 gfs2_rlist_free(&rlist);
1896 gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001897out_qs:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001898 gfs2_quota_unhold(dip);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001899out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001900 gfs2_alloc_put(dip);
1901 kfree(ht);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001902 return error;
1903}
1904
1905/**
1906 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1907 * @dip: the directory
1908 *
1909 * Dealloc all on-disk directory leaves to FREEMETA state
1910 * Change on-disk inode type to "regular file"
1911 *
1912 * Returns: errno
1913 */
1914
1915int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
1916{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001917 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001918 struct buffer_head *bh;
1919 int error;
1920
1921 /* Dealloc on-disk leaves to FREEMETA state */
1922 error = foreach_leaf(dip, leaf_dealloc, NULL);
1923 if (error)
1924 return error;
1925
1926 /* Make this a regular file in case we crash.
1927 (We don't want to free these blocks a second time.) */
1928
1929 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1930 if (error)
1931 return error;
1932
1933 error = gfs2_meta_inode_buffer(dip, &bh);
1934 if (!error) {
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001935 gfs2_trans_add_bh(dip->i_gl, bh, 1);
Steven Whitehouse568f4c92006-02-27 12:00:42 -05001936 ((struct gfs2_dinode *)bh->b_data)->di_mode =
1937 cpu_to_be32(S_IFREG);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001938 brelse(bh);
1939 }
1940
1941 gfs2_trans_end(sdp);
1942
1943 return error;
1944}
1945
1946/**
1947 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
1948 * @ip: the file being written to
1949 * @filname: the filename that's going to be added
David Teiglandb3b94fa2006-01-16 16:50:04 +00001950 *
Steven Whitehousec7526662006-03-20 12:30:04 -05001951 * Returns: 1 if alloc required, 0 if not, -ve on error
David Teiglandb3b94fa2006-01-16 16:50:04 +00001952 */
1953
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04001954int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001955{
Steven Whitehousec7526662006-03-20 12:30:04 -05001956 struct gfs2_dirent *dent;
1957 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001958
Steven Whitehousec7526662006-03-20 12:30:04 -05001959 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
Steven Whitehouseed386502006-04-07 16:28:07 -04001960 if (!dent) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001961 return 1;
Steven Whitehouseed386502006-04-07 16:28:07 -04001962 }
Steven Whitehousec7526662006-03-20 12:30:04 -05001963 if (IS_ERR(dent))
1964 return PTR_ERR(dent);
1965 brelse(bh);
1966 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001967}
1968