blob: c93ca8f361b55fa09bcd72cd9a2d21e1a3014393 [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;
Eric Sandeenddfe0622007-01-18 16:41:23 -0600134 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME_SEC;
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;
Eric Sandeenddfe0622007-01-18 16:41:23 -0600232 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME_SEC;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000233
234 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500235 gfs2_dinode_out(ip, dibh->b_data);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000236 brelse(dibh);
237
238 return copied;
239fail:
240 if (copied)
241 goto out;
242 return error;
243}
244
245static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf,
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400246 u64 offset, unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000247{
248 struct buffer_head *dibh;
249 int error;
250
251 error = gfs2_meta_inode_buffer(ip, &dibh);
252 if (!error) {
253 offset += sizeof(struct gfs2_dinode);
254 memcpy(buf, dibh->b_data + offset, size);
255 brelse(dibh);
256 }
257
258 return (error) ? error : size;
259}
260
261
262/**
263 * gfs2_dir_read_data - Read a data from a directory inode
264 * @ip: The GFS2 Inode
265 * @buf: The buffer to place result into
266 * @offset: File offset to begin jdata_readng from
267 * @size: Amount of data to transfer
268 *
269 * Returns: The amount of data actually copied or the error
270 */
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400271static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf, u64 offset,
272 unsigned int size, unsigned ra)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000273{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400274 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehousecd915492006-09-04 12:49:07 -0400275 u64 lblock, dblock;
276 u32 extlen = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000277 unsigned int o;
278 int copied = 0;
279 int error = 0;
280
281 if (offset >= ip->i_di.di_size)
282 return 0;
283
Jan Engelhardtc5392122006-09-05 14:30:40 +0200284 if (offset + size > ip->i_di.di_size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000285 size = ip->i_di.di_size - offset;
286
287 if (!size)
288 return 0;
289
290 if (gfs2_is_stuffed(ip))
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400291 return gfs2_dir_read_stuffed(ip, buf, offset, size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000292
293 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
294 return -EINVAL;
295
296 lblock = offset;
297 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
298
299 while (copied < size) {
300 unsigned int amount;
301 struct buffer_head *bh;
302 int new;
303
304 amount = size - copied;
305 if (amount > sdp->sd_sb.sb_bsize - o)
306 amount = sdp->sd_sb.sb_bsize - o;
307
308 if (!extlen) {
309 new = 0;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400310 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
Steven Whitehousefd88de562006-05-05 16:59:11 -0400311 &dblock, &extlen);
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400312 if (error || !dblock)
313 goto fail;
314 BUG_ON(extlen < 1);
315 if (!ra)
316 extlen = 1;
317 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
Adrian Bunkb7d8ac32006-10-19 16:02:07 +0200318 } else {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400319 error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, &bh);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000320 if (error)
321 goto fail;
322 }
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400323 error = gfs2_metatype_check(sdp, bh, GFS2_METATYPE_JD);
324 if (error) {
325 brelse(bh);
326 goto fail;
327 }
328 dblock++;
329 extlen--;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000330 memcpy(buf, bh->b_data + o, amount);
331 brelse(bh);
Steven Whitehouse899bb262006-08-01 15:28:57 -0400332 buf += amount;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000333 copied += amount;
334 lblock++;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000335 o = sizeof(struct gfs2_meta_header);
336 }
337
338 return copied;
339fail:
340 return (copied) ? copied : error;
341}
342
Steven 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 Engelhardtc5392122006-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,
Steven Whitehouse3699e3a2007-01-17 15:09:20 +00001201 void *opaque, 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;
Steven Whitehousecd915492006-09-04 12:49:07 -04001206 u64 off, off_next;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001207 unsigned int x, y;
1208 int run = 0;
1209 int error = 0;
1210
1211 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1212
1213 dent_next = darr[0];
1214 off_next = be32_to_cpu(dent_next->de_hash);
1215 off_next = gfs2_disk_hash2offset(off_next);
1216
1217 for (x = 0, y = 1; x < entries; x++, y++) {
1218 dent = dent_next;
1219 off = off_next;
1220
1221 if (y < entries) {
1222 dent_next = darr[y];
1223 off_next = be32_to_cpu(dent_next->de_hash);
1224 off_next = gfs2_disk_hash2offset(off_next);
1225
1226 if (off < *offset)
1227 continue;
1228 *offset = off;
1229
1230 if (off_next == off) {
1231 if (*copied && !run)
1232 return 1;
1233 run = 1;
1234 } else
1235 run = 0;
1236 } else {
1237 if (off < *offset)
1238 continue;
1239 *offset = off;
1240 }
1241
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001242 error = filldir(opaque, (const char *)(dent + 1),
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001243 be16_to_cpu(dent->de_name_len),
Steven Whitehouse3699e3a2007-01-17 15:09:20 +00001244 off, be64_to_cpu(dent->de_inum.no_addr),
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001245 be16_to_cpu(dent->de_type));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001246 if (error)
1247 return 1;
1248
1249 *copied = 1;
1250 }
1251
1252 /* Increment the *offset by one, so the next time we come into the
1253 do_filldir fxn, we get the next entry instead of the last one in the
1254 current leaf */
1255
1256 (*offset)++;
1257
1258 return 0;
1259}
1260
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001261static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
Steven Whitehouse3699e3a2007-01-17 15:09:20 +00001262 filldir_t filldir, int *copied, unsigned *depth,
1263 u64 leaf_no)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001264{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001265 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001266 struct buffer_head *bh;
1267 struct gfs2_leaf *lf;
1268 unsigned entries = 0;
1269 unsigned leaves = 0;
1270 const struct gfs2_dirent **darr, *dent;
1271 struct dirent_gather g;
1272 struct buffer_head **larr;
1273 int leaf = 0;
1274 int error, i;
1275 u64 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001276
David Teiglandb3b94fa2006-01-16 16:50:04 +00001277 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001278 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001279 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001280 goto out;
1281 lf = (struct gfs2_leaf *)bh->b_data;
1282 if (leaves == 0)
1283 *depth = be16_to_cpu(lf->lf_depth);
1284 entries += be16_to_cpu(lf->lf_entries);
1285 leaves++;
1286 lfn = be64_to_cpu(lf->lf_next);
1287 brelse(bh);
1288 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001289
1290 if (!entries)
1291 return 0;
1292
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001293 error = -ENOMEM;
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001294 larr = vmalloc((leaves + entries) * sizeof(void *));
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001295 if (!larr)
1296 goto out;
1297 darr = (const struct gfs2_dirent **)(larr + leaves);
1298 g.pdent = darr;
1299 g.offset = 0;
1300 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001301
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001302 do {
1303 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001304 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001305 goto out_kfree;
1306 lf = (struct gfs2_leaf *)bh->b_data;
1307 lfn = be64_to_cpu(lf->lf_next);
1308 if (lf->lf_entries) {
1309 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1310 gfs2_dirent_gather, NULL, &g);
1311 error = PTR_ERR(dent);
1312 if (IS_ERR(dent)) {
1313 goto out_kfree;
1314 }
1315 error = 0;
1316 larr[leaf++] = bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001317 } else {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001318 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001319 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001320 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001321
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001322 error = do_filldir_main(ip, offset, opaque, filldir, darr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001323 entries, copied);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001324out_kfree:
1325 for(i = 0; i < leaf; i++)
1326 brelse(larr[i]);
Steven Whitehousefe1bded2006-04-18 10:09:15 -04001327 vfree(larr);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001328out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001329 return error;
1330}
1331
1332/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001333 * dir_e_read - Reads the entries from a directory into a filldir buffer
1334 * @dip: dinode pointer
1335 * @offset: the hash of the last entry read shifted to the right once
1336 * @opaque: buffer for the filldir function to fill
1337 * @filldir: points to the filldir function to use
1338 *
1339 * Returns: errno
1340 */
1341
Steven Whitehousecd915492006-09-04 12:49:07 -04001342static int dir_e_read(struct inode *inode, u64 *offset, void *opaque,
Steven Whitehouse3699e3a2007-01-17 15:09:20 +00001343 filldir_t filldir)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001344{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001345 struct gfs2_inode *dip = GFS2_I(inode);
1346 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehousecd915492006-09-04 12:49:07 -04001347 u32 hsize, len = 0;
1348 u32 ht_offset, lp_offset, ht_offset_cur = -1;
1349 u32 hash, index;
Al Virob44b84d2006-10-14 10:46:30 -04001350 __be64 *lp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001351 int copied = 0;
1352 int error = 0;
Steven Whitehouse4da3c642006-07-11 13:19:13 -04001353 unsigned depth = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001354
1355 hsize = 1 << dip->i_di.di_depth;
Steven Whitehousecd915492006-09-04 12:49:07 -04001356 if (hsize * sizeof(u64) != dip->i_di.di_size) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001357 gfs2_consist_inode(dip);
1358 return -EIO;
1359 }
1360
1361 hash = gfs2_dir_offset2hash(*offset);
1362 index = hash >> (32 - dip->i_di.di_depth);
1363
1364 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1365 if (!lp)
1366 return -ENOMEM;
1367
1368 while (index < hsize) {
1369 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1370 ht_offset = index - lp_offset;
1371
1372 if (ht_offset_cur != ht_offset) {
Steven Whitehousee13940b2006-01-30 13:31:50 +00001373 error = gfs2_dir_read_data(dip, (char *)lp,
Al Virob44b84d2006-10-14 10:46:30 -04001374 ht_offset * sizeof(__be64),
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001375 sdp->sd_hash_bsize, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001376 if (error != sdp->sd_hash_bsize) {
1377 if (error >= 0)
1378 error = -EIO;
1379 goto out;
1380 }
1381 ht_offset_cur = ht_offset;
1382 }
1383
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001384 error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
1385 &copied, &depth,
1386 be64_to_cpu(lp[lp_offset]));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001387 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001388 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001389
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001390 len = 1 << (dip->i_di.di_depth - depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001391 index = (index & ~(len - 1)) + len;
1392 }
1393
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001394out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001395 kfree(lp);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001396 if (error > 0)
1397 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001398 return error;
1399}
1400
Steven Whitehousecd915492006-09-04 12:49:07 -04001401int gfs2_dir_read(struct inode *inode, u64 *offset, void *opaque,
Steven Whitehouse3699e3a2007-01-17 15:09:20 +00001402 filldir_t filldir)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001403{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001404 struct gfs2_inode *dip = GFS2_I(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001405 struct dirent_gather g;
1406 const struct gfs2_dirent **darr, *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001407 struct buffer_head *dibh;
1408 int copied = 0;
1409 int error;
1410
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001411 if (!dip->i_di.di_entries)
1412 return 0;
1413
1414 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
1415 return dir_e_read(inode, offset, opaque, filldir);
1416
David Teiglandb3b94fa2006-01-16 16:50:04 +00001417 if (!gfs2_is_stuffed(dip)) {
1418 gfs2_consist_inode(dip);
1419 return -EIO;
1420 }
1421
David Teiglandb3b94fa2006-01-16 16:50:04 +00001422 error = gfs2_meta_inode_buffer(dip, &dibh);
1423 if (error)
1424 return error;
1425
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001426 error = -ENOMEM;
1427 darr = kmalloc(dip->i_di.di_entries * sizeof(struct gfs2_dirent *),
1428 GFP_KERNEL);
1429 if (darr) {
1430 g.pdent = darr;
1431 g.offset = 0;
1432 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1433 gfs2_dirent_gather, NULL, &g);
1434 if (IS_ERR(dent)) {
1435 error = PTR_ERR(dent);
1436 goto out;
1437 }
1438 error = do_filldir_main(dip, offset, opaque, filldir, darr,
1439 dip->i_di.di_entries, &copied);
1440out:
1441 kfree(darr);
1442 }
1443
David Teiglandb3b94fa2006-01-16 16:50:04 +00001444 if (error > 0)
1445 error = 0;
1446
1447 brelse(dibh);
1448
1449 return error;
1450}
1451
David Teiglandb3b94fa2006-01-16 16:50:04 +00001452/**
1453 * gfs2_dir_search - Search a directory
1454 * @dip: The GFS2 inode
1455 * @filename:
1456 * @inode:
1457 *
1458 * This routine searches a directory for a file or another directory.
1459 * Assumes a glock is held on dip.
1460 *
1461 * Returns: errno
1462 */
1463
Steven Whitehousec7526662006-03-20 12:30:04 -05001464int gfs2_dir_search(struct inode *dir, const struct qstr *name,
Al Viro629a21e2006-10-13 22:51:24 -04001465 struct gfs2_inum_host *inum, unsigned int *type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001466{
Steven Whitehousec7526662006-03-20 12:30:04 -05001467 struct buffer_head *bh;
1468 struct gfs2_dirent *dent;
1469
1470 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1471 if (dent) {
1472 if (IS_ERR(dent))
1473 return PTR_ERR(dent);
1474 if (inum)
1475 gfs2_inum_in(inum, (char *)&dent->de_inum);
1476 if (type)
1477 *type = be16_to_cpu(dent->de_type);
1478 brelse(bh);
1479 return 0;
1480 }
1481 return -ENOENT;
1482}
1483
1484static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1485{
1486 struct buffer_head *bh, *obh;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001487 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001488 struct gfs2_leaf *leaf, *oleaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001489 int error;
Steven Whitehousec7526662006-03-20 12:30:04 -05001490 u32 index;
1491 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001492
Steven Whitehousec7526662006-03-20 12:30:04 -05001493 index = name->hash >> (32 - ip->i_di.di_depth);
1494 error = get_first_leaf(ip, index, &obh);
1495 if (error)
1496 return error;
1497 do {
1498 oleaf = (struct gfs2_leaf *)obh->b_data;
1499 bn = be64_to_cpu(oleaf->lf_next);
1500 if (!bn)
1501 break;
1502 brelse(obh);
1503 error = get_leaf(ip, bn, &obh);
1504 if (error)
1505 return error;
1506 } while(1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001507
Steven Whitehousec7526662006-03-20 12:30:04 -05001508 gfs2_trans_add_bh(ip->i_gl, obh, 1);
1509
1510 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1511 if (!leaf) {
1512 brelse(obh);
1513 return -ENOSPC;
1514 }
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04001515 oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
Steven Whitehousec7526662006-03-20 12:30:04 -05001516 brelse(bh);
1517 brelse(obh);
1518
1519 error = gfs2_meta_inode_buffer(ip, &bh);
1520 if (error)
1521 return error;
1522 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1523 ip->i_di.di_blocks++;
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -05001524 gfs2_set_inode_blocks(&ip->i_inode);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001525 gfs2_dinode_out(ip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001526 brelse(bh);
1527 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001528}
1529
1530/**
1531 * gfs2_dir_add - Add new filename into directory
1532 * @dip: The GFS2 inode
1533 * @filename: The new name
1534 * @inode: The inode number of the entry
1535 * @type: The type of the entry
1536 *
1537 * Returns: 0 on success, error code on failure
1538 */
1539
Steven Whitehousec7526662006-03-20 12:30:04 -05001540int gfs2_dir_add(struct inode *inode, const struct qstr *name,
Al Viro629a21e2006-10-13 22:51:24 -04001541 const struct gfs2_inum_host *inum, unsigned type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001542{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001543 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001544 struct buffer_head *bh;
1545 struct gfs2_dirent *dent;
1546 struct gfs2_leaf *leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001547 int error;
1548
Steven Whitehousec7526662006-03-20 12:30:04 -05001549 while(1) {
1550 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
1551 &bh);
1552 if (dent) {
1553 if (IS_ERR(dent))
1554 return PTR_ERR(dent);
1555 dent = gfs2_init_dirent(inode, dent, name, bh);
1556 gfs2_inum_out(inum, (char *)&dent->de_inum);
1557 dent->de_type = cpu_to_be16(type);
1558 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
1559 leaf = (struct gfs2_leaf *)bh->b_data;
1560 leaf->lf_entries = cpu_to_be16(be16_to_cpu(leaf->lf_entries) + 1);
1561 }
1562 brelse(bh);
1563 error = gfs2_meta_inode_buffer(ip, &bh);
1564 if (error)
1565 break;
1566 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1567 ip->i_di.di_entries++;
Eric Sandeenddfe0622007-01-18 16:41:23 -06001568 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME_SEC;
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001569 gfs2_dinode_out(ip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001570 brelse(bh);
1571 error = 0;
1572 break;
1573 }
1574 if (!(ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
1575 error = dir_make_exhash(inode);
1576 if (error)
1577 break;
1578 continue;
1579 }
1580 error = dir_split_leaf(inode, name);
1581 if (error == 0)
1582 continue;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001583 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001584 break;
1585 if (ip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) {
1586 error = dir_double_exhash(ip);
1587 if (error)
1588 break;
1589 error = dir_split_leaf(inode, name);
Steven Whitehousee90deff2006-03-29 19:02:15 -05001590 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001591 break;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001592 if (error == 0)
1593 continue;
Steven Whitehousec7526662006-03-20 12:30:04 -05001594 }
1595 error = dir_new_leaf(inode, name);
1596 if (!error)
1597 continue;
1598 error = -ENOSPC;
1599 break;
1600 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001601 return error;
1602}
1603
Steven Whitehousec7526662006-03-20 12:30:04 -05001604
David Teiglandb3b94fa2006-01-16 16:50:04 +00001605/**
1606 * gfs2_dir_del - Delete a directory entry
1607 * @dip: The GFS2 inode
1608 * @filename: The filename
1609 *
1610 * Returns: 0 on success, error code on failure
1611 */
1612
Steven Whitehousec7526662006-03-20 12:30:04 -05001613int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001614{
Steven Whitehousec7526662006-03-20 12:30:04 -05001615 struct gfs2_dirent *dent, *prev = NULL;
1616 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001617 int error;
1618
Steven Whitehousec7526662006-03-20 12:30:04 -05001619 /* Returns _either_ the entry (if its first in block) or the
1620 previous entry otherwise */
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001621 dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001622 if (!dent) {
1623 gfs2_consist_inode(dip);
1624 return -EIO;
1625 }
1626 if (IS_ERR(dent)) {
1627 gfs2_consist_inode(dip);
1628 return PTR_ERR(dent);
1629 }
1630 /* If not first in block, adjust pointers accordingly */
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001631 if (gfs2_dirent_find(dent, name, NULL) == 0) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001632 prev = dent;
1633 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1634 }
1635
1636 dirent_del(dip, bh, prev, dent);
1637 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1638 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1639 u16 entries = be16_to_cpu(leaf->lf_entries);
1640 if (!entries)
1641 gfs2_consist_inode(dip);
1642 leaf->lf_entries = cpu_to_be16(--entries);
Steven Whitehousec7526662006-03-20 12:30:04 -05001643 }
Steven Whitehouseed386502006-04-07 16:28:07 -04001644 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001645
1646 error = gfs2_meta_inode_buffer(dip, &bh);
1647 if (error)
1648 return error;
1649
1650 if (!dip->i_di.di_entries)
1651 gfs2_consist_inode(dip);
1652 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1653 dip->i_di.di_entries--;
Eric Sandeenddfe0622007-01-18 16:41:23 -06001654 dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME_SEC;
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001655 gfs2_dinode_out(dip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001656 brelse(bh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001657 mark_inode_dirty(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001658
1659 return error;
1660}
1661
David Teiglandb3b94fa2006-01-16 16:50:04 +00001662/**
1663 * gfs2_dir_mvino - Change inode number of directory entry
1664 * @dip: The GFS2 inode
1665 * @filename:
1666 * @new_inode:
1667 *
1668 * This routine changes the inode number of a directory entry. It's used
1669 * by rename to change ".." when a directory is moved.
1670 * Assumes a glock is held on dvp.
1671 *
1672 * Returns: errno
1673 */
1674
Steven Whitehousec7526662006-03-20 12:30:04 -05001675int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
Al Viro629a21e2006-10-13 22:51:24 -04001676 struct gfs2_inum_host *inum, unsigned int new_type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001677{
Steven Whitehousec7526662006-03-20 12:30:04 -05001678 struct buffer_head *bh;
1679 struct gfs2_dirent *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001680 int error;
1681
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001682 dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001683 if (!dent) {
1684 gfs2_consist_inode(dip);
1685 return -EIO;
1686 }
1687 if (IS_ERR(dent))
1688 return PTR_ERR(dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001689
Steven Whitehousec7526662006-03-20 12:30:04 -05001690 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1691 gfs2_inum_out(inum, (char *)&dent->de_inum);
1692 dent->de_type = cpu_to_be16(new_type);
1693
1694 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1695 brelse(bh);
1696 error = gfs2_meta_inode_buffer(dip, &bh);
1697 if (error)
1698 return error;
1699 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1700 }
1701
Eric Sandeenddfe0622007-01-18 16:41:23 -06001702 dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME_SEC;
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001703 gfs2_dinode_out(dip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001704 brelse(bh);
1705 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001706}
1707
1708/**
1709 * foreach_leaf - call a function for each leaf in a directory
1710 * @dip: the directory
1711 * @lc: the function to call for each each
1712 * @data: private data to pass to it
1713 *
1714 * Returns: errno
1715 */
1716
1717static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
1718{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001719 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001720 struct buffer_head *bh;
Steven Whitehousec7526662006-03-20 12:30:04 -05001721 struct gfs2_leaf *leaf;
Steven Whitehousecd915492006-09-04 12:49:07 -04001722 u32 hsize, len;
1723 u32 ht_offset, lp_offset, ht_offset_cur = -1;
1724 u32 index = 0;
Al Virob44b84d2006-10-14 10:46:30 -04001725 __be64 *lp;
Steven Whitehousecd915492006-09-04 12:49:07 -04001726 u64 leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001727 int error = 0;
1728
1729 hsize = 1 << dip->i_di.di_depth;
Steven Whitehousecd915492006-09-04 12:49:07 -04001730 if (hsize * sizeof(u64) != dip->i_di.di_size) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001731 gfs2_consist_inode(dip);
1732 return -EIO;
1733 }
1734
1735 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1736 if (!lp)
1737 return -ENOMEM;
1738
1739 while (index < hsize) {
1740 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1741 ht_offset = index - lp_offset;
1742
1743 if (ht_offset_cur != ht_offset) {
Steven Whitehousee13940b2006-01-30 13:31:50 +00001744 error = gfs2_dir_read_data(dip, (char *)lp,
Al Virob44b84d2006-10-14 10:46:30 -04001745 ht_offset * sizeof(__be64),
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001746 sdp->sd_hash_bsize, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001747 if (error != sdp->sd_hash_bsize) {
1748 if (error >= 0)
1749 error = -EIO;
1750 goto out;
1751 }
1752 ht_offset_cur = ht_offset;
1753 }
1754
1755 leaf_no = be64_to_cpu(lp[lp_offset]);
1756 if (leaf_no) {
1757 error = get_leaf(dip, leaf_no, &bh);
1758 if (error)
1759 goto out;
Steven Whitehousec7526662006-03-20 12:30:04 -05001760 leaf = (struct gfs2_leaf *)bh->b_data;
Steven Whitehousec7526662006-03-20 12:30:04 -05001761 len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth));
Steven Whitehouse634ee0b2006-07-17 09:32:37 -04001762 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001763
1764 error = lc(dip, index, len, leaf_no, data);
1765 if (error)
1766 goto out;
1767
1768 index = (index & ~(len - 1)) + len;
1769 } else
1770 index++;
1771 }
1772
1773 if (index != hsize) {
1774 gfs2_consist_inode(dip);
1775 error = -EIO;
1776 }
1777
Steven Whitehouse634ee0b2006-07-17 09:32:37 -04001778out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001779 kfree(lp);
1780
1781 return error;
1782}
1783
1784/**
1785 * leaf_dealloc - Deallocate a directory leaf
1786 * @dip: the directory
1787 * @index: the hash table offset in the directory
1788 * @len: the number of pointers to this leaf
1789 * @leaf_no: the leaf number
1790 * @data: not used
1791 *
1792 * Returns: errno
1793 */
1794
Steven Whitehousecd915492006-09-04 12:49:07 -04001795static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
1796 u64 leaf_no, void *data)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001797{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001798 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001799 struct gfs2_leaf *tmp_leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001800 struct gfs2_rgrp_list rlist;
1801 struct buffer_head *bh, *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001802 u64 blk, nblk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001803 unsigned int rg_blocks = 0, l_blocks = 0;
1804 char *ht;
Steven Whitehousecd915492006-09-04 12:49:07 -04001805 unsigned int x, size = len * sizeof(u64);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001806 int error;
1807
1808 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1809
1810 ht = kzalloc(size, GFP_KERNEL);
1811 if (!ht)
1812 return -ENOMEM;
1813
1814 gfs2_alloc_get(dip);
1815
1816 error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1817 if (error)
1818 goto out;
1819
1820 error = gfs2_rindex_hold(sdp, &dip->i_alloc.al_ri_gh);
1821 if (error)
1822 goto out_qs;
1823
1824 /* Count the number of leaves */
1825
Steven Whitehousec7526662006-03-20 12:30:04 -05001826 for (blk = leaf_no; blk; blk = nblk) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001827 error = get_leaf(dip, blk, &bh);
1828 if (error)
1829 goto out_rlist;
Steven Whitehousec7526662006-03-20 12:30:04 -05001830 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1831 nblk = be64_to_cpu(tmp_leaf->lf_next);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001832 brelse(bh);
1833
1834 gfs2_rlist_add(sdp, &rlist, blk);
1835 l_blocks++;
1836 }
1837
1838 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1839
1840 for (x = 0; x < rlist.rl_rgrps; x++) {
1841 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001842 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001843 rg_blocks += rgd->rd_ri.ri_length;
1844 }
1845
1846 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1847 if (error)
1848 goto out_rlist;
1849
1850 error = gfs2_trans_begin(sdp,
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001851 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
David Teiglandb3b94fa2006-01-16 16:50:04 +00001852 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1853 if (error)
1854 goto out_rg_gunlock;
1855
Steven Whitehousec7526662006-03-20 12:30:04 -05001856 for (blk = leaf_no; blk; blk = nblk) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001857 error = get_leaf(dip, blk, &bh);
1858 if (error)
1859 goto out_end_trans;
Steven Whitehousec7526662006-03-20 12:30:04 -05001860 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1861 nblk = be64_to_cpu(tmp_leaf->lf_next);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001862 brelse(bh);
1863
1864 gfs2_free_meta(dip, blk, 1);
1865
1866 if (!dip->i_di.di_blocks)
1867 gfs2_consist_inode(dip);
1868 dip->i_di.di_blocks--;
Steven Whitehouse9e2dbda2006-11-08 15:45:46 -05001869 gfs2_set_inode_blocks(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001870 }
1871
Steven Whitehousecd915492006-09-04 12:49:07 -04001872 error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001873 if (error != size) {
1874 if (error >= 0)
1875 error = -EIO;
1876 goto out_end_trans;
1877 }
1878
1879 error = gfs2_meta_inode_buffer(dip, &dibh);
1880 if (error)
1881 goto out_end_trans;
1882
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001883 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001884 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001885 brelse(dibh);
1886
Steven Whitehousea91ea692006-09-04 12:04:26 -04001887out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001888 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001889out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001890 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001891out_rlist:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001892 gfs2_rlist_free(&rlist);
1893 gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001894out_qs:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001895 gfs2_quota_unhold(dip);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001896out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001897 gfs2_alloc_put(dip);
1898 kfree(ht);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001899 return error;
1900}
1901
1902/**
1903 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1904 * @dip: the directory
1905 *
1906 * Dealloc all on-disk directory leaves to FREEMETA state
1907 * Change on-disk inode type to "regular file"
1908 *
1909 * Returns: errno
1910 */
1911
1912int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
1913{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001914 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001915 struct buffer_head *bh;
1916 int error;
1917
1918 /* Dealloc on-disk leaves to FREEMETA state */
1919 error = foreach_leaf(dip, leaf_dealloc, NULL);
1920 if (error)
1921 return error;
1922
1923 /* Make this a regular file in case we crash.
1924 (We don't want to free these blocks a second time.) */
1925
1926 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1927 if (error)
1928 return error;
1929
1930 error = gfs2_meta_inode_buffer(dip, &bh);
1931 if (!error) {
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001932 gfs2_trans_add_bh(dip->i_gl, bh, 1);
Steven Whitehouse568f4c92006-02-27 12:00:42 -05001933 ((struct gfs2_dinode *)bh->b_data)->di_mode =
1934 cpu_to_be32(S_IFREG);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001935 brelse(bh);
1936 }
1937
1938 gfs2_trans_end(sdp);
1939
1940 return error;
1941}
1942
1943/**
1944 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
1945 * @ip: the file being written to
1946 * @filname: the filename that's going to be added
David Teiglandb3b94fa2006-01-16 16:50:04 +00001947 *
Steven Whitehousec7526662006-03-20 12:30:04 -05001948 * Returns: 1 if alloc required, 0 if not, -ve on error
David Teiglandb3b94fa2006-01-16 16:50:04 +00001949 */
1950
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04001951int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001952{
Steven Whitehousec7526662006-03-20 12:30:04 -05001953 struct gfs2_dirent *dent;
1954 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001955
Steven Whitehousec7526662006-03-20 12:30:04 -05001956 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
Steven Whitehouseed386502006-04-07 16:28:07 -04001957 if (!dent) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001958 return 1;
Steven Whitehouseed386502006-04-07 16:28:07 -04001959 }
Steven Whitehousec7526662006-03-20 12:30:04 -05001960 if (IS_ERR(dent))
1961 return PTR_ERR(dent);
1962 brelse(bh);
1963 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001964}
1965