blob: 1a349f9a9685a88c7dc67141ed0cee0c2374fec9 [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
Joe Perchesd77d1b52014-03-06 12:10:45 -080056#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
57
David Teiglandb3b94fa2006-01-16 16:50:04 +000058#include <linux/slab.h>
59#include <linux/spinlock.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000060#include <linux/buffer_head.h>
61#include <linux/sort.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050062#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050063#include <linux/crc32.h>
Steven Whitehousefe1bded2006-04-18 10:09:15 -040064#include <linux/vmalloc.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000065
66#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050067#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000068#include "dir.h"
69#include "glock.h"
70#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000071#include "meta_io.h"
72#include "quota.h"
73#include "rgrp.h"
74#include "trans.h"
Steven Whitehousee13940b2006-01-30 13:31:50 +000075#include "bmap.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050076#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000077
78#define IS_LEAF 1 /* Hashed (leaf) directory */
79#define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
80
Bob Petersondfe4d342011-10-27 12:16:06 -040081#define MAX_RA_BLOCKS 32 /* max read-ahead blocks */
82
Steven Whitehousecd915492006-09-04 12:49:07 -040083#define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
84#define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
David Teiglandb3b94fa2006-01-16 16:50:04 +000085
Steven Whitehouse8d123582010-09-17 12:30:23 +010086struct qstr gfs2_qdot __read_mostly;
87struct qstr gfs2_qdotdot __read_mostly;
88
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -040089typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
90 const struct qstr *name, void *opaque);
David Teiglandb3b94fa2006-01-16 16:50:04 +000091
Steven Whitehousecd915492006-09-04 12:49:07 -040092int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
Steven Whitehouse61e085a2006-04-24 10:07:13 -040093 struct buffer_head **bhp)
Steven Whitehousee13940b2006-01-30 13:31:50 +000094{
95 struct buffer_head *bh;
Steven Whitehousee13940b2006-01-30 13:31:50 +000096
Steven Whitehouse61e085a2006-04-24 10:07:13 -040097 bh = gfs2_meta_new(ip->i_gl, block);
Steven Whitehouse350a9b02012-12-14 12:36:02 +000098 gfs2_trans_add_meta(ip->i_gl, bh);
Steven Whitehouse61e085a2006-04-24 10:07:13 -040099 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
100 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
Steven Whitehousee13940b2006-01-30 13:31:50 +0000101 *bhp = bh;
102 return 0;
103}
104
Steven Whitehousecd915492006-09-04 12:49:07 -0400105static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, u64 block,
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400106 struct buffer_head **bhp)
107{
108 struct buffer_head *bh;
109 int error;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000110
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400111 error = gfs2_meta_read(ip->i_gl, block, DIO_WAIT, &bh);
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400112 if (error)
113 return error;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400114 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) {
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400115 brelse(bh);
116 return -EIO;
117 }
118 *bhp = bh;
119 return 0;
120}
Steven Whitehousee13940b2006-01-30 13:31:50 +0000121
122static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
123 unsigned int offset, unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000124{
125 struct buffer_head *dibh;
126 int error;
127
128 error = gfs2_meta_inode_buffer(ip, &dibh);
129 if (error)
130 return error;
131
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000132 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500133 memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100134 if (ip->i_inode.i_size < offset + size)
135 i_size_write(&ip->i_inode, offset + size);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100136 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500137 gfs2_dinode_out(ip, dibh->b_data);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000138
139 brelse(dibh);
140
141 return size;
142}
143
144
145
146/**
147 * gfs2_dir_write_data - Write directory information to the inode
148 * @ip: The GFS2 inode
149 * @buf: The buffer containing information to be written
150 * @offset: The file offset to start writing at
151 * @size: The amount of data to write
152 *
153 * Returns: The number of bytes correctly written or error code
154 */
155static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
Steven Whitehousecd915492006-09-04 12:49:07 -0400156 u64 offset, unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000157{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400158 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000159 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400160 u64 lblock, dblock;
161 u32 extlen = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000162 unsigned int o;
163 int copied = 0;
164 int error = 0;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000165 int new = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000166
167 if (!size)
168 return 0;
169
170 if (gfs2_is_stuffed(ip) &&
171 offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500172 return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
173 size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000174
175 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
176 return -EINVAL;
177
178 if (gfs2_is_stuffed(ip)) {
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400179 error = gfs2_unstuff_dinode(ip, NULL);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000180 if (error)
Steven Whitehousec7526662006-03-20 12:30:04 -0500181 return error;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000182 }
183
184 lblock = offset;
185 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
186
187 while (copied < size) {
188 unsigned int amount;
189 struct buffer_head *bh;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000190
191 amount = size - copied;
192 if (amount > sdp->sd_sb.sb_bsize - o)
193 amount = sdp->sd_sb.sb_bsize - o;
194
195 if (!extlen) {
196 new = 1;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400197 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
Steven Whitehousefd88de562006-05-05 16:59:11 -0400198 &dblock, &extlen);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000199 if (error)
200 goto fail;
201 error = -EIO;
202 if (gfs2_assert_withdraw(sdp, dblock))
203 goto fail;
204 }
205
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400206 if (amount == sdp->sd_jbsize || new)
207 error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
208 else
209 error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
210
Steven Whitehousee13940b2006-01-30 13:31:50 +0000211 if (error)
212 goto fail;
213
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000214 gfs2_trans_add_meta(ip->i_gl, bh);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000215 memcpy(bh->b_data + o, buf, amount);
216 brelse(bh);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000217
Steven Whitehouse899bb262006-08-01 15:28:57 -0400218 buf += amount;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000219 copied += amount;
220 lblock++;
221 dblock++;
222 extlen--;
223
224 o = sizeof(struct gfs2_meta_header);
225 }
226
227out:
228 error = gfs2_meta_inode_buffer(ip, &dibh);
229 if (error)
230 return error;
231
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100232 if (ip->i_inode.i_size < offset + copied)
233 i_size_write(&ip->i_inode, offset + copied);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100234 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000235
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000236 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500237 gfs2_dinode_out(ip, dibh->b_data);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000238 brelse(dibh);
239
240 return copied;
241fail:
242 if (copied)
243 goto out;
244 return error;
245}
246
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100247static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, __be64 *buf,
248 unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000249{
250 struct buffer_head *dibh;
251 int error;
252
253 error = gfs2_meta_inode_buffer(ip, &dibh);
254 if (!error) {
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100255 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000256 brelse(dibh);
257 }
258
259 return (error) ? error : size;
260}
261
262
263/**
264 * gfs2_dir_read_data - Read a data from a directory inode
265 * @ip: The GFS2 Inode
266 * @buf: The buffer to place result into
Steven Whitehousee13940b2006-01-30 13:31:50 +0000267 * @size: Amount of data to transfer
268 *
269 * Returns: The amount of data actually copied or the error
270 */
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100271static int gfs2_dir_read_data(struct gfs2_inode *ip, __be64 *buf,
272 unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000273{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400274 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehousecd915492006-09-04 12:49:07 -0400275 u64 lblock, dblock;
276 u32 extlen = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000277 unsigned int o;
278 int copied = 0;
279 int error = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000280
281 if (gfs2_is_stuffed(ip))
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100282 return gfs2_dir_read_stuffed(ip, buf, size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000283
284 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
285 return -EINVAL;
286
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100287 lblock = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000288 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
289
290 while (copied < size) {
291 unsigned int amount;
292 struct buffer_head *bh;
293 int new;
294
295 amount = size - copied;
296 if (amount > sdp->sd_sb.sb_bsize - o)
297 amount = sdp->sd_sb.sb_bsize - o;
298
299 if (!extlen) {
300 new = 0;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400301 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
Steven Whitehousefd88de562006-05-05 16:59:11 -0400302 &dblock, &extlen);
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400303 if (error || !dblock)
304 goto fail;
305 BUG_ON(extlen < 1);
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400306 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
Adrian Bunkb7d8ac32006-10-19 16:02:07 +0200307 } else {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400308 error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, &bh);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000309 if (error)
310 goto fail;
311 }
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400312 error = gfs2_metatype_check(sdp, bh, GFS2_METATYPE_JD);
313 if (error) {
314 brelse(bh);
315 goto fail;
316 }
317 dblock++;
318 extlen--;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000319 memcpy(buf, bh->b_data + o, amount);
320 brelse(bh);
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100321 buf += (amount/sizeof(__be64));
Steven Whitehousee13940b2006-01-30 13:31:50 +0000322 copied += amount;
323 lblock++;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000324 o = sizeof(struct gfs2_meta_header);
325 }
326
327 return copied;
328fail:
329 return (copied) ? copied : error;
330}
331
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100332/**
333 * gfs2_dir_get_hash_table - Get pointer to the dir hash table
334 * @ip: The inode in question
335 *
336 * Returns: The hash table or an error
337 */
338
339static __be64 *gfs2_dir_get_hash_table(struct gfs2_inode *ip)
340{
341 struct inode *inode = &ip->i_inode;
342 int ret;
343 u32 hsize;
344 __be64 *hc;
345
346 BUG_ON(!(ip->i_diskflags & GFS2_DIF_EXHASH));
347
348 hc = ip->i_hash_cache;
349 if (hc)
350 return hc;
351
352 hsize = 1 << ip->i_depth;
353 hsize *= sizeof(__be64);
354 if (hsize != i_size_read(&ip->i_inode)) {
355 gfs2_consist_inode(ip);
356 return ERR_PTR(-EIO);
357 }
358
Bob Petersone8830d82013-05-30 09:48:56 -0400359 hc = kmalloc(hsize, GFP_NOFS | __GFP_NOWARN);
360 if (hc == NULL)
361 hc = __vmalloc(hsize, GFP_NOFS, PAGE_KERNEL);
362
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100363 if (hc == NULL)
364 return ERR_PTR(-ENOMEM);
365
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100366 ret = gfs2_dir_read_data(ip, hc, hsize);
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100367 if (ret < 0) {
Bob Petersone8830d82013-05-30 09:48:56 -0400368 if (is_vmalloc_addr(hc))
369 vfree(hc);
370 else
371 kfree(hc);
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100372 return ERR_PTR(ret);
373 }
374
375 spin_lock(&inode->i_lock);
Bob Petersone8830d82013-05-30 09:48:56 -0400376 if (ip->i_hash_cache) {
377 if (is_vmalloc_addr(hc))
378 vfree(hc);
379 else
380 kfree(hc);
381 } else {
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100382 ip->i_hash_cache = hc;
Bob Petersone8830d82013-05-30 09:48:56 -0400383 }
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100384 spin_unlock(&inode->i_lock);
385
386 return ip->i_hash_cache;
387}
388
389/**
390 * gfs2_dir_hash_inval - Invalidate dir hash
391 * @ip: The directory inode
392 *
393 * Must be called with an exclusive glock, or during glock invalidation.
394 */
395void gfs2_dir_hash_inval(struct gfs2_inode *ip)
396{
397 __be64 *hc = ip->i_hash_cache;
398 ip->i_hash_cache = NULL;
Bob Petersone8830d82013-05-30 09:48:56 -0400399 if (is_vmalloc_addr(hc))
400 vfree(hc);
401 else
402 kfree(hc);
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100403}
404
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500405static inline int gfs2_dirent_sentinel(const struct gfs2_dirent *dent)
406{
407 return dent->de_inum.no_addr == 0 || dent->de_inum.no_formal_ino == 0;
408}
409
Steven Whitehousec7526662006-03-20 12:30:04 -0500410static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
411 const struct qstr *name, int ret)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000412{
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500413 if (!gfs2_dirent_sentinel(dent) &&
Steven Whitehousec7526662006-03-20 12:30:04 -0500414 be32_to_cpu(dent->de_hash) == name->hash &&
415 be16_to_cpu(dent->de_name_len) == name->len &&
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400416 memcmp(dent+1, name->name, name->len) == 0)
Steven Whitehousec7526662006-03-20 12:30:04 -0500417 return ret;
418 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000419}
420
Steven Whitehousec7526662006-03-20 12:30:04 -0500421static int gfs2_dirent_find(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500422 const struct qstr *name,
423 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500424{
425 return __gfs2_dirent_find(dent, name, 1);
426}
427
428static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500429 const struct qstr *name,
430 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500431{
432 return __gfs2_dirent_find(dent, name, 2);
433}
434
435/*
436 * name->name holds ptr to start of block.
437 * name->len holds size of block.
438 */
439static int gfs2_dirent_last(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500440 const struct qstr *name,
441 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500442{
443 const char *start = name->name;
444 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
445 if (name->len == (end - start))
446 return 1;
447 return 0;
448}
449
450static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500451 const struct qstr *name,
452 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500453{
454 unsigned required = GFS2_DIRENT_SIZE(name->len);
455 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
456 unsigned totlen = be16_to_cpu(dent->de_rec_len);
457
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500458 if (gfs2_dirent_sentinel(dent))
Bob Peterson728a7562010-07-14 18:12:26 -0400459 actual = 0;
Jan Engelhardtc53921242006-09-05 14:30:40 +0200460 if (totlen - actual >= required)
Steven Whitehousec7526662006-03-20 12:30:04 -0500461 return 1;
462 return 0;
463}
464
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500465struct dirent_gather {
466 const struct gfs2_dirent **pdent;
467 unsigned offset;
468};
469
470static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
471 const struct qstr *name,
472 void *opaque)
473{
474 struct dirent_gather *g = opaque;
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500475 if (!gfs2_dirent_sentinel(dent)) {
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500476 g->pdent[g->offset++] = dent;
477 }
478 return 0;
479}
480
Steven Whitehousec7526662006-03-20 12:30:04 -0500481/*
482 * Other possible things to check:
483 * - Inode located within filesystem size (and on valid block)
484 * - Valid directory entry type
485 * Not sure how heavy-weight we want to make this... could also check
486 * hash is correct for example, but that would take a lot of extra time.
487 * For now the most important thing is to check that the various sizes
488 * are correct.
489 */
490static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
491 unsigned int size, unsigned int len, int first)
492{
493 const char *msg = "gfs2_dirent too small";
494 if (unlikely(size < sizeof(struct gfs2_dirent)))
495 goto error;
496 msg = "gfs2_dirent misaligned";
497 if (unlikely(offset & 0x7))
498 goto error;
499 msg = "gfs2_dirent points beyond end of block";
500 if (unlikely(offset + size > len))
501 goto error;
502 msg = "zero inode number";
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500503 if (unlikely(!first && gfs2_dirent_sentinel(dent)))
Steven Whitehousec7526662006-03-20 12:30:04 -0500504 goto error;
505 msg = "name length is greater than space in dirent";
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500506 if (!gfs2_dirent_sentinel(dent) &&
Steven Whitehousec7526662006-03-20 12:30:04 -0500507 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
508 size))
509 goto error;
510 return 0;
511error:
Joe Perchesd77d1b52014-03-06 12:10:45 -0800512 pr_warn("%s: %s (%s)\n",
513 __func__, msg, first ? "first in block" : "not first in block");
Steven Whitehousec7526662006-03-20 12:30:04 -0500514 return -EIO;
515}
516
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500517static int gfs2_dirent_offset(const void *buf)
Steven Whitehousec7526662006-03-20 12:30:04 -0500518{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500519 const struct gfs2_meta_header *h = buf;
520 int offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500521
522 BUG_ON(buf == NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500523
Steven Whitehousee3167de2006-03-30 15:46:23 -0500524 switch(be32_to_cpu(h->mh_type)) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500525 case GFS2_METATYPE_LF:
526 offset = sizeof(struct gfs2_leaf);
527 break;
528 case GFS2_METATYPE_DI:
529 offset = sizeof(struct gfs2_dinode);
530 break;
531 default:
532 goto wrong_type;
533 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500534 return offset;
535wrong_type:
Joe Perchesd77d1b52014-03-06 12:10:45 -0800536 pr_warn("%s: wrong block type %u\n", __func__, be32_to_cpu(h->mh_type));
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500537 return -1;
538}
Steven Whitehousec7526662006-03-20 12:30:04 -0500539
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400540static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500541 unsigned int len, gfs2_dscan_t scan,
542 const struct qstr *name,
543 void *opaque)
544{
545 struct gfs2_dirent *dent, *prev;
546 unsigned offset;
547 unsigned size;
548 int ret = 0;
549
550 ret = gfs2_dirent_offset(buf);
551 if (ret < 0)
552 goto consist_inode;
553
554 offset = ret;
Steven Whitehousec7526662006-03-20 12:30:04 -0500555 prev = NULL;
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400556 dent = buf + offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500557 size = be16_to_cpu(dent->de_rec_len);
558 if (gfs2_check_dirent(dent, offset, size, len, 1))
559 goto consist_inode;
560 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500561 ret = scan(dent, name, opaque);
Steven Whitehousec7526662006-03-20 12:30:04 -0500562 if (ret)
563 break;
564 offset += size;
565 if (offset == len)
566 break;
567 prev = dent;
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400568 dent = buf + offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500569 size = be16_to_cpu(dent->de_rec_len);
570 if (gfs2_check_dirent(dent, offset, size, len, 0))
571 goto consist_inode;
572 } while(1);
573
574 switch(ret) {
575 case 0:
576 return NULL;
577 case 1:
578 return dent;
579 case 2:
580 return prev ? prev : dent;
581 default:
582 BUG_ON(ret > 0);
583 return ERR_PTR(ret);
584 }
585
Steven Whitehousec7526662006-03-20 12:30:04 -0500586consist_inode:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400587 gfs2_consist_inode(GFS2_I(inode));
Steven Whitehousec7526662006-03-20 12:30:04 -0500588 return ERR_PTR(-EIO);
589}
590
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400591static int dirent_check_reclen(struct gfs2_inode *dip,
592 const struct gfs2_dirent *d, const void *end_p)
593{
594 const void *ptr = d;
595 u16 rec_len = be16_to_cpu(d->de_rec_len);
596
597 if (unlikely(rec_len < sizeof(struct gfs2_dirent)))
598 goto broken;
599 ptr += rec_len;
600 if (ptr < end_p)
601 return rec_len;
602 if (ptr == end_p)
603 return -ENOENT;
604broken:
605 gfs2_consist_inode(dip);
606 return -EIO;
607}
608
David Teiglandb3b94fa2006-01-16 16:50:04 +0000609/**
610 * dirent_next - Next dirent
611 * @dip: the directory
612 * @bh: The buffer
613 * @dent: Pointer to list of dirents
614 *
615 * Returns: 0 on success, error code otherwise
616 */
617
618static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
619 struct gfs2_dirent **dent)
620{
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400621 struct gfs2_dirent *cur = *dent, *tmp;
622 char *bh_end = bh->b_data + bh->b_size;
623 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000624
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400625 ret = dirent_check_reclen(dip, cur, bh_end);
626 if (ret < 0)
627 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000628
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400629 tmp = (void *)cur + ret;
630 ret = dirent_check_reclen(dip, tmp, bh_end);
631 if (ret == -EIO)
632 return ret;
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000633
David Teiglandb3b94fa2006-01-16 16:50:04 +0000634 /* Only the first dent could ever have de_inum.no_addr == 0 */
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500635 if (gfs2_dirent_sentinel(tmp)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000636 gfs2_consist_inode(dip);
637 return -EIO;
638 }
639
640 *dent = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000641 return 0;
642}
643
644/**
645 * dirent_del - Delete a dirent
646 * @dip: The GFS2 inode
647 * @bh: The buffer
648 * @prev: The previous dirent
649 * @cur: The current dirent
650 *
651 */
652
653static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
654 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
655{
Steven Whitehousecd915492006-09-04 12:49:07 -0400656 u16 cur_rec_len, prev_rec_len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000657
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500658 if (gfs2_dirent_sentinel(cur)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000659 gfs2_consist_inode(dip);
660 return;
661 }
662
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000663 gfs2_trans_add_meta(dip->i_gl, bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000664
665 /* If there is no prev entry, this is the first entry in the block.
666 The de_rec_len is already as big as it needs to be. Just zero
667 out the inode number and return. */
668
669 if (!prev) {
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500670 cur->de_inum.no_addr = 0;
671 cur->de_inum.no_formal_ino = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000672 return;
673 }
674
675 /* Combine this dentry with the previous one. */
676
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000677 prev_rec_len = be16_to_cpu(prev->de_rec_len);
678 cur_rec_len = be16_to_cpu(cur->de_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000679
680 if ((char *)prev + prev_rec_len != (char *)cur)
681 gfs2_consist_inode(dip);
682 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
683 gfs2_consist_inode(dip);
684
685 prev_rec_len += cur_rec_len;
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000686 prev->de_rec_len = cpu_to_be16(prev_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000687}
688
Steven Whitehousec7526662006-03-20 12:30:04 -0500689/*
690 * Takes a dent from which to grab space as an argument. Returns the
691 * newly created dent.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000692 */
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400693static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
694 struct gfs2_dirent *dent,
695 const struct qstr *name,
696 struct buffer_head *bh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000697{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400698 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -0500699 struct gfs2_dirent *ndent;
700 unsigned offset = 0, totlen;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000701
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500702 if (!gfs2_dirent_sentinel(dent))
Steven Whitehousec7526662006-03-20 12:30:04 -0500703 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
704 totlen = be16_to_cpu(dent->de_rec_len);
705 BUG_ON(offset + name->len > totlen);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000706 gfs2_trans_add_meta(ip->i_gl, bh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500707 ndent = (struct gfs2_dirent *)((char *)dent + offset);
708 dent->de_rec_len = cpu_to_be16(offset);
709 gfs2_qstr2dirent(name, totlen - offset, ndent);
710 return ndent;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000711}
712
Steven Whitehousec7526662006-03-20 12:30:04 -0500713static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
714 struct buffer_head *bh,
715 const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000716{
717 struct gfs2_dirent *dent;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400718 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500719 gfs2_dirent_find_space, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500720 if (!dent || IS_ERR(dent))
721 return dent;
722 return gfs2_init_dirent(inode, dent, name, bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000723}
724
Steven Whitehousecd915492006-09-04 12:49:07 -0400725static int get_leaf(struct gfs2_inode *dip, u64 leaf_no,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000726 struct buffer_head **bhp)
727{
728 int error;
729
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400730 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, bhp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400731 if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
Joe Perchesd77d1b52014-03-06 12:10:45 -0800732 /* pr_info("block num=%llu\n", leaf_no); */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000733 error = -EIO;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400734 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000735
736 return error;
737}
738
739/**
740 * get_leaf_nr - Get a leaf number associated with the index
741 * @dip: The GFS2 inode
742 * @index:
743 * @leaf_out:
744 *
745 * Returns: 0 on success, error code otherwise
746 */
747
Steven Whitehousecd915492006-09-04 12:49:07 -0400748static int get_leaf_nr(struct gfs2_inode *dip, u32 index,
749 u64 *leaf_out)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000750{
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100751 __be64 *hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000752
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100753 hash = gfs2_dir_get_hash_table(dip);
754 if (IS_ERR(hash))
755 return PTR_ERR(hash);
756 *leaf_out = be64_to_cpu(*(hash + index));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000757 return 0;
758}
759
Steven Whitehousecd915492006-09-04 12:49:07 -0400760static int get_first_leaf(struct gfs2_inode *dip, u32 index,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000761 struct buffer_head **bh_out)
762{
Steven Whitehousecd915492006-09-04 12:49:07 -0400763 u64 leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000764 int error;
765
766 error = get_leaf_nr(dip, index, &leaf_no);
767 if (!error)
768 error = get_leaf(dip, leaf_no, bh_out);
769
770 return error;
771}
772
Steven Whitehousec7526662006-03-20 12:30:04 -0500773static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
774 const struct qstr *name,
775 gfs2_dscan_t scan,
776 struct buffer_head **pbh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000777{
Steven Whitehousec7526662006-03-20 12:30:04 -0500778 struct buffer_head *bh;
779 struct gfs2_dirent *dent;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400780 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000781 int error;
782
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000783 if (ip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500784 struct gfs2_leaf *leaf;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000785 unsigned hsize = 1 << ip->i_depth;
Steven Whitehousec7526662006-03-20 12:30:04 -0500786 unsigned index;
787 u64 ln;
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100788 if (hsize * sizeof(u64) != i_size_read(inode)) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500789 gfs2_consist_inode(ip);
790 return ERR_PTR(-EIO);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000791 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400792
Steven Whitehouse9a004502008-02-01 09:23:44 +0000793 index = name->hash >> (32 - ip->i_depth);
Steven Whitehousec7526662006-03-20 12:30:04 -0500794 error = get_first_leaf(ip, index, &bh);
795 if (error)
796 return ERR_PTR(error);
797 do {
798 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500799 scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500800 if (dent)
801 goto got_dent;
802 leaf = (struct gfs2_leaf *)bh->b_data;
803 ln = be64_to_cpu(leaf->lf_next);
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400804 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500805 if (!ln)
806 break;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400807
Steven Whitehousec7526662006-03-20 12:30:04 -0500808 error = get_leaf(ip, ln, &bh);
809 } while(!error);
810
811 return error ? ERR_PTR(error) : NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000812 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000813
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400814
Steven Whitehousec7526662006-03-20 12:30:04 -0500815 error = gfs2_meta_inode_buffer(ip, &bh);
816 if (error)
817 return ERR_PTR(error);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500818 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500819got_dent:
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400820 if (unlikely(dent == NULL || IS_ERR(dent))) {
Steven Whitehouseed386502006-04-07 16:28:07 -0400821 brelse(bh);
822 bh = NULL;
823 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500824 *pbh = bh;
825 return dent;
826}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000827
Steven Whitehousec7526662006-03-20 12:30:04 -0500828static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
829{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400830 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000831 unsigned int n = 1;
Steven Whitehouse09010972009-05-20 10:48:47 +0100832 u64 bn;
833 int error;
834 struct buffer_head *bh;
Steven Whitehousec7526662006-03-20 12:30:04 -0500835 struct gfs2_leaf *leaf;
836 struct gfs2_dirent *dent;
Linus Torvalds26fe5752012-05-10 13:14:12 -0700837 struct qstr name = { .name = "" };
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +0000838 struct timespec tv = CURRENT_TIME;
Steven Whitehouse09010972009-05-20 10:48:47 +0100839
Bob Peterson6e87ed02011-11-18 10:58:32 -0500840 error = gfs2_alloc_blocks(ip, &bn, &n, 0, NULL);
Steven Whitehouse09010972009-05-20 10:48:47 +0100841 if (error)
842 return NULL;
843 bh = gfs2_meta_new(ip->i_gl, bn);
Steven Whitehousec7526662006-03-20 12:30:04 -0500844 if (!bh)
845 return NULL;
Steven Whitehouse09010972009-05-20 10:48:47 +0100846
Steven Whitehouse5731be52008-02-01 13:16:55 +0000847 gfs2_trans_add_unrevoke(GFS2_SB(inode), bn, 1);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000848 gfs2_trans_add_meta(ip->i_gl, bh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500849 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
850 leaf = (struct gfs2_leaf *)bh->b_data;
851 leaf->lf_depth = cpu_to_be16(depth);
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400852 leaf->lf_entries = 0;
Al Viroa2d7d022006-10-14 16:49:30 +0100853 leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400854 leaf->lf_next = 0;
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +0000855 leaf->lf_inode = cpu_to_be64(ip->i_no_addr);
856 leaf->lf_dist = cpu_to_be32(1);
857 leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
858 leaf->lf_sec = cpu_to_be64(tv.tv_sec);
859 memset(leaf->lf_reserved2, 0, sizeof(leaf->lf_reserved2));
Steven Whitehousec7526662006-03-20 12:30:04 -0500860 dent = (struct gfs2_dirent *)(leaf+1);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500861 gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
Steven Whitehousec7526662006-03-20 12:30:04 -0500862 *pbh = bh;
863 return leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000864}
865
866/**
867 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
868 * @dip: The GFS2 inode
869 *
870 * Returns: 0 on success, error code otherwise
871 */
872
Steven Whitehousec7526662006-03-20 12:30:04 -0500873static int dir_make_exhash(struct inode *inode)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000874{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400875 struct gfs2_inode *dip = GFS2_I(inode);
876 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000877 struct gfs2_dirent *dent;
Steven Whitehousec7526662006-03-20 12:30:04 -0500878 struct qstr args;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000879 struct buffer_head *bh, *dibh;
880 struct gfs2_leaf *leaf;
881 int y;
Steven Whitehousecd915492006-09-04 12:49:07 -0400882 u32 x;
Al Virob44b84d2006-10-14 10:46:30 -0400883 __be64 *lp;
884 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000885 int error;
886
887 error = gfs2_meta_inode_buffer(dip, &dibh);
888 if (error)
889 return error;
890
David Teiglandb3b94fa2006-01-16 16:50:04 +0000891 /* Turn over a new leaf */
892
Steven Whitehousec7526662006-03-20 12:30:04 -0500893 leaf = new_leaf(inode, &bh, 0);
894 if (!leaf)
895 return -ENOSPC;
896 bn = bh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000897
Steven Whitehousead6203f2008-11-03 13:59:19 +0000898 gfs2_assert(sdp, dip->i_entries < (1 << 16));
899 leaf->lf_entries = cpu_to_be16(dip->i_entries);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000900
901 /* Copy dirents */
902
903 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
904 sizeof(struct gfs2_dinode));
905
906 /* Find last entry */
907
908 x = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500909 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
910 sizeof(struct gfs2_leaf);
911 args.name = bh->b_data;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400912 dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500913 gfs2_dirent_last, &args, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500914 if (!dent) {
915 brelse(bh);
916 brelse(dibh);
917 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000918 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500919 if (IS_ERR(dent)) {
920 brelse(bh);
921 brelse(dibh);
922 return PTR_ERR(dent);
923 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000924
925 /* Adjust the last dirent's record length
926 (Remember that dent still points to the last entry.) */
927
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000928 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000929 sizeof(struct gfs2_dinode) -
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000930 sizeof(struct gfs2_leaf));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000931
932 brelse(bh);
933
934 /* We're done with the new leaf block, now setup the new
935 hash table. */
936
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000937 gfs2_trans_add_meta(dip->i_gl, dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000938 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
939
Al Virob44b84d2006-10-14 10:46:30 -0400940 lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000941
942 for (x = sdp->sd_hash_ptrs; x--; lp++)
943 *lp = cpu_to_be64(bn);
944
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100945 i_size_write(inode, sdp->sd_sb.sb_bsize / 2);
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000946 gfs2_add_inode_blocks(&dip->i_inode, 1);
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000947 dip->i_diskflags |= GFS2_DIF_EXHASH;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000948
949 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000950 dip->i_depth = y;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000951
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500952 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000953
954 brelse(dibh);
955
956 return 0;
957}
958
959/**
960 * dir_split_leaf - Split a leaf block into two
961 * @dip: The GFS2 inode
962 * @index:
963 * @leaf_no:
964 *
965 * Returns: 0 on success, error code on failure
966 */
967
Steven Whitehousec7526662006-03-20 12:30:04 -0500968static int dir_split_leaf(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000969{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400970 struct gfs2_inode *dip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000971 struct buffer_head *nbh, *obh, *dibh;
972 struct gfs2_leaf *nleaf, *oleaf;
Steven Whitehouse4da3c642006-07-11 13:19:13 -0400973 struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
Steven Whitehousecd915492006-09-04 12:49:07 -0400974 u32 start, len, half_len, divider;
Al Virob44b84d2006-10-14 10:46:30 -0400975 u64 bn, leaf_no;
976 __be64 *lp;
Steven Whitehousecd915492006-09-04 12:49:07 -0400977 u32 index;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000978 int x, moved = 0;
979 int error;
980
Steven Whitehouse9a004502008-02-01 09:23:44 +0000981 index = name->hash >> (32 - dip->i_depth);
Steven Whitehousec7526662006-03-20 12:30:04 -0500982 error = get_leaf_nr(dip, index, &leaf_no);
983 if (error)
984 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000985
986 /* Get the old leaf block */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000987 error = get_leaf(dip, leaf_no, &obh);
988 if (error)
Steven Whitehousee90deff2006-03-29 19:02:15 -0500989 return error;
990
991 oleaf = (struct gfs2_leaf *)obh->b_data;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000992 if (dip->i_depth == be16_to_cpu(oleaf->lf_depth)) {
Steven Whitehousee90deff2006-03-29 19:02:15 -0500993 brelse(obh);
994 return 1; /* can't split */
995 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000996
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000997 gfs2_trans_add_meta(dip->i_gl, obh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000998
Steven Whitehousec7526662006-03-20 12:30:04 -0500999 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
1000 if (!nleaf) {
1001 brelse(obh);
1002 return -ENOSPC;
1003 }
1004 bn = nbh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001005
Steven Whitehousec7526662006-03-20 12:30:04 -05001006 /* Compute the start and len of leaf pointers in the hash table. */
Steven Whitehouse9a004502008-02-01 09:23:44 +00001007 len = 1 << (dip->i_depth - be16_to_cpu(oleaf->lf_depth));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001008 half_len = len >> 1;
1009 if (!half_len) {
Joe Perchesd77d1b52014-03-06 12:10:45 -08001010 pr_warn("i_depth %u lf_depth %u index %u\n",
1011 dip->i_depth, be16_to_cpu(oleaf->lf_depth), index);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001012 gfs2_consist_inode(dip);
1013 error = -EIO;
1014 goto fail_brelse;
1015 }
1016
1017 start = (index & ~(len - 1));
1018
1019 /* Change the pointers.
1020 Don't bother distinguishing stuffed from non-stuffed.
1021 This code is complicated enough already. */
David Rientjes4244b522010-07-20 19:45:03 -07001022 lp = kmalloc(half_len * sizeof(__be64), GFP_NOFS);
1023 if (!lp) {
1024 error = -ENOMEM;
1025 goto fail_brelse;
1026 }
1027
David Teiglandb3b94fa2006-01-16 16:50:04 +00001028 /* Change the pointers */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001029 for (x = 0; x < half_len; x++)
1030 lp[x] = cpu_to_be64(bn);
1031
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001032 gfs2_dir_hash_inval(dip);
1033
Steven Whitehousecd915492006-09-04 12:49:07 -04001034 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
1035 half_len * sizeof(u64));
1036 if (error != half_len * sizeof(u64)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001037 if (error >= 0)
1038 error = -EIO;
1039 goto fail_lpfree;
1040 }
1041
1042 kfree(lp);
1043
1044 /* Compute the divider */
Steven Whitehouse9a004502008-02-01 09:23:44 +00001045 divider = (start + half_len) << (32 - dip->i_depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001046
1047 /* Copy the entries */
Steven Whitehouse15793432009-11-06 11:06:37 +00001048 dent = (struct gfs2_dirent *)(obh->b_data + sizeof(struct gfs2_leaf));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001049
1050 do {
1051 next = dent;
1052 if (dirent_next(dip, obh, &next))
1053 next = NULL;
1054
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -05001055 if (!gfs2_dirent_sentinel(dent) &&
David Teiglandb3b94fa2006-01-16 16:50:04 +00001056 be32_to_cpu(dent->de_hash) < divider) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001057 struct qstr str;
1058 str.name = (char*)(dent+1);
1059 str.len = be16_to_cpu(dent->de_name_len);
1060 str.hash = be32_to_cpu(dent->de_hash);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001061 new = gfs2_dirent_alloc(inode, nbh, &str);
Steven Whitehousec7526662006-03-20 12:30:04 -05001062 if (IS_ERR(new)) {
1063 error = PTR_ERR(new);
1064 break;
1065 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001066
1067 new->de_inum = dent->de_inum; /* No endian worries */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001068 new->de_type = dent->de_type; /* No endian worries */
Marcin Slusarzbb16b342008-02-13 00:06:10 +01001069 be16_add_cpu(&nleaf->lf_entries, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001070
1071 dirent_del(dip, obh, prev, dent);
1072
1073 if (!oleaf->lf_entries)
1074 gfs2_consist_inode(dip);
Marcin Slusarzbb16b342008-02-13 00:06:10 +01001075 be16_add_cpu(&oleaf->lf_entries, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001076
1077 if (!prev)
1078 prev = dent;
1079
1080 moved = 1;
Steven Whitehousec7526662006-03-20 12:30:04 -05001081 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001082 prev = dent;
Steven Whitehousec7526662006-03-20 12:30:04 -05001083 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001084 dent = next;
Steven Whitehousec7526662006-03-20 12:30:04 -05001085 } while (dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001086
Steven Whitehousec7526662006-03-20 12:30:04 -05001087 oleaf->lf_depth = nleaf->lf_depth;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001088
1089 error = gfs2_meta_inode_buffer(dip, &dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001090 if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001091 gfs2_trans_add_meta(dip->i_gl, dibh);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001092 gfs2_add_inode_blocks(&dip->i_inode, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001093 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001094 brelse(dibh);
1095 }
1096
1097 brelse(obh);
1098 brelse(nbh);
1099
1100 return error;
1101
Steven Whitehousee90deff2006-03-29 19:02:15 -05001102fail_lpfree:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001103 kfree(lp);
1104
Steven Whitehousee90deff2006-03-29 19:02:15 -05001105fail_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001106 brelse(obh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001107 brelse(nbh);
1108 return error;
1109}
1110
1111/**
1112 * dir_double_exhash - Double size of ExHash table
1113 * @dip: The GFS2 dinode
1114 *
1115 * Returns: 0 on success, error code on failure
1116 */
1117
1118static int dir_double_exhash(struct gfs2_inode *dip)
1119{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001120 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001121 u32 hsize;
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001122 u32 hsize_bytes;
1123 __be64 *hc;
1124 __be64 *hc2, *h;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001125 int x;
1126 int error = 0;
1127
Steven Whitehouse9a004502008-02-01 09:23:44 +00001128 hsize = 1 << dip->i_depth;
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001129 hsize_bytes = hsize * sizeof(__be64);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001130
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001131 hc = gfs2_dir_get_hash_table(dip);
1132 if (IS_ERR(hc))
1133 return PTR_ERR(hc);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001134
Bob Peterson512cbf02013-06-14 08:39:18 -04001135 hc2 = kmalloc(hsize_bytes * 2, GFP_NOFS | __GFP_NOWARN);
Bob Petersone8830d82013-05-30 09:48:56 -04001136 if (hc2 == NULL)
1137 hc2 = __vmalloc(hsize_bytes * 2, GFP_NOFS, PAGE_KERNEL);
1138
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001139 if (!hc2)
David Rientjes4244b522010-07-20 19:45:03 -07001140 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001141
Bob Peterson512cbf02013-06-14 08:39:18 -04001142 h = hc2;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001143 error = gfs2_meta_inode_buffer(dip, &dibh);
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001144 if (error)
1145 goto out_kfree;
1146
1147 for (x = 0; x < hsize; x++) {
1148 *h++ = *hc;
1149 *h++ = *hc;
1150 hc++;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001151 }
1152
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001153 error = gfs2_dir_write_data(dip, (char *)hc2, 0, hsize_bytes * 2);
1154 if (error != (hsize_bytes * 2))
1155 goto fail;
1156
1157 gfs2_dir_hash_inval(dip);
1158 dip->i_hash_cache = hc2;
1159 dip->i_depth++;
1160 gfs2_dinode_out(dip, dibh->b_data);
1161 brelse(dibh);
1162 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001163
Steven Whitehousea91ea692006-09-04 12:04:26 -04001164fail:
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001165 /* Replace original hash table & size */
1166 gfs2_dir_write_data(dip, (char *)hc, 0, hsize_bytes);
1167 i_size_write(&dip->i_inode, hsize_bytes);
1168 gfs2_dinode_out(dip, dibh->b_data);
1169 brelse(dibh);
1170out_kfree:
Bob Petersone8830d82013-05-30 09:48:56 -04001171 if (is_vmalloc_addr(hc2))
1172 vfree(hc2);
1173 else
1174 kfree(hc2);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001175 return error;
1176}
1177
1178/**
1179 * compare_dents - compare directory entries by hash value
1180 * @a: first dent
1181 * @b: second dent
1182 *
1183 * When comparing the hash entries of @a to @b:
1184 * gt: returns 1
1185 * lt: returns -1
1186 * eq: returns 0
1187 */
1188
1189static int compare_dents(const void *a, const void *b)
1190{
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001191 const struct gfs2_dirent *dent_a, *dent_b;
Steven Whitehousecd915492006-09-04 12:49:07 -04001192 u32 hash_a, hash_b;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001193 int ret = 0;
1194
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001195 dent_a = *(const struct gfs2_dirent **)a;
Steven Whitehousec7526662006-03-20 12:30:04 -05001196 hash_a = be32_to_cpu(dent_a->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001197
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001198 dent_b = *(const struct gfs2_dirent **)b;
Steven Whitehousec7526662006-03-20 12:30:04 -05001199 hash_b = be32_to_cpu(dent_b->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001200
1201 if (hash_a > hash_b)
1202 ret = 1;
1203 else if (hash_a < hash_b)
1204 ret = -1;
1205 else {
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001206 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1207 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001208
1209 if (len_a > len_b)
1210 ret = 1;
1211 else if (len_a < len_b)
1212 ret = -1;
1213 else
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001214 ret = memcmp(dent_a + 1, dent_b + 1, len_a);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001215 }
1216
1217 return ret;
1218}
1219
1220/**
1221 * do_filldir_main - read out directory entries
1222 * @dip: The GFS2 inode
Al Virod81a8ef2013-05-16 14:14:48 -04001223 * @ctx: what to feed the entries to
David Teiglandb3b94fa2006-01-16 16:50:04 +00001224 * @darr: an array of struct gfs2_dirent pointers to read
1225 * @entries: the number of entries in darr
1226 * @copied: pointer to int that's non-zero if a entry has been copied out
1227 *
1228 * Jump through some hoops to make sure that if there are hash collsions,
1229 * they are read out at the beginning of a buffer. We want to minimize
1230 * the possibility that they will fall into different readdir buffers or
1231 * that someone will want to seek to that location.
1232 *
Al Virod81a8ef2013-05-16 14:14:48 -04001233 * Returns: errno, >0 if the actor tells you to stop
David Teiglandb3b94fa2006-01-16 16:50:04 +00001234 */
1235
Al Virod81a8ef2013-05-16 14:14:48 -04001236static int do_filldir_main(struct gfs2_inode *dip, struct dir_context *ctx,
Steven Whitehousecd915492006-09-04 12:49:07 -04001237 const struct gfs2_dirent **darr, u32 entries,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001238 int *copied)
1239{
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001240 const struct gfs2_dirent *dent, *dent_next;
Steven Whitehousecd915492006-09-04 12:49:07 -04001241 u64 off, off_next;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001242 unsigned int x, y;
1243 int run = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001244
1245 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1246
1247 dent_next = darr[0];
1248 off_next = be32_to_cpu(dent_next->de_hash);
1249 off_next = gfs2_disk_hash2offset(off_next);
1250
1251 for (x = 0, y = 1; x < entries; x++, y++) {
1252 dent = dent_next;
1253 off = off_next;
1254
1255 if (y < entries) {
1256 dent_next = darr[y];
1257 off_next = be32_to_cpu(dent_next->de_hash);
1258 off_next = gfs2_disk_hash2offset(off_next);
1259
Al Virod81a8ef2013-05-16 14:14:48 -04001260 if (off < ctx->pos)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001261 continue;
Al Virod81a8ef2013-05-16 14:14:48 -04001262 ctx->pos = off;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001263
1264 if (off_next == off) {
1265 if (*copied && !run)
1266 return 1;
1267 run = 1;
1268 } else
1269 run = 0;
1270 } else {
Al Virod81a8ef2013-05-16 14:14:48 -04001271 if (off < ctx->pos)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001272 continue;
Al Virod81a8ef2013-05-16 14:14:48 -04001273 ctx->pos = off;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001274 }
1275
Al Virod81a8ef2013-05-16 14:14:48 -04001276 if (!dir_emit(ctx, (const char *)(dent + 1),
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001277 be16_to_cpu(dent->de_name_len),
Al Virod81a8ef2013-05-16 14:14:48 -04001278 be64_to_cpu(dent->de_inum.no_addr),
1279 be16_to_cpu(dent->de_type)))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001280 return 1;
1281
1282 *copied = 1;
1283 }
1284
Al Virod81a8ef2013-05-16 14:14:48 -04001285 /* Increment the ctx->pos by one, so the next time we come into the
David Teiglandb3b94fa2006-01-16 16:50:04 +00001286 do_filldir fxn, we get the next entry instead of the last one in the
1287 current leaf */
1288
Al Virod81a8ef2013-05-16 14:14:48 -04001289 ctx->pos++;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001290
1291 return 0;
1292}
1293
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001294static void *gfs2_alloc_sort_buffer(unsigned size)
1295{
1296 void *ptr = NULL;
1297
1298 if (size < KMALLOC_MAX_SIZE)
1299 ptr = kmalloc(size, GFP_NOFS | __GFP_NOWARN);
1300 if (!ptr)
1301 ptr = __vmalloc(size, GFP_NOFS, PAGE_KERNEL);
1302 return ptr;
1303}
1304
1305static void gfs2_free_sort_buffer(void *ptr)
1306{
1307 if (is_vmalloc_addr(ptr))
1308 vfree(ptr);
1309 else
1310 kfree(ptr);
1311}
1312
Al Virod81a8ef2013-05-16 14:14:48 -04001313static int gfs2_dir_read_leaf(struct inode *inode, struct dir_context *ctx,
1314 int *copied, unsigned *depth,
Steven Whitehouse3699e3a2007-01-17 15:09:20 +00001315 u64 leaf_no)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001316{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001317 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001318 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001319 struct buffer_head *bh;
1320 struct gfs2_leaf *lf;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001321 unsigned entries = 0, entries2 = 0;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001322 unsigned leaves = 0;
1323 const struct gfs2_dirent **darr, *dent;
1324 struct dirent_gather g;
1325 struct buffer_head **larr;
1326 int leaf = 0;
1327 int error, i;
1328 u64 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001329
David Teiglandb3b94fa2006-01-16 16:50:04 +00001330 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001331 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001332 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001333 goto out;
1334 lf = (struct gfs2_leaf *)bh->b_data;
1335 if (leaves == 0)
1336 *depth = be16_to_cpu(lf->lf_depth);
1337 entries += be16_to_cpu(lf->lf_entries);
1338 leaves++;
1339 lfn = be64_to_cpu(lf->lf_next);
1340 brelse(bh);
1341 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001342
1343 if (!entries)
1344 return 0;
1345
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001346 error = -ENOMEM;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001347 /*
1348 * The extra 99 entries are not normally used, but are a buffer
1349 * zone in case the number of entries in the leaf is corrupt.
1350 * 99 is the maximum number of entries that can fit in a single
1351 * leaf block.
1352 */
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001353 larr = gfs2_alloc_sort_buffer((leaves + entries + 99) * sizeof(void *));
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001354 if (!larr)
1355 goto out;
1356 darr = (const struct gfs2_dirent **)(larr + leaves);
1357 g.pdent = darr;
1358 g.offset = 0;
1359 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001360
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001361 do {
1362 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001363 if (error)
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001364 goto out_free;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001365 lf = (struct gfs2_leaf *)bh->b_data;
1366 lfn = be64_to_cpu(lf->lf_next);
1367 if (lf->lf_entries) {
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001368 entries2 += be16_to_cpu(lf->lf_entries);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001369 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1370 gfs2_dirent_gather, NULL, &g);
1371 error = PTR_ERR(dent);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001372 if (IS_ERR(dent))
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001373 goto out_free;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001374 if (entries2 != g.offset) {
akpm@linux-foundation.orgf391a4e2007-04-25 21:08:02 -07001375 fs_warn(sdp, "Number of entries corrupt in dir "
1376 "leaf %llu, entries2 (%u) != "
1377 "g.offset (%u)\n",
1378 (unsigned long long)bh->b_blocknr,
1379 entries2, g.offset);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001380
1381 error = -EIO;
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001382 goto out_free;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001383 }
1384 error = 0;
1385 larr[leaf++] = bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001386 } else {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001387 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001388 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001389 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001390
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001391 BUG_ON(entries2 != entries);
Al Virod81a8ef2013-05-16 14:14:48 -04001392 error = do_filldir_main(ip, ctx, darr, entries, copied);
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001393out_free:
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001394 for(i = 0; i < leaf; i++)
1395 brelse(larr[i]);
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001396 gfs2_free_sort_buffer(larr);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001397out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001398 return error;
1399}
1400
Steven Whitehouse79c4c372011-11-09 13:46:06 +00001401/**
1402 * gfs2_dir_readahead - Issue read-ahead requests for leaf blocks.
Bob Petersondfe4d342011-10-27 12:16:06 -04001403 *
1404 * Note: we can't calculate each index like dir_e_read can because we don't
1405 * have the leaf, and therefore we don't have the depth, and therefore we
1406 * don't have the length. So we have to just read enough ahead to make up
Steven Whitehouse79c4c372011-11-09 13:46:06 +00001407 * for the loss of information.
1408 */
Bob Petersondfe4d342011-10-27 12:16:06 -04001409static void gfs2_dir_readahead(struct inode *inode, unsigned hsize, u32 index,
1410 struct file_ra_state *f_ra)
1411{
1412 struct gfs2_inode *ip = GFS2_I(inode);
1413 struct gfs2_glock *gl = ip->i_gl;
1414 struct buffer_head *bh;
1415 u64 blocknr = 0, last;
1416 unsigned count;
1417
1418 /* First check if we've already read-ahead for the whole range. */
Steven Whitehouse79c4c372011-11-09 13:46:06 +00001419 if (index + MAX_RA_BLOCKS < f_ra->start)
Bob Petersondfe4d342011-10-27 12:16:06 -04001420 return;
1421
1422 f_ra->start = max((pgoff_t)index, f_ra->start);
1423 for (count = 0; count < MAX_RA_BLOCKS; count++) {
1424 if (f_ra->start >= hsize) /* if exceeded the hash table */
1425 break;
1426
1427 last = blocknr;
1428 blocknr = be64_to_cpu(ip->i_hash_cache[f_ra->start]);
1429 f_ra->start++;
1430 if (blocknr == last)
1431 continue;
1432
1433 bh = gfs2_getbuf(gl, blocknr, 1);
1434 if (trylock_buffer(bh)) {
1435 if (buffer_uptodate(bh)) {
1436 unlock_buffer(bh);
1437 brelse(bh);
1438 continue;
1439 }
1440 bh->b_end_io = end_buffer_read_sync;
1441 submit_bh(READA | REQ_META, bh);
1442 continue;
1443 }
1444 brelse(bh);
1445 }
1446}
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001447
David Teiglandb3b94fa2006-01-16 16:50:04 +00001448/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001449 * dir_e_read - Reads the entries from a directory into a filldir buffer
1450 * @dip: dinode pointer
Al Virod81a8ef2013-05-16 14:14:48 -04001451 * @ctx: actor to feed the entries to
David Teiglandb3b94fa2006-01-16 16:50:04 +00001452 *
1453 * Returns: errno
1454 */
1455
Al Virod81a8ef2013-05-16 14:14:48 -04001456static int dir_e_read(struct inode *inode, struct dir_context *ctx,
1457 struct file_ra_state *f_ra)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001458{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001459 struct gfs2_inode *dip = GFS2_I(inode);
Steven Whitehousecd915492006-09-04 12:49:07 -04001460 u32 hsize, len = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -04001461 u32 hash, index;
Al Virob44b84d2006-10-14 10:46:30 -04001462 __be64 *lp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001463 int copied = 0;
1464 int error = 0;
Steven Whitehouse4da3c642006-07-11 13:19:13 -04001465 unsigned depth = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001466
Steven Whitehouse9a004502008-02-01 09:23:44 +00001467 hsize = 1 << dip->i_depth;
Al Virod81a8ef2013-05-16 14:14:48 -04001468 hash = gfs2_dir_offset2hash(ctx->pos);
Steven Whitehouse9a004502008-02-01 09:23:44 +00001469 index = hash >> (32 - dip->i_depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001470
Steven Whitehouse79c4c372011-11-09 13:46:06 +00001471 if (dip->i_hash_cache == NULL)
Bob Petersondfe4d342011-10-27 12:16:06 -04001472 f_ra->start = 0;
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001473 lp = gfs2_dir_get_hash_table(dip);
1474 if (IS_ERR(lp))
1475 return PTR_ERR(lp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001476
Bob Petersondfe4d342011-10-27 12:16:06 -04001477 gfs2_dir_readahead(inode, hsize, index, f_ra);
1478
David Teiglandb3b94fa2006-01-16 16:50:04 +00001479 while (index < hsize) {
Al Virod81a8ef2013-05-16 14:14:48 -04001480 error = gfs2_dir_read_leaf(inode, ctx,
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001481 &copied, &depth,
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001482 be64_to_cpu(lp[index]));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001483 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001484 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001485
Steven Whitehouse9a004502008-02-01 09:23:44 +00001486 len = 1 << (dip->i_depth - depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001487 index = (index & ~(len - 1)) + len;
1488 }
1489
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001490 if (error > 0)
1491 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001492 return error;
1493}
1494
Al Virod81a8ef2013-05-16 14:14:48 -04001495int gfs2_dir_read(struct inode *inode, struct dir_context *ctx,
1496 struct file_ra_state *f_ra)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001497{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001498 struct gfs2_inode *dip = GFS2_I(inode);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001499 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001500 struct dirent_gather g;
1501 const struct gfs2_dirent **darr, *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001502 struct buffer_head *dibh;
1503 int copied = 0;
1504 int error;
1505
Steven Whitehousead6203f2008-11-03 13:59:19 +00001506 if (!dip->i_entries)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001507 return 0;
1508
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001509 if (dip->i_diskflags & GFS2_DIF_EXHASH)
Al Virod81a8ef2013-05-16 14:14:48 -04001510 return dir_e_read(inode, ctx, f_ra);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001511
David Teiglandb3b94fa2006-01-16 16:50:04 +00001512 if (!gfs2_is_stuffed(dip)) {
1513 gfs2_consist_inode(dip);
1514 return -EIO;
1515 }
1516
David Teiglandb3b94fa2006-01-16 16:50:04 +00001517 error = gfs2_meta_inode_buffer(dip, &dibh);
1518 if (error)
1519 return error;
1520
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001521 error = -ENOMEM;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001522 /* 96 is max number of dirents which can be stuffed into an inode */
Josef Bacik16c5f062008-04-09 09:33:41 -04001523 darr = kmalloc(96 * sizeof(struct gfs2_dirent *), GFP_NOFS);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001524 if (darr) {
1525 g.pdent = darr;
1526 g.offset = 0;
1527 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1528 gfs2_dirent_gather, NULL, &g);
1529 if (IS_ERR(dent)) {
1530 error = PTR_ERR(dent);
1531 goto out;
1532 }
Steven Whitehousead6203f2008-11-03 13:59:19 +00001533 if (dip->i_entries != g.offset) {
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001534 fs_warn(sdp, "Number of entries corrupt in dir %llu, "
Steven Whitehousead6203f2008-11-03 13:59:19 +00001535 "ip->i_entries (%u) != g.offset (%u)\n",
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001536 (unsigned long long)dip->i_no_addr,
Steven Whitehousead6203f2008-11-03 13:59:19 +00001537 dip->i_entries,
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001538 g.offset);
1539 error = -EIO;
1540 goto out;
1541 }
Al Virod81a8ef2013-05-16 14:14:48 -04001542 error = do_filldir_main(dip, ctx, darr,
Steven Whitehousead6203f2008-11-03 13:59:19 +00001543 dip->i_entries, &copied);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001544out:
1545 kfree(darr);
1546 }
1547
David Teiglandb3b94fa2006-01-16 16:50:04 +00001548 if (error > 0)
1549 error = 0;
1550
1551 brelse(dibh);
1552
1553 return error;
1554}
1555
David Teiglandb3b94fa2006-01-16 16:50:04 +00001556/**
1557 * gfs2_dir_search - Search a directory
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +01001558 * @dip: The GFS2 dir inode
1559 * @name: The name we are looking up
1560 * @fail_on_exist: Fail if the name exists rather than looking it up
David Teiglandb3b94fa2006-01-16 16:50:04 +00001561 *
1562 * This routine searches a directory for a file or another directory.
1563 * Assumes a glock is held on dip.
1564 *
1565 * Returns: errno
1566 */
1567
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +01001568struct inode *gfs2_dir_search(struct inode *dir, const struct qstr *name,
1569 bool fail_on_exist)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001570{
Steven Whitehousec7526662006-03-20 12:30:04 -05001571 struct buffer_head *bh;
1572 struct gfs2_dirent *dent;
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +01001573 u64 addr, formal_ino;
1574 u16 dtype;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001575
1576 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1577 if (dent) {
1578 if (IS_ERR(dent))
David Howellse231c2e2008-02-07 00:15:26 -08001579 return ERR_CAST(dent);
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +01001580 dtype = be16_to_cpu(dent->de_type);
1581 addr = be64_to_cpu(dent->de_inum.no_addr);
1582 formal_ino = be64_to_cpu(dent->de_inum.no_formal_ino);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001583 brelse(bh);
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +01001584 if (fail_on_exist)
1585 return ERR_PTR(-EEXIST);
1586 return gfs2_inode_lookup(dir->i_sb, dtype, addr, formal_ino, 0);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001587 }
1588 return ERR_PTR(-ENOENT);
1589}
1590
1591int gfs2_dir_check(struct inode *dir, const struct qstr *name,
1592 const struct gfs2_inode *ip)
1593{
1594 struct buffer_head *bh;
1595 struct gfs2_dirent *dent;
1596 int ret = -ENOENT;
Steven Whitehousec7526662006-03-20 12:30:04 -05001597
1598 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1599 if (dent) {
1600 if (IS_ERR(dent))
1601 return PTR_ERR(dent);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001602 if (ip) {
1603 if (be64_to_cpu(dent->de_inum.no_addr) != ip->i_no_addr)
1604 goto out;
1605 if (be64_to_cpu(dent->de_inum.no_formal_ino) !=
1606 ip->i_no_formal_ino)
1607 goto out;
1608 if (unlikely(IF2DT(ip->i_inode.i_mode) !=
1609 be16_to_cpu(dent->de_type))) {
1610 gfs2_consist_inode(GFS2_I(dir));
1611 ret = -EIO;
1612 goto out;
1613 }
1614 }
1615 ret = 0;
1616out:
Steven Whitehousec7526662006-03-20 12:30:04 -05001617 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001618 }
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001619 return ret;
Steven Whitehousec7526662006-03-20 12:30:04 -05001620}
1621
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001622/**
1623 * dir_new_leaf - Add a new leaf onto hash chain
1624 * @inode: The directory
1625 * @name: The name we are adding
1626 *
1627 * This adds a new dir leaf onto an existing leaf when there is not
1628 * enough space to add a new dir entry. This is a last resort after
1629 * we've expanded the hash table to max size and also split existing
1630 * leaf blocks, so it will only occur for very large directories.
1631 *
1632 * The dist parameter is set to 1 for leaf blocks directly attached
1633 * to the hash table, 2 for one layer of indirection, 3 for two layers
1634 * etc. We are thus able to tell the difference between an old leaf
1635 * with dist set to zero (i.e. "don't know") and a new one where we
1636 * set this information for debug/fsck purposes.
1637 *
1638 * Returns: 0 on success, or -ve on error
1639 */
1640
Steven Whitehousec7526662006-03-20 12:30:04 -05001641static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1642{
1643 struct buffer_head *bh, *obh;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001644 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001645 struct gfs2_leaf *leaf, *oleaf;
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001646 u32 dist = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001647 int error;
Steven Whitehousec7526662006-03-20 12:30:04 -05001648 u32 index;
1649 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001650
Steven Whitehouse9a004502008-02-01 09:23:44 +00001651 index = name->hash >> (32 - ip->i_depth);
Steven Whitehousec7526662006-03-20 12:30:04 -05001652 error = get_first_leaf(ip, index, &obh);
1653 if (error)
1654 return error;
1655 do {
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001656 dist++;
Steven Whitehousec7526662006-03-20 12:30:04 -05001657 oleaf = (struct gfs2_leaf *)obh->b_data;
1658 bn = be64_to_cpu(oleaf->lf_next);
1659 if (!bn)
1660 break;
1661 brelse(obh);
1662 error = get_leaf(ip, bn, &obh);
1663 if (error)
1664 return error;
1665 } while(1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001666
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001667 gfs2_trans_add_meta(ip->i_gl, obh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001668
1669 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1670 if (!leaf) {
1671 brelse(obh);
1672 return -ENOSPC;
1673 }
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001674 leaf->lf_dist = cpu_to_be32(dist);
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04001675 oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
Steven Whitehousec7526662006-03-20 12:30:04 -05001676 brelse(bh);
1677 brelse(obh);
1678
1679 error = gfs2_meta_inode_buffer(ip, &bh);
1680 if (error)
1681 return error;
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001682 gfs2_trans_add_meta(ip->i_gl, bh);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001683 gfs2_add_inode_blocks(&ip->i_inode, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001684 gfs2_dinode_out(ip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001685 brelse(bh);
1686 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001687}
1688
Steven Whitehouse44aaada2014-02-07 11:23:22 +00001689static u16 gfs2_inode_ra_len(const struct gfs2_inode *ip)
1690{
1691 u64 where = ip->i_no_addr + 1;
1692 if (ip->i_eattr == where)
1693 return 1;
1694 return 0;
1695}
1696
David Teiglandb3b94fa2006-01-16 16:50:04 +00001697/**
1698 * gfs2_dir_add - Add new filename into directory
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001699 * @inode: The directory inode
1700 * @name: The new name
1701 * @nip: The GFS2 inode to be linked in to the directory
1702 * @da: The directory addition info
1703 *
1704 * If the call to gfs2_diradd_alloc_required resulted in there being
1705 * no need to allocate any new directory blocks, then it will contain
1706 * a pointer to the directory entry and the bh in which it resides. We
1707 * can use that without having to repeat the search. If there was no
1708 * free space, then we must now create more space.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001709 *
1710 * Returns: 0 on success, error code on failure
1711 */
1712
Steven Whitehousec7526662006-03-20 12:30:04 -05001713int gfs2_dir_add(struct inode *inode, const struct qstr *name,
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001714 const struct gfs2_inode *nip, struct gfs2_diradd *da)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001715{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001716 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001717 struct buffer_head *bh = da->bh;
1718 struct gfs2_dirent *dent = da->dent;
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001719 struct timespec tv;
Steven Whitehousec7526662006-03-20 12:30:04 -05001720 struct gfs2_leaf *leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001721 int error;
1722
Steven Whitehousec7526662006-03-20 12:30:04 -05001723 while(1) {
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001724 if (da->bh == NULL) {
1725 dent = gfs2_dirent_search(inode, name,
1726 gfs2_dirent_find_space, &bh);
1727 }
Steven Whitehousec7526662006-03-20 12:30:04 -05001728 if (dent) {
1729 if (IS_ERR(dent))
1730 return PTR_ERR(dent);
1731 dent = gfs2_init_dirent(inode, dent, name, bh);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001732 gfs2_inum_out(nip, dent);
Steven Whitehouse3d6ecb72011-05-09 13:30:08 +01001733 dent->de_type = cpu_to_be16(IF2DT(nip->i_inode.i_mode));
Steven Whitehouse44aaada2014-02-07 11:23:22 +00001734 dent->de_rahead = cpu_to_be16(gfs2_inode_ra_len(nip));
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001735 tv = CURRENT_TIME;
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001736 if (ip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001737 leaf = (struct gfs2_leaf *)bh->b_data;
Marcin Slusarzbb16b342008-02-13 00:06:10 +01001738 be16_add_cpu(&leaf->lf_entries, 1);
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001739 leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
1740 leaf->lf_sec = cpu_to_be64(tv.tv_sec);
Steven Whitehousec7526662006-03-20 12:30:04 -05001741 }
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001742 da->dent = NULL;
1743 da->bh = NULL;
Steven Whitehousec7526662006-03-20 12:30:04 -05001744 brelse(bh);
Steven Whitehousead6203f2008-11-03 13:59:19 +00001745 ip->i_entries++;
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001746 ip->i_inode.i_mtime = ip->i_inode.i_ctime = tv;
Steven Whitehouse3d6ecb72011-05-09 13:30:08 +01001747 if (S_ISDIR(nip->i_inode.i_mode))
1748 inc_nlink(&ip->i_inode);
Bob Peterson343cd8f2012-11-12 13:04:54 -05001749 mark_inode_dirty(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001750 error = 0;
1751 break;
1752 }
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001753 if (!(ip->i_diskflags & GFS2_DIF_EXHASH)) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001754 error = dir_make_exhash(inode);
1755 if (error)
1756 break;
1757 continue;
1758 }
1759 error = dir_split_leaf(inode, name);
1760 if (error == 0)
1761 continue;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001762 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001763 break;
Steven Whitehouse9a004502008-02-01 09:23:44 +00001764 if (ip->i_depth < GFS2_DIR_MAX_DEPTH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001765 error = dir_double_exhash(ip);
1766 if (error)
1767 break;
1768 error = dir_split_leaf(inode, name);
Steven Whitehousee90deff2006-03-29 19:02:15 -05001769 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001770 break;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001771 if (error == 0)
1772 continue;
Steven Whitehousec7526662006-03-20 12:30:04 -05001773 }
1774 error = dir_new_leaf(inode, name);
1775 if (!error)
1776 continue;
1777 error = -ENOSPC;
1778 break;
1779 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001780 return error;
1781}
1782
Steven Whitehousec7526662006-03-20 12:30:04 -05001783
David Teiglandb3b94fa2006-01-16 16:50:04 +00001784/**
1785 * gfs2_dir_del - Delete a directory entry
1786 * @dip: The GFS2 inode
1787 * @filename: The filename
1788 *
1789 * Returns: 0 on success, error code on failure
1790 */
1791
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001792int gfs2_dir_del(struct gfs2_inode *dip, const struct dentry *dentry)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001793{
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001794 const struct qstr *name = &dentry->d_name;
Steven Whitehousec7526662006-03-20 12:30:04 -05001795 struct gfs2_dirent *dent, *prev = NULL;
1796 struct buffer_head *bh;
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001797 struct timespec tv = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001798
Steven Whitehousec7526662006-03-20 12:30:04 -05001799 /* Returns _either_ the entry (if its first in block) or the
1800 previous entry otherwise */
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001801 dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001802 if (!dent) {
1803 gfs2_consist_inode(dip);
1804 return -EIO;
1805 }
1806 if (IS_ERR(dent)) {
1807 gfs2_consist_inode(dip);
1808 return PTR_ERR(dent);
1809 }
1810 /* If not first in block, adjust pointers accordingly */
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001811 if (gfs2_dirent_find(dent, name, NULL) == 0) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001812 prev = dent;
1813 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1814 }
1815
1816 dirent_del(dip, bh, prev, dent);
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001817 if (dip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001818 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1819 u16 entries = be16_to_cpu(leaf->lf_entries);
1820 if (!entries)
1821 gfs2_consist_inode(dip);
1822 leaf->lf_entries = cpu_to_be16(--entries);
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001823 leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
1824 leaf->lf_sec = cpu_to_be64(tv.tv_sec);
Steven Whitehousec7526662006-03-20 12:30:04 -05001825 }
Steven Whitehouseed386502006-04-07 16:28:07 -04001826 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001827
Steven Whitehousead6203f2008-11-03 13:59:19 +00001828 if (!dip->i_entries)
Steven Whitehousec7526662006-03-20 12:30:04 -05001829 gfs2_consist_inode(dip);
Steven Whitehousead6203f2008-11-03 13:59:19 +00001830 dip->i_entries--;
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001831 dip->i_inode.i_mtime = dip->i_inode.i_ctime = tv;
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001832 if (S_ISDIR(dentry->d_inode->i_mode))
1833 drop_nlink(&dip->i_inode);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001834 mark_inode_dirty(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001835
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001836 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001837}
1838
David Teiglandb3b94fa2006-01-16 16:50:04 +00001839/**
1840 * gfs2_dir_mvino - Change inode number of directory entry
1841 * @dip: The GFS2 inode
1842 * @filename:
1843 * @new_inode:
1844 *
1845 * This routine changes the inode number of a directory entry. It's used
1846 * by rename to change ".." when a directory is moved.
1847 * Assumes a glock is held on dvp.
1848 *
1849 * Returns: errno
1850 */
1851
Steven Whitehousec7526662006-03-20 12:30:04 -05001852int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001853 const struct gfs2_inode *nip, unsigned int new_type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001854{
Steven Whitehousec7526662006-03-20 12:30:04 -05001855 struct buffer_head *bh;
1856 struct gfs2_dirent *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001857 int error;
1858
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001859 dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001860 if (!dent) {
1861 gfs2_consist_inode(dip);
1862 return -EIO;
1863 }
1864 if (IS_ERR(dent))
1865 return PTR_ERR(dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001866
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001867 gfs2_trans_add_meta(dip->i_gl, bh);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001868 gfs2_inum_out(nip, dent);
Steven Whitehousec7526662006-03-20 12:30:04 -05001869 dent->de_type = cpu_to_be16(new_type);
1870
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001871 if (dip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001872 brelse(bh);
1873 error = gfs2_meta_inode_buffer(dip, &bh);
1874 if (error)
1875 return error;
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001876 gfs2_trans_add_meta(dip->i_gl, bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001877 }
1878
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001879 dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001880 gfs2_dinode_out(dip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001881 brelse(bh);
1882 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001883}
1884
1885/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001886 * leaf_dealloc - Deallocate a directory leaf
1887 * @dip: the directory
1888 * @index: the hash table offset in the directory
1889 * @len: the number of pointers to this leaf
1890 * @leaf_no: the leaf number
Bob Petersonec038c82011-03-22 13:55:23 -04001891 * @leaf_bh: buffer_head for the starting leaf
Bob Petersond24a7a42011-03-22 13:54:03 -04001892 * last_dealloc: 1 if this is the final dealloc for the leaf, else 0
David Teiglandb3b94fa2006-01-16 16:50:04 +00001893 *
1894 * Returns: errno
1895 */
1896
Steven Whitehousecd915492006-09-04 12:49:07 -04001897static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
Bob Petersonec038c82011-03-22 13:55:23 -04001898 u64 leaf_no, struct buffer_head *leaf_bh,
1899 int last_dealloc)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001900{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001901 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001902 struct gfs2_leaf *tmp_leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001903 struct gfs2_rgrp_list rlist;
1904 struct buffer_head *bh, *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001905 u64 blk, nblk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001906 unsigned int rg_blocks = 0, l_blocks = 0;
1907 char *ht;
Steven Whitehousecd915492006-09-04 12:49:07 -04001908 unsigned int x, size = len * sizeof(u64);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001909 int error;
1910
Bob Peterson5e2f7d62012-04-04 22:11:16 -04001911 error = gfs2_rindex_update(sdp);
1912 if (error)
1913 return error;
1914
David Teiglandb3b94fa2006-01-16 16:50:04 +00001915 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1916
Joe Perches8be04b92013-06-19 12:15:53 -07001917 ht = kzalloc(size, GFP_NOFS | __GFP_NOWARN);
Bob Petersone8830d82013-05-30 09:48:56 -04001918 if (ht == NULL)
1919 ht = vzalloc(size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001920 if (!ht)
1921 return -ENOMEM;
1922
Eric W. Biedermanf4108a62013-01-31 17:49:26 -08001923 error = gfs2_quota_hold(dip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001924 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -04001925 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001926
David Teiglandb3b94fa2006-01-16 16:50:04 +00001927 /* Count the number of leaves */
Bob Petersonec038c82011-03-22 13:55:23 -04001928 bh = leaf_bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001929
Steven Whitehousec7526662006-03-20 12:30:04 -05001930 for (blk = leaf_no; blk; blk = nblk) {
Bob Petersonec038c82011-03-22 13:55:23 -04001931 if (blk != leaf_no) {
1932 error = get_leaf(dip, blk, &bh);
1933 if (error)
1934 goto out_rlist;
1935 }
Steven Whitehousec7526662006-03-20 12:30:04 -05001936 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1937 nblk = be64_to_cpu(tmp_leaf->lf_next);
Bob Petersonec038c82011-03-22 13:55:23 -04001938 if (blk != leaf_no)
1939 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001940
Steven Whitehouse70b0c362011-09-02 16:08:09 +01001941 gfs2_rlist_add(dip, &rlist, blk);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001942 l_blocks++;
1943 }
1944
Bob Petersonfe6c9912008-01-28 11:13:02 -06001945 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001946
1947 for (x = 0; x < rlist.rl_rgrps; x++) {
1948 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001949 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001950 rg_blocks += rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001951 }
1952
1953 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1954 if (error)
1955 goto out_rlist;
1956
1957 error = gfs2_trans_begin(sdp,
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001958 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
David Teiglandb3b94fa2006-01-16 16:50:04 +00001959 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1960 if (error)
1961 goto out_rg_gunlock;
1962
Bob Petersonec038c82011-03-22 13:55:23 -04001963 bh = leaf_bh;
1964
Steven Whitehousec7526662006-03-20 12:30:04 -05001965 for (blk = leaf_no; blk; blk = nblk) {
Bob Petersonec038c82011-03-22 13:55:23 -04001966 if (blk != leaf_no) {
1967 error = get_leaf(dip, blk, &bh);
1968 if (error)
1969 goto out_end_trans;
1970 }
Steven Whitehousec7526662006-03-20 12:30:04 -05001971 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1972 nblk = be64_to_cpu(tmp_leaf->lf_next);
Bob Petersonec038c82011-03-22 13:55:23 -04001973 if (blk != leaf_no)
1974 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001975
1976 gfs2_free_meta(dip, blk, 1);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001977 gfs2_add_inode_blocks(&dip->i_inode, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001978 }
1979
Steven Whitehousecd915492006-09-04 12:49:07 -04001980 error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001981 if (error != size) {
1982 if (error >= 0)
1983 error = -EIO;
1984 goto out_end_trans;
1985 }
1986
1987 error = gfs2_meta_inode_buffer(dip, &dibh);
1988 if (error)
1989 goto out_end_trans;
1990
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001991 gfs2_trans_add_meta(dip->i_gl, dibh);
Bob Petersond24a7a42011-03-22 13:54:03 -04001992 /* On the last dealloc, make this a regular file in case we crash.
1993 (We don't want to free these blocks a second time.) */
1994 if (last_dealloc)
1995 dip->i_inode.i_mode = S_IFREG;
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001996 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001997 brelse(dibh);
1998
Steven Whitehousea91ea692006-09-04 12:04:26 -04001999out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00002000 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04002001out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +00002002 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -04002003out_rlist:
David Teiglandb3b94fa2006-01-16 16:50:04 +00002004 gfs2_rlist_free(&rlist);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002005 gfs2_quota_unhold(dip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03002006out:
Bob Petersone8830d82013-05-30 09:48:56 -04002007 if (is_vmalloc_addr(ht))
2008 vfree(ht);
2009 else
2010 kfree(ht);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002011 return error;
2012}
2013
2014/**
2015 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
2016 * @dip: the directory
2017 *
2018 * Dealloc all on-disk directory leaves to FREEMETA state
2019 * Change on-disk inode type to "regular file"
2020 *
2021 * Returns: errno
2022 */
2023
2024int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
2025{
Bob Peterson556bb172011-03-22 13:56:37 -04002026 struct buffer_head *bh;
2027 struct gfs2_leaf *leaf;
2028 u32 hsize, len;
Bob Peterson556bb172011-03-22 13:56:37 -04002029 u32 index = 0, next_index;
2030 __be64 *lp;
2031 u64 leaf_no;
2032 int error = 0, last;
2033
2034 hsize = 1 << dip->i_depth;
Bob Peterson556bb172011-03-22 13:56:37 -04002035
Steven Whitehouse17d539f2011-06-15 10:29:37 +01002036 lp = gfs2_dir_get_hash_table(dip);
2037 if (IS_ERR(lp))
2038 return PTR_ERR(lp);
Bob Peterson556bb172011-03-22 13:56:37 -04002039
2040 while (index < hsize) {
Steven Whitehouse17d539f2011-06-15 10:29:37 +01002041 leaf_no = be64_to_cpu(lp[index]);
Bob Peterson556bb172011-03-22 13:56:37 -04002042 if (leaf_no) {
2043 error = get_leaf(dip, leaf_no, &bh);
2044 if (error)
2045 goto out;
2046 leaf = (struct gfs2_leaf *)bh->b_data;
2047 len = 1 << (dip->i_depth - be16_to_cpu(leaf->lf_depth));
2048
2049 next_index = (index & ~(len - 1)) + len;
2050 last = ((next_index >= hsize) ? 1 : 0);
2051 error = leaf_dealloc(dip, index, len, leaf_no, bh,
2052 last);
2053 brelse(bh);
2054 if (error)
2055 goto out;
2056 index = next_index;
2057 } else
2058 index++;
2059 }
2060
2061 if (index != hsize) {
2062 gfs2_consist_inode(dip);
2063 error = -EIO;
2064 }
2065
2066out:
Bob Peterson556bb172011-03-22 13:56:37 -04002067
2068 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002069}
2070
2071/**
2072 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
2073 * @ip: the file being written to
2074 * @filname: the filename that's going to be added
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00002075 * @da: The structure to return dir alloc info
David Teiglandb3b94fa2006-01-16 16:50:04 +00002076 *
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00002077 * Returns: 0 if ok, -ve on error
David Teiglandb3b94fa2006-01-16 16:50:04 +00002078 */
2079
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00002080int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name,
2081 struct gfs2_diradd *da)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002082{
Steven Whitehouse22b5a6c2014-01-08 11:05:29 +00002083 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00002084 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse22b5a6c2014-01-08 11:05:29 +00002085 const unsigned int extra = sizeof(struct gfs2_dinode) - sizeof(struct gfs2_leaf);
Steven Whitehousec7526662006-03-20 12:30:04 -05002086 struct gfs2_dirent *dent;
2087 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002088
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00002089 da->nr_blocks = 0;
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00002090 da->bh = NULL;
2091 da->dent = NULL;
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00002092
Steven Whitehousec7526662006-03-20 12:30:04 -05002093 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
Steven Whitehouseed386502006-04-07 16:28:07 -04002094 if (!dent) {
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00002095 da->nr_blocks = sdp->sd_max_dirres;
Steven Whitehouse22b5a6c2014-01-08 11:05:29 +00002096 if (!(ip->i_diskflags & GFS2_DIF_EXHASH) &&
2097 (GFS2_DIRENT_SIZE(name->len) < extra))
2098 da->nr_blocks = 1;
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00002099 return 0;
Steven Whitehouseed386502006-04-07 16:28:07 -04002100 }
Steven Whitehousec7526662006-03-20 12:30:04 -05002101 if (IS_ERR(dent))
2102 return PTR_ERR(dent);
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00002103 da->bh = bh;
2104 da->dent = dent;
Steven Whitehousec7526662006-03-20 12:30:04 -05002105 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002106}
2107