blob: c35573abd3710a123112edcdf30c29c5e9f41023 [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
Bob Petersondfe4d342011-10-27 12:16:06 -040079#define MAX_RA_BLOCKS 32 /* max read-ahead blocks */
80
Steven Whitehousecd915492006-09-04 12:49:07 -040081#define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
82#define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
David Teiglandb3b94fa2006-01-16 16:50:04 +000083
Steven Whitehouse8d123582010-09-17 12:30:23 +010084struct qstr gfs2_qdot __read_mostly;
85struct qstr gfs2_qdotdot __read_mostly;
86
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -040087typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
88 const struct qstr *name, void *opaque);
David Teiglandb3b94fa2006-01-16 16:50:04 +000089
Steven Whitehousecd915492006-09-04 12:49:07 -040090int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
Steven Whitehouse61e085a2006-04-24 10:07:13 -040091 struct buffer_head **bhp)
Steven Whitehousee13940b2006-01-30 13:31:50 +000092{
93 struct buffer_head *bh;
Steven Whitehousee13940b2006-01-30 13:31:50 +000094
Steven Whitehouse61e085a2006-04-24 10:07:13 -040095 bh = gfs2_meta_new(ip->i_gl, block);
96 gfs2_trans_add_bh(ip->i_gl, bh, 1);
97 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
98 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
Steven Whitehousee13940b2006-01-30 13:31:50 +000099 *bhp = bh;
100 return 0;
101}
102
Steven Whitehousecd915492006-09-04 12:49:07 -0400103static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, u64 block,
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400104 struct buffer_head **bhp)
105{
106 struct buffer_head *bh;
107 int error;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000108
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400109 error = gfs2_meta_read(ip->i_gl, block, DIO_WAIT, &bh);
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400110 if (error)
111 return error;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400112 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) {
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400113 brelse(bh);
114 return -EIO;
115 }
116 *bhp = bh;
117 return 0;
118}
Steven Whitehousee13940b2006-01-30 13:31:50 +0000119
120static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
121 unsigned int offset, unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000122{
123 struct buffer_head *dibh;
124 int error;
125
126 error = gfs2_meta_inode_buffer(ip, &dibh);
127 if (error)
128 return error;
129
130 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehousec7526662006-03-20 12:30:04 -0500131 memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100132 if (ip->i_inode.i_size < offset + size)
133 i_size_write(&ip->i_inode, offset + size);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100134 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500135 gfs2_dinode_out(ip, dibh->b_data);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000136
137 brelse(dibh);
138
139 return size;
140}
141
142
143
144/**
145 * gfs2_dir_write_data - Write directory information to the inode
146 * @ip: The GFS2 inode
147 * @buf: The buffer containing information to be written
148 * @offset: The file offset to start writing at
149 * @size: The amount of data to write
150 *
151 * Returns: The number of bytes correctly written or error code
152 */
153static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
Steven Whitehousecd915492006-09-04 12:49:07 -0400154 u64 offset, unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000155{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400156 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000157 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400158 u64 lblock, dblock;
159 u32 extlen = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000160 unsigned int o;
161 int copied = 0;
162 int error = 0;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000163 int new = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000164
165 if (!size)
166 return 0;
167
168 if (gfs2_is_stuffed(ip) &&
169 offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500170 return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
171 size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000172
173 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
174 return -EINVAL;
175
176 if (gfs2_is_stuffed(ip)) {
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400177 error = gfs2_unstuff_dinode(ip, NULL);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000178 if (error)
Steven Whitehousec7526662006-03-20 12:30:04 -0500179 return error;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000180 }
181
182 lblock = offset;
183 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
184
185 while (copied < size) {
186 unsigned int amount;
187 struct buffer_head *bh;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000188
189 amount = size - copied;
190 if (amount > sdp->sd_sb.sb_bsize - o)
191 amount = sdp->sd_sb.sb_bsize - o;
192
193 if (!extlen) {
194 new = 1;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400195 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
Steven Whitehousefd88de562006-05-05 16:59:11 -0400196 &dblock, &extlen);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000197 if (error)
198 goto fail;
199 error = -EIO;
200 if (gfs2_assert_withdraw(sdp, dblock))
201 goto fail;
202 }
203
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400204 if (amount == sdp->sd_jbsize || new)
205 error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
206 else
207 error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
208
Steven Whitehousee13940b2006-01-30 13:31:50 +0000209 if (error)
210 goto fail;
211
212 gfs2_trans_add_bh(ip->i_gl, bh, 1);
213 memcpy(bh->b_data + o, buf, amount);
214 brelse(bh);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000215
Steven Whitehouse899bb262006-08-01 15:28:57 -0400216 buf += amount;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000217 copied += amount;
218 lblock++;
219 dblock++;
220 extlen--;
221
222 o = sizeof(struct gfs2_meta_header);
223 }
224
225out:
226 error = gfs2_meta_inode_buffer(ip, &dibh);
227 if (error)
228 return error;
229
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100230 if (ip->i_inode.i_size < offset + copied)
231 i_size_write(&ip->i_inode, offset + copied);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100232 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000233
234 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500235 gfs2_dinode_out(ip, dibh->b_data);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000236 brelse(dibh);
237
238 return copied;
239fail:
240 if (copied)
241 goto out;
242 return error;
243}
244
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100245static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, __be64 *buf,
246 unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000247{
248 struct buffer_head *dibh;
249 int error;
250
251 error = gfs2_meta_inode_buffer(ip, &dibh);
252 if (!error) {
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100253 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000254 brelse(dibh);
255 }
256
257 return (error) ? error : size;
258}
259
260
261/**
262 * gfs2_dir_read_data - Read a data from a directory inode
263 * @ip: The GFS2 Inode
264 * @buf: The buffer to place result into
Steven Whitehousee13940b2006-01-30 13:31:50 +0000265 * @size: Amount of data to transfer
266 *
267 * Returns: The amount of data actually copied or the error
268 */
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100269static int gfs2_dir_read_data(struct gfs2_inode *ip, __be64 *buf,
270 unsigned int size)
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 Whitehousee13940b2006-01-30 13:31:50 +0000278
279 if (gfs2_is_stuffed(ip))
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100280 return gfs2_dir_read_stuffed(ip, buf, size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000281
282 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
283 return -EINVAL;
284
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100285 lblock = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000286 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
287
288 while (copied < size) {
289 unsigned int amount;
290 struct buffer_head *bh;
291 int new;
292
293 amount = size - copied;
294 if (amount > sdp->sd_sb.sb_bsize - o)
295 amount = sdp->sd_sb.sb_bsize - o;
296
297 if (!extlen) {
298 new = 0;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400299 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
Steven Whitehousefd88de562006-05-05 16:59:11 -0400300 &dblock, &extlen);
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400301 if (error || !dblock)
302 goto fail;
303 BUG_ON(extlen < 1);
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400304 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
Adrian Bunkb7d8ac32006-10-19 16:02:07 +0200305 } else {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400306 error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, &bh);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000307 if (error)
308 goto fail;
309 }
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400310 error = gfs2_metatype_check(sdp, bh, GFS2_METATYPE_JD);
311 if (error) {
312 brelse(bh);
313 goto fail;
314 }
315 dblock++;
316 extlen--;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000317 memcpy(buf, bh->b_data + o, amount);
318 brelse(bh);
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100319 buf += (amount/sizeof(__be64));
Steven Whitehousee13940b2006-01-30 13:31:50 +0000320 copied += amount;
321 lblock++;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000322 o = sizeof(struct gfs2_meta_header);
323 }
324
325 return copied;
326fail:
327 return (copied) ? copied : error;
328}
329
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100330/**
331 * gfs2_dir_get_hash_table - Get pointer to the dir hash table
332 * @ip: The inode in question
333 *
334 * Returns: The hash table or an error
335 */
336
337static __be64 *gfs2_dir_get_hash_table(struct gfs2_inode *ip)
338{
339 struct inode *inode = &ip->i_inode;
340 int ret;
341 u32 hsize;
342 __be64 *hc;
343
344 BUG_ON(!(ip->i_diskflags & GFS2_DIF_EXHASH));
345
346 hc = ip->i_hash_cache;
347 if (hc)
348 return hc;
349
350 hsize = 1 << ip->i_depth;
351 hsize *= sizeof(__be64);
352 if (hsize != i_size_read(&ip->i_inode)) {
353 gfs2_consist_inode(ip);
354 return ERR_PTR(-EIO);
355 }
356
357 hc = kmalloc(hsize, GFP_NOFS);
358 ret = -ENOMEM;
359 if (hc == NULL)
360 return ERR_PTR(-ENOMEM);
361
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100362 ret = gfs2_dir_read_data(ip, hc, hsize);
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100363 if (ret < 0) {
364 kfree(hc);
365 return ERR_PTR(ret);
366 }
367
368 spin_lock(&inode->i_lock);
369 if (ip->i_hash_cache)
370 kfree(hc);
371 else
372 ip->i_hash_cache = hc;
373 spin_unlock(&inode->i_lock);
374
375 return ip->i_hash_cache;
376}
377
378/**
379 * gfs2_dir_hash_inval - Invalidate dir hash
380 * @ip: The directory inode
381 *
382 * Must be called with an exclusive glock, or during glock invalidation.
383 */
384void gfs2_dir_hash_inval(struct gfs2_inode *ip)
385{
386 __be64 *hc = ip->i_hash_cache;
387 ip->i_hash_cache = NULL;
388 kfree(hc);
389}
390
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500391static inline int gfs2_dirent_sentinel(const struct gfs2_dirent *dent)
392{
393 return dent->de_inum.no_addr == 0 || dent->de_inum.no_formal_ino == 0;
394}
395
Steven Whitehousec7526662006-03-20 12:30:04 -0500396static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
397 const struct qstr *name, int ret)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000398{
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500399 if (!gfs2_dirent_sentinel(dent) &&
Steven Whitehousec7526662006-03-20 12:30:04 -0500400 be32_to_cpu(dent->de_hash) == name->hash &&
401 be16_to_cpu(dent->de_name_len) == name->len &&
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400402 memcmp(dent+1, name->name, name->len) == 0)
Steven Whitehousec7526662006-03-20 12:30:04 -0500403 return ret;
404 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000405}
406
Steven Whitehousec7526662006-03-20 12:30:04 -0500407static int gfs2_dirent_find(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500408 const struct qstr *name,
409 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500410{
411 return __gfs2_dirent_find(dent, name, 1);
412}
413
414static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500415 const struct qstr *name,
416 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500417{
418 return __gfs2_dirent_find(dent, name, 2);
419}
420
421/*
422 * name->name holds ptr to start of block.
423 * name->len holds size of block.
424 */
425static int gfs2_dirent_last(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500426 const struct qstr *name,
427 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500428{
429 const char *start = name->name;
430 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
431 if (name->len == (end - start))
432 return 1;
433 return 0;
434}
435
436static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500437 const struct qstr *name,
438 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500439{
440 unsigned required = GFS2_DIRENT_SIZE(name->len);
441 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
442 unsigned totlen = be16_to_cpu(dent->de_rec_len);
443
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500444 if (gfs2_dirent_sentinel(dent))
Bob Peterson728a7562010-07-14 18:12:26 -0400445 actual = 0;
Jan Engelhardtc5392122006-09-05 14:30:40 +0200446 if (totlen - actual >= required)
Steven Whitehousec7526662006-03-20 12:30:04 -0500447 return 1;
448 return 0;
449}
450
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500451struct dirent_gather {
452 const struct gfs2_dirent **pdent;
453 unsigned offset;
454};
455
456static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
457 const struct qstr *name,
458 void *opaque)
459{
460 struct dirent_gather *g = opaque;
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500461 if (!gfs2_dirent_sentinel(dent)) {
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500462 g->pdent[g->offset++] = dent;
463 }
464 return 0;
465}
466
Steven Whitehousec7526662006-03-20 12:30:04 -0500467/*
468 * Other possible things to check:
469 * - Inode located within filesystem size (and on valid block)
470 * - Valid directory entry type
471 * Not sure how heavy-weight we want to make this... could also check
472 * hash is correct for example, but that would take a lot of extra time.
473 * For now the most important thing is to check that the various sizes
474 * are correct.
475 */
476static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
477 unsigned int size, unsigned int len, int first)
478{
479 const char *msg = "gfs2_dirent too small";
480 if (unlikely(size < sizeof(struct gfs2_dirent)))
481 goto error;
482 msg = "gfs2_dirent misaligned";
483 if (unlikely(offset & 0x7))
484 goto error;
485 msg = "gfs2_dirent points beyond end of block";
486 if (unlikely(offset + size > len))
487 goto error;
488 msg = "zero inode number";
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500489 if (unlikely(!first && gfs2_dirent_sentinel(dent)))
Steven Whitehousec7526662006-03-20 12:30:04 -0500490 goto error;
491 msg = "name length is greater than space in dirent";
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500492 if (!gfs2_dirent_sentinel(dent) &&
Steven Whitehousec7526662006-03-20 12:30:04 -0500493 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
494 size))
495 goto error;
496 return 0;
497error:
498 printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
499 first ? "first in block" : "not first in block");
500 return -EIO;
501}
502
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500503static int gfs2_dirent_offset(const void *buf)
Steven Whitehousec7526662006-03-20 12:30:04 -0500504{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500505 const struct gfs2_meta_header *h = buf;
506 int offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500507
508 BUG_ON(buf == NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500509
Steven Whitehousee3167de2006-03-30 15:46:23 -0500510 switch(be32_to_cpu(h->mh_type)) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500511 case GFS2_METATYPE_LF:
512 offset = sizeof(struct gfs2_leaf);
513 break;
514 case GFS2_METATYPE_DI:
515 offset = sizeof(struct gfs2_dinode);
516 break;
517 default:
518 goto wrong_type;
519 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500520 return offset;
521wrong_type:
522 printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
Steven Whitehousee3167de2006-03-30 15:46:23 -0500523 be32_to_cpu(h->mh_type));
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500524 return -1;
525}
Steven Whitehousec7526662006-03-20 12:30:04 -0500526
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400527static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500528 unsigned int len, gfs2_dscan_t scan,
529 const struct qstr *name,
530 void *opaque)
531{
532 struct gfs2_dirent *dent, *prev;
533 unsigned offset;
534 unsigned size;
535 int ret = 0;
536
537 ret = gfs2_dirent_offset(buf);
538 if (ret < 0)
539 goto consist_inode;
540
541 offset = ret;
Steven Whitehousec7526662006-03-20 12:30:04 -0500542 prev = NULL;
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400543 dent = buf + offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500544 size = be16_to_cpu(dent->de_rec_len);
545 if (gfs2_check_dirent(dent, offset, size, len, 1))
546 goto consist_inode;
547 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500548 ret = scan(dent, name, opaque);
Steven Whitehousec7526662006-03-20 12:30:04 -0500549 if (ret)
550 break;
551 offset += size;
552 if (offset == len)
553 break;
554 prev = dent;
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400555 dent = buf + offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500556 size = be16_to_cpu(dent->de_rec_len);
557 if (gfs2_check_dirent(dent, offset, size, len, 0))
558 goto consist_inode;
559 } while(1);
560
561 switch(ret) {
562 case 0:
563 return NULL;
564 case 1:
565 return dent;
566 case 2:
567 return prev ? prev : dent;
568 default:
569 BUG_ON(ret > 0);
570 return ERR_PTR(ret);
571 }
572
Steven Whitehousec7526662006-03-20 12:30:04 -0500573consist_inode:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400574 gfs2_consist_inode(GFS2_I(inode));
Steven Whitehousec7526662006-03-20 12:30:04 -0500575 return ERR_PTR(-EIO);
576}
577
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400578static int dirent_check_reclen(struct gfs2_inode *dip,
579 const struct gfs2_dirent *d, const void *end_p)
580{
581 const void *ptr = d;
582 u16 rec_len = be16_to_cpu(d->de_rec_len);
583
584 if (unlikely(rec_len < sizeof(struct gfs2_dirent)))
585 goto broken;
586 ptr += rec_len;
587 if (ptr < end_p)
588 return rec_len;
589 if (ptr == end_p)
590 return -ENOENT;
591broken:
592 gfs2_consist_inode(dip);
593 return -EIO;
594}
595
David Teiglandb3b94fa2006-01-16 16:50:04 +0000596/**
597 * dirent_next - Next dirent
598 * @dip: the directory
599 * @bh: The buffer
600 * @dent: Pointer to list of dirents
601 *
602 * Returns: 0 on success, error code otherwise
603 */
604
605static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
606 struct gfs2_dirent **dent)
607{
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400608 struct gfs2_dirent *cur = *dent, *tmp;
609 char *bh_end = bh->b_data + bh->b_size;
610 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000611
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400612 ret = dirent_check_reclen(dip, cur, bh_end);
613 if (ret < 0)
614 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000615
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400616 tmp = (void *)cur + ret;
617 ret = dirent_check_reclen(dip, tmp, bh_end);
618 if (ret == -EIO)
619 return ret;
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000620
David Teiglandb3b94fa2006-01-16 16:50:04 +0000621 /* Only the first dent could ever have de_inum.no_addr == 0 */
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500622 if (gfs2_dirent_sentinel(tmp)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000623 gfs2_consist_inode(dip);
624 return -EIO;
625 }
626
627 *dent = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000628 return 0;
629}
630
631/**
632 * dirent_del - Delete a dirent
633 * @dip: The GFS2 inode
634 * @bh: The buffer
635 * @prev: The previous dirent
636 * @cur: The current dirent
637 *
638 */
639
640static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
641 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
642{
Steven Whitehousecd915492006-09-04 12:49:07 -0400643 u16 cur_rec_len, prev_rec_len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000644
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500645 if (gfs2_dirent_sentinel(cur)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000646 gfs2_consist_inode(dip);
647 return;
648 }
649
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000650 gfs2_trans_add_bh(dip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000651
652 /* If there is no prev entry, this is the first entry in the block.
653 The de_rec_len is already as big as it needs to be. Just zero
654 out the inode number and return. */
655
656 if (!prev) {
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500657 cur->de_inum.no_addr = 0;
658 cur->de_inum.no_formal_ino = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000659 return;
660 }
661
662 /* Combine this dentry with the previous one. */
663
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000664 prev_rec_len = be16_to_cpu(prev->de_rec_len);
665 cur_rec_len = be16_to_cpu(cur->de_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000666
667 if ((char *)prev + prev_rec_len != (char *)cur)
668 gfs2_consist_inode(dip);
669 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
670 gfs2_consist_inode(dip);
671
672 prev_rec_len += cur_rec_len;
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000673 prev->de_rec_len = cpu_to_be16(prev_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000674}
675
Steven Whitehousec7526662006-03-20 12:30:04 -0500676/*
677 * Takes a dent from which to grab space as an argument. Returns the
678 * newly created dent.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000679 */
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400680static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
681 struct gfs2_dirent *dent,
682 const struct qstr *name,
683 struct buffer_head *bh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000684{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400685 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -0500686 struct gfs2_dirent *ndent;
687 unsigned offset = 0, totlen;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000688
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500689 if (!gfs2_dirent_sentinel(dent))
Steven Whitehousec7526662006-03-20 12:30:04 -0500690 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
691 totlen = be16_to_cpu(dent->de_rec_len);
692 BUG_ON(offset + name->len > totlen);
693 gfs2_trans_add_bh(ip->i_gl, bh, 1);
694 ndent = (struct gfs2_dirent *)((char *)dent + offset);
695 dent->de_rec_len = cpu_to_be16(offset);
696 gfs2_qstr2dirent(name, totlen - offset, ndent);
697 return ndent;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000698}
699
Steven Whitehousec7526662006-03-20 12:30:04 -0500700static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
701 struct buffer_head *bh,
702 const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000703{
704 struct gfs2_dirent *dent;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400705 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500706 gfs2_dirent_find_space, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500707 if (!dent || IS_ERR(dent))
708 return dent;
709 return gfs2_init_dirent(inode, dent, name, bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000710}
711
Steven Whitehousecd915492006-09-04 12:49:07 -0400712static int get_leaf(struct gfs2_inode *dip, u64 leaf_no,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000713 struct buffer_head **bhp)
714{
715 int error;
716
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400717 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, bhp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400718 if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
719 /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000720 error = -EIO;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400721 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000722
723 return error;
724}
725
726/**
727 * get_leaf_nr - Get a leaf number associated with the index
728 * @dip: The GFS2 inode
729 * @index:
730 * @leaf_out:
731 *
732 * Returns: 0 on success, error code otherwise
733 */
734
Steven Whitehousecd915492006-09-04 12:49:07 -0400735static int get_leaf_nr(struct gfs2_inode *dip, u32 index,
736 u64 *leaf_out)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000737{
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100738 __be64 *hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000739
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100740 hash = gfs2_dir_get_hash_table(dip);
741 if (IS_ERR(hash))
742 return PTR_ERR(hash);
743 *leaf_out = be64_to_cpu(*(hash + index));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000744 return 0;
745}
746
Steven Whitehousecd915492006-09-04 12:49:07 -0400747static int get_first_leaf(struct gfs2_inode *dip, u32 index,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000748 struct buffer_head **bh_out)
749{
Steven Whitehousecd915492006-09-04 12:49:07 -0400750 u64 leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000751 int error;
752
753 error = get_leaf_nr(dip, index, &leaf_no);
754 if (!error)
755 error = get_leaf(dip, leaf_no, bh_out);
756
757 return error;
758}
759
Steven Whitehousec7526662006-03-20 12:30:04 -0500760static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
761 const struct qstr *name,
762 gfs2_dscan_t scan,
763 struct buffer_head **pbh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000764{
Steven Whitehousec7526662006-03-20 12:30:04 -0500765 struct buffer_head *bh;
766 struct gfs2_dirent *dent;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400767 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000768 int error;
769
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000770 if (ip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500771 struct gfs2_leaf *leaf;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000772 unsigned hsize = 1 << ip->i_depth;
Steven Whitehousec7526662006-03-20 12:30:04 -0500773 unsigned index;
774 u64 ln;
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100775 if (hsize * sizeof(u64) != i_size_read(inode)) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500776 gfs2_consist_inode(ip);
777 return ERR_PTR(-EIO);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000778 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400779
Steven Whitehouse9a004502008-02-01 09:23:44 +0000780 index = name->hash >> (32 - ip->i_depth);
Steven Whitehousec7526662006-03-20 12:30:04 -0500781 error = get_first_leaf(ip, index, &bh);
782 if (error)
783 return ERR_PTR(error);
784 do {
785 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500786 scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500787 if (dent)
788 goto got_dent;
789 leaf = (struct gfs2_leaf *)bh->b_data;
790 ln = be64_to_cpu(leaf->lf_next);
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400791 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500792 if (!ln)
793 break;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400794
Steven Whitehousec7526662006-03-20 12:30:04 -0500795 error = get_leaf(ip, ln, &bh);
796 } while(!error);
797
798 return error ? ERR_PTR(error) : NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000799 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000800
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400801
Steven Whitehousec7526662006-03-20 12:30:04 -0500802 error = gfs2_meta_inode_buffer(ip, &bh);
803 if (error)
804 return ERR_PTR(error);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500805 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500806got_dent:
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400807 if (unlikely(dent == NULL || IS_ERR(dent))) {
Steven Whitehouseed386502006-04-07 16:28:07 -0400808 brelse(bh);
809 bh = NULL;
810 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500811 *pbh = bh;
812 return dent;
813}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000814
Steven Whitehousec7526662006-03-20 12:30:04 -0500815static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
816{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400817 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000818 unsigned int n = 1;
Steven Whitehouse09010972009-05-20 10:48:47 +0100819 u64 bn;
820 int error;
821 struct buffer_head *bh;
Steven Whitehousec7526662006-03-20 12:30:04 -0500822 struct gfs2_leaf *leaf;
823 struct gfs2_dirent *dent;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500824 struct qstr name = { .name = "", .len = 0, .hash = 0 };
Steven Whitehouse09010972009-05-20 10:48:47 +0100825
Bob Peterson6e87ed02011-11-18 10:58:32 -0500826 error = gfs2_alloc_blocks(ip, &bn, &n, 0, NULL);
Steven Whitehouse09010972009-05-20 10:48:47 +0100827 if (error)
828 return NULL;
829 bh = gfs2_meta_new(ip->i_gl, bn);
Steven Whitehousec7526662006-03-20 12:30:04 -0500830 if (!bh)
831 return NULL;
Steven Whitehouse09010972009-05-20 10:48:47 +0100832
Steven Whitehouse5731be52008-02-01 13:16:55 +0000833 gfs2_trans_add_unrevoke(GFS2_SB(inode), bn, 1);
Steven Whitehousec7526662006-03-20 12:30:04 -0500834 gfs2_trans_add_bh(ip->i_gl, bh, 1);
835 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
836 leaf = (struct gfs2_leaf *)bh->b_data;
837 leaf->lf_depth = cpu_to_be16(depth);
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400838 leaf->lf_entries = 0;
Al Viroa2d7d022006-10-14 16:49:30 +0100839 leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400840 leaf->lf_next = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500841 memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
842 dent = (struct gfs2_dirent *)(leaf+1);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500843 gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
Steven Whitehousec7526662006-03-20 12:30:04 -0500844 *pbh = bh;
845 return leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000846}
847
848/**
849 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
850 * @dip: The GFS2 inode
851 *
852 * Returns: 0 on success, error code otherwise
853 */
854
Steven Whitehousec7526662006-03-20 12:30:04 -0500855static int dir_make_exhash(struct inode *inode)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000856{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400857 struct gfs2_inode *dip = GFS2_I(inode);
858 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000859 struct gfs2_dirent *dent;
Steven Whitehousec7526662006-03-20 12:30:04 -0500860 struct qstr args;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000861 struct buffer_head *bh, *dibh;
862 struct gfs2_leaf *leaf;
863 int y;
Steven Whitehousecd915492006-09-04 12:49:07 -0400864 u32 x;
Al Virob44b84d2006-10-14 10:46:30 -0400865 __be64 *lp;
866 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000867 int error;
868
869 error = gfs2_meta_inode_buffer(dip, &dibh);
870 if (error)
871 return error;
872
David Teiglandb3b94fa2006-01-16 16:50:04 +0000873 /* Turn over a new leaf */
874
Steven Whitehousec7526662006-03-20 12:30:04 -0500875 leaf = new_leaf(inode, &bh, 0);
876 if (!leaf)
877 return -ENOSPC;
878 bn = bh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000879
Steven Whitehousead6203f2008-11-03 13:59:19 +0000880 gfs2_assert(sdp, dip->i_entries < (1 << 16));
881 leaf->lf_entries = cpu_to_be16(dip->i_entries);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000882
883 /* Copy dirents */
884
885 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
886 sizeof(struct gfs2_dinode));
887
888 /* Find last entry */
889
890 x = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500891 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
892 sizeof(struct gfs2_leaf);
893 args.name = bh->b_data;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400894 dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500895 gfs2_dirent_last, &args, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500896 if (!dent) {
897 brelse(bh);
898 brelse(dibh);
899 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000900 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500901 if (IS_ERR(dent)) {
902 brelse(bh);
903 brelse(dibh);
904 return PTR_ERR(dent);
905 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000906
907 /* Adjust the last dirent's record length
908 (Remember that dent still points to the last entry.) */
909
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000910 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000911 sizeof(struct gfs2_dinode) -
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000912 sizeof(struct gfs2_leaf));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000913
914 brelse(bh);
915
916 /* We're done with the new leaf block, now setup the new
917 hash table. */
918
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000919 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000920 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
921
Al Virob44b84d2006-10-14 10:46:30 -0400922 lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000923
924 for (x = sdp->sd_hash_ptrs; x--; lp++)
925 *lp = cpu_to_be64(bn);
926
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100927 i_size_write(inode, sdp->sd_sb.sb_bsize / 2);
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000928 gfs2_add_inode_blocks(&dip->i_inode, 1);
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000929 dip->i_diskflags |= GFS2_DIF_EXHASH;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000930
931 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000932 dip->i_depth = y;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000933
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500934 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000935
936 brelse(dibh);
937
938 return 0;
939}
940
941/**
942 * dir_split_leaf - Split a leaf block into two
943 * @dip: The GFS2 inode
944 * @index:
945 * @leaf_no:
946 *
947 * Returns: 0 on success, error code on failure
948 */
949
Steven Whitehousec7526662006-03-20 12:30:04 -0500950static int dir_split_leaf(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000951{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400952 struct gfs2_inode *dip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000953 struct buffer_head *nbh, *obh, *dibh;
954 struct gfs2_leaf *nleaf, *oleaf;
Steven Whitehouse4da3c642006-07-11 13:19:13 -0400955 struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
Steven Whitehousecd915492006-09-04 12:49:07 -0400956 u32 start, len, half_len, divider;
Al Virob44b84d2006-10-14 10:46:30 -0400957 u64 bn, leaf_no;
958 __be64 *lp;
Steven Whitehousecd915492006-09-04 12:49:07 -0400959 u32 index;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000960 int x, moved = 0;
961 int error;
962
Steven Whitehouse9a004502008-02-01 09:23:44 +0000963 index = name->hash >> (32 - dip->i_depth);
Steven Whitehousec7526662006-03-20 12:30:04 -0500964 error = get_leaf_nr(dip, index, &leaf_no);
965 if (error)
966 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000967
968 /* Get the old leaf block */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000969 error = get_leaf(dip, leaf_no, &obh);
970 if (error)
Steven Whitehousee90deff2006-03-29 19:02:15 -0500971 return error;
972
973 oleaf = (struct gfs2_leaf *)obh->b_data;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000974 if (dip->i_depth == be16_to_cpu(oleaf->lf_depth)) {
Steven Whitehousee90deff2006-03-29 19:02:15 -0500975 brelse(obh);
976 return 1; /* can't split */
977 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000978
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000979 gfs2_trans_add_bh(dip->i_gl, obh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000980
Steven Whitehousec7526662006-03-20 12:30:04 -0500981 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
982 if (!nleaf) {
983 brelse(obh);
984 return -ENOSPC;
985 }
986 bn = nbh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000987
Steven Whitehousec7526662006-03-20 12:30:04 -0500988 /* Compute the start and len of leaf pointers in the hash table. */
Steven Whitehouse9a004502008-02-01 09:23:44 +0000989 len = 1 << (dip->i_depth - be16_to_cpu(oleaf->lf_depth));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000990 half_len = len >> 1;
991 if (!half_len) {
Steven Whitehouse9a004502008-02-01 09:23:44 +0000992 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 +0000993 gfs2_consist_inode(dip);
994 error = -EIO;
995 goto fail_brelse;
996 }
997
998 start = (index & ~(len - 1));
999
1000 /* Change the pointers.
1001 Don't bother distinguishing stuffed from non-stuffed.
1002 This code is complicated enough already. */
David Rientjes4244b522010-07-20 19:45:03 -07001003 lp = kmalloc(half_len * sizeof(__be64), GFP_NOFS);
1004 if (!lp) {
1005 error = -ENOMEM;
1006 goto fail_brelse;
1007 }
1008
David Teiglandb3b94fa2006-01-16 16:50:04 +00001009 /* Change the pointers */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001010 for (x = 0; x < half_len; x++)
1011 lp[x] = cpu_to_be64(bn);
1012
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001013 gfs2_dir_hash_inval(dip);
1014
Steven Whitehousecd915492006-09-04 12:49:07 -04001015 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
1016 half_len * sizeof(u64));
1017 if (error != half_len * sizeof(u64)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001018 if (error >= 0)
1019 error = -EIO;
1020 goto fail_lpfree;
1021 }
1022
1023 kfree(lp);
1024
1025 /* Compute the divider */
Steven Whitehouse9a004502008-02-01 09:23:44 +00001026 divider = (start + half_len) << (32 - dip->i_depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001027
1028 /* Copy the entries */
Steven Whitehouse15793432009-11-06 11:06:37 +00001029 dent = (struct gfs2_dirent *)(obh->b_data + sizeof(struct gfs2_leaf));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001030
1031 do {
1032 next = dent;
1033 if (dirent_next(dip, obh, &next))
1034 next = NULL;
1035
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -05001036 if (!gfs2_dirent_sentinel(dent) &&
David Teiglandb3b94fa2006-01-16 16:50:04 +00001037 be32_to_cpu(dent->de_hash) < divider) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001038 struct qstr str;
1039 str.name = (char*)(dent+1);
1040 str.len = be16_to_cpu(dent->de_name_len);
1041 str.hash = be32_to_cpu(dent->de_hash);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001042 new = gfs2_dirent_alloc(inode, nbh, &str);
Steven Whitehousec7526662006-03-20 12:30:04 -05001043 if (IS_ERR(new)) {
1044 error = PTR_ERR(new);
1045 break;
1046 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001047
1048 new->de_inum = dent->de_inum; /* No endian worries */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001049 new->de_type = dent->de_type; /* No endian worries */
Marcin Slusarzbb16b342008-02-13 00:06:10 +01001050 be16_add_cpu(&nleaf->lf_entries, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001051
1052 dirent_del(dip, obh, prev, dent);
1053
1054 if (!oleaf->lf_entries)
1055 gfs2_consist_inode(dip);
Marcin Slusarzbb16b342008-02-13 00:06:10 +01001056 be16_add_cpu(&oleaf->lf_entries, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001057
1058 if (!prev)
1059 prev = dent;
1060
1061 moved = 1;
Steven Whitehousec7526662006-03-20 12:30:04 -05001062 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001063 prev = dent;
Steven Whitehousec7526662006-03-20 12:30:04 -05001064 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001065 dent = next;
Steven Whitehousec7526662006-03-20 12:30:04 -05001066 } while (dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001067
Steven Whitehousec7526662006-03-20 12:30:04 -05001068 oleaf->lf_depth = nleaf->lf_depth;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001069
1070 error = gfs2_meta_inode_buffer(dip, &dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001071 if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
Steven Whitehouse382e6e22007-08-16 17:08:20 +01001072 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001073 gfs2_add_inode_blocks(&dip->i_inode, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001074 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001075 brelse(dibh);
1076 }
1077
1078 brelse(obh);
1079 brelse(nbh);
1080
1081 return error;
1082
Steven Whitehousee90deff2006-03-29 19:02:15 -05001083fail_lpfree:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001084 kfree(lp);
1085
Steven Whitehousee90deff2006-03-29 19:02:15 -05001086fail_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001087 brelse(obh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001088 brelse(nbh);
1089 return error;
1090}
1091
1092/**
1093 * dir_double_exhash - Double size of ExHash table
1094 * @dip: The GFS2 dinode
1095 *
1096 * Returns: 0 on success, error code on failure
1097 */
1098
1099static int dir_double_exhash(struct gfs2_inode *dip)
1100{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001101 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001102 u32 hsize;
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001103 u32 hsize_bytes;
1104 __be64 *hc;
1105 __be64 *hc2, *h;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001106 int x;
1107 int error = 0;
1108
Steven Whitehouse9a004502008-02-01 09:23:44 +00001109 hsize = 1 << dip->i_depth;
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001110 hsize_bytes = hsize * sizeof(__be64);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001111
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001112 hc = gfs2_dir_get_hash_table(dip);
1113 if (IS_ERR(hc))
1114 return PTR_ERR(hc);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001115
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001116 h = hc2 = kmalloc(hsize_bytes * 2, GFP_NOFS);
1117 if (!hc2)
David Rientjes4244b522010-07-20 19:45:03 -07001118 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001119
David Teiglandb3b94fa2006-01-16 16:50:04 +00001120 error = gfs2_meta_inode_buffer(dip, &dibh);
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001121 if (error)
1122 goto out_kfree;
1123
1124 for (x = 0; x < hsize; x++) {
1125 *h++ = *hc;
1126 *h++ = *hc;
1127 hc++;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001128 }
1129
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001130 error = gfs2_dir_write_data(dip, (char *)hc2, 0, hsize_bytes * 2);
1131 if (error != (hsize_bytes * 2))
1132 goto fail;
1133
1134 gfs2_dir_hash_inval(dip);
1135 dip->i_hash_cache = hc2;
1136 dip->i_depth++;
1137 gfs2_dinode_out(dip, dibh->b_data);
1138 brelse(dibh);
1139 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001140
Steven Whitehousea91ea692006-09-04 12:04:26 -04001141fail:
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001142 /* Replace original hash table & size */
1143 gfs2_dir_write_data(dip, (char *)hc, 0, hsize_bytes);
1144 i_size_write(&dip->i_inode, hsize_bytes);
1145 gfs2_dinode_out(dip, dibh->b_data);
1146 brelse(dibh);
1147out_kfree:
1148 kfree(hc2);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001149 return error;
1150}
1151
1152/**
1153 * compare_dents - compare directory entries by hash value
1154 * @a: first dent
1155 * @b: second dent
1156 *
1157 * When comparing the hash entries of @a to @b:
1158 * gt: returns 1
1159 * lt: returns -1
1160 * eq: returns 0
1161 */
1162
1163static int compare_dents(const void *a, const void *b)
1164{
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001165 const struct gfs2_dirent *dent_a, *dent_b;
Steven Whitehousecd915492006-09-04 12:49:07 -04001166 u32 hash_a, hash_b;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001167 int ret = 0;
1168
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001169 dent_a = *(const struct gfs2_dirent **)a;
Steven Whitehousec7526662006-03-20 12:30:04 -05001170 hash_a = be32_to_cpu(dent_a->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001171
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001172 dent_b = *(const struct gfs2_dirent **)b;
Steven Whitehousec7526662006-03-20 12:30:04 -05001173 hash_b = be32_to_cpu(dent_b->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001174
1175 if (hash_a > hash_b)
1176 ret = 1;
1177 else if (hash_a < hash_b)
1178 ret = -1;
1179 else {
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001180 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1181 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001182
1183 if (len_a > len_b)
1184 ret = 1;
1185 else if (len_a < len_b)
1186 ret = -1;
1187 else
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001188 ret = memcmp(dent_a + 1, dent_b + 1, len_a);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001189 }
1190
1191 return ret;
1192}
1193
1194/**
1195 * do_filldir_main - read out directory entries
1196 * @dip: The GFS2 inode
1197 * @offset: The offset in the file to read from
1198 * @opaque: opaque data to pass to filldir
1199 * @filldir: The function to pass entries to
1200 * @darr: an array of struct gfs2_dirent pointers to read
1201 * @entries: the number of entries in darr
1202 * @copied: pointer to int that's non-zero if a entry has been copied out
1203 *
1204 * Jump through some hoops to make sure that if there are hash collsions,
1205 * they are read out at the beginning of a buffer. We want to minimize
1206 * the possibility that they will fall into different readdir buffers or
1207 * that someone will want to seek to that location.
1208 *
1209 * Returns: errno, >0 on exception from filldir
1210 */
1211
Steven Whitehousecd915492006-09-04 12:49:07 -04001212static int do_filldir_main(struct gfs2_inode *dip, u64 *offset,
Steven Whitehouse3699e3a2007-01-17 15:09:20 +00001213 void *opaque, filldir_t filldir,
Steven Whitehousecd915492006-09-04 12:49:07 -04001214 const struct gfs2_dirent **darr, u32 entries,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001215 int *copied)
1216{
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001217 const struct gfs2_dirent *dent, *dent_next;
Steven Whitehousecd915492006-09-04 12:49:07 -04001218 u64 off, off_next;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001219 unsigned int x, y;
1220 int run = 0;
1221 int error = 0;
1222
1223 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1224
1225 dent_next = darr[0];
1226 off_next = be32_to_cpu(dent_next->de_hash);
1227 off_next = gfs2_disk_hash2offset(off_next);
1228
1229 for (x = 0, y = 1; x < entries; x++, y++) {
1230 dent = dent_next;
1231 off = off_next;
1232
1233 if (y < entries) {
1234 dent_next = darr[y];
1235 off_next = be32_to_cpu(dent_next->de_hash);
1236 off_next = gfs2_disk_hash2offset(off_next);
1237
1238 if (off < *offset)
1239 continue;
1240 *offset = off;
1241
1242 if (off_next == off) {
1243 if (*copied && !run)
1244 return 1;
1245 run = 1;
1246 } else
1247 run = 0;
1248 } else {
1249 if (off < *offset)
1250 continue;
1251 *offset = off;
1252 }
1253
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001254 error = filldir(opaque, (const char *)(dent + 1),
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001255 be16_to_cpu(dent->de_name_len),
Steven Whitehouse3699e3a2007-01-17 15:09:20 +00001256 off, be64_to_cpu(dent->de_inum.no_addr),
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001257 be16_to_cpu(dent->de_type));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001258 if (error)
1259 return 1;
1260
1261 *copied = 1;
1262 }
1263
1264 /* Increment the *offset by one, so the next time we come into the
1265 do_filldir fxn, we get the next entry instead of the last one in the
1266 current leaf */
1267
1268 (*offset)++;
1269
1270 return 0;
1271}
1272
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001273static void *gfs2_alloc_sort_buffer(unsigned size)
1274{
1275 void *ptr = NULL;
1276
1277 if (size < KMALLOC_MAX_SIZE)
1278 ptr = kmalloc(size, GFP_NOFS | __GFP_NOWARN);
1279 if (!ptr)
1280 ptr = __vmalloc(size, GFP_NOFS, PAGE_KERNEL);
1281 return ptr;
1282}
1283
1284static void gfs2_free_sort_buffer(void *ptr)
1285{
1286 if (is_vmalloc_addr(ptr))
1287 vfree(ptr);
1288 else
1289 kfree(ptr);
1290}
1291
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001292static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
Steven Whitehouse3699e3a2007-01-17 15:09:20 +00001293 filldir_t filldir, int *copied, unsigned *depth,
1294 u64 leaf_no)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001295{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001296 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001297 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001298 struct buffer_head *bh;
1299 struct gfs2_leaf *lf;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001300 unsigned entries = 0, entries2 = 0;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001301 unsigned leaves = 0;
1302 const struct gfs2_dirent **darr, *dent;
1303 struct dirent_gather g;
1304 struct buffer_head **larr;
1305 int leaf = 0;
1306 int error, i;
1307 u64 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001308
David Teiglandb3b94fa2006-01-16 16:50:04 +00001309 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001310 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001311 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001312 goto out;
1313 lf = (struct gfs2_leaf *)bh->b_data;
1314 if (leaves == 0)
1315 *depth = be16_to_cpu(lf->lf_depth);
1316 entries += be16_to_cpu(lf->lf_entries);
1317 leaves++;
1318 lfn = be64_to_cpu(lf->lf_next);
1319 brelse(bh);
1320 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001321
1322 if (!entries)
1323 return 0;
1324
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001325 error = -ENOMEM;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001326 /*
1327 * The extra 99 entries are not normally used, but are a buffer
1328 * zone in case the number of entries in the leaf is corrupt.
1329 * 99 is the maximum number of entries that can fit in a single
1330 * leaf block.
1331 */
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001332 larr = gfs2_alloc_sort_buffer((leaves + entries + 99) * sizeof(void *));
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001333 if (!larr)
1334 goto out;
1335 darr = (const struct gfs2_dirent **)(larr + leaves);
1336 g.pdent = darr;
1337 g.offset = 0;
1338 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001339
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001340 do {
1341 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001342 if (error)
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001343 goto out_free;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001344 lf = (struct gfs2_leaf *)bh->b_data;
1345 lfn = be64_to_cpu(lf->lf_next);
1346 if (lf->lf_entries) {
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001347 entries2 += be16_to_cpu(lf->lf_entries);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001348 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1349 gfs2_dirent_gather, NULL, &g);
1350 error = PTR_ERR(dent);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001351 if (IS_ERR(dent))
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001352 goto out_free;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001353 if (entries2 != g.offset) {
akpm@linux-foundation.orgf391a4e2007-04-25 21:08:02 -07001354 fs_warn(sdp, "Number of entries corrupt in dir "
1355 "leaf %llu, entries2 (%u) != "
1356 "g.offset (%u)\n",
1357 (unsigned long long)bh->b_blocknr,
1358 entries2, g.offset);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001359
1360 error = -EIO;
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001361 goto out_free;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001362 }
1363 error = 0;
1364 larr[leaf++] = bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001365 } else {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001366 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001367 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001368 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001369
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001370 BUG_ON(entries2 != entries);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001371 error = do_filldir_main(ip, offset, opaque, filldir, darr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001372 entries, copied);
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001373out_free:
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001374 for(i = 0; i < leaf; i++)
1375 brelse(larr[i]);
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001376 gfs2_free_sort_buffer(larr);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001377out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001378 return error;
1379}
1380
Steven Whitehouse79c4c372011-11-09 13:46:06 +00001381/**
1382 * gfs2_dir_readahead - Issue read-ahead requests for leaf blocks.
Bob Petersondfe4d342011-10-27 12:16:06 -04001383 *
1384 * Note: we can't calculate each index like dir_e_read can because we don't
1385 * have the leaf, and therefore we don't have the depth, and therefore we
1386 * don't have the length. So we have to just read enough ahead to make up
Steven Whitehouse79c4c372011-11-09 13:46:06 +00001387 * for the loss of information.
1388 */
Bob Petersondfe4d342011-10-27 12:16:06 -04001389static void gfs2_dir_readahead(struct inode *inode, unsigned hsize, u32 index,
1390 struct file_ra_state *f_ra)
1391{
1392 struct gfs2_inode *ip = GFS2_I(inode);
1393 struct gfs2_glock *gl = ip->i_gl;
1394 struct buffer_head *bh;
1395 u64 blocknr = 0, last;
1396 unsigned count;
1397
1398 /* First check if we've already read-ahead for the whole range. */
Steven Whitehouse79c4c372011-11-09 13:46:06 +00001399 if (index + MAX_RA_BLOCKS < f_ra->start)
Bob Petersondfe4d342011-10-27 12:16:06 -04001400 return;
1401
1402 f_ra->start = max((pgoff_t)index, f_ra->start);
1403 for (count = 0; count < MAX_RA_BLOCKS; count++) {
1404 if (f_ra->start >= hsize) /* if exceeded the hash table */
1405 break;
1406
1407 last = blocknr;
1408 blocknr = be64_to_cpu(ip->i_hash_cache[f_ra->start]);
1409 f_ra->start++;
1410 if (blocknr == last)
1411 continue;
1412
1413 bh = gfs2_getbuf(gl, blocknr, 1);
1414 if (trylock_buffer(bh)) {
1415 if (buffer_uptodate(bh)) {
1416 unlock_buffer(bh);
1417 brelse(bh);
1418 continue;
1419 }
1420 bh->b_end_io = end_buffer_read_sync;
1421 submit_bh(READA | REQ_META, bh);
1422 continue;
1423 }
1424 brelse(bh);
1425 }
1426}
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001427
David Teiglandb3b94fa2006-01-16 16:50:04 +00001428/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001429 * dir_e_read - Reads the entries from a directory into a filldir buffer
1430 * @dip: dinode pointer
1431 * @offset: the hash of the last entry read shifted to the right once
1432 * @opaque: buffer for the filldir function to fill
1433 * @filldir: points to the filldir function to use
1434 *
1435 * Returns: errno
1436 */
1437
Steven Whitehousecd915492006-09-04 12:49:07 -04001438static int dir_e_read(struct inode *inode, u64 *offset, void *opaque,
Bob Petersondfe4d342011-10-27 12:16:06 -04001439 filldir_t filldir, struct file_ra_state *f_ra)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001440{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001441 struct gfs2_inode *dip = GFS2_I(inode);
Steven Whitehousecd915492006-09-04 12:49:07 -04001442 u32 hsize, len = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -04001443 u32 hash, index;
Al Virob44b84d2006-10-14 10:46:30 -04001444 __be64 *lp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001445 int copied = 0;
1446 int error = 0;
Steven Whitehouse4da3c642006-07-11 13:19:13 -04001447 unsigned depth = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001448
Steven Whitehouse9a004502008-02-01 09:23:44 +00001449 hsize = 1 << dip->i_depth;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001450 hash = gfs2_dir_offset2hash(*offset);
Steven Whitehouse9a004502008-02-01 09:23:44 +00001451 index = hash >> (32 - dip->i_depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001452
Steven Whitehouse79c4c372011-11-09 13:46:06 +00001453 if (dip->i_hash_cache == NULL)
Bob Petersondfe4d342011-10-27 12:16:06 -04001454 f_ra->start = 0;
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001455 lp = gfs2_dir_get_hash_table(dip);
1456 if (IS_ERR(lp))
1457 return PTR_ERR(lp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001458
Bob Petersondfe4d342011-10-27 12:16:06 -04001459 gfs2_dir_readahead(inode, hsize, index, f_ra);
1460
David Teiglandb3b94fa2006-01-16 16:50:04 +00001461 while (index < hsize) {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001462 error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
1463 &copied, &depth,
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001464 be64_to_cpu(lp[index]));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001465 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001466 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001467
Steven Whitehouse9a004502008-02-01 09:23:44 +00001468 len = 1 << (dip->i_depth - depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001469 index = (index & ~(len - 1)) + len;
1470 }
1471
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001472 if (error > 0)
1473 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001474 return error;
1475}
1476
Steven Whitehousecd915492006-09-04 12:49:07 -04001477int gfs2_dir_read(struct inode *inode, u64 *offset, void *opaque,
Bob Petersondfe4d342011-10-27 12:16:06 -04001478 filldir_t filldir, struct file_ra_state *f_ra)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001479{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001480 struct gfs2_inode *dip = GFS2_I(inode);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001481 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001482 struct dirent_gather g;
1483 const struct gfs2_dirent **darr, *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001484 struct buffer_head *dibh;
1485 int copied = 0;
1486 int error;
1487
Steven Whitehousead6203f2008-11-03 13:59:19 +00001488 if (!dip->i_entries)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001489 return 0;
1490
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001491 if (dip->i_diskflags & GFS2_DIF_EXHASH)
Bob Petersondfe4d342011-10-27 12:16:06 -04001492 return dir_e_read(inode, offset, opaque, filldir, f_ra);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001493
David Teiglandb3b94fa2006-01-16 16:50:04 +00001494 if (!gfs2_is_stuffed(dip)) {
1495 gfs2_consist_inode(dip);
1496 return -EIO;
1497 }
1498
David Teiglandb3b94fa2006-01-16 16:50:04 +00001499 error = gfs2_meta_inode_buffer(dip, &dibh);
1500 if (error)
1501 return error;
1502
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001503 error = -ENOMEM;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001504 /* 96 is max number of dirents which can be stuffed into an inode */
Josef Bacik16c5f062008-04-09 09:33:41 -04001505 darr = kmalloc(96 * sizeof(struct gfs2_dirent *), GFP_NOFS);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001506 if (darr) {
1507 g.pdent = darr;
1508 g.offset = 0;
1509 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1510 gfs2_dirent_gather, NULL, &g);
1511 if (IS_ERR(dent)) {
1512 error = PTR_ERR(dent);
1513 goto out;
1514 }
Steven Whitehousead6203f2008-11-03 13:59:19 +00001515 if (dip->i_entries != g.offset) {
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001516 fs_warn(sdp, "Number of entries corrupt in dir %llu, "
Steven Whitehousead6203f2008-11-03 13:59:19 +00001517 "ip->i_entries (%u) != g.offset (%u)\n",
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001518 (unsigned long long)dip->i_no_addr,
Steven Whitehousead6203f2008-11-03 13:59:19 +00001519 dip->i_entries,
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001520 g.offset);
1521 error = -EIO;
1522 goto out;
1523 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001524 error = do_filldir_main(dip, offset, opaque, filldir, darr,
Steven Whitehousead6203f2008-11-03 13:59:19 +00001525 dip->i_entries, &copied);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001526out:
1527 kfree(darr);
1528 }
1529
David Teiglandb3b94fa2006-01-16 16:50:04 +00001530 if (error > 0)
1531 error = 0;
1532
1533 brelse(dibh);
1534
1535 return error;
1536}
1537
David Teiglandb3b94fa2006-01-16 16:50:04 +00001538/**
1539 * gfs2_dir_search - Search a directory
1540 * @dip: The GFS2 inode
1541 * @filename:
1542 * @inode:
1543 *
1544 * This routine searches a directory for a file or another directory.
1545 * Assumes a glock is held on dip.
1546 *
1547 * Returns: errno
1548 */
1549
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001550struct inode *gfs2_dir_search(struct inode *dir, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001551{
Steven Whitehousec7526662006-03-20 12:30:04 -05001552 struct buffer_head *bh;
1553 struct gfs2_dirent *dent;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001554 struct inode *inode;
1555
1556 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1557 if (dent) {
1558 if (IS_ERR(dent))
David Howellse231c2e2008-02-07 00:15:26 -08001559 return ERR_CAST(dent);
Wendy Chengbb9bcf02007-06-27 17:07:08 -04001560 inode = gfs2_inode_lookup(dir->i_sb,
1561 be16_to_cpu(dent->de_type),
1562 be64_to_cpu(dent->de_inum.no_addr),
Bob Peterson44ad37d2011-03-17 16:19:58 -04001563 be64_to_cpu(dent->de_inum.no_formal_ino), 0);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001564 brelse(bh);
1565 return inode;
1566 }
1567 return ERR_PTR(-ENOENT);
1568}
1569
1570int gfs2_dir_check(struct inode *dir, const struct qstr *name,
1571 const struct gfs2_inode *ip)
1572{
1573 struct buffer_head *bh;
1574 struct gfs2_dirent *dent;
1575 int ret = -ENOENT;
Steven Whitehousec7526662006-03-20 12:30:04 -05001576
1577 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1578 if (dent) {
1579 if (IS_ERR(dent))
1580 return PTR_ERR(dent);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001581 if (ip) {
1582 if (be64_to_cpu(dent->de_inum.no_addr) != ip->i_no_addr)
1583 goto out;
1584 if (be64_to_cpu(dent->de_inum.no_formal_ino) !=
1585 ip->i_no_formal_ino)
1586 goto out;
1587 if (unlikely(IF2DT(ip->i_inode.i_mode) !=
1588 be16_to_cpu(dent->de_type))) {
1589 gfs2_consist_inode(GFS2_I(dir));
1590 ret = -EIO;
1591 goto out;
1592 }
1593 }
1594 ret = 0;
1595out:
Steven Whitehousec7526662006-03-20 12:30:04 -05001596 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001597 }
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001598 return ret;
Steven Whitehousec7526662006-03-20 12:30:04 -05001599}
1600
1601static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1602{
1603 struct buffer_head *bh, *obh;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001604 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001605 struct gfs2_leaf *leaf, *oleaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001606 int error;
Steven Whitehousec7526662006-03-20 12:30:04 -05001607 u32 index;
1608 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001609
Steven Whitehouse9a004502008-02-01 09:23:44 +00001610 index = name->hash >> (32 - ip->i_depth);
Steven Whitehousec7526662006-03-20 12:30:04 -05001611 error = get_first_leaf(ip, index, &obh);
1612 if (error)
1613 return error;
1614 do {
1615 oleaf = (struct gfs2_leaf *)obh->b_data;
1616 bn = be64_to_cpu(oleaf->lf_next);
1617 if (!bn)
1618 break;
1619 brelse(obh);
1620 error = get_leaf(ip, bn, &obh);
1621 if (error)
1622 return error;
1623 } while(1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001624
Steven Whitehousec7526662006-03-20 12:30:04 -05001625 gfs2_trans_add_bh(ip->i_gl, obh, 1);
1626
1627 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1628 if (!leaf) {
1629 brelse(obh);
1630 return -ENOSPC;
1631 }
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04001632 oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
Steven Whitehousec7526662006-03-20 12:30:04 -05001633 brelse(bh);
1634 brelse(obh);
1635
1636 error = gfs2_meta_inode_buffer(ip, &bh);
1637 if (error)
1638 return error;
1639 gfs2_trans_add_bh(ip->i_gl, bh, 1);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001640 gfs2_add_inode_blocks(&ip->i_inode, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001641 gfs2_dinode_out(ip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001642 brelse(bh);
1643 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001644}
1645
1646/**
1647 * gfs2_dir_add - Add new filename into directory
1648 * @dip: The GFS2 inode
1649 * @filename: The new name
1650 * @inode: The inode number of the entry
1651 * @type: The type of the entry
1652 *
1653 * Returns: 0 on success, error code on failure
1654 */
1655
Steven Whitehousec7526662006-03-20 12:30:04 -05001656int gfs2_dir_add(struct inode *inode, const struct qstr *name,
Steven Whitehouse3d6ecb72011-05-09 13:30:08 +01001657 const struct gfs2_inode *nip)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001658{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001659 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001660 struct buffer_head *bh;
1661 struct gfs2_dirent *dent;
1662 struct gfs2_leaf *leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001663 int error;
1664
Steven Whitehousec7526662006-03-20 12:30:04 -05001665 while(1) {
1666 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
1667 &bh);
1668 if (dent) {
1669 if (IS_ERR(dent))
1670 return PTR_ERR(dent);
1671 dent = gfs2_init_dirent(inode, dent, name, bh);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001672 gfs2_inum_out(nip, dent);
Steven Whitehouse3d6ecb72011-05-09 13:30:08 +01001673 dent->de_type = cpu_to_be16(IF2DT(nip->i_inode.i_mode));
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001674 if (ip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001675 leaf = (struct gfs2_leaf *)bh->b_data;
Marcin Slusarzbb16b342008-02-13 00:06:10 +01001676 be16_add_cpu(&leaf->lf_entries, 1);
Steven Whitehousec7526662006-03-20 12:30:04 -05001677 }
1678 brelse(bh);
1679 error = gfs2_meta_inode_buffer(ip, &bh);
1680 if (error)
1681 break;
1682 gfs2_trans_add_bh(ip->i_gl, bh, 1);
Steven Whitehousead6203f2008-11-03 13:59:19 +00001683 ip->i_entries++;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001684 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehouse3d6ecb72011-05-09 13:30:08 +01001685 if (S_ISDIR(nip->i_inode.i_mode))
1686 inc_nlink(&ip->i_inode);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001687 gfs2_dinode_out(ip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001688 brelse(bh);
1689 error = 0;
1690 break;
1691 }
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001692 if (!(ip->i_diskflags & GFS2_DIF_EXHASH)) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001693 error = dir_make_exhash(inode);
1694 if (error)
1695 break;
1696 continue;
1697 }
1698 error = dir_split_leaf(inode, name);
1699 if (error == 0)
1700 continue;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001701 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001702 break;
Steven Whitehouse9a004502008-02-01 09:23:44 +00001703 if (ip->i_depth < GFS2_DIR_MAX_DEPTH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001704 error = dir_double_exhash(ip);
1705 if (error)
1706 break;
1707 error = dir_split_leaf(inode, name);
Steven Whitehousee90deff2006-03-29 19:02:15 -05001708 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001709 break;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001710 if (error == 0)
1711 continue;
Steven Whitehousec7526662006-03-20 12:30:04 -05001712 }
1713 error = dir_new_leaf(inode, name);
1714 if (!error)
1715 continue;
1716 error = -ENOSPC;
1717 break;
1718 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001719 return error;
1720}
1721
Steven Whitehousec7526662006-03-20 12:30:04 -05001722
David Teiglandb3b94fa2006-01-16 16:50:04 +00001723/**
1724 * gfs2_dir_del - Delete a directory entry
1725 * @dip: The GFS2 inode
1726 * @filename: The filename
1727 *
1728 * Returns: 0 on success, error code on failure
1729 */
1730
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001731int gfs2_dir_del(struct gfs2_inode *dip, const struct dentry *dentry)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001732{
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001733 const struct qstr *name = &dentry->d_name;
Steven Whitehousec7526662006-03-20 12:30:04 -05001734 struct gfs2_dirent *dent, *prev = NULL;
1735 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001736
Steven Whitehousec7526662006-03-20 12:30:04 -05001737 /* Returns _either_ the entry (if its first in block) or the
1738 previous entry otherwise */
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001739 dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001740 if (!dent) {
1741 gfs2_consist_inode(dip);
1742 return -EIO;
1743 }
1744 if (IS_ERR(dent)) {
1745 gfs2_consist_inode(dip);
1746 return PTR_ERR(dent);
1747 }
1748 /* If not first in block, adjust pointers accordingly */
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001749 if (gfs2_dirent_find(dent, name, NULL) == 0) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001750 prev = dent;
1751 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1752 }
1753
1754 dirent_del(dip, bh, prev, dent);
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001755 if (dip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001756 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1757 u16 entries = be16_to_cpu(leaf->lf_entries);
1758 if (!entries)
1759 gfs2_consist_inode(dip);
1760 leaf->lf_entries = cpu_to_be16(--entries);
Steven Whitehousec7526662006-03-20 12:30:04 -05001761 }
Steven Whitehouseed386502006-04-07 16:28:07 -04001762 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001763
Steven Whitehousead6203f2008-11-03 13:59:19 +00001764 if (!dip->i_entries)
Steven Whitehousec7526662006-03-20 12:30:04 -05001765 gfs2_consist_inode(dip);
Steven Whitehousead6203f2008-11-03 13:59:19 +00001766 dip->i_entries--;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001767 dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001768 if (S_ISDIR(dentry->d_inode->i_mode))
1769 drop_nlink(&dip->i_inode);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001770 mark_inode_dirty(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001771
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001772 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001773}
1774
David Teiglandb3b94fa2006-01-16 16:50:04 +00001775/**
1776 * gfs2_dir_mvino - Change inode number of directory entry
1777 * @dip: The GFS2 inode
1778 * @filename:
1779 * @new_inode:
1780 *
1781 * This routine changes the inode number of a directory entry. It's used
1782 * by rename to change ".." when a directory is moved.
1783 * Assumes a glock is held on dvp.
1784 *
1785 * Returns: errno
1786 */
1787
Steven Whitehousec7526662006-03-20 12:30:04 -05001788int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001789 const struct gfs2_inode *nip, unsigned int new_type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001790{
Steven Whitehousec7526662006-03-20 12:30:04 -05001791 struct buffer_head *bh;
1792 struct gfs2_dirent *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001793 int error;
1794
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001795 dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001796 if (!dent) {
1797 gfs2_consist_inode(dip);
1798 return -EIO;
1799 }
1800 if (IS_ERR(dent))
1801 return PTR_ERR(dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001802
Steven Whitehousec7526662006-03-20 12:30:04 -05001803 gfs2_trans_add_bh(dip->i_gl, bh, 1);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001804 gfs2_inum_out(nip, dent);
Steven Whitehousec7526662006-03-20 12:30:04 -05001805 dent->de_type = cpu_to_be16(new_type);
1806
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001807 if (dip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001808 brelse(bh);
1809 error = gfs2_meta_inode_buffer(dip, &bh);
1810 if (error)
1811 return error;
1812 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1813 }
1814
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001815 dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001816 gfs2_dinode_out(dip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001817 brelse(bh);
1818 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001819}
1820
1821/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001822 * leaf_dealloc - Deallocate a directory leaf
1823 * @dip: the directory
1824 * @index: the hash table offset in the directory
1825 * @len: the number of pointers to this leaf
1826 * @leaf_no: the leaf number
Bob Petersonec038c82011-03-22 13:55:23 -04001827 * @leaf_bh: buffer_head for the starting leaf
Bob Petersond24a7a42011-03-22 13:54:03 -04001828 * last_dealloc: 1 if this is the final dealloc for the leaf, else 0
David Teiglandb3b94fa2006-01-16 16:50:04 +00001829 *
1830 * Returns: errno
1831 */
1832
Steven Whitehousecd915492006-09-04 12:49:07 -04001833static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
Bob Petersonec038c82011-03-22 13:55:23 -04001834 u64 leaf_no, struct buffer_head *leaf_bh,
1835 int last_dealloc)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001836{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001837 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001838 struct gfs2_leaf *tmp_leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001839 struct gfs2_rgrp_list rlist;
1840 struct buffer_head *bh, *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001841 u64 blk, nblk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001842 unsigned int rg_blocks = 0, l_blocks = 0;
1843 char *ht;
Steven Whitehousecd915492006-09-04 12:49:07 -04001844 unsigned int x, size = len * sizeof(u64);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001845 int error;
1846
1847 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1848
Josef Bacik16c5f062008-04-09 09:33:41 -04001849 ht = kzalloc(size, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001850 if (!ht)
1851 return -ENOMEM;
1852
Bob Peterson564e12b2011-11-21 13:36:17 -05001853 if (!gfs2_qadata_get(dip)) {
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001854 error = -ENOMEM;
1855 goto out;
1856 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001857
1858 error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1859 if (error)
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001860 goto out_put;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001861
David Teiglandb3b94fa2006-01-16 16:50:04 +00001862 /* Count the number of leaves */
Bob Petersonec038c82011-03-22 13:55:23 -04001863 bh = leaf_bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001864
Steven Whitehousec7526662006-03-20 12:30:04 -05001865 for (blk = leaf_no; blk; blk = nblk) {
Bob Petersonec038c82011-03-22 13:55:23 -04001866 if (blk != leaf_no) {
1867 error = get_leaf(dip, blk, &bh);
1868 if (error)
1869 goto out_rlist;
1870 }
Steven Whitehousec7526662006-03-20 12:30:04 -05001871 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1872 nblk = be64_to_cpu(tmp_leaf->lf_next);
Bob Petersonec038c82011-03-22 13:55:23 -04001873 if (blk != leaf_no)
1874 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001875
Steven Whitehouse70b0c362011-09-02 16:08:09 +01001876 gfs2_rlist_add(dip, &rlist, blk);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001877 l_blocks++;
1878 }
1879
Bob Petersonfe6c9912008-01-28 11:13:02 -06001880 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001881
1882 for (x = 0; x < rlist.rl_rgrps; x++) {
1883 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001884 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001885 rg_blocks += rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001886 }
1887
1888 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1889 if (error)
1890 goto out_rlist;
1891
1892 error = gfs2_trans_begin(sdp,
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001893 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
David Teiglandb3b94fa2006-01-16 16:50:04 +00001894 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1895 if (error)
1896 goto out_rg_gunlock;
1897
Bob Petersonec038c82011-03-22 13:55:23 -04001898 bh = leaf_bh;
1899
Steven Whitehousec7526662006-03-20 12:30:04 -05001900 for (blk = leaf_no; blk; blk = nblk) {
Bob Petersonec038c82011-03-22 13:55:23 -04001901 if (blk != leaf_no) {
1902 error = get_leaf(dip, blk, &bh);
1903 if (error)
1904 goto out_end_trans;
1905 }
Steven Whitehousec7526662006-03-20 12:30:04 -05001906 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1907 nblk = be64_to_cpu(tmp_leaf->lf_next);
Bob Petersonec038c82011-03-22 13:55:23 -04001908 if (blk != leaf_no)
1909 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001910
1911 gfs2_free_meta(dip, blk, 1);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001912 gfs2_add_inode_blocks(&dip->i_inode, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001913 }
1914
Steven Whitehousecd915492006-09-04 12:49:07 -04001915 error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001916 if (error != size) {
1917 if (error >= 0)
1918 error = -EIO;
1919 goto out_end_trans;
1920 }
1921
1922 error = gfs2_meta_inode_buffer(dip, &dibh);
1923 if (error)
1924 goto out_end_trans;
1925
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001926 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
Bob Petersond24a7a42011-03-22 13:54:03 -04001927 /* On the last dealloc, make this a regular file in case we crash.
1928 (We don't want to free these blocks a second time.) */
1929 if (last_dealloc)
1930 dip->i_inode.i_mode = S_IFREG;
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001931 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001932 brelse(dibh);
1933
Steven Whitehousea91ea692006-09-04 12:04:26 -04001934out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001935 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001936out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001937 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001938out_rlist:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001939 gfs2_rlist_free(&rlist);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001940 gfs2_quota_unhold(dip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001941out_put:
Bob Peterson564e12b2011-11-21 13:36:17 -05001942 gfs2_qadata_put(dip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001943out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001944 kfree(ht);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001945 return error;
1946}
1947
1948/**
1949 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1950 * @dip: the directory
1951 *
1952 * Dealloc all on-disk directory leaves to FREEMETA state
1953 * Change on-disk inode type to "regular file"
1954 *
1955 * Returns: errno
1956 */
1957
1958int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
1959{
Bob Peterson556bb172011-03-22 13:56:37 -04001960 struct buffer_head *bh;
1961 struct gfs2_leaf *leaf;
1962 u32 hsize, len;
Bob Peterson556bb172011-03-22 13:56:37 -04001963 u32 index = 0, next_index;
1964 __be64 *lp;
1965 u64 leaf_no;
1966 int error = 0, last;
1967
1968 hsize = 1 << dip->i_depth;
Bob Peterson556bb172011-03-22 13:56:37 -04001969
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001970 lp = gfs2_dir_get_hash_table(dip);
1971 if (IS_ERR(lp))
1972 return PTR_ERR(lp);
Bob Peterson556bb172011-03-22 13:56:37 -04001973
1974 while (index < hsize) {
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001975 leaf_no = be64_to_cpu(lp[index]);
Bob Peterson556bb172011-03-22 13:56:37 -04001976 if (leaf_no) {
1977 error = get_leaf(dip, leaf_no, &bh);
1978 if (error)
1979 goto out;
1980 leaf = (struct gfs2_leaf *)bh->b_data;
1981 len = 1 << (dip->i_depth - be16_to_cpu(leaf->lf_depth));
1982
1983 next_index = (index & ~(len - 1)) + len;
1984 last = ((next_index >= hsize) ? 1 : 0);
1985 error = leaf_dealloc(dip, index, len, leaf_no, bh,
1986 last);
1987 brelse(bh);
1988 if (error)
1989 goto out;
1990 index = next_index;
1991 } else
1992 index++;
1993 }
1994
1995 if (index != hsize) {
1996 gfs2_consist_inode(dip);
1997 error = -EIO;
1998 }
1999
2000out:
Bob Peterson556bb172011-03-22 13:56:37 -04002001
2002 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002003}
2004
2005/**
2006 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
2007 * @ip: the file being written to
2008 * @filname: the filename that's going to be added
David Teiglandb3b94fa2006-01-16 16:50:04 +00002009 *
Steven Whitehousec7526662006-03-20 12:30:04 -05002010 * Returns: 1 if alloc required, 0 if not, -ve on error
David Teiglandb3b94fa2006-01-16 16:50:04 +00002011 */
2012
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04002013int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002014{
Steven Whitehousec7526662006-03-20 12:30:04 -05002015 struct gfs2_dirent *dent;
2016 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002017
Steven Whitehousec7526662006-03-20 12:30:04 -05002018 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
Steven Whitehouseed386502006-04-07 16:28:07 -04002019 if (!dent) {
Steven Whitehousec7526662006-03-20 12:30:04 -05002020 return 1;
Steven Whitehouseed386502006-04-07 16:28:07 -04002021 }
Steven Whitehousec7526662006-03-20 12:30:04 -05002022 if (IS_ERR(dent))
2023 return PTR_ERR(dent);
2024 brelse(bh);
2025 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002026}
2027