blob: 091ee4779538fc5dd5b7e996007de8b2d18f9f8d [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 *
Steven Whitehouse383f01f2008-11-04 10:05:22 +000039 * dip->i_diskflags & GFS2_DIF_EXHASH is true
Steven Whitehouse61e085a2006-04-24 10:07:13 -040040 *
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
David Teiglandb3b94fa2006-01-16 16:50:04 +000056#include <linux/slab.h>
57#include <linux/spinlock.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000058#include <linux/buffer_head.h>
59#include <linux/sort.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050060#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050061#include <linux/crc32.h>
Steven Whitehousefe1bded2006-04-18 10:09:15 -040062#include <linux/vmalloc.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000063
64#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050065#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000066#include "dir.h"
67#include "glock.h"
68#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000069#include "meta_io.h"
70#include "quota.h"
71#include "rgrp.h"
72#include "trans.h"
Steven Whitehousee13940b2006-01-30 13:31:50 +000073#include "bmap.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050074#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000075
76#define IS_LEAF 1 /* Hashed (leaf) directory */
77#define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
78
Steven Whitehousecd915492006-09-04 12:49:07 -040079#define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
80#define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
David Teiglandb3b94fa2006-01-16 16:50:04 +000081
Steven Whitehouse8d123582010-09-17 12:30:23 +010082struct qstr gfs2_qdot __read_mostly;
83struct qstr gfs2_qdotdot __read_mostly;
84
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -040085typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
86 const struct qstr *name, void *opaque);
David Teiglandb3b94fa2006-01-16 16:50:04 +000087
Steven Whitehousecd915492006-09-04 12:49:07 -040088int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
Steven Whitehouse61e085a2006-04-24 10:07:13 -040089 struct buffer_head **bhp)
Steven Whitehousee13940b2006-01-30 13:31:50 +000090{
91 struct buffer_head *bh;
Steven Whitehousee13940b2006-01-30 13:31:50 +000092
Steven Whitehouse61e085a2006-04-24 10:07:13 -040093 bh = gfs2_meta_new(ip->i_gl, block);
94 gfs2_trans_add_bh(ip->i_gl, bh, 1);
95 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
96 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
Steven Whitehousee13940b2006-01-30 13:31:50 +000097 *bhp = bh;
98 return 0;
99}
100
Steven Whitehousecd915492006-09-04 12:49:07 -0400101static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, u64 block,
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400102 struct buffer_head **bhp)
103{
104 struct buffer_head *bh;
105 int error;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000106
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400107 error = gfs2_meta_read(ip->i_gl, block, DIO_WAIT, &bh);
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400108 if (error)
109 return error;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400110 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) {
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400111 brelse(bh);
112 return -EIO;
113 }
114 *bhp = bh;
115 return 0;
116}
Steven Whitehousee13940b2006-01-30 13:31:50 +0000117
118static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
119 unsigned int offset, unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000120{
121 struct buffer_head *dibh;
122 int error;
123
124 error = gfs2_meta_inode_buffer(ip, &dibh);
125 if (error)
126 return error;
127
128 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehousec7526662006-03-20 12:30:04 -0500129 memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100130 if (ip->i_inode.i_size < offset + size)
131 i_size_write(&ip->i_inode, offset + size);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100132 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500133 gfs2_dinode_out(ip, dibh->b_data);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000134
135 brelse(dibh);
136
137 return size;
138}
139
140
141
142/**
143 * gfs2_dir_write_data - Write directory information to the inode
144 * @ip: The GFS2 inode
145 * @buf: The buffer containing information to be written
146 * @offset: The file offset to start writing at
147 * @size: The amount of data to write
148 *
149 * Returns: The number of bytes correctly written or error code
150 */
151static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
Steven Whitehousecd915492006-09-04 12:49:07 -0400152 u64 offset, unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000153{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400154 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000155 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400156 u64 lblock, dblock;
157 u32 extlen = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000158 unsigned int o;
159 int copied = 0;
160 int error = 0;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000161 int new = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000162
163 if (!size)
164 return 0;
165
166 if (gfs2_is_stuffed(ip) &&
167 offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500168 return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
169 size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000170
171 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
172 return -EINVAL;
173
174 if (gfs2_is_stuffed(ip)) {
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400175 error = gfs2_unstuff_dinode(ip, NULL);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000176 if (error)
Steven Whitehousec7526662006-03-20 12:30:04 -0500177 return error;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000178 }
179
180 lblock = offset;
181 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
182
183 while (copied < size) {
184 unsigned int amount;
185 struct buffer_head *bh;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000186
187 amount = size - copied;
188 if (amount > sdp->sd_sb.sb_bsize - o)
189 amount = sdp->sd_sb.sb_bsize - o;
190
191 if (!extlen) {
192 new = 1;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400193 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
Steven Whitehousefd88de562006-05-05 16:59:11 -0400194 &dblock, &extlen);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000195 if (error)
196 goto fail;
197 error = -EIO;
198 if (gfs2_assert_withdraw(sdp, dblock))
199 goto fail;
200 }
201
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400202 if (amount == sdp->sd_jbsize || new)
203 error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
204 else
205 error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
206
Steven Whitehousee13940b2006-01-30 13:31:50 +0000207 if (error)
208 goto fail;
209
210 gfs2_trans_add_bh(ip->i_gl, bh, 1);
211 memcpy(bh->b_data + o, buf, amount);
212 brelse(bh);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000213
Steven Whitehouse899bb262006-08-01 15:28:57 -0400214 buf += amount;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000215 copied += amount;
216 lblock++;
217 dblock++;
218 extlen--;
219
220 o = sizeof(struct gfs2_meta_header);
221 }
222
223out:
224 error = gfs2_meta_inode_buffer(ip, &dibh);
225 if (error)
226 return error;
227
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100228 if (ip->i_inode.i_size < offset + copied)
229 i_size_write(&ip->i_inode, offset + copied);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100230 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000231
232 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500233 gfs2_dinode_out(ip, dibh->b_data);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000234 brelse(dibh);
235
236 return copied;
237fail:
238 if (copied)
239 goto out;
240 return error;
241}
242
243static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf,
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400244 u64 offset, unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000245{
246 struct buffer_head *dibh;
247 int error;
248
249 error = gfs2_meta_inode_buffer(ip, &dibh);
250 if (!error) {
251 offset += sizeof(struct gfs2_dinode);
252 memcpy(buf, dibh->b_data + offset, size);
253 brelse(dibh);
254 }
255
256 return (error) ? error : size;
257}
258
259
260/**
261 * gfs2_dir_read_data - Read a data from a directory inode
262 * @ip: The GFS2 Inode
263 * @buf: The buffer to place result into
264 * @offset: File offset to begin jdata_readng from
265 * @size: Amount of data to transfer
266 *
267 * Returns: The amount of data actually copied or the error
268 */
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400269static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf, u64 offset,
270 unsigned int size, unsigned ra)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000271{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400272 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehousecd915492006-09-04 12:49:07 -0400273 u64 lblock, dblock;
274 u32 extlen = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000275 unsigned int o;
276 int copied = 0;
277 int error = 0;
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100278 u64 disksize = i_size_read(&ip->i_inode);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000279
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100280 if (offset >= disksize)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000281 return 0;
282
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100283 if (offset + size > disksize)
284 size = disksize - offset;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000285
286 if (!size)
287 return 0;
288
289 if (gfs2_is_stuffed(ip))
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400290 return gfs2_dir_read_stuffed(ip, buf, offset, size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000291
292 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
293 return -EINVAL;
294
295 lblock = offset;
296 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
297
298 while (copied < size) {
299 unsigned int amount;
300 struct buffer_head *bh;
301 int new;
302
303 amount = size - copied;
304 if (amount > sdp->sd_sb.sb_bsize - o)
305 amount = sdp->sd_sb.sb_bsize - o;
306
307 if (!extlen) {
308 new = 0;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400309 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
Steven Whitehousefd88de562006-05-05 16:59:11 -0400310 &dblock, &extlen);
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400311 if (error || !dblock)
312 goto fail;
313 BUG_ON(extlen < 1);
314 if (!ra)
315 extlen = 1;
316 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
Adrian Bunkb7d8ac32006-10-19 16:02:07 +0200317 } else {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400318 error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, &bh);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000319 if (error)
320 goto fail;
321 }
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400322 error = gfs2_metatype_check(sdp, bh, GFS2_METATYPE_JD);
323 if (error) {
324 brelse(bh);
325 goto fail;
326 }
327 dblock++;
328 extlen--;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000329 memcpy(buf, bh->b_data + o, amount);
330 brelse(bh);
Steven Whitehouse899bb262006-08-01 15:28:57 -0400331 buf += amount;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000332 copied += amount;
333 lblock++;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000334 o = sizeof(struct gfs2_meta_header);
335 }
336
337 return copied;
338fail:
339 return (copied) ? copied : error;
340}
341
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500342static inline int gfs2_dirent_sentinel(const struct gfs2_dirent *dent)
343{
344 return dent->de_inum.no_addr == 0 || dent->de_inum.no_formal_ino == 0;
345}
346
Steven Whitehousec7526662006-03-20 12:30:04 -0500347static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
348 const struct qstr *name, int ret)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000349{
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500350 if (!gfs2_dirent_sentinel(dent) &&
Steven Whitehousec7526662006-03-20 12:30:04 -0500351 be32_to_cpu(dent->de_hash) == name->hash &&
352 be16_to_cpu(dent->de_name_len) == name->len &&
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400353 memcmp(dent+1, name->name, name->len) == 0)
Steven Whitehousec7526662006-03-20 12:30:04 -0500354 return ret;
355 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000356}
357
Steven Whitehousec7526662006-03-20 12:30:04 -0500358static int gfs2_dirent_find(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500359 const struct qstr *name,
360 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500361{
362 return __gfs2_dirent_find(dent, name, 1);
363}
364
365static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500366 const struct qstr *name,
367 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500368{
369 return __gfs2_dirent_find(dent, name, 2);
370}
371
372/*
373 * name->name holds ptr to start of block.
374 * name->len holds size of block.
375 */
376static int gfs2_dirent_last(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500377 const struct qstr *name,
378 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500379{
380 const char *start = name->name;
381 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
382 if (name->len == (end - start))
383 return 1;
384 return 0;
385}
386
387static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500388 const struct qstr *name,
389 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500390{
391 unsigned required = GFS2_DIRENT_SIZE(name->len);
392 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
393 unsigned totlen = be16_to_cpu(dent->de_rec_len);
394
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500395 if (gfs2_dirent_sentinel(dent))
Bob Peterson728a7562010-07-14 18:12:26 -0400396 actual = 0;
Jan Engelhardtc5392122006-09-05 14:30:40 +0200397 if (totlen - actual >= required)
Steven Whitehousec7526662006-03-20 12:30:04 -0500398 return 1;
399 return 0;
400}
401
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500402struct dirent_gather {
403 const struct gfs2_dirent **pdent;
404 unsigned offset;
405};
406
407static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
408 const struct qstr *name,
409 void *opaque)
410{
411 struct dirent_gather *g = opaque;
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500412 if (!gfs2_dirent_sentinel(dent)) {
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500413 g->pdent[g->offset++] = dent;
414 }
415 return 0;
416}
417
Steven Whitehousec7526662006-03-20 12:30:04 -0500418/*
419 * Other possible things to check:
420 * - Inode located within filesystem size (and on valid block)
421 * - Valid directory entry type
422 * Not sure how heavy-weight we want to make this... could also check
423 * hash is correct for example, but that would take a lot of extra time.
424 * For now the most important thing is to check that the various sizes
425 * are correct.
426 */
427static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
428 unsigned int size, unsigned int len, int first)
429{
430 const char *msg = "gfs2_dirent too small";
431 if (unlikely(size < sizeof(struct gfs2_dirent)))
432 goto error;
433 msg = "gfs2_dirent misaligned";
434 if (unlikely(offset & 0x7))
435 goto error;
436 msg = "gfs2_dirent points beyond end of block";
437 if (unlikely(offset + size > len))
438 goto error;
439 msg = "zero inode number";
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500440 if (unlikely(!first && gfs2_dirent_sentinel(dent)))
Steven Whitehousec7526662006-03-20 12:30:04 -0500441 goto error;
442 msg = "name length is greater than space in dirent";
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500443 if (!gfs2_dirent_sentinel(dent) &&
Steven Whitehousec7526662006-03-20 12:30:04 -0500444 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
445 size))
446 goto error;
447 return 0;
448error:
449 printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
450 first ? "first in block" : "not first in block");
451 return -EIO;
452}
453
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500454static int gfs2_dirent_offset(const void *buf)
Steven Whitehousec7526662006-03-20 12:30:04 -0500455{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500456 const struct gfs2_meta_header *h = buf;
457 int offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500458
459 BUG_ON(buf == NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500460
Steven Whitehousee3167de2006-03-30 15:46:23 -0500461 switch(be32_to_cpu(h->mh_type)) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500462 case GFS2_METATYPE_LF:
463 offset = sizeof(struct gfs2_leaf);
464 break;
465 case GFS2_METATYPE_DI:
466 offset = sizeof(struct gfs2_dinode);
467 break;
468 default:
469 goto wrong_type;
470 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500471 return offset;
472wrong_type:
473 printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
Steven Whitehousee3167de2006-03-30 15:46:23 -0500474 be32_to_cpu(h->mh_type));
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500475 return -1;
476}
Steven Whitehousec7526662006-03-20 12:30:04 -0500477
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400478static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500479 unsigned int len, gfs2_dscan_t scan,
480 const struct qstr *name,
481 void *opaque)
482{
483 struct gfs2_dirent *dent, *prev;
484 unsigned offset;
485 unsigned size;
486 int ret = 0;
487
488 ret = gfs2_dirent_offset(buf);
489 if (ret < 0)
490 goto consist_inode;
491
492 offset = ret;
Steven Whitehousec7526662006-03-20 12:30:04 -0500493 prev = NULL;
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400494 dent = buf + offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500495 size = be16_to_cpu(dent->de_rec_len);
496 if (gfs2_check_dirent(dent, offset, size, len, 1))
497 goto consist_inode;
498 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500499 ret = scan(dent, name, opaque);
Steven Whitehousec7526662006-03-20 12:30:04 -0500500 if (ret)
501 break;
502 offset += size;
503 if (offset == len)
504 break;
505 prev = dent;
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400506 dent = buf + offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500507 size = be16_to_cpu(dent->de_rec_len);
508 if (gfs2_check_dirent(dent, offset, size, len, 0))
509 goto consist_inode;
510 } while(1);
511
512 switch(ret) {
513 case 0:
514 return NULL;
515 case 1:
516 return dent;
517 case 2:
518 return prev ? prev : dent;
519 default:
520 BUG_ON(ret > 0);
521 return ERR_PTR(ret);
522 }
523
Steven Whitehousec7526662006-03-20 12:30:04 -0500524consist_inode:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400525 gfs2_consist_inode(GFS2_I(inode));
Steven Whitehousec7526662006-03-20 12:30:04 -0500526 return ERR_PTR(-EIO);
527}
528
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400529static int dirent_check_reclen(struct gfs2_inode *dip,
530 const struct gfs2_dirent *d, const void *end_p)
531{
532 const void *ptr = d;
533 u16 rec_len = be16_to_cpu(d->de_rec_len);
534
535 if (unlikely(rec_len < sizeof(struct gfs2_dirent)))
536 goto broken;
537 ptr += rec_len;
538 if (ptr < end_p)
539 return rec_len;
540 if (ptr == end_p)
541 return -ENOENT;
542broken:
543 gfs2_consist_inode(dip);
544 return -EIO;
545}
546
David Teiglandb3b94fa2006-01-16 16:50:04 +0000547/**
548 * dirent_next - Next dirent
549 * @dip: the directory
550 * @bh: The buffer
551 * @dent: Pointer to list of dirents
552 *
553 * Returns: 0 on success, error code otherwise
554 */
555
556static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
557 struct gfs2_dirent **dent)
558{
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400559 struct gfs2_dirent *cur = *dent, *tmp;
560 char *bh_end = bh->b_data + bh->b_size;
561 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000562
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400563 ret = dirent_check_reclen(dip, cur, bh_end);
564 if (ret < 0)
565 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000566
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400567 tmp = (void *)cur + ret;
568 ret = dirent_check_reclen(dip, tmp, bh_end);
569 if (ret == -EIO)
570 return ret;
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000571
David Teiglandb3b94fa2006-01-16 16:50:04 +0000572 /* Only the first dent could ever have de_inum.no_addr == 0 */
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500573 if (gfs2_dirent_sentinel(tmp)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000574 gfs2_consist_inode(dip);
575 return -EIO;
576 }
577
578 *dent = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000579 return 0;
580}
581
582/**
583 * dirent_del - Delete a dirent
584 * @dip: The GFS2 inode
585 * @bh: The buffer
586 * @prev: The previous dirent
587 * @cur: The current dirent
588 *
589 */
590
591static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
592 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
593{
Steven Whitehousecd915492006-09-04 12:49:07 -0400594 u16 cur_rec_len, prev_rec_len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000595
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500596 if (gfs2_dirent_sentinel(cur)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000597 gfs2_consist_inode(dip);
598 return;
599 }
600
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000601 gfs2_trans_add_bh(dip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000602
603 /* If there is no prev entry, this is the first entry in the block.
604 The de_rec_len is already as big as it needs to be. Just zero
605 out the inode number and return. */
606
607 if (!prev) {
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500608 cur->de_inum.no_addr = 0;
609 cur->de_inum.no_formal_ino = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000610 return;
611 }
612
613 /* Combine this dentry with the previous one. */
614
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000615 prev_rec_len = be16_to_cpu(prev->de_rec_len);
616 cur_rec_len = be16_to_cpu(cur->de_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000617
618 if ((char *)prev + prev_rec_len != (char *)cur)
619 gfs2_consist_inode(dip);
620 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
621 gfs2_consist_inode(dip);
622
623 prev_rec_len += cur_rec_len;
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000624 prev->de_rec_len = cpu_to_be16(prev_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000625}
626
Steven Whitehousec7526662006-03-20 12:30:04 -0500627/*
628 * Takes a dent from which to grab space as an argument. Returns the
629 * newly created dent.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000630 */
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400631static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
632 struct gfs2_dirent *dent,
633 const struct qstr *name,
634 struct buffer_head *bh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000635{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400636 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -0500637 struct gfs2_dirent *ndent;
638 unsigned offset = 0, totlen;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000639
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500640 if (!gfs2_dirent_sentinel(dent))
Steven Whitehousec7526662006-03-20 12:30:04 -0500641 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
642 totlen = be16_to_cpu(dent->de_rec_len);
643 BUG_ON(offset + name->len > totlen);
644 gfs2_trans_add_bh(ip->i_gl, bh, 1);
645 ndent = (struct gfs2_dirent *)((char *)dent + offset);
646 dent->de_rec_len = cpu_to_be16(offset);
647 gfs2_qstr2dirent(name, totlen - offset, ndent);
648 return ndent;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000649}
650
Steven Whitehousec7526662006-03-20 12:30:04 -0500651static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
652 struct buffer_head *bh,
653 const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000654{
655 struct gfs2_dirent *dent;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400656 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500657 gfs2_dirent_find_space, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500658 if (!dent || IS_ERR(dent))
659 return dent;
660 return gfs2_init_dirent(inode, dent, name, bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000661}
662
Steven Whitehousecd915492006-09-04 12:49:07 -0400663static int get_leaf(struct gfs2_inode *dip, u64 leaf_no,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000664 struct buffer_head **bhp)
665{
666 int error;
667
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400668 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, bhp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400669 if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
670 /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000671 error = -EIO;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400672 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000673
674 return error;
675}
676
677/**
678 * get_leaf_nr - Get a leaf number associated with the index
679 * @dip: The GFS2 inode
680 * @index:
681 * @leaf_out:
682 *
683 * Returns: 0 on success, error code otherwise
684 */
685
Steven Whitehousecd915492006-09-04 12:49:07 -0400686static int get_leaf_nr(struct gfs2_inode *dip, u32 index,
687 u64 *leaf_out)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000688{
Al Virob44b84d2006-10-14 10:46:30 -0400689 __be64 leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000690 int error;
691
Steven Whitehousee13940b2006-01-30 13:31:50 +0000692 error = gfs2_dir_read_data(dip, (char *)&leaf_no,
Al Virob44b84d2006-10-14 10:46:30 -0400693 index * sizeof(__be64),
694 sizeof(__be64), 0);
Steven Whitehousecd915492006-09-04 12:49:07 -0400695 if (error != sizeof(u64))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000696 return (error < 0) ? error : -EIO;
697
698 *leaf_out = be64_to_cpu(leaf_no);
699
700 return 0;
701}
702
Steven Whitehousecd915492006-09-04 12:49:07 -0400703static int get_first_leaf(struct gfs2_inode *dip, u32 index,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000704 struct buffer_head **bh_out)
705{
Steven Whitehousecd915492006-09-04 12:49:07 -0400706 u64 leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000707 int error;
708
709 error = get_leaf_nr(dip, index, &leaf_no);
710 if (!error)
711 error = get_leaf(dip, leaf_no, bh_out);
712
713 return error;
714}
715
Steven Whitehousec7526662006-03-20 12:30:04 -0500716static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
717 const struct qstr *name,
718 gfs2_dscan_t scan,
719 struct buffer_head **pbh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000720{
Steven Whitehousec7526662006-03-20 12:30:04 -0500721 struct buffer_head *bh;
722 struct gfs2_dirent *dent;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400723 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000724 int error;
725
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000726 if (ip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500727 struct gfs2_leaf *leaf;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000728 unsigned hsize = 1 << ip->i_depth;
Steven Whitehousec7526662006-03-20 12:30:04 -0500729 unsigned index;
730 u64 ln;
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100731 if (hsize * sizeof(u64) != i_size_read(inode)) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500732 gfs2_consist_inode(ip);
733 return ERR_PTR(-EIO);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000734 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400735
Steven Whitehouse9a004502008-02-01 09:23:44 +0000736 index = name->hash >> (32 - ip->i_depth);
Steven Whitehousec7526662006-03-20 12:30:04 -0500737 error = get_first_leaf(ip, index, &bh);
738 if (error)
739 return ERR_PTR(error);
740 do {
741 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500742 scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500743 if (dent)
744 goto got_dent;
745 leaf = (struct gfs2_leaf *)bh->b_data;
746 ln = be64_to_cpu(leaf->lf_next);
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400747 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500748 if (!ln)
749 break;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400750
Steven Whitehousec7526662006-03-20 12:30:04 -0500751 error = get_leaf(ip, ln, &bh);
752 } while(!error);
753
754 return error ? ERR_PTR(error) : NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000755 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000756
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400757
Steven Whitehousec7526662006-03-20 12:30:04 -0500758 error = gfs2_meta_inode_buffer(ip, &bh);
759 if (error)
760 return ERR_PTR(error);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500761 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500762got_dent:
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400763 if (unlikely(dent == NULL || IS_ERR(dent))) {
Steven Whitehouseed386502006-04-07 16:28:07 -0400764 brelse(bh);
765 bh = NULL;
766 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500767 *pbh = bh;
768 return dent;
769}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000770
Steven Whitehousec7526662006-03-20 12:30:04 -0500771static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
772{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400773 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000774 unsigned int n = 1;
Steven Whitehouse09010972009-05-20 10:48:47 +0100775 u64 bn;
776 int error;
777 struct buffer_head *bh;
Steven Whitehousec7526662006-03-20 12:30:04 -0500778 struct gfs2_leaf *leaf;
779 struct gfs2_dirent *dent;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500780 struct qstr name = { .name = "", .len = 0, .hash = 0 };
Steven Whitehouse09010972009-05-20 10:48:47 +0100781
782 error = gfs2_alloc_block(ip, &bn, &n);
783 if (error)
784 return NULL;
785 bh = gfs2_meta_new(ip->i_gl, bn);
Steven Whitehousec7526662006-03-20 12:30:04 -0500786 if (!bh)
787 return NULL;
Steven Whitehouse09010972009-05-20 10:48:47 +0100788
Steven Whitehouse5731be52008-02-01 13:16:55 +0000789 gfs2_trans_add_unrevoke(GFS2_SB(inode), bn, 1);
Steven Whitehousec7526662006-03-20 12:30:04 -0500790 gfs2_trans_add_bh(ip->i_gl, bh, 1);
791 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
792 leaf = (struct gfs2_leaf *)bh->b_data;
793 leaf->lf_depth = cpu_to_be16(depth);
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400794 leaf->lf_entries = 0;
Al Viroa2d7d022006-10-14 16:49:30 +0100795 leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400796 leaf->lf_next = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500797 memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
798 dent = (struct gfs2_dirent *)(leaf+1);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500799 gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
Steven Whitehousec7526662006-03-20 12:30:04 -0500800 *pbh = bh;
801 return leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000802}
803
804/**
805 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
806 * @dip: The GFS2 inode
807 *
808 * Returns: 0 on success, error code otherwise
809 */
810
Steven Whitehousec7526662006-03-20 12:30:04 -0500811static int dir_make_exhash(struct inode *inode)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000812{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400813 struct gfs2_inode *dip = GFS2_I(inode);
814 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000815 struct gfs2_dirent *dent;
Steven Whitehousec7526662006-03-20 12:30:04 -0500816 struct qstr args;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000817 struct buffer_head *bh, *dibh;
818 struct gfs2_leaf *leaf;
819 int y;
Steven Whitehousecd915492006-09-04 12:49:07 -0400820 u32 x;
Al Virob44b84d2006-10-14 10:46:30 -0400821 __be64 *lp;
822 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000823 int error;
824
825 error = gfs2_meta_inode_buffer(dip, &dibh);
826 if (error)
827 return error;
828
David Teiglandb3b94fa2006-01-16 16:50:04 +0000829 /* Turn over a new leaf */
830
Steven Whitehousec7526662006-03-20 12:30:04 -0500831 leaf = new_leaf(inode, &bh, 0);
832 if (!leaf)
833 return -ENOSPC;
834 bn = bh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000835
Steven Whitehousead6203f2008-11-03 13:59:19 +0000836 gfs2_assert(sdp, dip->i_entries < (1 << 16));
837 leaf->lf_entries = cpu_to_be16(dip->i_entries);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000838
839 /* Copy dirents */
840
841 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
842 sizeof(struct gfs2_dinode));
843
844 /* Find last entry */
845
846 x = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500847 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
848 sizeof(struct gfs2_leaf);
849 args.name = bh->b_data;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400850 dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500851 gfs2_dirent_last, &args, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500852 if (!dent) {
853 brelse(bh);
854 brelse(dibh);
855 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000856 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500857 if (IS_ERR(dent)) {
858 brelse(bh);
859 brelse(dibh);
860 return PTR_ERR(dent);
861 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000862
863 /* Adjust the last dirent's record length
864 (Remember that dent still points to the last entry.) */
865
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000866 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000867 sizeof(struct gfs2_dinode) -
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000868 sizeof(struct gfs2_leaf));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000869
870 brelse(bh);
871
872 /* We're done with the new leaf block, now setup the new
873 hash table. */
874
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000875 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000876 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
877
Al Virob44b84d2006-10-14 10:46:30 -0400878 lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000879
880 for (x = sdp->sd_hash_ptrs; x--; lp++)
881 *lp = cpu_to_be64(bn);
882
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100883 i_size_write(inode, sdp->sd_sb.sb_bsize / 2);
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000884 gfs2_add_inode_blocks(&dip->i_inode, 1);
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000885 dip->i_diskflags |= GFS2_DIF_EXHASH;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000886
887 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000888 dip->i_depth = y;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000889
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500890 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000891
892 brelse(dibh);
893
894 return 0;
895}
896
897/**
898 * dir_split_leaf - Split a leaf block into two
899 * @dip: The GFS2 inode
900 * @index:
901 * @leaf_no:
902 *
903 * Returns: 0 on success, error code on failure
904 */
905
Steven Whitehousec7526662006-03-20 12:30:04 -0500906static int dir_split_leaf(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000907{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400908 struct gfs2_inode *dip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000909 struct buffer_head *nbh, *obh, *dibh;
910 struct gfs2_leaf *nleaf, *oleaf;
Steven Whitehouse4da3c642006-07-11 13:19:13 -0400911 struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
Steven Whitehousecd915492006-09-04 12:49:07 -0400912 u32 start, len, half_len, divider;
Al Virob44b84d2006-10-14 10:46:30 -0400913 u64 bn, leaf_no;
914 __be64 *lp;
Steven Whitehousecd915492006-09-04 12:49:07 -0400915 u32 index;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000916 int x, moved = 0;
917 int error;
918
Steven Whitehouse9a004502008-02-01 09:23:44 +0000919 index = name->hash >> (32 - dip->i_depth);
Steven Whitehousec7526662006-03-20 12:30:04 -0500920 error = get_leaf_nr(dip, index, &leaf_no);
921 if (error)
922 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000923
924 /* Get the old leaf block */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000925 error = get_leaf(dip, leaf_no, &obh);
926 if (error)
Steven Whitehousee90deff2006-03-29 19:02:15 -0500927 return error;
928
929 oleaf = (struct gfs2_leaf *)obh->b_data;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000930 if (dip->i_depth == be16_to_cpu(oleaf->lf_depth)) {
Steven Whitehousee90deff2006-03-29 19:02:15 -0500931 brelse(obh);
932 return 1; /* can't split */
933 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000934
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000935 gfs2_trans_add_bh(dip->i_gl, obh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000936
Steven Whitehousec7526662006-03-20 12:30:04 -0500937 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
938 if (!nleaf) {
939 brelse(obh);
940 return -ENOSPC;
941 }
942 bn = nbh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000943
Steven Whitehousec7526662006-03-20 12:30:04 -0500944 /* Compute the start and len of leaf pointers in the hash table. */
Steven Whitehouse9a004502008-02-01 09:23:44 +0000945 len = 1 << (dip->i_depth - be16_to_cpu(oleaf->lf_depth));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000946 half_len = len >> 1;
947 if (!half_len) {
Steven Whitehouse9a004502008-02-01 09:23:44 +0000948 printk(KERN_WARNING "i_depth %u lf_depth %u index %u\n", dip->i_depth, be16_to_cpu(oleaf->lf_depth), index);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000949 gfs2_consist_inode(dip);
950 error = -EIO;
951 goto fail_brelse;
952 }
953
954 start = (index & ~(len - 1));
955
956 /* Change the pointers.
957 Don't bother distinguishing stuffed from non-stuffed.
958 This code is complicated enough already. */
David Rientjes4244b522010-07-20 19:45:03 -0700959 lp = kmalloc(half_len * sizeof(__be64), GFP_NOFS);
960 if (!lp) {
961 error = -ENOMEM;
962 goto fail_brelse;
963 }
964
David Teiglandb3b94fa2006-01-16 16:50:04 +0000965 /* Change the pointers */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000966 for (x = 0; x < half_len; x++)
967 lp[x] = cpu_to_be64(bn);
968
Steven Whitehousecd915492006-09-04 12:49:07 -0400969 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
970 half_len * sizeof(u64));
971 if (error != half_len * sizeof(u64)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000972 if (error >= 0)
973 error = -EIO;
974 goto fail_lpfree;
975 }
976
977 kfree(lp);
978
979 /* Compute the divider */
Steven Whitehouse9a004502008-02-01 09:23:44 +0000980 divider = (start + half_len) << (32 - dip->i_depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000981
982 /* Copy the entries */
Steven Whitehouse15793432009-11-06 11:06:37 +0000983 dent = (struct gfs2_dirent *)(obh->b_data + sizeof(struct gfs2_leaf));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000984
985 do {
986 next = dent;
987 if (dirent_next(dip, obh, &next))
988 next = NULL;
989
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500990 if (!gfs2_dirent_sentinel(dent) &&
David Teiglandb3b94fa2006-01-16 16:50:04 +0000991 be32_to_cpu(dent->de_hash) < divider) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500992 struct qstr str;
993 str.name = (char*)(dent+1);
994 str.len = be16_to_cpu(dent->de_name_len);
995 str.hash = be32_to_cpu(dent->de_hash);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500996 new = gfs2_dirent_alloc(inode, nbh, &str);
Steven Whitehousec7526662006-03-20 12:30:04 -0500997 if (IS_ERR(new)) {
998 error = PTR_ERR(new);
999 break;
1000 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001001
1002 new->de_inum = dent->de_inum; /* No endian worries */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001003 new->de_type = dent->de_type; /* No endian worries */
Marcin Slusarzbb16b342008-02-13 00:06:10 +01001004 be16_add_cpu(&nleaf->lf_entries, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001005
1006 dirent_del(dip, obh, prev, dent);
1007
1008 if (!oleaf->lf_entries)
1009 gfs2_consist_inode(dip);
Marcin Slusarzbb16b342008-02-13 00:06:10 +01001010 be16_add_cpu(&oleaf->lf_entries, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001011
1012 if (!prev)
1013 prev = dent;
1014
1015 moved = 1;
Steven Whitehousec7526662006-03-20 12:30:04 -05001016 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001017 prev = dent;
Steven Whitehousec7526662006-03-20 12:30:04 -05001018 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001019 dent = next;
Steven Whitehousec7526662006-03-20 12:30:04 -05001020 } while (dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001021
Steven Whitehousec7526662006-03-20 12:30:04 -05001022 oleaf->lf_depth = nleaf->lf_depth;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001023
1024 error = gfs2_meta_inode_buffer(dip, &dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001025 if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
Steven Whitehouse382e6e22007-08-16 17:08:20 +01001026 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001027 gfs2_add_inode_blocks(&dip->i_inode, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001028 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001029 brelse(dibh);
1030 }
1031
1032 brelse(obh);
1033 brelse(nbh);
1034
1035 return error;
1036
Steven Whitehousee90deff2006-03-29 19:02:15 -05001037fail_lpfree:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001038 kfree(lp);
1039
Steven Whitehousee90deff2006-03-29 19:02:15 -05001040fail_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001041 brelse(obh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001042 brelse(nbh);
1043 return error;
1044}
1045
1046/**
1047 * dir_double_exhash - Double size of ExHash table
1048 * @dip: The GFS2 dinode
1049 *
1050 * Returns: 0 on success, error code on failure
1051 */
1052
1053static int dir_double_exhash(struct gfs2_inode *dip)
1054{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001055 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001056 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001057 u32 hsize;
1058 u64 *buf;
1059 u64 *from, *to;
1060 u64 block;
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001061 u64 disksize = i_size_read(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001062 int x;
1063 int error = 0;
1064
Steven Whitehouse9a004502008-02-01 09:23:44 +00001065 hsize = 1 << dip->i_depth;
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001066 if (hsize * sizeof(u64) != disksize) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001067 gfs2_consist_inode(dip);
1068 return -EIO;
1069 }
1070
1071 /* Allocate both the "from" and "to" buffers in one big chunk */
1072
David Rientjes4244b522010-07-20 19:45:03 -07001073 buf = kcalloc(3, sdp->sd_hash_bsize, GFP_NOFS);
1074 if (!buf)
1075 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001076
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001077 for (block = disksize >> sdp->sd_hash_bsize_shift; block--;) {
Steven Whitehousee13940b2006-01-30 13:31:50 +00001078 error = gfs2_dir_read_data(dip, (char *)buf,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001079 block * sdp->sd_hash_bsize,
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001080 sdp->sd_hash_bsize, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001081 if (error != sdp->sd_hash_bsize) {
1082 if (error >= 0)
1083 error = -EIO;
1084 goto fail;
1085 }
1086
1087 from = buf;
Steven Whitehousecd915492006-09-04 12:49:07 -04001088 to = (u64 *)((char *)buf + sdp->sd_hash_bsize);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001089
1090 for (x = sdp->sd_hash_ptrs; x--; from++) {
1091 *to++ = *from; /* No endianess worries */
1092 *to++ = *from;
1093 }
1094
Steven Whitehousee13940b2006-01-30 13:31:50 +00001095 error = gfs2_dir_write_data(dip,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001096 (char *)buf + sdp->sd_hash_bsize,
1097 block * sdp->sd_sb.sb_bsize,
1098 sdp->sd_sb.sb_bsize);
1099 if (error != sdp->sd_sb.sb_bsize) {
1100 if (error >= 0)
1101 error = -EIO;
1102 goto fail;
1103 }
1104 }
1105
1106 kfree(buf);
1107
1108 error = gfs2_meta_inode_buffer(dip, &dibh);
1109 if (!gfs2_assert_withdraw(sdp, !error)) {
Steven Whitehouse9a004502008-02-01 09:23:44 +00001110 dip->i_depth++;
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001111 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001112 brelse(dibh);
1113 }
1114
1115 return error;
1116
Steven Whitehousea91ea692006-09-04 12:04:26 -04001117fail:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001118 kfree(buf);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001119 return error;
1120}
1121
1122/**
1123 * compare_dents - compare directory entries by hash value
1124 * @a: first dent
1125 * @b: second dent
1126 *
1127 * When comparing the hash entries of @a to @b:
1128 * gt: returns 1
1129 * lt: returns -1
1130 * eq: returns 0
1131 */
1132
1133static int compare_dents(const void *a, const void *b)
1134{
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001135 const struct gfs2_dirent *dent_a, *dent_b;
Steven Whitehousecd915492006-09-04 12:49:07 -04001136 u32 hash_a, hash_b;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001137 int ret = 0;
1138
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001139 dent_a = *(const struct gfs2_dirent **)a;
Steven Whitehousec7526662006-03-20 12:30:04 -05001140 hash_a = be32_to_cpu(dent_a->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001141
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001142 dent_b = *(const struct gfs2_dirent **)b;
Steven Whitehousec7526662006-03-20 12:30:04 -05001143 hash_b = be32_to_cpu(dent_b->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001144
1145 if (hash_a > hash_b)
1146 ret = 1;
1147 else if (hash_a < hash_b)
1148 ret = -1;
1149 else {
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001150 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1151 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001152
1153 if (len_a > len_b)
1154 ret = 1;
1155 else if (len_a < len_b)
1156 ret = -1;
1157 else
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001158 ret = memcmp(dent_a + 1, dent_b + 1, len_a);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001159 }
1160
1161 return ret;
1162}
1163
1164/**
1165 * do_filldir_main - read out directory entries
1166 * @dip: The GFS2 inode
1167 * @offset: The offset in the file to read from
1168 * @opaque: opaque data to pass to filldir
1169 * @filldir: The function to pass entries to
1170 * @darr: an array of struct gfs2_dirent pointers to read
1171 * @entries: the number of entries in darr
1172 * @copied: pointer to int that's non-zero if a entry has been copied out
1173 *
1174 * Jump through some hoops to make sure that if there are hash collsions,
1175 * they are read out at the beginning of a buffer. We want to minimize
1176 * the possibility that they will fall into different readdir buffers or
1177 * that someone will want to seek to that location.
1178 *
1179 * Returns: errno, >0 on exception from filldir
1180 */
1181
Steven Whitehousecd915492006-09-04 12:49:07 -04001182static int do_filldir_main(struct gfs2_inode *dip, u64 *offset,
Steven Whitehouse3699e3a2007-01-17 15:09:20 +00001183 void *opaque, filldir_t filldir,
Steven Whitehousecd915492006-09-04 12:49:07 -04001184 const struct gfs2_dirent **darr, u32 entries,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001185 int *copied)
1186{
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001187 const struct gfs2_dirent *dent, *dent_next;
Steven Whitehousecd915492006-09-04 12:49:07 -04001188 u64 off, off_next;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001189 unsigned int x, y;
1190 int run = 0;
1191 int error = 0;
1192
1193 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1194
1195 dent_next = darr[0];
1196 off_next = be32_to_cpu(dent_next->de_hash);
1197 off_next = gfs2_disk_hash2offset(off_next);
1198
1199 for (x = 0, y = 1; x < entries; x++, y++) {
1200 dent = dent_next;
1201 off = off_next;
1202
1203 if (y < entries) {
1204 dent_next = darr[y];
1205 off_next = be32_to_cpu(dent_next->de_hash);
1206 off_next = gfs2_disk_hash2offset(off_next);
1207
1208 if (off < *offset)
1209 continue;
1210 *offset = off;
1211
1212 if (off_next == off) {
1213 if (*copied && !run)
1214 return 1;
1215 run = 1;
1216 } else
1217 run = 0;
1218 } else {
1219 if (off < *offset)
1220 continue;
1221 *offset = off;
1222 }
1223
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001224 error = filldir(opaque, (const char *)(dent + 1),
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001225 be16_to_cpu(dent->de_name_len),
Steven Whitehouse3699e3a2007-01-17 15:09:20 +00001226 off, be64_to_cpu(dent->de_inum.no_addr),
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001227 be16_to_cpu(dent->de_type));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001228 if (error)
1229 return 1;
1230
1231 *copied = 1;
1232 }
1233
1234 /* Increment the *offset by one, so the next time we come into the
1235 do_filldir fxn, we get the next entry instead of the last one in the
1236 current leaf */
1237
1238 (*offset)++;
1239
1240 return 0;
1241}
1242
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001243static void *gfs2_alloc_sort_buffer(unsigned size)
1244{
1245 void *ptr = NULL;
1246
1247 if (size < KMALLOC_MAX_SIZE)
1248 ptr = kmalloc(size, GFP_NOFS | __GFP_NOWARN);
1249 if (!ptr)
1250 ptr = __vmalloc(size, GFP_NOFS, PAGE_KERNEL);
1251 return ptr;
1252}
1253
1254static void gfs2_free_sort_buffer(void *ptr)
1255{
1256 if (is_vmalloc_addr(ptr))
1257 vfree(ptr);
1258 else
1259 kfree(ptr);
1260}
1261
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001262static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
Steven Whitehouse3699e3a2007-01-17 15:09:20 +00001263 filldir_t filldir, int *copied, unsigned *depth,
1264 u64 leaf_no)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001265{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001266 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001267 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001268 struct buffer_head *bh;
1269 struct gfs2_leaf *lf;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001270 unsigned entries = 0, entries2 = 0;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001271 unsigned leaves = 0;
1272 const struct gfs2_dirent **darr, *dent;
1273 struct dirent_gather g;
1274 struct buffer_head **larr;
1275 int leaf = 0;
1276 int error, i;
1277 u64 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001278
David Teiglandb3b94fa2006-01-16 16:50:04 +00001279 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001280 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001281 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001282 goto out;
1283 lf = (struct gfs2_leaf *)bh->b_data;
1284 if (leaves == 0)
1285 *depth = be16_to_cpu(lf->lf_depth);
1286 entries += be16_to_cpu(lf->lf_entries);
1287 leaves++;
1288 lfn = be64_to_cpu(lf->lf_next);
1289 brelse(bh);
1290 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001291
1292 if (!entries)
1293 return 0;
1294
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001295 error = -ENOMEM;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001296 /*
1297 * The extra 99 entries are not normally used, but are a buffer
1298 * zone in case the number of entries in the leaf is corrupt.
1299 * 99 is the maximum number of entries that can fit in a single
1300 * leaf block.
1301 */
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001302 larr = gfs2_alloc_sort_buffer((leaves + entries + 99) * sizeof(void *));
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001303 if (!larr)
1304 goto out;
1305 darr = (const struct gfs2_dirent **)(larr + leaves);
1306 g.pdent = darr;
1307 g.offset = 0;
1308 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001309
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001310 do {
1311 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001312 if (error)
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001313 goto out_free;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001314 lf = (struct gfs2_leaf *)bh->b_data;
1315 lfn = be64_to_cpu(lf->lf_next);
1316 if (lf->lf_entries) {
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001317 entries2 += be16_to_cpu(lf->lf_entries);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001318 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1319 gfs2_dirent_gather, NULL, &g);
1320 error = PTR_ERR(dent);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001321 if (IS_ERR(dent))
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001322 goto out_free;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001323 if (entries2 != g.offset) {
akpm@linux-foundation.orgf391a4e2007-04-25 21:08:02 -07001324 fs_warn(sdp, "Number of entries corrupt in dir "
1325 "leaf %llu, entries2 (%u) != "
1326 "g.offset (%u)\n",
1327 (unsigned long long)bh->b_blocknr,
1328 entries2, g.offset);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001329
1330 error = -EIO;
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001331 goto out_free;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001332 }
1333 error = 0;
1334 larr[leaf++] = bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001335 } else {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001336 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001337 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001338 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001339
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001340 BUG_ON(entries2 != entries);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001341 error = do_filldir_main(ip, offset, opaque, filldir, darr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001342 entries, copied);
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001343out_free:
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001344 for(i = 0; i < leaf; i++)
1345 brelse(larr[i]);
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001346 gfs2_free_sort_buffer(larr);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001347out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001348 return error;
1349}
1350
1351/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001352 * dir_e_read - Reads the entries from a directory into a filldir buffer
1353 * @dip: dinode pointer
1354 * @offset: the hash of the last entry read shifted to the right once
1355 * @opaque: buffer for the filldir function to fill
1356 * @filldir: points to the filldir function to use
1357 *
1358 * Returns: errno
1359 */
1360
Steven Whitehousecd915492006-09-04 12:49:07 -04001361static int dir_e_read(struct inode *inode, u64 *offset, void *opaque,
Steven Whitehouse3699e3a2007-01-17 15:09:20 +00001362 filldir_t filldir)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001363{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001364 struct gfs2_inode *dip = GFS2_I(inode);
1365 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehousecd915492006-09-04 12:49:07 -04001366 u32 hsize, len = 0;
1367 u32 ht_offset, lp_offset, ht_offset_cur = -1;
1368 u32 hash, index;
Al Virob44b84d2006-10-14 10:46:30 -04001369 __be64 *lp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001370 int copied = 0;
1371 int error = 0;
Steven Whitehouse4da3c642006-07-11 13:19:13 -04001372 unsigned depth = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001373
Steven Whitehouse9a004502008-02-01 09:23:44 +00001374 hsize = 1 << dip->i_depth;
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001375 if (hsize * sizeof(u64) != i_size_read(inode)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001376 gfs2_consist_inode(dip);
1377 return -EIO;
1378 }
1379
1380 hash = gfs2_dir_offset2hash(*offset);
Steven Whitehouse9a004502008-02-01 09:23:44 +00001381 index = hash >> (32 - dip->i_depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001382
Josef Bacik16c5f062008-04-09 09:33:41 -04001383 lp = kmalloc(sdp->sd_hash_bsize, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001384 if (!lp)
1385 return -ENOMEM;
1386
1387 while (index < hsize) {
1388 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1389 ht_offset = index - lp_offset;
1390
1391 if (ht_offset_cur != ht_offset) {
Steven Whitehousee13940b2006-01-30 13:31:50 +00001392 error = gfs2_dir_read_data(dip, (char *)lp,
Al Virob44b84d2006-10-14 10:46:30 -04001393 ht_offset * sizeof(__be64),
Steven Whitehouse7276b3b2006-09-21 17:05:23 -04001394 sdp->sd_hash_bsize, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001395 if (error != sdp->sd_hash_bsize) {
1396 if (error >= 0)
1397 error = -EIO;
1398 goto out;
1399 }
1400 ht_offset_cur = ht_offset;
1401 }
1402
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001403 error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
1404 &copied, &depth,
1405 be64_to_cpu(lp[lp_offset]));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001406 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001407 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001408
Steven Whitehouse9a004502008-02-01 09:23:44 +00001409 len = 1 << (dip->i_depth - depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001410 index = (index & ~(len - 1)) + len;
1411 }
1412
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001413out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001414 kfree(lp);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001415 if (error > 0)
1416 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001417 return error;
1418}
1419
Steven Whitehousecd915492006-09-04 12:49:07 -04001420int gfs2_dir_read(struct inode *inode, u64 *offset, void *opaque,
Steven Whitehouse3699e3a2007-01-17 15:09:20 +00001421 filldir_t filldir)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001422{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001423 struct gfs2_inode *dip = GFS2_I(inode);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001424 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001425 struct dirent_gather g;
1426 const struct gfs2_dirent **darr, *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001427 struct buffer_head *dibh;
1428 int copied = 0;
1429 int error;
1430
Steven Whitehousead6203f2008-11-03 13:59:19 +00001431 if (!dip->i_entries)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001432 return 0;
1433
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001434 if (dip->i_diskflags & GFS2_DIF_EXHASH)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001435 return dir_e_read(inode, offset, opaque, filldir);
1436
David Teiglandb3b94fa2006-01-16 16:50:04 +00001437 if (!gfs2_is_stuffed(dip)) {
1438 gfs2_consist_inode(dip);
1439 return -EIO;
1440 }
1441
David Teiglandb3b94fa2006-01-16 16:50:04 +00001442 error = gfs2_meta_inode_buffer(dip, &dibh);
1443 if (error)
1444 return error;
1445
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001446 error = -ENOMEM;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001447 /* 96 is max number of dirents which can be stuffed into an inode */
Josef Bacik16c5f062008-04-09 09:33:41 -04001448 darr = kmalloc(96 * sizeof(struct gfs2_dirent *), GFP_NOFS);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001449 if (darr) {
1450 g.pdent = darr;
1451 g.offset = 0;
1452 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1453 gfs2_dirent_gather, NULL, &g);
1454 if (IS_ERR(dent)) {
1455 error = PTR_ERR(dent);
1456 goto out;
1457 }
Steven Whitehousead6203f2008-11-03 13:59:19 +00001458 if (dip->i_entries != g.offset) {
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001459 fs_warn(sdp, "Number of entries corrupt in dir %llu, "
Steven Whitehousead6203f2008-11-03 13:59:19 +00001460 "ip->i_entries (%u) != g.offset (%u)\n",
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001461 (unsigned long long)dip->i_no_addr,
Steven Whitehousead6203f2008-11-03 13:59:19 +00001462 dip->i_entries,
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001463 g.offset);
1464 error = -EIO;
1465 goto out;
1466 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001467 error = do_filldir_main(dip, offset, opaque, filldir, darr,
Steven Whitehousead6203f2008-11-03 13:59:19 +00001468 dip->i_entries, &copied);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001469out:
1470 kfree(darr);
1471 }
1472
David Teiglandb3b94fa2006-01-16 16:50:04 +00001473 if (error > 0)
1474 error = 0;
1475
1476 brelse(dibh);
1477
1478 return error;
1479}
1480
David Teiglandb3b94fa2006-01-16 16:50:04 +00001481/**
1482 * gfs2_dir_search - Search a directory
1483 * @dip: The GFS2 inode
1484 * @filename:
1485 * @inode:
1486 *
1487 * This routine searches a directory for a file or another directory.
1488 * Assumes a glock is held on dip.
1489 *
1490 * Returns: errno
1491 */
1492
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001493struct inode *gfs2_dir_search(struct inode *dir, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001494{
Steven Whitehousec7526662006-03-20 12:30:04 -05001495 struct buffer_head *bh;
1496 struct gfs2_dirent *dent;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001497 struct inode *inode;
1498
1499 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1500 if (dent) {
1501 if (IS_ERR(dent))
David Howellse231c2e2008-02-07 00:15:26 -08001502 return ERR_CAST(dent);
Wendy Chengbb9bcf02007-06-27 17:07:08 -04001503 inode = gfs2_inode_lookup(dir->i_sb,
1504 be16_to_cpu(dent->de_type),
1505 be64_to_cpu(dent->de_inum.no_addr),
Bob Peterson44ad37d2011-03-17 16:19:58 -04001506 be64_to_cpu(dent->de_inum.no_formal_ino), 0);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001507 brelse(bh);
1508 return inode;
1509 }
1510 return ERR_PTR(-ENOENT);
1511}
1512
1513int gfs2_dir_check(struct inode *dir, const struct qstr *name,
1514 const struct gfs2_inode *ip)
1515{
1516 struct buffer_head *bh;
1517 struct gfs2_dirent *dent;
1518 int ret = -ENOENT;
Steven Whitehousec7526662006-03-20 12:30:04 -05001519
1520 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1521 if (dent) {
1522 if (IS_ERR(dent))
1523 return PTR_ERR(dent);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001524 if (ip) {
1525 if (be64_to_cpu(dent->de_inum.no_addr) != ip->i_no_addr)
1526 goto out;
1527 if (be64_to_cpu(dent->de_inum.no_formal_ino) !=
1528 ip->i_no_formal_ino)
1529 goto out;
1530 if (unlikely(IF2DT(ip->i_inode.i_mode) !=
1531 be16_to_cpu(dent->de_type))) {
1532 gfs2_consist_inode(GFS2_I(dir));
1533 ret = -EIO;
1534 goto out;
1535 }
1536 }
1537 ret = 0;
1538out:
Steven Whitehousec7526662006-03-20 12:30:04 -05001539 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001540 }
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001541 return ret;
Steven Whitehousec7526662006-03-20 12:30:04 -05001542}
1543
1544static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1545{
1546 struct buffer_head *bh, *obh;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001547 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001548 struct gfs2_leaf *leaf, *oleaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001549 int error;
Steven Whitehousec7526662006-03-20 12:30:04 -05001550 u32 index;
1551 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001552
Steven Whitehouse9a004502008-02-01 09:23:44 +00001553 index = name->hash >> (32 - ip->i_depth);
Steven Whitehousec7526662006-03-20 12:30:04 -05001554 error = get_first_leaf(ip, index, &obh);
1555 if (error)
1556 return error;
1557 do {
1558 oleaf = (struct gfs2_leaf *)obh->b_data;
1559 bn = be64_to_cpu(oleaf->lf_next);
1560 if (!bn)
1561 break;
1562 brelse(obh);
1563 error = get_leaf(ip, bn, &obh);
1564 if (error)
1565 return error;
1566 } while(1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001567
Steven Whitehousec7526662006-03-20 12:30:04 -05001568 gfs2_trans_add_bh(ip->i_gl, obh, 1);
1569
1570 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1571 if (!leaf) {
1572 brelse(obh);
1573 return -ENOSPC;
1574 }
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04001575 oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
Steven Whitehousec7526662006-03-20 12:30:04 -05001576 brelse(bh);
1577 brelse(obh);
1578
1579 error = gfs2_meta_inode_buffer(ip, &bh);
1580 if (error)
1581 return error;
1582 gfs2_trans_add_bh(ip->i_gl, bh, 1);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001583 gfs2_add_inode_blocks(&ip->i_inode, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001584 gfs2_dinode_out(ip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001585 brelse(bh);
1586 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001587}
1588
1589/**
1590 * gfs2_dir_add - Add new filename into directory
1591 * @dip: The GFS2 inode
1592 * @filename: The new name
1593 * @inode: The inode number of the entry
1594 * @type: The type of the entry
1595 *
1596 * Returns: 0 on success, error code on failure
1597 */
1598
Steven Whitehousec7526662006-03-20 12:30:04 -05001599int gfs2_dir_add(struct inode *inode, const struct qstr *name,
Steven Whitehouse3d6ecb72011-05-09 13:30:08 +01001600 const struct gfs2_inode *nip)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001601{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001602 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001603 struct buffer_head *bh;
1604 struct gfs2_dirent *dent;
1605 struct gfs2_leaf *leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001606 int error;
1607
Steven Whitehousec7526662006-03-20 12:30:04 -05001608 while(1) {
1609 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
1610 &bh);
1611 if (dent) {
1612 if (IS_ERR(dent))
1613 return PTR_ERR(dent);
1614 dent = gfs2_init_dirent(inode, dent, name, bh);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001615 gfs2_inum_out(nip, dent);
Steven Whitehouse3d6ecb72011-05-09 13:30:08 +01001616 dent->de_type = cpu_to_be16(IF2DT(nip->i_inode.i_mode));
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001617 if (ip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001618 leaf = (struct gfs2_leaf *)bh->b_data;
Marcin Slusarzbb16b342008-02-13 00:06:10 +01001619 be16_add_cpu(&leaf->lf_entries, 1);
Steven Whitehousec7526662006-03-20 12:30:04 -05001620 }
1621 brelse(bh);
1622 error = gfs2_meta_inode_buffer(ip, &bh);
1623 if (error)
1624 break;
1625 gfs2_trans_add_bh(ip->i_gl, bh, 1);
Steven Whitehousead6203f2008-11-03 13:59:19 +00001626 ip->i_entries++;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001627 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehouse3d6ecb72011-05-09 13:30:08 +01001628 if (S_ISDIR(nip->i_inode.i_mode))
1629 inc_nlink(&ip->i_inode);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001630 gfs2_dinode_out(ip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001631 brelse(bh);
1632 error = 0;
1633 break;
1634 }
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001635 if (!(ip->i_diskflags & GFS2_DIF_EXHASH)) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001636 error = dir_make_exhash(inode);
1637 if (error)
1638 break;
1639 continue;
1640 }
1641 error = dir_split_leaf(inode, name);
1642 if (error == 0)
1643 continue;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001644 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001645 break;
Steven Whitehouse9a004502008-02-01 09:23:44 +00001646 if (ip->i_depth < GFS2_DIR_MAX_DEPTH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001647 error = dir_double_exhash(ip);
1648 if (error)
1649 break;
1650 error = dir_split_leaf(inode, name);
Steven Whitehousee90deff2006-03-29 19:02:15 -05001651 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001652 break;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001653 if (error == 0)
1654 continue;
Steven Whitehousec7526662006-03-20 12:30:04 -05001655 }
1656 error = dir_new_leaf(inode, name);
1657 if (!error)
1658 continue;
1659 error = -ENOSPC;
1660 break;
1661 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001662 return error;
1663}
1664
Steven Whitehousec7526662006-03-20 12:30:04 -05001665
David Teiglandb3b94fa2006-01-16 16:50:04 +00001666/**
1667 * gfs2_dir_del - Delete a directory entry
1668 * @dip: The GFS2 inode
1669 * @filename: The filename
1670 *
1671 * Returns: 0 on success, error code on failure
1672 */
1673
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001674int gfs2_dir_del(struct gfs2_inode *dip, const struct dentry *dentry)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001675{
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001676 const struct qstr *name = &dentry->d_name;
Steven Whitehousec7526662006-03-20 12:30:04 -05001677 struct gfs2_dirent *dent, *prev = NULL;
1678 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001679 int error;
1680
Steven Whitehousec7526662006-03-20 12:30:04 -05001681 /* Returns _either_ the entry (if its first in block) or the
1682 previous entry otherwise */
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001683 dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001684 if (!dent) {
1685 gfs2_consist_inode(dip);
1686 return -EIO;
1687 }
1688 if (IS_ERR(dent)) {
1689 gfs2_consist_inode(dip);
1690 return PTR_ERR(dent);
1691 }
1692 /* If not first in block, adjust pointers accordingly */
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001693 if (gfs2_dirent_find(dent, name, NULL) == 0) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001694 prev = dent;
1695 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1696 }
1697
1698 dirent_del(dip, bh, prev, dent);
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001699 if (dip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001700 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1701 u16 entries = be16_to_cpu(leaf->lf_entries);
1702 if (!entries)
1703 gfs2_consist_inode(dip);
1704 leaf->lf_entries = cpu_to_be16(--entries);
Steven Whitehousec7526662006-03-20 12:30:04 -05001705 }
Steven Whitehouseed386502006-04-07 16:28:07 -04001706 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001707
1708 error = gfs2_meta_inode_buffer(dip, &bh);
1709 if (error)
1710 return error;
1711
Steven Whitehousead6203f2008-11-03 13:59:19 +00001712 if (!dip->i_entries)
Steven Whitehousec7526662006-03-20 12:30:04 -05001713 gfs2_consist_inode(dip);
1714 gfs2_trans_add_bh(dip->i_gl, bh, 1);
Steven Whitehousead6203f2008-11-03 13:59:19 +00001715 dip->i_entries--;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001716 dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001717 if (S_ISDIR(dentry->d_inode->i_mode))
1718 drop_nlink(&dip->i_inode);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001719 gfs2_dinode_out(dip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001720 brelse(bh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001721 mark_inode_dirty(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001722
1723 return error;
1724}
1725
David Teiglandb3b94fa2006-01-16 16:50:04 +00001726/**
1727 * gfs2_dir_mvino - Change inode number of directory entry
1728 * @dip: The GFS2 inode
1729 * @filename:
1730 * @new_inode:
1731 *
1732 * This routine changes the inode number of a directory entry. It's used
1733 * by rename to change ".." when a directory is moved.
1734 * Assumes a glock is held on dvp.
1735 *
1736 * Returns: errno
1737 */
1738
Steven Whitehousec7526662006-03-20 12:30:04 -05001739int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001740 const struct gfs2_inode *nip, unsigned int new_type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001741{
Steven Whitehousec7526662006-03-20 12:30:04 -05001742 struct buffer_head *bh;
1743 struct gfs2_dirent *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001744 int error;
1745
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001746 dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001747 if (!dent) {
1748 gfs2_consist_inode(dip);
1749 return -EIO;
1750 }
1751 if (IS_ERR(dent))
1752 return PTR_ERR(dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001753
Steven Whitehousec7526662006-03-20 12:30:04 -05001754 gfs2_trans_add_bh(dip->i_gl, bh, 1);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001755 gfs2_inum_out(nip, dent);
Steven Whitehousec7526662006-03-20 12:30:04 -05001756 dent->de_type = cpu_to_be16(new_type);
1757
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001758 if (dip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001759 brelse(bh);
1760 error = gfs2_meta_inode_buffer(dip, &bh);
1761 if (error)
1762 return error;
1763 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1764 }
1765
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001766 dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001767 gfs2_dinode_out(dip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001768 brelse(bh);
1769 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001770}
1771
1772/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001773 * leaf_dealloc - Deallocate a directory leaf
1774 * @dip: the directory
1775 * @index: the hash table offset in the directory
1776 * @len: the number of pointers to this leaf
1777 * @leaf_no: the leaf number
Bob Petersonec038c82011-03-22 13:55:23 -04001778 * @leaf_bh: buffer_head for the starting leaf
Bob Petersond24a7a42011-03-22 13:54:03 -04001779 * last_dealloc: 1 if this is the final dealloc for the leaf, else 0
David Teiglandb3b94fa2006-01-16 16:50:04 +00001780 *
1781 * Returns: errno
1782 */
1783
Steven Whitehousecd915492006-09-04 12:49:07 -04001784static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
Bob Petersonec038c82011-03-22 13:55:23 -04001785 u64 leaf_no, struct buffer_head *leaf_bh,
1786 int last_dealloc)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001787{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001788 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001789 struct gfs2_leaf *tmp_leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001790 struct gfs2_rgrp_list rlist;
1791 struct buffer_head *bh, *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001792 u64 blk, nblk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001793 unsigned int rg_blocks = 0, l_blocks = 0;
1794 char *ht;
Steven Whitehousecd915492006-09-04 12:49:07 -04001795 unsigned int x, size = len * sizeof(u64);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001796 int error;
1797
1798 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1799
Josef Bacik16c5f062008-04-09 09:33:41 -04001800 ht = kzalloc(size, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001801 if (!ht)
1802 return -ENOMEM;
1803
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001804 if (!gfs2_alloc_get(dip)) {
1805 error = -ENOMEM;
1806 goto out;
1807 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001808
1809 error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1810 if (error)
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001811 goto out_put;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001812
Steven Whitehouse6dbd8222008-01-10 15:18:55 +00001813 error = gfs2_rindex_hold(sdp, &dip->i_alloc->al_ri_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001814 if (error)
1815 goto out_qs;
1816
1817 /* Count the number of leaves */
Bob Petersonec038c82011-03-22 13:55:23 -04001818 bh = leaf_bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001819
Steven Whitehousec7526662006-03-20 12:30:04 -05001820 for (blk = leaf_no; blk; blk = nblk) {
Bob Petersonec038c82011-03-22 13:55:23 -04001821 if (blk != leaf_no) {
1822 error = get_leaf(dip, blk, &bh);
1823 if (error)
1824 goto out_rlist;
1825 }
Steven Whitehousec7526662006-03-20 12:30:04 -05001826 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1827 nblk = be64_to_cpu(tmp_leaf->lf_next);
Bob Petersonec038c82011-03-22 13:55:23 -04001828 if (blk != leaf_no)
1829 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001830
1831 gfs2_rlist_add(sdp, &rlist, blk);
1832 l_blocks++;
1833 }
1834
Bob Petersonfe6c9912008-01-28 11:13:02 -06001835 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001836
1837 for (x = 0; x < rlist.rl_rgrps; x++) {
1838 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001839 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001840 rg_blocks += rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001841 }
1842
1843 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1844 if (error)
1845 goto out_rlist;
1846
1847 error = gfs2_trans_begin(sdp,
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001848 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
David Teiglandb3b94fa2006-01-16 16:50:04 +00001849 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1850 if (error)
1851 goto out_rg_gunlock;
1852
Bob Petersonec038c82011-03-22 13:55:23 -04001853 bh = leaf_bh;
1854
Steven Whitehousec7526662006-03-20 12:30:04 -05001855 for (blk = leaf_no; blk; blk = nblk) {
Bob Petersonec038c82011-03-22 13:55:23 -04001856 if (blk != leaf_no) {
1857 error = get_leaf(dip, blk, &bh);
1858 if (error)
1859 goto out_end_trans;
1860 }
Steven Whitehousec7526662006-03-20 12:30:04 -05001861 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1862 nblk = be64_to_cpu(tmp_leaf->lf_next);
Bob Petersonec038c82011-03-22 13:55:23 -04001863 if (blk != leaf_no)
1864 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001865
1866 gfs2_free_meta(dip, blk, 1);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001867 gfs2_add_inode_blocks(&dip->i_inode, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001868 }
1869
Steven Whitehousecd915492006-09-04 12:49:07 -04001870 error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001871 if (error != size) {
1872 if (error >= 0)
1873 error = -EIO;
1874 goto out_end_trans;
1875 }
1876
1877 error = gfs2_meta_inode_buffer(dip, &dibh);
1878 if (error)
1879 goto out_end_trans;
1880
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001881 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
Bob Petersond24a7a42011-03-22 13:54:03 -04001882 /* On the last dealloc, make this a regular file in case we crash.
1883 (We don't want to free these blocks a second time.) */
1884 if (last_dealloc)
1885 dip->i_inode.i_mode = S_IFREG;
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001886 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001887 brelse(dibh);
1888
Steven Whitehousea91ea692006-09-04 12:04:26 -04001889out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001890 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001891out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001892 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001893out_rlist:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001894 gfs2_rlist_free(&rlist);
Steven Whitehouse6dbd8222008-01-10 15:18:55 +00001895 gfs2_glock_dq_uninit(&dip->i_alloc->al_ri_gh);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001896out_qs:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001897 gfs2_quota_unhold(dip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001898out_put:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001899 gfs2_alloc_put(dip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001900out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001901 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{
Bob Peterson556bb172011-03-22 13:56:37 -04001917 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1918 struct buffer_head *bh;
1919 struct gfs2_leaf *leaf;
1920 u32 hsize, len;
1921 u32 ht_offset, lp_offset, ht_offset_cur = -1;
1922 u32 index = 0, next_index;
1923 __be64 *lp;
1924 u64 leaf_no;
1925 int error = 0, last;
1926
1927 hsize = 1 << dip->i_depth;
1928 if (hsize * sizeof(u64) != i_size_read(&dip->i_inode)) {
1929 gfs2_consist_inode(dip);
1930 return -EIO;
1931 }
1932
1933 lp = kmalloc(sdp->sd_hash_bsize, GFP_NOFS);
1934 if (!lp)
1935 return -ENOMEM;
1936
1937 while (index < hsize) {
1938 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1939 ht_offset = index - lp_offset;
1940
1941 if (ht_offset_cur != ht_offset) {
1942 error = gfs2_dir_read_data(dip, (char *)lp,
1943 ht_offset * sizeof(__be64),
1944 sdp->sd_hash_bsize, 1);
1945 if (error != sdp->sd_hash_bsize) {
1946 if (error >= 0)
1947 error = -EIO;
1948 goto out;
1949 }
1950 ht_offset_cur = ht_offset;
1951 }
1952
1953 leaf_no = be64_to_cpu(lp[lp_offset]);
1954 if (leaf_no) {
1955 error = get_leaf(dip, leaf_no, &bh);
1956 if (error)
1957 goto out;
1958 leaf = (struct gfs2_leaf *)bh->b_data;
1959 len = 1 << (dip->i_depth - be16_to_cpu(leaf->lf_depth));
1960
1961 next_index = (index & ~(len - 1)) + len;
1962 last = ((next_index >= hsize) ? 1 : 0);
1963 error = leaf_dealloc(dip, index, len, leaf_no, bh,
1964 last);
1965 brelse(bh);
1966 if (error)
1967 goto out;
1968 index = next_index;
1969 } else
1970 index++;
1971 }
1972
1973 if (index != hsize) {
1974 gfs2_consist_inode(dip);
1975 error = -EIO;
1976 }
1977
1978out:
1979 kfree(lp);
1980
1981 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001982}
1983
1984/**
1985 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
1986 * @ip: the file being written to
1987 * @filname: the filename that's going to be added
David Teiglandb3b94fa2006-01-16 16:50:04 +00001988 *
Steven Whitehousec7526662006-03-20 12:30:04 -05001989 * Returns: 1 if alloc required, 0 if not, -ve on error
David Teiglandb3b94fa2006-01-16 16:50:04 +00001990 */
1991
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04001992int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001993{
Steven Whitehousec7526662006-03-20 12:30:04 -05001994 struct gfs2_dirent *dent;
1995 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001996
Steven Whitehousec7526662006-03-20 12:30:04 -05001997 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
Steven Whitehouseed386502006-04-07 16:28:07 -04001998 if (!dent) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001999 return 1;
Steven Whitehouseed386502006-04-07 16:28:07 -04002000 }
Steven Whitehousec7526662006-03-20 12:30:04 -05002001 if (IS_ERR(dent))
2002 return PTR_ERR(dent);
2003 brelse(bh);
2004 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002005}
2006