blob: 8ccad2467cb64fe5afbaec07db4e97ad2a75206f [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
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100243static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, __be64 *buf,
244 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) {
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100251 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000252 brelse(dibh);
253 }
254
255 return (error) ? error : size;
256}
257
258
259/**
260 * gfs2_dir_read_data - Read a data from a directory inode
261 * @ip: The GFS2 Inode
262 * @buf: The buffer to place result into
Steven Whitehousee13940b2006-01-30 13:31:50 +0000263 * @size: Amount of data to transfer
264 *
265 * Returns: The amount of data actually copied or the error
266 */
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100267static int gfs2_dir_read_data(struct gfs2_inode *ip, __be64 *buf,
268 unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000269{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400270 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehousecd915492006-09-04 12:49:07 -0400271 u64 lblock, dblock;
272 u32 extlen = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000273 unsigned int o;
274 int copied = 0;
275 int error = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000276
277 if (gfs2_is_stuffed(ip))
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100278 return gfs2_dir_read_stuffed(ip, buf, size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000279
280 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
281 return -EINVAL;
282
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100283 lblock = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000284 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
285
286 while (copied < size) {
287 unsigned int amount;
288 struct buffer_head *bh;
289 int new;
290
291 amount = size - copied;
292 if (amount > sdp->sd_sb.sb_bsize - o)
293 amount = sdp->sd_sb.sb_bsize - o;
294
295 if (!extlen) {
296 new = 0;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400297 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
Steven Whitehousefd88de562006-05-05 16:59:11 -0400298 &dblock, &extlen);
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400299 if (error || !dblock)
300 goto fail;
301 BUG_ON(extlen < 1);
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400302 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
Adrian Bunkb7d8ac32006-10-19 16:02:07 +0200303 } else {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400304 error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, &bh);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000305 if (error)
306 goto fail;
307 }
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400308 error = gfs2_metatype_check(sdp, bh, GFS2_METATYPE_JD);
309 if (error) {
310 brelse(bh);
311 goto fail;
312 }
313 dblock++;
314 extlen--;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000315 memcpy(buf, bh->b_data + o, amount);
316 brelse(bh);
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100317 buf += (amount/sizeof(__be64));
Steven Whitehousee13940b2006-01-30 13:31:50 +0000318 copied += amount;
319 lblock++;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000320 o = sizeof(struct gfs2_meta_header);
321 }
322
323 return copied;
324fail:
325 return (copied) ? copied : error;
326}
327
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100328/**
329 * gfs2_dir_get_hash_table - Get pointer to the dir hash table
330 * @ip: The inode in question
331 *
332 * Returns: The hash table or an error
333 */
334
335static __be64 *gfs2_dir_get_hash_table(struct gfs2_inode *ip)
336{
337 struct inode *inode = &ip->i_inode;
338 int ret;
339 u32 hsize;
340 __be64 *hc;
341
342 BUG_ON(!(ip->i_diskflags & GFS2_DIF_EXHASH));
343
344 hc = ip->i_hash_cache;
345 if (hc)
346 return hc;
347
348 hsize = 1 << ip->i_depth;
349 hsize *= sizeof(__be64);
350 if (hsize != i_size_read(&ip->i_inode)) {
351 gfs2_consist_inode(ip);
352 return ERR_PTR(-EIO);
353 }
354
355 hc = kmalloc(hsize, GFP_NOFS);
356 ret = -ENOMEM;
357 if (hc == NULL)
358 return ERR_PTR(-ENOMEM);
359
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100360 ret = gfs2_dir_read_data(ip, hc, hsize);
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100361 if (ret < 0) {
362 kfree(hc);
363 return ERR_PTR(ret);
364 }
365
366 spin_lock(&inode->i_lock);
367 if (ip->i_hash_cache)
368 kfree(hc);
369 else
370 ip->i_hash_cache = hc;
371 spin_unlock(&inode->i_lock);
372
373 return ip->i_hash_cache;
374}
375
376/**
377 * gfs2_dir_hash_inval - Invalidate dir hash
378 * @ip: The directory inode
379 *
380 * Must be called with an exclusive glock, or during glock invalidation.
381 */
382void gfs2_dir_hash_inval(struct gfs2_inode *ip)
383{
384 __be64 *hc = ip->i_hash_cache;
385 ip->i_hash_cache = NULL;
386 kfree(hc);
387}
388
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500389static inline int gfs2_dirent_sentinel(const struct gfs2_dirent *dent)
390{
391 return dent->de_inum.no_addr == 0 || dent->de_inum.no_formal_ino == 0;
392}
393
Steven Whitehousec7526662006-03-20 12:30:04 -0500394static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
395 const struct qstr *name, int ret)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000396{
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500397 if (!gfs2_dirent_sentinel(dent) &&
Steven Whitehousec7526662006-03-20 12:30:04 -0500398 be32_to_cpu(dent->de_hash) == name->hash &&
399 be16_to_cpu(dent->de_name_len) == name->len &&
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400400 memcmp(dent+1, name->name, name->len) == 0)
Steven Whitehousec7526662006-03-20 12:30:04 -0500401 return ret;
402 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000403}
404
Steven Whitehousec7526662006-03-20 12:30:04 -0500405static int gfs2_dirent_find(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500406 const struct qstr *name,
407 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500408{
409 return __gfs2_dirent_find(dent, name, 1);
410}
411
412static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500413 const struct qstr *name,
414 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500415{
416 return __gfs2_dirent_find(dent, name, 2);
417}
418
419/*
420 * name->name holds ptr to start of block.
421 * name->len holds size of block.
422 */
423static int gfs2_dirent_last(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500424 const struct qstr *name,
425 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500426{
427 const char *start = name->name;
428 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
429 if (name->len == (end - start))
430 return 1;
431 return 0;
432}
433
434static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500435 const struct qstr *name,
436 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500437{
438 unsigned required = GFS2_DIRENT_SIZE(name->len);
439 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
440 unsigned totlen = be16_to_cpu(dent->de_rec_len);
441
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500442 if (gfs2_dirent_sentinel(dent))
Bob Peterson728a7562010-07-14 18:12:26 -0400443 actual = 0;
Jan Engelhardtc53921242006-09-05 14:30:40 +0200444 if (totlen - actual >= required)
Steven Whitehousec7526662006-03-20 12:30:04 -0500445 return 1;
446 return 0;
447}
448
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500449struct dirent_gather {
450 const struct gfs2_dirent **pdent;
451 unsigned offset;
452};
453
454static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
455 const struct qstr *name,
456 void *opaque)
457{
458 struct dirent_gather *g = opaque;
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500459 if (!gfs2_dirent_sentinel(dent)) {
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500460 g->pdent[g->offset++] = dent;
461 }
462 return 0;
463}
464
Steven Whitehousec7526662006-03-20 12:30:04 -0500465/*
466 * Other possible things to check:
467 * - Inode located within filesystem size (and on valid block)
468 * - Valid directory entry type
469 * Not sure how heavy-weight we want to make this... could also check
470 * hash is correct for example, but that would take a lot of extra time.
471 * For now the most important thing is to check that the various sizes
472 * are correct.
473 */
474static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
475 unsigned int size, unsigned int len, int first)
476{
477 const char *msg = "gfs2_dirent too small";
478 if (unlikely(size < sizeof(struct gfs2_dirent)))
479 goto error;
480 msg = "gfs2_dirent misaligned";
481 if (unlikely(offset & 0x7))
482 goto error;
483 msg = "gfs2_dirent points beyond end of block";
484 if (unlikely(offset + size > len))
485 goto error;
486 msg = "zero inode number";
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500487 if (unlikely(!first && gfs2_dirent_sentinel(dent)))
Steven Whitehousec7526662006-03-20 12:30:04 -0500488 goto error;
489 msg = "name length is greater than space in dirent";
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500490 if (!gfs2_dirent_sentinel(dent) &&
Steven Whitehousec7526662006-03-20 12:30:04 -0500491 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
492 size))
493 goto error;
494 return 0;
495error:
496 printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
497 first ? "first in block" : "not first in block");
498 return -EIO;
499}
500
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500501static int gfs2_dirent_offset(const void *buf)
Steven Whitehousec7526662006-03-20 12:30:04 -0500502{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500503 const struct gfs2_meta_header *h = buf;
504 int offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500505
506 BUG_ON(buf == NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500507
Steven Whitehousee3167de2006-03-30 15:46:23 -0500508 switch(be32_to_cpu(h->mh_type)) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500509 case GFS2_METATYPE_LF:
510 offset = sizeof(struct gfs2_leaf);
511 break;
512 case GFS2_METATYPE_DI:
513 offset = sizeof(struct gfs2_dinode);
514 break;
515 default:
516 goto wrong_type;
517 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500518 return offset;
519wrong_type:
520 printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
Steven Whitehousee3167de2006-03-30 15:46:23 -0500521 be32_to_cpu(h->mh_type));
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500522 return -1;
523}
Steven Whitehousec7526662006-03-20 12:30:04 -0500524
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400525static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500526 unsigned int len, gfs2_dscan_t scan,
527 const struct qstr *name,
528 void *opaque)
529{
530 struct gfs2_dirent *dent, *prev;
531 unsigned offset;
532 unsigned size;
533 int ret = 0;
534
535 ret = gfs2_dirent_offset(buf);
536 if (ret < 0)
537 goto consist_inode;
538
539 offset = ret;
Steven Whitehousec7526662006-03-20 12:30:04 -0500540 prev = NULL;
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400541 dent = buf + offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500542 size = be16_to_cpu(dent->de_rec_len);
543 if (gfs2_check_dirent(dent, offset, size, len, 1))
544 goto consist_inode;
545 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500546 ret = scan(dent, name, opaque);
Steven Whitehousec7526662006-03-20 12:30:04 -0500547 if (ret)
548 break;
549 offset += size;
550 if (offset == len)
551 break;
552 prev = dent;
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400553 dent = buf + offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500554 size = be16_to_cpu(dent->de_rec_len);
555 if (gfs2_check_dirent(dent, offset, size, len, 0))
556 goto consist_inode;
557 } while(1);
558
559 switch(ret) {
560 case 0:
561 return NULL;
562 case 1:
563 return dent;
564 case 2:
565 return prev ? prev : dent;
566 default:
567 BUG_ON(ret > 0);
568 return ERR_PTR(ret);
569 }
570
Steven Whitehousec7526662006-03-20 12:30:04 -0500571consist_inode:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400572 gfs2_consist_inode(GFS2_I(inode));
Steven Whitehousec7526662006-03-20 12:30:04 -0500573 return ERR_PTR(-EIO);
574}
575
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400576static int dirent_check_reclen(struct gfs2_inode *dip,
577 const struct gfs2_dirent *d, const void *end_p)
578{
579 const void *ptr = d;
580 u16 rec_len = be16_to_cpu(d->de_rec_len);
581
582 if (unlikely(rec_len < sizeof(struct gfs2_dirent)))
583 goto broken;
584 ptr += rec_len;
585 if (ptr < end_p)
586 return rec_len;
587 if (ptr == end_p)
588 return -ENOENT;
589broken:
590 gfs2_consist_inode(dip);
591 return -EIO;
592}
593
David Teiglandb3b94fa2006-01-16 16:50:04 +0000594/**
595 * dirent_next - Next dirent
596 * @dip: the directory
597 * @bh: The buffer
598 * @dent: Pointer to list of dirents
599 *
600 * Returns: 0 on success, error code otherwise
601 */
602
603static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
604 struct gfs2_dirent **dent)
605{
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400606 struct gfs2_dirent *cur = *dent, *tmp;
607 char *bh_end = bh->b_data + bh->b_size;
608 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000609
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400610 ret = dirent_check_reclen(dip, cur, bh_end);
611 if (ret < 0)
612 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000613
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400614 tmp = (void *)cur + ret;
615 ret = dirent_check_reclen(dip, tmp, bh_end);
616 if (ret == -EIO)
617 return ret;
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000618
David Teiglandb3b94fa2006-01-16 16:50:04 +0000619 /* Only the first dent could ever have de_inum.no_addr == 0 */
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500620 if (gfs2_dirent_sentinel(tmp)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000621 gfs2_consist_inode(dip);
622 return -EIO;
623 }
624
625 *dent = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000626 return 0;
627}
628
629/**
630 * dirent_del - Delete a dirent
631 * @dip: The GFS2 inode
632 * @bh: The buffer
633 * @prev: The previous dirent
634 * @cur: The current dirent
635 *
636 */
637
638static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
639 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
640{
Steven Whitehousecd915492006-09-04 12:49:07 -0400641 u16 cur_rec_len, prev_rec_len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000642
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500643 if (gfs2_dirent_sentinel(cur)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000644 gfs2_consist_inode(dip);
645 return;
646 }
647
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000648 gfs2_trans_add_bh(dip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000649
650 /* If there is no prev entry, this is the first entry in the block.
651 The de_rec_len is already as big as it needs to be. Just zero
652 out the inode number and return. */
653
654 if (!prev) {
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500655 cur->de_inum.no_addr = 0;
656 cur->de_inum.no_formal_ino = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000657 return;
658 }
659
660 /* Combine this dentry with the previous one. */
661
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000662 prev_rec_len = be16_to_cpu(prev->de_rec_len);
663 cur_rec_len = be16_to_cpu(cur->de_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000664
665 if ((char *)prev + prev_rec_len != (char *)cur)
666 gfs2_consist_inode(dip);
667 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
668 gfs2_consist_inode(dip);
669
670 prev_rec_len += cur_rec_len;
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000671 prev->de_rec_len = cpu_to_be16(prev_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000672}
673
Steven Whitehousec7526662006-03-20 12:30:04 -0500674/*
675 * Takes a dent from which to grab space as an argument. Returns the
676 * newly created dent.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000677 */
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400678static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
679 struct gfs2_dirent *dent,
680 const struct qstr *name,
681 struct buffer_head *bh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000682{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400683 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -0500684 struct gfs2_dirent *ndent;
685 unsigned offset = 0, totlen;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000686
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500687 if (!gfs2_dirent_sentinel(dent))
Steven Whitehousec7526662006-03-20 12:30:04 -0500688 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
689 totlen = be16_to_cpu(dent->de_rec_len);
690 BUG_ON(offset + name->len > totlen);
691 gfs2_trans_add_bh(ip->i_gl, bh, 1);
692 ndent = (struct gfs2_dirent *)((char *)dent + offset);
693 dent->de_rec_len = cpu_to_be16(offset);
694 gfs2_qstr2dirent(name, totlen - offset, ndent);
695 return ndent;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000696}
697
Steven Whitehousec7526662006-03-20 12:30:04 -0500698static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
699 struct buffer_head *bh,
700 const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000701{
702 struct gfs2_dirent *dent;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400703 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500704 gfs2_dirent_find_space, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500705 if (!dent || IS_ERR(dent))
706 return dent;
707 return gfs2_init_dirent(inode, dent, name, bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000708}
709
Steven Whitehousecd915492006-09-04 12:49:07 -0400710static int get_leaf(struct gfs2_inode *dip, u64 leaf_no,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000711 struct buffer_head **bhp)
712{
713 int error;
714
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400715 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, bhp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400716 if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
717 /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000718 error = -EIO;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400719 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000720
721 return error;
722}
723
724/**
725 * get_leaf_nr - Get a leaf number associated with the index
726 * @dip: The GFS2 inode
727 * @index:
728 * @leaf_out:
729 *
730 * Returns: 0 on success, error code otherwise
731 */
732
Steven Whitehousecd915492006-09-04 12:49:07 -0400733static int get_leaf_nr(struct gfs2_inode *dip, u32 index,
734 u64 *leaf_out)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000735{
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100736 __be64 *hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000737
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100738 hash = gfs2_dir_get_hash_table(dip);
739 if (IS_ERR(hash))
740 return PTR_ERR(hash);
741 *leaf_out = be64_to_cpu(*(hash + index));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000742 return 0;
743}
744
Steven Whitehousecd915492006-09-04 12:49:07 -0400745static int get_first_leaf(struct gfs2_inode *dip, u32 index,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000746 struct buffer_head **bh_out)
747{
Steven Whitehousecd915492006-09-04 12:49:07 -0400748 u64 leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000749 int error;
750
751 error = get_leaf_nr(dip, index, &leaf_no);
752 if (!error)
753 error = get_leaf(dip, leaf_no, bh_out);
754
755 return error;
756}
757
Steven Whitehousec7526662006-03-20 12:30:04 -0500758static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
759 const struct qstr *name,
760 gfs2_dscan_t scan,
761 struct buffer_head **pbh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000762{
Steven Whitehousec7526662006-03-20 12:30:04 -0500763 struct buffer_head *bh;
764 struct gfs2_dirent *dent;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400765 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000766 int error;
767
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000768 if (ip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500769 struct gfs2_leaf *leaf;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000770 unsigned hsize = 1 << ip->i_depth;
Steven Whitehousec7526662006-03-20 12:30:04 -0500771 unsigned index;
772 u64 ln;
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100773 if (hsize * sizeof(u64) != i_size_read(inode)) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500774 gfs2_consist_inode(ip);
775 return ERR_PTR(-EIO);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000776 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400777
Steven Whitehouse9a004502008-02-01 09:23:44 +0000778 index = name->hash >> (32 - ip->i_depth);
Steven Whitehousec7526662006-03-20 12:30:04 -0500779 error = get_first_leaf(ip, index, &bh);
780 if (error)
781 return ERR_PTR(error);
782 do {
783 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500784 scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500785 if (dent)
786 goto got_dent;
787 leaf = (struct gfs2_leaf *)bh->b_data;
788 ln = be64_to_cpu(leaf->lf_next);
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400789 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500790 if (!ln)
791 break;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400792
Steven Whitehousec7526662006-03-20 12:30:04 -0500793 error = get_leaf(ip, ln, &bh);
794 } while(!error);
795
796 return error ? ERR_PTR(error) : NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000797 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000798
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400799
Steven Whitehousec7526662006-03-20 12:30:04 -0500800 error = gfs2_meta_inode_buffer(ip, &bh);
801 if (error)
802 return ERR_PTR(error);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500803 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500804got_dent:
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400805 if (unlikely(dent == NULL || IS_ERR(dent))) {
Steven Whitehouseed386502006-04-07 16:28:07 -0400806 brelse(bh);
807 bh = NULL;
808 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500809 *pbh = bh;
810 return dent;
811}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000812
Steven Whitehousec7526662006-03-20 12:30:04 -0500813static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
814{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400815 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000816 unsigned int n = 1;
Steven Whitehouse09010972009-05-20 10:48:47 +0100817 u64 bn;
818 int error;
819 struct buffer_head *bh;
Steven Whitehousec7526662006-03-20 12:30:04 -0500820 struct gfs2_leaf *leaf;
821 struct gfs2_dirent *dent;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500822 struct qstr name = { .name = "", .len = 0, .hash = 0 };
Steven Whitehouse09010972009-05-20 10:48:47 +0100823
824 error = gfs2_alloc_block(ip, &bn, &n);
825 if (error)
826 return NULL;
827 bh = gfs2_meta_new(ip->i_gl, bn);
Steven Whitehousec7526662006-03-20 12:30:04 -0500828 if (!bh)
829 return NULL;
Steven Whitehouse09010972009-05-20 10:48:47 +0100830
Steven Whitehouse5731be52008-02-01 13:16:55 +0000831 gfs2_trans_add_unrevoke(GFS2_SB(inode), bn, 1);
Steven Whitehousec7526662006-03-20 12:30:04 -0500832 gfs2_trans_add_bh(ip->i_gl, bh, 1);
833 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
834 leaf = (struct gfs2_leaf *)bh->b_data;
835 leaf->lf_depth = cpu_to_be16(depth);
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400836 leaf->lf_entries = 0;
Al Viroa2d7d022006-10-14 16:49:30 +0100837 leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400838 leaf->lf_next = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500839 memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
840 dent = (struct gfs2_dirent *)(leaf+1);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500841 gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
Steven Whitehousec7526662006-03-20 12:30:04 -0500842 *pbh = bh;
843 return leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000844}
845
846/**
847 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
848 * @dip: The GFS2 inode
849 *
850 * Returns: 0 on success, error code otherwise
851 */
852
Steven Whitehousec7526662006-03-20 12:30:04 -0500853static int dir_make_exhash(struct inode *inode)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000854{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400855 struct gfs2_inode *dip = GFS2_I(inode);
856 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000857 struct gfs2_dirent *dent;
Steven Whitehousec7526662006-03-20 12:30:04 -0500858 struct qstr args;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000859 struct buffer_head *bh, *dibh;
860 struct gfs2_leaf *leaf;
861 int y;
Steven Whitehousecd915492006-09-04 12:49:07 -0400862 u32 x;
Al Virob44b84d2006-10-14 10:46:30 -0400863 __be64 *lp;
864 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000865 int error;
866
867 error = gfs2_meta_inode_buffer(dip, &dibh);
868 if (error)
869 return error;
870
David Teiglandb3b94fa2006-01-16 16:50:04 +0000871 /* Turn over a new leaf */
872
Steven Whitehousec7526662006-03-20 12:30:04 -0500873 leaf = new_leaf(inode, &bh, 0);
874 if (!leaf)
875 return -ENOSPC;
876 bn = bh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000877
Steven Whitehousead6203f2008-11-03 13:59:19 +0000878 gfs2_assert(sdp, dip->i_entries < (1 << 16));
879 leaf->lf_entries = cpu_to_be16(dip->i_entries);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000880
881 /* Copy dirents */
882
883 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
884 sizeof(struct gfs2_dinode));
885
886 /* Find last entry */
887
888 x = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500889 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
890 sizeof(struct gfs2_leaf);
891 args.name = bh->b_data;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400892 dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500893 gfs2_dirent_last, &args, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500894 if (!dent) {
895 brelse(bh);
896 brelse(dibh);
897 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000898 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500899 if (IS_ERR(dent)) {
900 brelse(bh);
901 brelse(dibh);
902 return PTR_ERR(dent);
903 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000904
905 /* Adjust the last dirent's record length
906 (Remember that dent still points to the last entry.) */
907
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000908 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000909 sizeof(struct gfs2_dinode) -
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000910 sizeof(struct gfs2_leaf));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000911
912 brelse(bh);
913
914 /* We're done with the new leaf block, now setup the new
915 hash table. */
916
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000917 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000918 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
919
Al Virob44b84d2006-10-14 10:46:30 -0400920 lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000921
922 for (x = sdp->sd_hash_ptrs; x--; lp++)
923 *lp = cpu_to_be64(bn);
924
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100925 i_size_write(inode, sdp->sd_sb.sb_bsize / 2);
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000926 gfs2_add_inode_blocks(&dip->i_inode, 1);
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000927 dip->i_diskflags |= GFS2_DIF_EXHASH;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000928
929 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000930 dip->i_depth = y;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000931
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500932 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000933
934 brelse(dibh);
935
936 return 0;
937}
938
939/**
940 * dir_split_leaf - Split a leaf block into two
941 * @dip: The GFS2 inode
942 * @index:
943 * @leaf_no:
944 *
945 * Returns: 0 on success, error code on failure
946 */
947
Steven Whitehousec7526662006-03-20 12:30:04 -0500948static int dir_split_leaf(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000949{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400950 struct gfs2_inode *dip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000951 struct buffer_head *nbh, *obh, *dibh;
952 struct gfs2_leaf *nleaf, *oleaf;
Steven Whitehouse4da3c642006-07-11 13:19:13 -0400953 struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
Steven Whitehousecd915492006-09-04 12:49:07 -0400954 u32 start, len, half_len, divider;
Al Virob44b84d2006-10-14 10:46:30 -0400955 u64 bn, leaf_no;
956 __be64 *lp;
Steven Whitehousecd915492006-09-04 12:49:07 -0400957 u32 index;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000958 int x, moved = 0;
959 int error;
960
Steven Whitehouse9a004502008-02-01 09:23:44 +0000961 index = name->hash >> (32 - dip->i_depth);
Steven Whitehousec7526662006-03-20 12:30:04 -0500962 error = get_leaf_nr(dip, index, &leaf_no);
963 if (error)
964 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000965
966 /* Get the old leaf block */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000967 error = get_leaf(dip, leaf_no, &obh);
968 if (error)
Steven Whitehousee90deff2006-03-29 19:02:15 -0500969 return error;
970
971 oleaf = (struct gfs2_leaf *)obh->b_data;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000972 if (dip->i_depth == be16_to_cpu(oleaf->lf_depth)) {
Steven Whitehousee90deff2006-03-29 19:02:15 -0500973 brelse(obh);
974 return 1; /* can't split */
975 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000976
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000977 gfs2_trans_add_bh(dip->i_gl, obh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000978
Steven Whitehousec7526662006-03-20 12:30:04 -0500979 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
980 if (!nleaf) {
981 brelse(obh);
982 return -ENOSPC;
983 }
984 bn = nbh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000985
Steven Whitehousec7526662006-03-20 12:30:04 -0500986 /* Compute the start and len of leaf pointers in the hash table. */
Steven Whitehouse9a004502008-02-01 09:23:44 +0000987 len = 1 << (dip->i_depth - be16_to_cpu(oleaf->lf_depth));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000988 half_len = len >> 1;
989 if (!half_len) {
Steven Whitehouse9a004502008-02-01 09:23:44 +0000990 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 +0000991 gfs2_consist_inode(dip);
992 error = -EIO;
993 goto fail_brelse;
994 }
995
996 start = (index & ~(len - 1));
997
998 /* Change the pointers.
999 Don't bother distinguishing stuffed from non-stuffed.
1000 This code is complicated enough already. */
David Rientjes4244b522010-07-20 19:45:03 -07001001 lp = kmalloc(half_len * sizeof(__be64), GFP_NOFS);
1002 if (!lp) {
1003 error = -ENOMEM;
1004 goto fail_brelse;
1005 }
1006
David Teiglandb3b94fa2006-01-16 16:50:04 +00001007 /* Change the pointers */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001008 for (x = 0; x < half_len; x++)
1009 lp[x] = cpu_to_be64(bn);
1010
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001011 gfs2_dir_hash_inval(dip);
1012
Steven Whitehousecd915492006-09-04 12:49:07 -04001013 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
1014 half_len * sizeof(u64));
1015 if (error != half_len * sizeof(u64)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001016 if (error >= 0)
1017 error = -EIO;
1018 goto fail_lpfree;
1019 }
1020
1021 kfree(lp);
1022
1023 /* Compute the divider */
Steven Whitehouse9a004502008-02-01 09:23:44 +00001024 divider = (start + half_len) << (32 - dip->i_depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001025
1026 /* Copy the entries */
Steven Whitehouse15793432009-11-06 11:06:37 +00001027 dent = (struct gfs2_dirent *)(obh->b_data + sizeof(struct gfs2_leaf));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001028
1029 do {
1030 next = dent;
1031 if (dirent_next(dip, obh, &next))
1032 next = NULL;
1033
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -05001034 if (!gfs2_dirent_sentinel(dent) &&
David Teiglandb3b94fa2006-01-16 16:50:04 +00001035 be32_to_cpu(dent->de_hash) < divider) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001036 struct qstr str;
1037 str.name = (char*)(dent+1);
1038 str.len = be16_to_cpu(dent->de_name_len);
1039 str.hash = be32_to_cpu(dent->de_hash);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001040 new = gfs2_dirent_alloc(inode, nbh, &str);
Steven Whitehousec7526662006-03-20 12:30:04 -05001041 if (IS_ERR(new)) {
1042 error = PTR_ERR(new);
1043 break;
1044 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001045
1046 new->de_inum = dent->de_inum; /* No endian worries */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001047 new->de_type = dent->de_type; /* No endian worries */
Marcin Slusarzbb16b342008-02-13 00:06:10 +01001048 be16_add_cpu(&nleaf->lf_entries, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001049
1050 dirent_del(dip, obh, prev, dent);
1051
1052 if (!oleaf->lf_entries)
1053 gfs2_consist_inode(dip);
Marcin Slusarzbb16b342008-02-13 00:06:10 +01001054 be16_add_cpu(&oleaf->lf_entries, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001055
1056 if (!prev)
1057 prev = dent;
1058
1059 moved = 1;
Steven Whitehousec7526662006-03-20 12:30:04 -05001060 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001061 prev = dent;
Steven Whitehousec7526662006-03-20 12:30:04 -05001062 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001063 dent = next;
Steven Whitehousec7526662006-03-20 12:30:04 -05001064 } while (dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001065
Steven Whitehousec7526662006-03-20 12:30:04 -05001066 oleaf->lf_depth = nleaf->lf_depth;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001067
1068 error = gfs2_meta_inode_buffer(dip, &dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001069 if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
Steven Whitehouse382e6e22007-08-16 17:08:20 +01001070 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001071 gfs2_add_inode_blocks(&dip->i_inode, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001072 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001073 brelse(dibh);
1074 }
1075
1076 brelse(obh);
1077 brelse(nbh);
1078
1079 return error;
1080
Steven Whitehousee90deff2006-03-29 19:02:15 -05001081fail_lpfree:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001082 kfree(lp);
1083
Steven Whitehousee90deff2006-03-29 19:02:15 -05001084fail_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001085 brelse(obh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001086 brelse(nbh);
1087 return error;
1088}
1089
1090/**
1091 * dir_double_exhash - Double size of ExHash table
1092 * @dip: The GFS2 dinode
1093 *
1094 * Returns: 0 on success, error code on failure
1095 */
1096
1097static int dir_double_exhash(struct gfs2_inode *dip)
1098{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001099 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001100 u32 hsize;
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001101 u32 hsize_bytes;
1102 __be64 *hc;
1103 __be64 *hc2, *h;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001104 int x;
1105 int error = 0;
1106
Steven Whitehouse9a004502008-02-01 09:23:44 +00001107 hsize = 1 << dip->i_depth;
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001108 hsize_bytes = hsize * sizeof(__be64);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001109
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001110 hc = gfs2_dir_get_hash_table(dip);
1111 if (IS_ERR(hc))
1112 return PTR_ERR(hc);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001113
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001114 h = hc2 = kmalloc(hsize_bytes * 2, GFP_NOFS);
1115 if (!hc2)
David Rientjes4244b522010-07-20 19:45:03 -07001116 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001117
David Teiglandb3b94fa2006-01-16 16:50:04 +00001118 error = gfs2_meta_inode_buffer(dip, &dibh);
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001119 if (error)
1120 goto out_kfree;
1121
1122 for (x = 0; x < hsize; x++) {
1123 *h++ = *hc;
1124 *h++ = *hc;
1125 hc++;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001126 }
1127
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001128 error = gfs2_dir_write_data(dip, (char *)hc2, 0, hsize_bytes * 2);
1129 if (error != (hsize_bytes * 2))
1130 goto fail;
1131
1132 gfs2_dir_hash_inval(dip);
1133 dip->i_hash_cache = hc2;
1134 dip->i_depth++;
1135 gfs2_dinode_out(dip, dibh->b_data);
1136 brelse(dibh);
1137 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001138
Steven Whitehousea91ea692006-09-04 12:04:26 -04001139fail:
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001140 /* Replace original hash table & size */
1141 gfs2_dir_write_data(dip, (char *)hc, 0, hsize_bytes);
1142 i_size_write(&dip->i_inode, hsize_bytes);
1143 gfs2_dinode_out(dip, dibh->b_data);
1144 brelse(dibh);
1145out_kfree:
1146 kfree(hc2);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001147 return error;
1148}
1149
1150/**
1151 * compare_dents - compare directory entries by hash value
1152 * @a: first dent
1153 * @b: second dent
1154 *
1155 * When comparing the hash entries of @a to @b:
1156 * gt: returns 1
1157 * lt: returns -1
1158 * eq: returns 0
1159 */
1160
1161static int compare_dents(const void *a, const void *b)
1162{
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001163 const struct gfs2_dirent *dent_a, *dent_b;
Steven Whitehousecd915492006-09-04 12:49:07 -04001164 u32 hash_a, hash_b;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001165 int ret = 0;
1166
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001167 dent_a = *(const struct gfs2_dirent **)a;
Steven Whitehousec7526662006-03-20 12:30:04 -05001168 hash_a = be32_to_cpu(dent_a->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001169
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001170 dent_b = *(const struct gfs2_dirent **)b;
Steven Whitehousec7526662006-03-20 12:30:04 -05001171 hash_b = be32_to_cpu(dent_b->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001172
1173 if (hash_a > hash_b)
1174 ret = 1;
1175 else if (hash_a < hash_b)
1176 ret = -1;
1177 else {
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001178 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1179 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001180
1181 if (len_a > len_b)
1182 ret = 1;
1183 else if (len_a < len_b)
1184 ret = -1;
1185 else
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001186 ret = memcmp(dent_a + 1, dent_b + 1, len_a);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001187 }
1188
1189 return ret;
1190}
1191
1192/**
1193 * do_filldir_main - read out directory entries
1194 * @dip: The GFS2 inode
1195 * @offset: The offset in the file to read from
1196 * @opaque: opaque data to pass to filldir
1197 * @filldir: The function to pass entries to
1198 * @darr: an array of struct gfs2_dirent pointers to read
1199 * @entries: the number of entries in darr
1200 * @copied: pointer to int that's non-zero if a entry has been copied out
1201 *
1202 * Jump through some hoops to make sure that if there are hash collsions,
1203 * they are read out at the beginning of a buffer. We want to minimize
1204 * the possibility that they will fall into different readdir buffers or
1205 * that someone will want to seek to that location.
1206 *
1207 * Returns: errno, >0 on exception from filldir
1208 */
1209
Steven Whitehousecd915492006-09-04 12:49:07 -04001210static int do_filldir_main(struct gfs2_inode *dip, u64 *offset,
Steven Whitehouse3699e3a2007-01-17 15:09:20 +00001211 void *opaque, filldir_t filldir,
Steven Whitehousecd915492006-09-04 12:49:07 -04001212 const struct gfs2_dirent **darr, u32 entries,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001213 int *copied)
1214{
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001215 const struct gfs2_dirent *dent, *dent_next;
Steven Whitehousecd915492006-09-04 12:49:07 -04001216 u64 off, off_next;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001217 unsigned int x, y;
1218 int run = 0;
1219 int error = 0;
1220
1221 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1222
1223 dent_next = darr[0];
1224 off_next = be32_to_cpu(dent_next->de_hash);
1225 off_next = gfs2_disk_hash2offset(off_next);
1226
1227 for (x = 0, y = 1; x < entries; x++, y++) {
1228 dent = dent_next;
1229 off = off_next;
1230
1231 if (y < entries) {
1232 dent_next = darr[y];
1233 off_next = be32_to_cpu(dent_next->de_hash);
1234 off_next = gfs2_disk_hash2offset(off_next);
1235
1236 if (off < *offset)
1237 continue;
1238 *offset = off;
1239
1240 if (off_next == off) {
1241 if (*copied && !run)
1242 return 1;
1243 run = 1;
1244 } else
1245 run = 0;
1246 } else {
1247 if (off < *offset)
1248 continue;
1249 *offset = off;
1250 }
1251
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001252 error = filldir(opaque, (const char *)(dent + 1),
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001253 be16_to_cpu(dent->de_name_len),
Steven Whitehouse3699e3a2007-01-17 15:09:20 +00001254 off, be64_to_cpu(dent->de_inum.no_addr),
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001255 be16_to_cpu(dent->de_type));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001256 if (error)
1257 return 1;
1258
1259 *copied = 1;
1260 }
1261
1262 /* Increment the *offset by one, so the next time we come into the
1263 do_filldir fxn, we get the next entry instead of the last one in the
1264 current leaf */
1265
1266 (*offset)++;
1267
1268 return 0;
1269}
1270
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001271static void *gfs2_alloc_sort_buffer(unsigned size)
1272{
1273 void *ptr = NULL;
1274
1275 if (size < KMALLOC_MAX_SIZE)
1276 ptr = kmalloc(size, GFP_NOFS | __GFP_NOWARN);
1277 if (!ptr)
1278 ptr = __vmalloc(size, GFP_NOFS, PAGE_KERNEL);
1279 return ptr;
1280}
1281
1282static void gfs2_free_sort_buffer(void *ptr)
1283{
1284 if (is_vmalloc_addr(ptr))
1285 vfree(ptr);
1286 else
1287 kfree(ptr);
1288}
1289
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001290static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
Steven Whitehouse3699e3a2007-01-17 15:09:20 +00001291 filldir_t filldir, int *copied, unsigned *depth,
1292 u64 leaf_no)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001293{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001294 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001295 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001296 struct buffer_head *bh;
1297 struct gfs2_leaf *lf;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001298 unsigned entries = 0, entries2 = 0;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001299 unsigned leaves = 0;
1300 const struct gfs2_dirent **darr, *dent;
1301 struct dirent_gather g;
1302 struct buffer_head **larr;
1303 int leaf = 0;
1304 int error, i;
1305 u64 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001306
David Teiglandb3b94fa2006-01-16 16:50:04 +00001307 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001308 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001309 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001310 goto out;
1311 lf = (struct gfs2_leaf *)bh->b_data;
1312 if (leaves == 0)
1313 *depth = be16_to_cpu(lf->lf_depth);
1314 entries += be16_to_cpu(lf->lf_entries);
1315 leaves++;
1316 lfn = be64_to_cpu(lf->lf_next);
1317 brelse(bh);
1318 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001319
1320 if (!entries)
1321 return 0;
1322
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001323 error = -ENOMEM;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001324 /*
1325 * The extra 99 entries are not normally used, but are a buffer
1326 * zone in case the number of entries in the leaf is corrupt.
1327 * 99 is the maximum number of entries that can fit in a single
1328 * leaf block.
1329 */
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001330 larr = gfs2_alloc_sort_buffer((leaves + entries + 99) * sizeof(void *));
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001331 if (!larr)
1332 goto out;
1333 darr = (const struct gfs2_dirent **)(larr + leaves);
1334 g.pdent = darr;
1335 g.offset = 0;
1336 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001337
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001338 do {
1339 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001340 if (error)
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001341 goto out_free;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001342 lf = (struct gfs2_leaf *)bh->b_data;
1343 lfn = be64_to_cpu(lf->lf_next);
1344 if (lf->lf_entries) {
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001345 entries2 += be16_to_cpu(lf->lf_entries);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001346 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1347 gfs2_dirent_gather, NULL, &g);
1348 error = PTR_ERR(dent);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001349 if (IS_ERR(dent))
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001350 goto out_free;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001351 if (entries2 != g.offset) {
akpm@linux-foundation.orgf391a4e2007-04-25 21:08:02 -07001352 fs_warn(sdp, "Number of entries corrupt in dir "
1353 "leaf %llu, entries2 (%u) != "
1354 "g.offset (%u)\n",
1355 (unsigned long long)bh->b_blocknr,
1356 entries2, g.offset);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001357
1358 error = -EIO;
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001359 goto out_free;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001360 }
1361 error = 0;
1362 larr[leaf++] = bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001363 } else {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001364 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001365 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001366 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001367
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001368 BUG_ON(entries2 != entries);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001369 error = do_filldir_main(ip, offset, opaque, filldir, darr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001370 entries, copied);
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001371out_free:
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001372 for(i = 0; i < leaf; i++)
1373 brelse(larr[i]);
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001374 gfs2_free_sort_buffer(larr);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001375out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001376 return error;
1377}
1378
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001379
David Teiglandb3b94fa2006-01-16 16:50:04 +00001380/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001381 * dir_e_read - Reads the entries from a directory into a filldir buffer
1382 * @dip: dinode pointer
1383 * @offset: the hash of the last entry read shifted to the right once
1384 * @opaque: buffer for the filldir function to fill
1385 * @filldir: points to the filldir function to use
1386 *
1387 * Returns: errno
1388 */
1389
Steven Whitehousecd915492006-09-04 12:49:07 -04001390static int dir_e_read(struct inode *inode, u64 *offset, void *opaque,
Steven Whitehouse3699e3a2007-01-17 15:09:20 +00001391 filldir_t filldir)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001392{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001393 struct gfs2_inode *dip = GFS2_I(inode);
Steven Whitehousecd915492006-09-04 12:49:07 -04001394 u32 hsize, len = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -04001395 u32 hash, index;
Al Virob44b84d2006-10-14 10:46:30 -04001396 __be64 *lp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001397 int copied = 0;
1398 int error = 0;
Steven Whitehouse4da3c642006-07-11 13:19:13 -04001399 unsigned depth = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001400
Steven Whitehouse9a004502008-02-01 09:23:44 +00001401 hsize = 1 << dip->i_depth;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001402 hash = gfs2_dir_offset2hash(*offset);
Steven Whitehouse9a004502008-02-01 09:23:44 +00001403 index = hash >> (32 - dip->i_depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001404
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001405 lp = gfs2_dir_get_hash_table(dip);
1406 if (IS_ERR(lp))
1407 return PTR_ERR(lp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001408
1409 while (index < hsize) {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001410 error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
1411 &copied, &depth,
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001412 be64_to_cpu(lp[index]));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001413 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001414 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001415
Steven Whitehouse9a004502008-02-01 09:23:44 +00001416 len = 1 << (dip->i_depth - depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001417 index = (index & ~(len - 1)) + len;
1418 }
1419
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001420 if (error > 0)
1421 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001422 return error;
1423}
1424
Steven Whitehousecd915492006-09-04 12:49:07 -04001425int gfs2_dir_read(struct inode *inode, u64 *offset, void *opaque,
Steven Whitehouse3699e3a2007-01-17 15:09:20 +00001426 filldir_t filldir)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001427{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001428 struct gfs2_inode *dip = GFS2_I(inode);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001429 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001430 struct dirent_gather g;
1431 const struct gfs2_dirent **darr, *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001432 struct buffer_head *dibh;
1433 int copied = 0;
1434 int error;
1435
Steven Whitehousead6203f2008-11-03 13:59:19 +00001436 if (!dip->i_entries)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001437 return 0;
1438
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001439 if (dip->i_diskflags & GFS2_DIF_EXHASH)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001440 return dir_e_read(inode, offset, opaque, filldir);
1441
David Teiglandb3b94fa2006-01-16 16:50:04 +00001442 if (!gfs2_is_stuffed(dip)) {
1443 gfs2_consist_inode(dip);
1444 return -EIO;
1445 }
1446
David Teiglandb3b94fa2006-01-16 16:50:04 +00001447 error = gfs2_meta_inode_buffer(dip, &dibh);
1448 if (error)
1449 return error;
1450
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001451 error = -ENOMEM;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001452 /* 96 is max number of dirents which can be stuffed into an inode */
Josef Bacik16c5f062008-04-09 09:33:41 -04001453 darr = kmalloc(96 * sizeof(struct gfs2_dirent *), GFP_NOFS);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001454 if (darr) {
1455 g.pdent = darr;
1456 g.offset = 0;
1457 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1458 gfs2_dirent_gather, NULL, &g);
1459 if (IS_ERR(dent)) {
1460 error = PTR_ERR(dent);
1461 goto out;
1462 }
Steven Whitehousead6203f2008-11-03 13:59:19 +00001463 if (dip->i_entries != g.offset) {
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001464 fs_warn(sdp, "Number of entries corrupt in dir %llu, "
Steven Whitehousead6203f2008-11-03 13:59:19 +00001465 "ip->i_entries (%u) != g.offset (%u)\n",
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001466 (unsigned long long)dip->i_no_addr,
Steven Whitehousead6203f2008-11-03 13:59:19 +00001467 dip->i_entries,
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001468 g.offset);
1469 error = -EIO;
1470 goto out;
1471 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001472 error = do_filldir_main(dip, offset, opaque, filldir, darr,
Steven Whitehousead6203f2008-11-03 13:59:19 +00001473 dip->i_entries, &copied);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001474out:
1475 kfree(darr);
1476 }
1477
David Teiglandb3b94fa2006-01-16 16:50:04 +00001478 if (error > 0)
1479 error = 0;
1480
1481 brelse(dibh);
1482
1483 return error;
1484}
1485
David Teiglandb3b94fa2006-01-16 16:50:04 +00001486/**
1487 * gfs2_dir_search - Search a directory
1488 * @dip: The GFS2 inode
1489 * @filename:
1490 * @inode:
1491 *
1492 * This routine searches a directory for a file or another directory.
1493 * Assumes a glock is held on dip.
1494 *
1495 * Returns: errno
1496 */
1497
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001498struct inode *gfs2_dir_search(struct inode *dir, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001499{
Steven Whitehousec7526662006-03-20 12:30:04 -05001500 struct buffer_head *bh;
1501 struct gfs2_dirent *dent;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001502 struct inode *inode;
1503
1504 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1505 if (dent) {
1506 if (IS_ERR(dent))
David Howellse231c2e2008-02-07 00:15:26 -08001507 return ERR_CAST(dent);
Wendy Chengbb9bcf02007-06-27 17:07:08 -04001508 inode = gfs2_inode_lookup(dir->i_sb,
1509 be16_to_cpu(dent->de_type),
1510 be64_to_cpu(dent->de_inum.no_addr),
Bob Peterson44ad37d2011-03-17 16:19:58 -04001511 be64_to_cpu(dent->de_inum.no_formal_ino), 0);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001512 brelse(bh);
1513 return inode;
1514 }
1515 return ERR_PTR(-ENOENT);
1516}
1517
1518int gfs2_dir_check(struct inode *dir, const struct qstr *name,
1519 const struct gfs2_inode *ip)
1520{
1521 struct buffer_head *bh;
1522 struct gfs2_dirent *dent;
1523 int ret = -ENOENT;
Steven Whitehousec7526662006-03-20 12:30:04 -05001524
1525 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1526 if (dent) {
1527 if (IS_ERR(dent))
1528 return PTR_ERR(dent);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001529 if (ip) {
1530 if (be64_to_cpu(dent->de_inum.no_addr) != ip->i_no_addr)
1531 goto out;
1532 if (be64_to_cpu(dent->de_inum.no_formal_ino) !=
1533 ip->i_no_formal_ino)
1534 goto out;
1535 if (unlikely(IF2DT(ip->i_inode.i_mode) !=
1536 be16_to_cpu(dent->de_type))) {
1537 gfs2_consist_inode(GFS2_I(dir));
1538 ret = -EIO;
1539 goto out;
1540 }
1541 }
1542 ret = 0;
1543out:
Steven Whitehousec7526662006-03-20 12:30:04 -05001544 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001545 }
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001546 return ret;
Steven Whitehousec7526662006-03-20 12:30:04 -05001547}
1548
1549static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1550{
1551 struct buffer_head *bh, *obh;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001552 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001553 struct gfs2_leaf *leaf, *oleaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001554 int error;
Steven Whitehousec7526662006-03-20 12:30:04 -05001555 u32 index;
1556 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001557
Steven Whitehouse9a004502008-02-01 09:23:44 +00001558 index = name->hash >> (32 - ip->i_depth);
Steven Whitehousec7526662006-03-20 12:30:04 -05001559 error = get_first_leaf(ip, index, &obh);
1560 if (error)
1561 return error;
1562 do {
1563 oleaf = (struct gfs2_leaf *)obh->b_data;
1564 bn = be64_to_cpu(oleaf->lf_next);
1565 if (!bn)
1566 break;
1567 brelse(obh);
1568 error = get_leaf(ip, bn, &obh);
1569 if (error)
1570 return error;
1571 } while(1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001572
Steven Whitehousec7526662006-03-20 12:30:04 -05001573 gfs2_trans_add_bh(ip->i_gl, obh, 1);
1574
1575 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1576 if (!leaf) {
1577 brelse(obh);
1578 return -ENOSPC;
1579 }
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04001580 oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
Steven Whitehousec7526662006-03-20 12:30:04 -05001581 brelse(bh);
1582 brelse(obh);
1583
1584 error = gfs2_meta_inode_buffer(ip, &bh);
1585 if (error)
1586 return error;
1587 gfs2_trans_add_bh(ip->i_gl, bh, 1);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001588 gfs2_add_inode_blocks(&ip->i_inode, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001589 gfs2_dinode_out(ip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001590 brelse(bh);
1591 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001592}
1593
1594/**
1595 * gfs2_dir_add - Add new filename into directory
1596 * @dip: The GFS2 inode
1597 * @filename: The new name
1598 * @inode: The inode number of the entry
1599 * @type: The type of the entry
1600 *
1601 * Returns: 0 on success, error code on failure
1602 */
1603
Steven Whitehousec7526662006-03-20 12:30:04 -05001604int gfs2_dir_add(struct inode *inode, const struct qstr *name,
Steven Whitehouse3d6ecb72011-05-09 13:30:08 +01001605 const struct gfs2_inode *nip)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001606{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001607 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001608 struct buffer_head *bh;
1609 struct gfs2_dirent *dent;
1610 struct gfs2_leaf *leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001611 int error;
1612
Steven Whitehousec7526662006-03-20 12:30:04 -05001613 while(1) {
1614 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
1615 &bh);
1616 if (dent) {
1617 if (IS_ERR(dent))
1618 return PTR_ERR(dent);
1619 dent = gfs2_init_dirent(inode, dent, name, bh);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001620 gfs2_inum_out(nip, dent);
Steven Whitehouse3d6ecb72011-05-09 13:30:08 +01001621 dent->de_type = cpu_to_be16(IF2DT(nip->i_inode.i_mode));
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001622 if (ip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001623 leaf = (struct gfs2_leaf *)bh->b_data;
Marcin Slusarzbb16b342008-02-13 00:06:10 +01001624 be16_add_cpu(&leaf->lf_entries, 1);
Steven Whitehousec7526662006-03-20 12:30:04 -05001625 }
1626 brelse(bh);
1627 error = gfs2_meta_inode_buffer(ip, &bh);
1628 if (error)
1629 break;
1630 gfs2_trans_add_bh(ip->i_gl, bh, 1);
Steven Whitehousead6203f2008-11-03 13:59:19 +00001631 ip->i_entries++;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001632 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehouse3d6ecb72011-05-09 13:30:08 +01001633 if (S_ISDIR(nip->i_inode.i_mode))
1634 inc_nlink(&ip->i_inode);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001635 gfs2_dinode_out(ip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001636 brelse(bh);
1637 error = 0;
1638 break;
1639 }
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001640 if (!(ip->i_diskflags & GFS2_DIF_EXHASH)) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001641 error = dir_make_exhash(inode);
1642 if (error)
1643 break;
1644 continue;
1645 }
1646 error = dir_split_leaf(inode, name);
1647 if (error == 0)
1648 continue;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001649 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001650 break;
Steven Whitehouse9a004502008-02-01 09:23:44 +00001651 if (ip->i_depth < GFS2_DIR_MAX_DEPTH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001652 error = dir_double_exhash(ip);
1653 if (error)
1654 break;
1655 error = dir_split_leaf(inode, name);
Steven Whitehousee90deff2006-03-29 19:02:15 -05001656 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001657 break;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001658 if (error == 0)
1659 continue;
Steven Whitehousec7526662006-03-20 12:30:04 -05001660 }
1661 error = dir_new_leaf(inode, name);
1662 if (!error)
1663 continue;
1664 error = -ENOSPC;
1665 break;
1666 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001667 return error;
1668}
1669
Steven Whitehousec7526662006-03-20 12:30:04 -05001670
David Teiglandb3b94fa2006-01-16 16:50:04 +00001671/**
1672 * gfs2_dir_del - Delete a directory entry
1673 * @dip: The GFS2 inode
1674 * @filename: The filename
1675 *
1676 * Returns: 0 on success, error code on failure
1677 */
1678
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001679int gfs2_dir_del(struct gfs2_inode *dip, const struct dentry *dentry)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001680{
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001681 const struct qstr *name = &dentry->d_name;
Steven Whitehousec7526662006-03-20 12:30:04 -05001682 struct gfs2_dirent *dent, *prev = NULL;
1683 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001684
Steven Whitehousec7526662006-03-20 12:30:04 -05001685 /* Returns _either_ the entry (if its first in block) or the
1686 previous entry otherwise */
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001687 dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001688 if (!dent) {
1689 gfs2_consist_inode(dip);
1690 return -EIO;
1691 }
1692 if (IS_ERR(dent)) {
1693 gfs2_consist_inode(dip);
1694 return PTR_ERR(dent);
1695 }
1696 /* If not first in block, adjust pointers accordingly */
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001697 if (gfs2_dirent_find(dent, name, NULL) == 0) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001698 prev = dent;
1699 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1700 }
1701
1702 dirent_del(dip, bh, prev, dent);
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001703 if (dip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001704 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1705 u16 entries = be16_to_cpu(leaf->lf_entries);
1706 if (!entries)
1707 gfs2_consist_inode(dip);
1708 leaf->lf_entries = cpu_to_be16(--entries);
Steven Whitehousec7526662006-03-20 12:30:04 -05001709 }
Steven Whitehouseed386502006-04-07 16:28:07 -04001710 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001711
Steven Whitehousead6203f2008-11-03 13:59:19 +00001712 if (!dip->i_entries)
Steven Whitehousec7526662006-03-20 12:30:04 -05001713 gfs2_consist_inode(dip);
Steven Whitehousead6203f2008-11-03 13:59:19 +00001714 dip->i_entries--;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001715 dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001716 if (S_ISDIR(dentry->d_inode->i_mode))
1717 drop_nlink(&dip->i_inode);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001718 mark_inode_dirty(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001719
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001720 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001721}
1722
David Teiglandb3b94fa2006-01-16 16:50:04 +00001723/**
1724 * gfs2_dir_mvino - Change inode number of directory entry
1725 * @dip: The GFS2 inode
1726 * @filename:
1727 * @new_inode:
1728 *
1729 * This routine changes the inode number of a directory entry. It's used
1730 * by rename to change ".." when a directory is moved.
1731 * Assumes a glock is held on dvp.
1732 *
1733 * Returns: errno
1734 */
1735
Steven Whitehousec7526662006-03-20 12:30:04 -05001736int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001737 const struct gfs2_inode *nip, unsigned int new_type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001738{
Steven Whitehousec7526662006-03-20 12:30:04 -05001739 struct buffer_head *bh;
1740 struct gfs2_dirent *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001741 int error;
1742
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001743 dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001744 if (!dent) {
1745 gfs2_consist_inode(dip);
1746 return -EIO;
1747 }
1748 if (IS_ERR(dent))
1749 return PTR_ERR(dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001750
Steven Whitehousec7526662006-03-20 12:30:04 -05001751 gfs2_trans_add_bh(dip->i_gl, bh, 1);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001752 gfs2_inum_out(nip, dent);
Steven Whitehousec7526662006-03-20 12:30:04 -05001753 dent->de_type = cpu_to_be16(new_type);
1754
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001755 if (dip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001756 brelse(bh);
1757 error = gfs2_meta_inode_buffer(dip, &bh);
1758 if (error)
1759 return error;
1760 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1761 }
1762
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001763 dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001764 gfs2_dinode_out(dip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001765 brelse(bh);
1766 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001767}
1768
1769/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001770 * leaf_dealloc - Deallocate a directory leaf
1771 * @dip: the directory
1772 * @index: the hash table offset in the directory
1773 * @len: the number of pointers to this leaf
1774 * @leaf_no: the leaf number
Bob Petersonec038c82011-03-22 13:55:23 -04001775 * @leaf_bh: buffer_head for the starting leaf
Bob Petersond24a7a42011-03-22 13:54:03 -04001776 * last_dealloc: 1 if this is the final dealloc for the leaf, else 0
David Teiglandb3b94fa2006-01-16 16:50:04 +00001777 *
1778 * Returns: errno
1779 */
1780
Steven Whitehousecd915492006-09-04 12:49:07 -04001781static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
Bob Petersonec038c82011-03-22 13:55:23 -04001782 u64 leaf_no, struct buffer_head *leaf_bh,
1783 int last_dealloc)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001784{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001785 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001786 struct gfs2_leaf *tmp_leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001787 struct gfs2_rgrp_list rlist;
1788 struct buffer_head *bh, *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001789 u64 blk, nblk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001790 unsigned int rg_blocks = 0, l_blocks = 0;
1791 char *ht;
Steven Whitehousecd915492006-09-04 12:49:07 -04001792 unsigned int x, size = len * sizeof(u64);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001793 int error;
1794
1795 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1796
Josef Bacik16c5f062008-04-09 09:33:41 -04001797 ht = kzalloc(size, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001798 if (!ht)
1799 return -ENOMEM;
1800
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001801 if (!gfs2_alloc_get(dip)) {
1802 error = -ENOMEM;
1803 goto out;
1804 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001805
1806 error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1807 if (error)
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001808 goto out_put;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001809
David Teiglandb3b94fa2006-01-16 16:50:04 +00001810 /* Count the number of leaves */
Bob Petersonec038c82011-03-22 13:55:23 -04001811 bh = leaf_bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001812
Steven Whitehousec7526662006-03-20 12:30:04 -05001813 for (blk = leaf_no; blk; blk = nblk) {
Bob Petersonec038c82011-03-22 13:55:23 -04001814 if (blk != leaf_no) {
1815 error = get_leaf(dip, blk, &bh);
1816 if (error)
1817 goto out_rlist;
1818 }
Steven Whitehousec7526662006-03-20 12:30:04 -05001819 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1820 nblk = be64_to_cpu(tmp_leaf->lf_next);
Bob Petersonec038c82011-03-22 13:55:23 -04001821 if (blk != leaf_no)
1822 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001823
Steven Whitehouse70b0c362011-09-02 16:08:09 +01001824 gfs2_rlist_add(dip, &rlist, blk);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001825 l_blocks++;
1826 }
1827
Bob Petersonfe6c9912008-01-28 11:13:02 -06001828 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001829
1830 for (x = 0; x < rlist.rl_rgrps; x++) {
1831 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001832 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001833 rg_blocks += rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001834 }
1835
1836 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1837 if (error)
1838 goto out_rlist;
1839
1840 error = gfs2_trans_begin(sdp,
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001841 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
David Teiglandb3b94fa2006-01-16 16:50:04 +00001842 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1843 if (error)
1844 goto out_rg_gunlock;
1845
Bob Petersonec038c82011-03-22 13:55:23 -04001846 bh = leaf_bh;
1847
Steven Whitehousec7526662006-03-20 12:30:04 -05001848 for (blk = leaf_no; blk; blk = nblk) {
Bob Petersonec038c82011-03-22 13:55:23 -04001849 if (blk != leaf_no) {
1850 error = get_leaf(dip, blk, &bh);
1851 if (error)
1852 goto out_end_trans;
1853 }
Steven Whitehousec7526662006-03-20 12:30:04 -05001854 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1855 nblk = be64_to_cpu(tmp_leaf->lf_next);
Bob Petersonec038c82011-03-22 13:55:23 -04001856 if (blk != leaf_no)
1857 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001858
1859 gfs2_free_meta(dip, blk, 1);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001860 gfs2_add_inode_blocks(&dip->i_inode, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001861 }
1862
Steven Whitehousecd915492006-09-04 12:49:07 -04001863 error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001864 if (error != size) {
1865 if (error >= 0)
1866 error = -EIO;
1867 goto out_end_trans;
1868 }
1869
1870 error = gfs2_meta_inode_buffer(dip, &dibh);
1871 if (error)
1872 goto out_end_trans;
1873
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001874 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
Bob Petersond24a7a42011-03-22 13:54:03 -04001875 /* On the last dealloc, make this a regular file in case we crash.
1876 (We don't want to free these blocks a second time.) */
1877 if (last_dealloc)
1878 dip->i_inode.i_mode = S_IFREG;
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001879 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001880 brelse(dibh);
1881
Steven Whitehousea91ea692006-09-04 12:04:26 -04001882out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001883 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001884out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001885 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001886out_rlist:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001887 gfs2_rlist_free(&rlist);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001888 gfs2_quota_unhold(dip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001889out_put:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001890 gfs2_alloc_put(dip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001891out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001892 kfree(ht);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001893 return error;
1894}
1895
1896/**
1897 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1898 * @dip: the directory
1899 *
1900 * Dealloc all on-disk directory leaves to FREEMETA state
1901 * Change on-disk inode type to "regular file"
1902 *
1903 * Returns: errno
1904 */
1905
1906int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
1907{
Bob Peterson556bb172011-03-22 13:56:37 -04001908 struct buffer_head *bh;
1909 struct gfs2_leaf *leaf;
1910 u32 hsize, len;
Bob Peterson556bb172011-03-22 13:56:37 -04001911 u32 index = 0, next_index;
1912 __be64 *lp;
1913 u64 leaf_no;
1914 int error = 0, last;
1915
1916 hsize = 1 << dip->i_depth;
Bob Peterson556bb172011-03-22 13:56:37 -04001917
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001918 lp = gfs2_dir_get_hash_table(dip);
1919 if (IS_ERR(lp))
1920 return PTR_ERR(lp);
Bob Peterson556bb172011-03-22 13:56:37 -04001921
1922 while (index < hsize) {
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001923 leaf_no = be64_to_cpu(lp[index]);
Bob Peterson556bb172011-03-22 13:56:37 -04001924 if (leaf_no) {
1925 error = get_leaf(dip, leaf_no, &bh);
1926 if (error)
1927 goto out;
1928 leaf = (struct gfs2_leaf *)bh->b_data;
1929 len = 1 << (dip->i_depth - be16_to_cpu(leaf->lf_depth));
1930
1931 next_index = (index & ~(len - 1)) + len;
1932 last = ((next_index >= hsize) ? 1 : 0);
1933 error = leaf_dealloc(dip, index, len, leaf_no, bh,
1934 last);
1935 brelse(bh);
1936 if (error)
1937 goto out;
1938 index = next_index;
1939 } else
1940 index++;
1941 }
1942
1943 if (index != hsize) {
1944 gfs2_consist_inode(dip);
1945 error = -EIO;
1946 }
1947
1948out:
Bob Peterson556bb172011-03-22 13:56:37 -04001949
1950 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001951}
1952
1953/**
1954 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
1955 * @ip: the file being written to
1956 * @filname: the filename that's going to be added
David Teiglandb3b94fa2006-01-16 16:50:04 +00001957 *
Steven Whitehousec7526662006-03-20 12:30:04 -05001958 * Returns: 1 if alloc required, 0 if not, -ve on error
David Teiglandb3b94fa2006-01-16 16:50:04 +00001959 */
1960
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04001961int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001962{
Steven Whitehousec7526662006-03-20 12:30:04 -05001963 struct gfs2_dirent *dent;
1964 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001965
Steven Whitehousec7526662006-03-20 12:30:04 -05001966 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
Steven Whitehouseed386502006-04-07 16:28:07 -04001967 if (!dent) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001968 return 1;
Steven Whitehouseed386502006-04-07 16:28:07 -04001969 }
Steven Whitehousec7526662006-03-20 12:30:04 -05001970 if (IS_ERR(dent))
1971 return PTR_ERR(dent);
1972 brelse(bh);
1973 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001974}
1975