blob: c2486598fb8767e585a6474e739c2767927245df [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
Andreas Gruenbacherc8d57702015-11-11 15:00:35 -0600111 error = gfs2_meta_read(ip->i_gl, block, DIO_WAIT, 0, &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 {
Andreas Gruenbacherc8d57702015-11-11 15:00:35 -0600308 error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, 0, &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) {
Al Viro3cdcf632014-11-20 05:18:38 +0000368 kvfree(hc);
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100369 return ERR_PTR(ret);
370 }
371
372 spin_lock(&inode->i_lock);
Al Viro9265f1d2014-11-20 05:19:47 +0000373 if (likely(!ip->i_hash_cache)) {
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100374 ip->i_hash_cache = hc;
Al Viro9265f1d2014-11-20 05:19:47 +0000375 hc = NULL;
376 }
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100377 spin_unlock(&inode->i_lock);
Al Viro9265f1d2014-11-20 05:19:47 +0000378 kvfree(hc);
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100379
380 return ip->i_hash_cache;
381}
382
383/**
384 * gfs2_dir_hash_inval - Invalidate dir hash
385 * @ip: The directory inode
386 *
387 * Must be called with an exclusive glock, or during glock invalidation.
388 */
389void gfs2_dir_hash_inval(struct gfs2_inode *ip)
390{
Bob Petersonc36b97e2015-10-29 10:03:41 -0500391 __be64 *hc;
392
393 spin_lock(&ip->i_inode.i_lock);
394 hc = ip->i_hash_cache;
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100395 ip->i_hash_cache = NULL;
Bob Petersonc36b97e2015-10-29 10:03:41 -0500396 spin_unlock(&ip->i_inode.i_lock);
397
Al Viro3cdcf632014-11-20 05:18:38 +0000398 kvfree(hc);
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100399}
400
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500401static inline int gfs2_dirent_sentinel(const struct gfs2_dirent *dent)
402{
403 return dent->de_inum.no_addr == 0 || dent->de_inum.no_formal_ino == 0;
404}
405
Steven Whitehousec7526662006-03-20 12:30:04 -0500406static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
407 const struct qstr *name, int ret)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000408{
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500409 if (!gfs2_dirent_sentinel(dent) &&
Steven Whitehousec7526662006-03-20 12:30:04 -0500410 be32_to_cpu(dent->de_hash) == name->hash &&
411 be16_to_cpu(dent->de_name_len) == name->len &&
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400412 memcmp(dent+1, name->name, name->len) == 0)
Steven Whitehousec7526662006-03-20 12:30:04 -0500413 return ret;
414 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000415}
416
Steven Whitehousec7526662006-03-20 12:30:04 -0500417static int gfs2_dirent_find(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500418 const struct qstr *name,
419 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500420{
421 return __gfs2_dirent_find(dent, name, 1);
422}
423
424static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500425 const struct qstr *name,
426 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500427{
428 return __gfs2_dirent_find(dent, name, 2);
429}
430
431/*
432 * name->name holds ptr to start of block.
433 * name->len holds size of block.
434 */
435static int gfs2_dirent_last(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500436 const struct qstr *name,
437 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500438{
439 const char *start = name->name;
440 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
441 if (name->len == (end - start))
442 return 1;
443 return 0;
444}
445
446static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500447 const struct qstr *name,
448 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500449{
450 unsigned required = GFS2_DIRENT_SIZE(name->len);
451 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
452 unsigned totlen = be16_to_cpu(dent->de_rec_len);
453
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500454 if (gfs2_dirent_sentinel(dent))
Bob Peterson728a7562010-07-14 18:12:26 -0400455 actual = 0;
Jan Engelhardtc53921242006-09-05 14:30:40 +0200456 if (totlen - actual >= required)
Steven Whitehousec7526662006-03-20 12:30:04 -0500457 return 1;
458 return 0;
459}
460
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500461struct dirent_gather {
462 const struct gfs2_dirent **pdent;
463 unsigned offset;
464};
465
466static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
467 const struct qstr *name,
468 void *opaque)
469{
470 struct dirent_gather *g = opaque;
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500471 if (!gfs2_dirent_sentinel(dent)) {
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500472 g->pdent[g->offset++] = dent;
473 }
474 return 0;
475}
476
Steven Whitehousec7526662006-03-20 12:30:04 -0500477/*
478 * Other possible things to check:
479 * - Inode located within filesystem size (and on valid block)
480 * - Valid directory entry type
481 * Not sure how heavy-weight we want to make this... could also check
482 * hash is correct for example, but that would take a lot of extra time.
483 * For now the most important thing is to check that the various sizes
484 * are correct.
485 */
486static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
487 unsigned int size, unsigned int len, int first)
488{
489 const char *msg = "gfs2_dirent too small";
490 if (unlikely(size < sizeof(struct gfs2_dirent)))
491 goto error;
492 msg = "gfs2_dirent misaligned";
493 if (unlikely(offset & 0x7))
494 goto error;
495 msg = "gfs2_dirent points beyond end of block";
496 if (unlikely(offset + size > len))
497 goto error;
498 msg = "zero inode number";
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500499 if (unlikely(!first && gfs2_dirent_sentinel(dent)))
Steven Whitehousec7526662006-03-20 12:30:04 -0500500 goto error;
501 msg = "name length is greater than space in dirent";
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500502 if (!gfs2_dirent_sentinel(dent) &&
Steven Whitehousec7526662006-03-20 12:30:04 -0500503 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
504 size))
505 goto error;
506 return 0;
507error:
Joe Perchesd77d1b52014-03-06 12:10:45 -0800508 pr_warn("%s: %s (%s)\n",
509 __func__, msg, first ? "first in block" : "not first in block");
Steven Whitehousec7526662006-03-20 12:30:04 -0500510 return -EIO;
511}
512
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500513static int gfs2_dirent_offset(const void *buf)
Steven Whitehousec7526662006-03-20 12:30:04 -0500514{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500515 const struct gfs2_meta_header *h = buf;
516 int offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500517
518 BUG_ON(buf == NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500519
Steven Whitehousee3167de2006-03-30 15:46:23 -0500520 switch(be32_to_cpu(h->mh_type)) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500521 case GFS2_METATYPE_LF:
522 offset = sizeof(struct gfs2_leaf);
523 break;
524 case GFS2_METATYPE_DI:
525 offset = sizeof(struct gfs2_dinode);
526 break;
527 default:
528 goto wrong_type;
529 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500530 return offset;
531wrong_type:
Joe Perchesd77d1b52014-03-06 12:10:45 -0800532 pr_warn("%s: wrong block type %u\n", __func__, be32_to_cpu(h->mh_type));
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500533 return -1;
534}
Steven Whitehousec7526662006-03-20 12:30:04 -0500535
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400536static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500537 unsigned int len, gfs2_dscan_t scan,
538 const struct qstr *name,
539 void *opaque)
540{
541 struct gfs2_dirent *dent, *prev;
542 unsigned offset;
543 unsigned size;
544 int ret = 0;
545
546 ret = gfs2_dirent_offset(buf);
547 if (ret < 0)
548 goto consist_inode;
549
550 offset = ret;
Steven Whitehousec7526662006-03-20 12:30:04 -0500551 prev = NULL;
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400552 dent = buf + offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500553 size = be16_to_cpu(dent->de_rec_len);
554 if (gfs2_check_dirent(dent, offset, size, len, 1))
555 goto consist_inode;
556 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500557 ret = scan(dent, name, opaque);
Steven Whitehousec7526662006-03-20 12:30:04 -0500558 if (ret)
559 break;
560 offset += size;
561 if (offset == len)
562 break;
563 prev = dent;
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400564 dent = buf + offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500565 size = be16_to_cpu(dent->de_rec_len);
566 if (gfs2_check_dirent(dent, offset, size, len, 0))
567 goto consist_inode;
568 } while(1);
569
570 switch(ret) {
571 case 0:
572 return NULL;
573 case 1:
574 return dent;
575 case 2:
576 return prev ? prev : dent;
577 default:
578 BUG_ON(ret > 0);
579 return ERR_PTR(ret);
580 }
581
Steven Whitehousec7526662006-03-20 12:30:04 -0500582consist_inode:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400583 gfs2_consist_inode(GFS2_I(inode));
Steven Whitehousec7526662006-03-20 12:30:04 -0500584 return ERR_PTR(-EIO);
585}
586
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400587static int dirent_check_reclen(struct gfs2_inode *dip,
588 const struct gfs2_dirent *d, const void *end_p)
589{
590 const void *ptr = d;
591 u16 rec_len = be16_to_cpu(d->de_rec_len);
592
593 if (unlikely(rec_len < sizeof(struct gfs2_dirent)))
594 goto broken;
595 ptr += rec_len;
596 if (ptr < end_p)
597 return rec_len;
598 if (ptr == end_p)
599 return -ENOENT;
600broken:
601 gfs2_consist_inode(dip);
602 return -EIO;
603}
604
David Teiglandb3b94fa2006-01-16 16:50:04 +0000605/**
606 * dirent_next - Next dirent
607 * @dip: the directory
608 * @bh: The buffer
609 * @dent: Pointer to list of dirents
610 *
611 * Returns: 0 on success, error code otherwise
612 */
613
614static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
615 struct gfs2_dirent **dent)
616{
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400617 struct gfs2_dirent *cur = *dent, *tmp;
618 char *bh_end = bh->b_data + bh->b_size;
619 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000620
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400621 ret = dirent_check_reclen(dip, cur, bh_end);
622 if (ret < 0)
623 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000624
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400625 tmp = (void *)cur + ret;
626 ret = dirent_check_reclen(dip, tmp, bh_end);
627 if (ret == -EIO)
628 return ret;
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000629
David Teiglandb3b94fa2006-01-16 16:50:04 +0000630 /* Only the first dent could ever have de_inum.no_addr == 0 */
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500631 if (gfs2_dirent_sentinel(tmp)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000632 gfs2_consist_inode(dip);
633 return -EIO;
634 }
635
636 *dent = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000637 return 0;
638}
639
640/**
641 * dirent_del - Delete a dirent
642 * @dip: The GFS2 inode
643 * @bh: The buffer
644 * @prev: The previous dirent
645 * @cur: The current dirent
646 *
647 */
648
649static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
650 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
651{
Steven Whitehousecd915492006-09-04 12:49:07 -0400652 u16 cur_rec_len, prev_rec_len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000653
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500654 if (gfs2_dirent_sentinel(cur)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000655 gfs2_consist_inode(dip);
656 return;
657 }
658
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000659 gfs2_trans_add_meta(dip->i_gl, bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000660
661 /* If there is no prev entry, this is the first entry in the block.
662 The de_rec_len is already as big as it needs to be. Just zero
663 out the inode number and return. */
664
665 if (!prev) {
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500666 cur->de_inum.no_addr = 0;
667 cur->de_inum.no_formal_ino = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000668 return;
669 }
670
671 /* Combine this dentry with the previous one. */
672
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000673 prev_rec_len = be16_to_cpu(prev->de_rec_len);
674 cur_rec_len = be16_to_cpu(cur->de_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000675
676 if ((char *)prev + prev_rec_len != (char *)cur)
677 gfs2_consist_inode(dip);
678 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
679 gfs2_consist_inode(dip);
680
681 prev_rec_len += cur_rec_len;
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000682 prev->de_rec_len = cpu_to_be16(prev_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000683}
684
Steven Whitehousec7526662006-03-20 12:30:04 -0500685/*
686 * Takes a dent from which to grab space as an argument. Returns the
687 * newly created dent.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000688 */
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400689static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
690 struct gfs2_dirent *dent,
691 const struct qstr *name,
692 struct buffer_head *bh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000693{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400694 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -0500695 struct gfs2_dirent *ndent;
696 unsigned offset = 0, totlen;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000697
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500698 if (!gfs2_dirent_sentinel(dent))
Steven Whitehousec7526662006-03-20 12:30:04 -0500699 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
700 totlen = be16_to_cpu(dent->de_rec_len);
701 BUG_ON(offset + name->len > totlen);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000702 gfs2_trans_add_meta(ip->i_gl, bh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500703 ndent = (struct gfs2_dirent *)((char *)dent + offset);
704 dent->de_rec_len = cpu_to_be16(offset);
705 gfs2_qstr2dirent(name, totlen - offset, ndent);
706 return ndent;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000707}
708
Steven Whitehousec7526662006-03-20 12:30:04 -0500709static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
710 struct buffer_head *bh,
711 const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000712{
713 struct gfs2_dirent *dent;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400714 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500715 gfs2_dirent_find_space, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500716 if (!dent || IS_ERR(dent))
717 return dent;
718 return gfs2_init_dirent(inode, dent, name, bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000719}
720
Steven Whitehousecd915492006-09-04 12:49:07 -0400721static int get_leaf(struct gfs2_inode *dip, u64 leaf_no,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000722 struct buffer_head **bhp)
723{
724 int error;
725
Andreas Gruenbacherc8d57702015-11-11 15:00:35 -0600726 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, 0, bhp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400727 if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
Joe Perchesd77d1b52014-03-06 12:10:45 -0800728 /* pr_info("block num=%llu\n", leaf_no); */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000729 error = -EIO;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400730 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000731
732 return error;
733}
734
735/**
736 * get_leaf_nr - Get a leaf number associated with the index
737 * @dip: The GFS2 inode
738 * @index:
739 * @leaf_out:
740 *
741 * Returns: 0 on success, error code otherwise
742 */
743
Steven Whitehousecd915492006-09-04 12:49:07 -0400744static int get_leaf_nr(struct gfs2_inode *dip, u32 index,
745 u64 *leaf_out)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000746{
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100747 __be64 *hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000748
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100749 hash = gfs2_dir_get_hash_table(dip);
750 if (IS_ERR(hash))
751 return PTR_ERR(hash);
752 *leaf_out = be64_to_cpu(*(hash + index));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000753 return 0;
754}
755
Steven Whitehousecd915492006-09-04 12:49:07 -0400756static int get_first_leaf(struct gfs2_inode *dip, u32 index,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000757 struct buffer_head **bh_out)
758{
Steven Whitehousecd915492006-09-04 12:49:07 -0400759 u64 leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000760 int error;
761
762 error = get_leaf_nr(dip, index, &leaf_no);
763 if (!error)
764 error = get_leaf(dip, leaf_no, bh_out);
765
766 return error;
767}
768
Steven Whitehousec7526662006-03-20 12:30:04 -0500769static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
770 const struct qstr *name,
771 gfs2_dscan_t scan,
772 struct buffer_head **pbh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000773{
Steven Whitehousec7526662006-03-20 12:30:04 -0500774 struct buffer_head *bh;
775 struct gfs2_dirent *dent;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400776 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000777 int error;
778
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000779 if (ip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500780 struct gfs2_leaf *leaf;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000781 unsigned hsize = 1 << ip->i_depth;
Steven Whitehousec7526662006-03-20 12:30:04 -0500782 unsigned index;
783 u64 ln;
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100784 if (hsize * sizeof(u64) != i_size_read(inode)) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500785 gfs2_consist_inode(ip);
786 return ERR_PTR(-EIO);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000787 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400788
Steven Whitehouse9a004502008-02-01 09:23:44 +0000789 index = name->hash >> (32 - ip->i_depth);
Steven Whitehousec7526662006-03-20 12:30:04 -0500790 error = get_first_leaf(ip, index, &bh);
791 if (error)
792 return ERR_PTR(error);
793 do {
794 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500795 scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500796 if (dent)
797 goto got_dent;
798 leaf = (struct gfs2_leaf *)bh->b_data;
799 ln = be64_to_cpu(leaf->lf_next);
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400800 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500801 if (!ln)
802 break;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400803
Steven Whitehousec7526662006-03-20 12:30:04 -0500804 error = get_leaf(ip, ln, &bh);
805 } while(!error);
806
807 return error ? ERR_PTR(error) : NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000808 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000809
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400810
Steven Whitehousec7526662006-03-20 12:30:04 -0500811 error = gfs2_meta_inode_buffer(ip, &bh);
812 if (error)
813 return ERR_PTR(error);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500814 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500815got_dent:
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400816 if (unlikely(dent == NULL || IS_ERR(dent))) {
Steven Whitehouseed386502006-04-07 16:28:07 -0400817 brelse(bh);
818 bh = NULL;
819 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500820 *pbh = bh;
821 return dent;
822}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000823
Steven Whitehousec7526662006-03-20 12:30:04 -0500824static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
825{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400826 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000827 unsigned int n = 1;
Steven Whitehouse09010972009-05-20 10:48:47 +0100828 u64 bn;
829 int error;
830 struct buffer_head *bh;
Steven Whitehousec7526662006-03-20 12:30:04 -0500831 struct gfs2_leaf *leaf;
832 struct gfs2_dirent *dent;
Linus Torvalds26fe5752012-05-10 13:14:12 -0700833 struct qstr name = { .name = "" };
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +0000834 struct timespec tv = CURRENT_TIME;
Steven Whitehouse09010972009-05-20 10:48:47 +0100835
Bob Peterson6e87ed02011-11-18 10:58:32 -0500836 error = gfs2_alloc_blocks(ip, &bn, &n, 0, NULL);
Steven Whitehouse09010972009-05-20 10:48:47 +0100837 if (error)
838 return NULL;
839 bh = gfs2_meta_new(ip->i_gl, bn);
Steven Whitehousec7526662006-03-20 12:30:04 -0500840 if (!bh)
841 return NULL;
Steven Whitehouse09010972009-05-20 10:48:47 +0100842
Steven Whitehouse5731be52008-02-01 13:16:55 +0000843 gfs2_trans_add_unrevoke(GFS2_SB(inode), bn, 1);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000844 gfs2_trans_add_meta(ip->i_gl, bh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500845 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
846 leaf = (struct gfs2_leaf *)bh->b_data;
847 leaf->lf_depth = cpu_to_be16(depth);
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400848 leaf->lf_entries = 0;
Al Viroa2d7d022006-10-14 16:49:30 +0100849 leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400850 leaf->lf_next = 0;
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +0000851 leaf->lf_inode = cpu_to_be64(ip->i_no_addr);
852 leaf->lf_dist = cpu_to_be32(1);
853 leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
854 leaf->lf_sec = cpu_to_be64(tv.tv_sec);
855 memset(leaf->lf_reserved2, 0, sizeof(leaf->lf_reserved2));
Steven Whitehousec7526662006-03-20 12:30:04 -0500856 dent = (struct gfs2_dirent *)(leaf+1);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500857 gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
Steven Whitehousec7526662006-03-20 12:30:04 -0500858 *pbh = bh;
859 return leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000860}
861
862/**
863 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
864 * @dip: The GFS2 inode
865 *
866 * Returns: 0 on success, error code otherwise
867 */
868
Steven Whitehousec7526662006-03-20 12:30:04 -0500869static int dir_make_exhash(struct inode *inode)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000870{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400871 struct gfs2_inode *dip = GFS2_I(inode);
872 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000873 struct gfs2_dirent *dent;
Steven Whitehousec7526662006-03-20 12:30:04 -0500874 struct qstr args;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000875 struct buffer_head *bh, *dibh;
876 struct gfs2_leaf *leaf;
877 int y;
Steven Whitehousecd915492006-09-04 12:49:07 -0400878 u32 x;
Al Virob44b84d2006-10-14 10:46:30 -0400879 __be64 *lp;
880 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000881 int error;
882
883 error = gfs2_meta_inode_buffer(dip, &dibh);
884 if (error)
885 return error;
886
David Teiglandb3b94fa2006-01-16 16:50:04 +0000887 /* Turn over a new leaf */
888
Steven Whitehousec7526662006-03-20 12:30:04 -0500889 leaf = new_leaf(inode, &bh, 0);
890 if (!leaf)
891 return -ENOSPC;
892 bn = bh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000893
Steven Whitehousead6203f22008-11-03 13:59:19 +0000894 gfs2_assert(sdp, dip->i_entries < (1 << 16));
895 leaf->lf_entries = cpu_to_be16(dip->i_entries);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000896
897 /* Copy dirents */
898
899 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
900 sizeof(struct gfs2_dinode));
901
902 /* Find last entry */
903
904 x = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500905 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
906 sizeof(struct gfs2_leaf);
907 args.name = bh->b_data;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400908 dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500909 gfs2_dirent_last, &args, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500910 if (!dent) {
911 brelse(bh);
912 brelse(dibh);
913 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000914 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500915 if (IS_ERR(dent)) {
916 brelse(bh);
917 brelse(dibh);
918 return PTR_ERR(dent);
919 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000920
921 /* Adjust the last dirent's record length
922 (Remember that dent still points to the last entry.) */
923
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000924 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000925 sizeof(struct gfs2_dinode) -
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000926 sizeof(struct gfs2_leaf));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000927
928 brelse(bh);
929
930 /* We're done with the new leaf block, now setup the new
931 hash table. */
932
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000933 gfs2_trans_add_meta(dip->i_gl, dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000934 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
935
Al Virob44b84d2006-10-14 10:46:30 -0400936 lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000937
938 for (x = sdp->sd_hash_ptrs; x--; lp++)
939 *lp = cpu_to_be64(bn);
940
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100941 i_size_write(inode, sdp->sd_sb.sb_bsize / 2);
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000942 gfs2_add_inode_blocks(&dip->i_inode, 1);
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000943 dip->i_diskflags |= GFS2_DIF_EXHASH;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000944
945 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000946 dip->i_depth = y;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000947
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500948 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000949
950 brelse(dibh);
951
952 return 0;
953}
954
955/**
956 * dir_split_leaf - Split a leaf block into two
957 * @dip: The GFS2 inode
958 * @index:
959 * @leaf_no:
960 *
961 * Returns: 0 on success, error code on failure
962 */
963
Steven Whitehousec7526662006-03-20 12:30:04 -0500964static int dir_split_leaf(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000965{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400966 struct gfs2_inode *dip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000967 struct buffer_head *nbh, *obh, *dibh;
968 struct gfs2_leaf *nleaf, *oleaf;
Steven Whitehouse4da3c642006-07-11 13:19:13 -0400969 struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
Steven Whitehousecd915492006-09-04 12:49:07 -0400970 u32 start, len, half_len, divider;
Al Virob44b84d2006-10-14 10:46:30 -0400971 u64 bn, leaf_no;
972 __be64 *lp;
Steven Whitehousecd915492006-09-04 12:49:07 -0400973 u32 index;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000974 int x, moved = 0;
975 int error;
976
Steven Whitehouse9a004502008-02-01 09:23:44 +0000977 index = name->hash >> (32 - dip->i_depth);
Steven Whitehousec7526662006-03-20 12:30:04 -0500978 error = get_leaf_nr(dip, index, &leaf_no);
979 if (error)
980 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000981
982 /* Get the old leaf block */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000983 error = get_leaf(dip, leaf_no, &obh);
984 if (error)
Steven Whitehousee90deff2006-03-29 19:02:15 -0500985 return error;
986
987 oleaf = (struct gfs2_leaf *)obh->b_data;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000988 if (dip->i_depth == be16_to_cpu(oleaf->lf_depth)) {
Steven Whitehousee90deff2006-03-29 19:02:15 -0500989 brelse(obh);
990 return 1; /* can't split */
991 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000992
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000993 gfs2_trans_add_meta(dip->i_gl, obh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000994
Steven Whitehousec7526662006-03-20 12:30:04 -0500995 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
996 if (!nleaf) {
997 brelse(obh);
998 return -ENOSPC;
999 }
1000 bn = nbh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001001
Steven Whitehousec7526662006-03-20 12:30:04 -05001002 /* Compute the start and len of leaf pointers in the hash table. */
Steven Whitehouse9a004502008-02-01 09:23:44 +00001003 len = 1 << (dip->i_depth - be16_to_cpu(oleaf->lf_depth));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001004 half_len = len >> 1;
1005 if (!half_len) {
Joe Perchesd77d1b52014-03-06 12:10:45 -08001006 pr_warn("i_depth %u lf_depth %u index %u\n",
1007 dip->i_depth, be16_to_cpu(oleaf->lf_depth), index);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001008 gfs2_consist_inode(dip);
1009 error = -EIO;
1010 goto fail_brelse;
1011 }
1012
1013 start = (index & ~(len - 1));
1014
1015 /* Change the pointers.
1016 Don't bother distinguishing stuffed from non-stuffed.
1017 This code is complicated enough already. */
David Rientjes4244b522010-07-20 19:45:03 -07001018 lp = kmalloc(half_len * sizeof(__be64), GFP_NOFS);
1019 if (!lp) {
1020 error = -ENOMEM;
1021 goto fail_brelse;
1022 }
1023
David Teiglandb3b94fa2006-01-16 16:50:04 +00001024 /* Change the pointers */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001025 for (x = 0; x < half_len; x++)
1026 lp[x] = cpu_to_be64(bn);
1027
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001028 gfs2_dir_hash_inval(dip);
1029
Steven Whitehousecd915492006-09-04 12:49:07 -04001030 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
1031 half_len * sizeof(u64));
1032 if (error != half_len * sizeof(u64)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001033 if (error >= 0)
1034 error = -EIO;
1035 goto fail_lpfree;
1036 }
1037
1038 kfree(lp);
1039
1040 /* Compute the divider */
Steven Whitehouse9a004502008-02-01 09:23:44 +00001041 divider = (start + half_len) << (32 - dip->i_depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001042
1043 /* Copy the entries */
Steven Whitehouse15793432009-11-06 11:06:37 +00001044 dent = (struct gfs2_dirent *)(obh->b_data + sizeof(struct gfs2_leaf));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001045
1046 do {
1047 next = dent;
1048 if (dirent_next(dip, obh, &next))
1049 next = NULL;
1050
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -05001051 if (!gfs2_dirent_sentinel(dent) &&
David Teiglandb3b94fa2006-01-16 16:50:04 +00001052 be32_to_cpu(dent->de_hash) < divider) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001053 struct qstr str;
1054 str.name = (char*)(dent+1);
1055 str.len = be16_to_cpu(dent->de_name_len);
1056 str.hash = be32_to_cpu(dent->de_hash);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001057 new = gfs2_dirent_alloc(inode, nbh, &str);
Steven Whitehousec7526662006-03-20 12:30:04 -05001058 if (IS_ERR(new)) {
1059 error = PTR_ERR(new);
1060 break;
1061 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001062
1063 new->de_inum = dent->de_inum; /* No endian worries */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001064 new->de_type = dent->de_type; /* No endian worries */
Marcin Slusarzbb16b342008-02-13 00:06:10 +01001065 be16_add_cpu(&nleaf->lf_entries, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001066
1067 dirent_del(dip, obh, prev, dent);
1068
1069 if (!oleaf->lf_entries)
1070 gfs2_consist_inode(dip);
Marcin Slusarzbb16b342008-02-13 00:06:10 +01001071 be16_add_cpu(&oleaf->lf_entries, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001072
1073 if (!prev)
1074 prev = dent;
1075
1076 moved = 1;
Steven Whitehousec7526662006-03-20 12:30:04 -05001077 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001078 prev = dent;
Steven Whitehousec7526662006-03-20 12:30:04 -05001079 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001080 dent = next;
Steven Whitehousec7526662006-03-20 12:30:04 -05001081 } while (dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001082
Steven Whitehousec7526662006-03-20 12:30:04 -05001083 oleaf->lf_depth = nleaf->lf_depth;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001084
1085 error = gfs2_meta_inode_buffer(dip, &dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001086 if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001087 gfs2_trans_add_meta(dip->i_gl, dibh);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001088 gfs2_add_inode_blocks(&dip->i_inode, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001089 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001090 brelse(dibh);
1091 }
1092
1093 brelse(obh);
1094 brelse(nbh);
1095
1096 return error;
1097
Steven Whitehousee90deff2006-03-29 19:02:15 -05001098fail_lpfree:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001099 kfree(lp);
1100
Steven Whitehousee90deff2006-03-29 19:02:15 -05001101fail_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001102 brelse(obh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001103 brelse(nbh);
1104 return error;
1105}
1106
1107/**
1108 * dir_double_exhash - Double size of ExHash table
1109 * @dip: The GFS2 dinode
1110 *
1111 * Returns: 0 on success, error code on failure
1112 */
1113
1114static int dir_double_exhash(struct gfs2_inode *dip)
1115{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001116 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001117 u32 hsize;
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001118 u32 hsize_bytes;
1119 __be64 *hc;
1120 __be64 *hc2, *h;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001121 int x;
1122 int error = 0;
1123
Steven Whitehouse9a004502008-02-01 09:23:44 +00001124 hsize = 1 << dip->i_depth;
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001125 hsize_bytes = hsize * sizeof(__be64);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001126
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001127 hc = gfs2_dir_get_hash_table(dip);
1128 if (IS_ERR(hc))
1129 return PTR_ERR(hc);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001130
Bob Peterson512cbf02013-06-14 08:39:18 -04001131 hc2 = kmalloc(hsize_bytes * 2, GFP_NOFS | __GFP_NOWARN);
Bob Petersone8830d82013-05-30 09:48:56 -04001132 if (hc2 == NULL)
1133 hc2 = __vmalloc(hsize_bytes * 2, GFP_NOFS, PAGE_KERNEL);
1134
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001135 if (!hc2)
David Rientjes4244b522010-07-20 19:45:03 -07001136 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001137
Bob Peterson512cbf02013-06-14 08:39:18 -04001138 h = hc2;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001139 error = gfs2_meta_inode_buffer(dip, &dibh);
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001140 if (error)
1141 goto out_kfree;
1142
1143 for (x = 0; x < hsize; x++) {
1144 *h++ = *hc;
1145 *h++ = *hc;
1146 hc++;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001147 }
1148
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001149 error = gfs2_dir_write_data(dip, (char *)hc2, 0, hsize_bytes * 2);
1150 if (error != (hsize_bytes * 2))
1151 goto fail;
1152
1153 gfs2_dir_hash_inval(dip);
1154 dip->i_hash_cache = hc2;
1155 dip->i_depth++;
1156 gfs2_dinode_out(dip, dibh->b_data);
1157 brelse(dibh);
1158 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001159
Steven Whitehousea91ea692006-09-04 12:04:26 -04001160fail:
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001161 /* Replace original hash table & size */
1162 gfs2_dir_write_data(dip, (char *)hc, 0, hsize_bytes);
1163 i_size_write(&dip->i_inode, hsize_bytes);
1164 gfs2_dinode_out(dip, dibh->b_data);
1165 brelse(dibh);
1166out_kfree:
Al Viro3cdcf632014-11-20 05:18:38 +00001167 kvfree(hc2);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001168 return error;
1169}
1170
1171/**
1172 * compare_dents - compare directory entries by hash value
1173 * @a: first dent
1174 * @b: second dent
1175 *
1176 * When comparing the hash entries of @a to @b:
1177 * gt: returns 1
1178 * lt: returns -1
1179 * eq: returns 0
1180 */
1181
1182static int compare_dents(const void *a, const void *b)
1183{
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001184 const struct gfs2_dirent *dent_a, *dent_b;
Steven Whitehousecd915492006-09-04 12:49:07 -04001185 u32 hash_a, hash_b;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001186 int ret = 0;
1187
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001188 dent_a = *(const struct gfs2_dirent **)a;
Steven Whitehousec7526662006-03-20 12:30:04 -05001189 hash_a = be32_to_cpu(dent_a->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001190
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001191 dent_b = *(const struct gfs2_dirent **)b;
Steven Whitehousec7526662006-03-20 12:30:04 -05001192 hash_b = be32_to_cpu(dent_b->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001193
1194 if (hash_a > hash_b)
1195 ret = 1;
1196 else if (hash_a < hash_b)
1197 ret = -1;
1198 else {
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001199 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1200 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001201
1202 if (len_a > len_b)
1203 ret = 1;
1204 else if (len_a < len_b)
1205 ret = -1;
1206 else
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001207 ret = memcmp(dent_a + 1, dent_b + 1, len_a);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001208 }
1209
1210 return ret;
1211}
1212
1213/**
1214 * do_filldir_main - read out directory entries
1215 * @dip: The GFS2 inode
Al Virod81a8ef2013-05-16 14:14:48 -04001216 * @ctx: what to feed the entries to
David Teiglandb3b94fa2006-01-16 16:50:04 +00001217 * @darr: an array of struct gfs2_dirent pointers to read
1218 * @entries: the number of entries in darr
1219 * @copied: pointer to int that's non-zero if a entry has been copied out
1220 *
1221 * Jump through some hoops to make sure that if there are hash collsions,
1222 * they are read out at the beginning of a buffer. We want to minimize
1223 * the possibility that they will fall into different readdir buffers or
1224 * that someone will want to seek to that location.
1225 *
Al Virod81a8ef2013-05-16 14:14:48 -04001226 * Returns: errno, >0 if the actor tells you to stop
David Teiglandb3b94fa2006-01-16 16:50:04 +00001227 */
1228
Al Virod81a8ef2013-05-16 14:14:48 -04001229static int do_filldir_main(struct gfs2_inode *dip, struct dir_context *ctx,
Steven Whitehousecd915492006-09-04 12:49:07 -04001230 const struct gfs2_dirent **darr, u32 entries,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001231 int *copied)
1232{
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001233 const struct gfs2_dirent *dent, *dent_next;
Steven Whitehousecd915492006-09-04 12:49:07 -04001234 u64 off, off_next;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001235 unsigned int x, y;
1236 int run = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001237
1238 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1239
1240 dent_next = darr[0];
1241 off_next = be32_to_cpu(dent_next->de_hash);
1242 off_next = gfs2_disk_hash2offset(off_next);
1243
1244 for (x = 0, y = 1; x < entries; x++, y++) {
1245 dent = dent_next;
1246 off = off_next;
1247
1248 if (y < entries) {
1249 dent_next = darr[y];
1250 off_next = be32_to_cpu(dent_next->de_hash);
1251 off_next = gfs2_disk_hash2offset(off_next);
1252
Al Virod81a8ef2013-05-16 14:14:48 -04001253 if (off < ctx->pos)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001254 continue;
Al Virod81a8ef2013-05-16 14:14:48 -04001255 ctx->pos = off;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001256
1257 if (off_next == off) {
1258 if (*copied && !run)
1259 return 1;
1260 run = 1;
1261 } else
1262 run = 0;
1263 } else {
Al Virod81a8ef2013-05-16 14:14:48 -04001264 if (off < ctx->pos)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001265 continue;
Al Virod81a8ef2013-05-16 14:14:48 -04001266 ctx->pos = off;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001267 }
1268
Al Virod81a8ef2013-05-16 14:14:48 -04001269 if (!dir_emit(ctx, (const char *)(dent + 1),
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001270 be16_to_cpu(dent->de_name_len),
Al Virod81a8ef2013-05-16 14:14:48 -04001271 be64_to_cpu(dent->de_inum.no_addr),
1272 be16_to_cpu(dent->de_type)))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001273 return 1;
1274
1275 *copied = 1;
1276 }
1277
Al Virod81a8ef2013-05-16 14:14:48 -04001278 /* Increment the ctx->pos by one, so the next time we come into the
David Teiglandb3b94fa2006-01-16 16:50:04 +00001279 do_filldir fxn, we get the next entry instead of the last one in the
1280 current leaf */
1281
Al Virod81a8ef2013-05-16 14:14:48 -04001282 ctx->pos++;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001283
1284 return 0;
1285}
1286
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001287static void *gfs2_alloc_sort_buffer(unsigned size)
1288{
1289 void *ptr = NULL;
1290
1291 if (size < KMALLOC_MAX_SIZE)
1292 ptr = kmalloc(size, GFP_NOFS | __GFP_NOWARN);
1293 if (!ptr)
1294 ptr = __vmalloc(size, GFP_NOFS, PAGE_KERNEL);
1295 return ptr;
1296}
1297
Al Virod81a8ef2013-05-16 14:14:48 -04001298static int gfs2_dir_read_leaf(struct inode *inode, struct dir_context *ctx,
1299 int *copied, unsigned *depth,
Steven Whitehouse3699e3a2007-01-17 15:09:20 +00001300 u64 leaf_no)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001301{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001302 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001303 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001304 struct buffer_head *bh;
1305 struct gfs2_leaf *lf;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001306 unsigned entries = 0, entries2 = 0;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001307 unsigned leaves = 0;
1308 const struct gfs2_dirent **darr, *dent;
1309 struct dirent_gather g;
1310 struct buffer_head **larr;
1311 int leaf = 0;
1312 int error, i;
1313 u64 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001314
David Teiglandb3b94fa2006-01-16 16:50:04 +00001315 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001316 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001317 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001318 goto out;
1319 lf = (struct gfs2_leaf *)bh->b_data;
1320 if (leaves == 0)
1321 *depth = be16_to_cpu(lf->lf_depth);
1322 entries += be16_to_cpu(lf->lf_entries);
1323 leaves++;
1324 lfn = be64_to_cpu(lf->lf_next);
1325 brelse(bh);
1326 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001327
1328 if (!entries)
1329 return 0;
1330
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001331 error = -ENOMEM;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001332 /*
1333 * The extra 99 entries are not normally used, but are a buffer
1334 * zone in case the number of entries in the leaf is corrupt.
1335 * 99 is the maximum number of entries that can fit in a single
1336 * leaf block.
1337 */
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001338 larr = gfs2_alloc_sort_buffer((leaves + entries + 99) * sizeof(void *));
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001339 if (!larr)
1340 goto out;
1341 darr = (const struct gfs2_dirent **)(larr + leaves);
1342 g.pdent = darr;
1343 g.offset = 0;
1344 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001345
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001346 do {
1347 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001348 if (error)
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001349 goto out_free;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001350 lf = (struct gfs2_leaf *)bh->b_data;
1351 lfn = be64_to_cpu(lf->lf_next);
1352 if (lf->lf_entries) {
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001353 entries2 += be16_to_cpu(lf->lf_entries);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001354 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1355 gfs2_dirent_gather, NULL, &g);
1356 error = PTR_ERR(dent);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001357 if (IS_ERR(dent))
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001358 goto out_free;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001359 if (entries2 != g.offset) {
akpm@linux-foundation.orgf391a4e2007-04-25 21:08:02 -07001360 fs_warn(sdp, "Number of entries corrupt in dir "
1361 "leaf %llu, entries2 (%u) != "
1362 "g.offset (%u)\n",
1363 (unsigned long long)bh->b_blocknr,
1364 entries2, g.offset);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001365
1366 error = -EIO;
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001367 goto out_free;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001368 }
1369 error = 0;
1370 larr[leaf++] = bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001371 } else {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001372 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001373 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001374 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001375
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001376 BUG_ON(entries2 != entries);
Al Virod81a8ef2013-05-16 14:14:48 -04001377 error = do_filldir_main(ip, ctx, darr, entries, copied);
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001378out_free:
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001379 for(i = 0; i < leaf; i++)
1380 brelse(larr[i]);
Al Viro3cdcf632014-11-20 05:18:38 +00001381 kvfree(larr);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001382out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001383 return error;
1384}
1385
Steven Whitehouse79c4c372011-11-09 13:46:06 +00001386/**
1387 * gfs2_dir_readahead - Issue read-ahead requests for leaf blocks.
Bob Petersondfe4d342011-10-27 12:16:06 -04001388 *
1389 * Note: we can't calculate each index like dir_e_read can because we don't
1390 * have the leaf, and therefore we don't have the depth, and therefore we
1391 * don't have the length. So we have to just read enough ahead to make up
Steven Whitehouse79c4c372011-11-09 13:46:06 +00001392 * for the loss of information.
1393 */
Bob Petersondfe4d342011-10-27 12:16:06 -04001394static void gfs2_dir_readahead(struct inode *inode, unsigned hsize, u32 index,
1395 struct file_ra_state *f_ra)
1396{
1397 struct gfs2_inode *ip = GFS2_I(inode);
1398 struct gfs2_glock *gl = ip->i_gl;
1399 struct buffer_head *bh;
1400 u64 blocknr = 0, last;
1401 unsigned count;
1402
1403 /* First check if we've already read-ahead for the whole range. */
Steven Whitehouse79c4c372011-11-09 13:46:06 +00001404 if (index + MAX_RA_BLOCKS < f_ra->start)
Bob Petersondfe4d342011-10-27 12:16:06 -04001405 return;
1406
1407 f_ra->start = max((pgoff_t)index, f_ra->start);
1408 for (count = 0; count < MAX_RA_BLOCKS; count++) {
1409 if (f_ra->start >= hsize) /* if exceeded the hash table */
1410 break;
1411
1412 last = blocknr;
1413 blocknr = be64_to_cpu(ip->i_hash_cache[f_ra->start]);
1414 f_ra->start++;
1415 if (blocknr == last)
1416 continue;
1417
1418 bh = gfs2_getbuf(gl, blocknr, 1);
1419 if (trylock_buffer(bh)) {
1420 if (buffer_uptodate(bh)) {
1421 unlock_buffer(bh);
1422 brelse(bh);
1423 continue;
1424 }
1425 bh->b_end_io = end_buffer_read_sync;
1426 submit_bh(READA | REQ_META, bh);
1427 continue;
1428 }
1429 brelse(bh);
1430 }
1431}
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001432
David Teiglandb3b94fa2006-01-16 16:50:04 +00001433/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001434 * dir_e_read - Reads the entries from a directory into a filldir buffer
1435 * @dip: dinode pointer
Al Virod81a8ef2013-05-16 14:14:48 -04001436 * @ctx: actor to feed the entries to
David Teiglandb3b94fa2006-01-16 16:50:04 +00001437 *
1438 * Returns: errno
1439 */
1440
Al Virod81a8ef2013-05-16 14:14:48 -04001441static int dir_e_read(struct inode *inode, struct dir_context *ctx,
1442 struct file_ra_state *f_ra)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001443{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001444 struct gfs2_inode *dip = GFS2_I(inode);
Steven Whitehousecd915492006-09-04 12:49:07 -04001445 u32 hsize, len = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -04001446 u32 hash, index;
Al Virob44b84d2006-10-14 10:46:30 -04001447 __be64 *lp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001448 int copied = 0;
1449 int error = 0;
Steven Whitehouse4da3c642006-07-11 13:19:13 -04001450 unsigned depth = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001451
Steven Whitehouse9a004502008-02-01 09:23:44 +00001452 hsize = 1 << dip->i_depth;
Al Virod81a8ef2013-05-16 14:14:48 -04001453 hash = gfs2_dir_offset2hash(ctx->pos);
Steven Whitehouse9a004502008-02-01 09:23:44 +00001454 index = hash >> (32 - dip->i_depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001455
Steven Whitehouse79c4c372011-11-09 13:46:06 +00001456 if (dip->i_hash_cache == NULL)
Bob Petersondfe4d342011-10-27 12:16:06 -04001457 f_ra->start = 0;
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001458 lp = gfs2_dir_get_hash_table(dip);
1459 if (IS_ERR(lp))
1460 return PTR_ERR(lp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001461
Bob Petersondfe4d342011-10-27 12:16:06 -04001462 gfs2_dir_readahead(inode, hsize, index, f_ra);
1463
David Teiglandb3b94fa2006-01-16 16:50:04 +00001464 while (index < hsize) {
Al Virod81a8ef2013-05-16 14:14:48 -04001465 error = gfs2_dir_read_leaf(inode, ctx,
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001466 &copied, &depth,
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001467 be64_to_cpu(lp[index]));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001468 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001469 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001470
Steven Whitehouse9a004502008-02-01 09:23:44 +00001471 len = 1 << (dip->i_depth - depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001472 index = (index & ~(len - 1)) + len;
1473 }
1474
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001475 if (error > 0)
1476 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001477 return error;
1478}
1479
Al Virod81a8ef2013-05-16 14:14:48 -04001480int gfs2_dir_read(struct inode *inode, struct dir_context *ctx,
1481 struct file_ra_state *f_ra)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001482{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001483 struct gfs2_inode *dip = GFS2_I(inode);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001484 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001485 struct dirent_gather g;
1486 const struct gfs2_dirent **darr, *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001487 struct buffer_head *dibh;
1488 int copied = 0;
1489 int error;
1490
Steven Whitehousead6203f22008-11-03 13:59:19 +00001491 if (!dip->i_entries)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001492 return 0;
1493
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001494 if (dip->i_diskflags & GFS2_DIF_EXHASH)
Al Virod81a8ef2013-05-16 14:14:48 -04001495 return dir_e_read(inode, ctx, f_ra);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001496
David Teiglandb3b94fa2006-01-16 16:50:04 +00001497 if (!gfs2_is_stuffed(dip)) {
1498 gfs2_consist_inode(dip);
1499 return -EIO;
1500 }
1501
David Teiglandb3b94fa2006-01-16 16:50:04 +00001502 error = gfs2_meta_inode_buffer(dip, &dibh);
1503 if (error)
1504 return error;
1505
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001506 error = -ENOMEM;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001507 /* 96 is max number of dirents which can be stuffed into an inode */
Josef Bacik16c5f062008-04-09 09:33:41 -04001508 darr = kmalloc(96 * sizeof(struct gfs2_dirent *), GFP_NOFS);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001509 if (darr) {
1510 g.pdent = darr;
1511 g.offset = 0;
1512 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1513 gfs2_dirent_gather, NULL, &g);
1514 if (IS_ERR(dent)) {
1515 error = PTR_ERR(dent);
1516 goto out;
1517 }
Steven Whitehousead6203f22008-11-03 13:59:19 +00001518 if (dip->i_entries != g.offset) {
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001519 fs_warn(sdp, "Number of entries corrupt in dir %llu, "
Steven Whitehousead6203f22008-11-03 13:59:19 +00001520 "ip->i_entries (%u) != g.offset (%u)\n",
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001521 (unsigned long long)dip->i_no_addr,
Steven Whitehousead6203f22008-11-03 13:59:19 +00001522 dip->i_entries,
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001523 g.offset);
1524 error = -EIO;
1525 goto out;
1526 }
Al Virod81a8ef2013-05-16 14:14:48 -04001527 error = do_filldir_main(dip, ctx, darr,
Steven Whitehousead6203f22008-11-03 13:59:19 +00001528 dip->i_entries, &copied);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001529out:
1530 kfree(darr);
1531 }
1532
David Teiglandb3b94fa2006-01-16 16:50:04 +00001533 if (error > 0)
1534 error = 0;
1535
1536 brelse(dibh);
1537
1538 return error;
1539}
1540
David Teiglandb3b94fa2006-01-16 16:50:04 +00001541/**
1542 * gfs2_dir_search - Search a directory
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +01001543 * @dip: The GFS2 dir inode
1544 * @name: The name we are looking up
1545 * @fail_on_exist: Fail if the name exists rather than looking it up
David Teiglandb3b94fa2006-01-16 16:50:04 +00001546 *
1547 * This routine searches a directory for a file or another directory.
1548 * Assumes a glock is held on dip.
1549 *
1550 * Returns: errno
1551 */
1552
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +01001553struct inode *gfs2_dir_search(struct inode *dir, const struct qstr *name,
1554 bool fail_on_exist)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001555{
Steven Whitehousec7526662006-03-20 12:30:04 -05001556 struct buffer_head *bh;
1557 struct gfs2_dirent *dent;
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +01001558 u64 addr, formal_ino;
1559 u16 dtype;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001560
1561 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1562 if (dent) {
Andreas Gruenbacherc8d57702015-11-11 15:00:35 -06001563 struct inode *inode;
1564 u16 rahead;
1565
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001566 if (IS_ERR(dent))
David Howellse231c2e2008-02-07 00:15:26 -08001567 return ERR_CAST(dent);
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +01001568 dtype = be16_to_cpu(dent->de_type);
Andreas Gruenbacherc8d57702015-11-11 15:00:35 -06001569 rahead = be16_to_cpu(dent->de_rahead);
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +01001570 addr = be64_to_cpu(dent->de_inum.no_addr);
1571 formal_ino = be64_to_cpu(dent->de_inum.no_formal_ino);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001572 brelse(bh);
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +01001573 if (fail_on_exist)
1574 return ERR_PTR(-EEXIST);
Andreas Gruenbacherc8d57702015-11-11 15:00:35 -06001575 inode = gfs2_inode_lookup(dir->i_sb, dtype, addr, formal_ino, 0);
1576 if (!IS_ERR(inode))
1577 GFS2_I(inode)->i_rahead = rahead;
1578 return inode;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001579 }
1580 return ERR_PTR(-ENOENT);
1581}
1582
1583int gfs2_dir_check(struct inode *dir, const struct qstr *name,
1584 const struct gfs2_inode *ip)
1585{
1586 struct buffer_head *bh;
1587 struct gfs2_dirent *dent;
1588 int ret = -ENOENT;
Steven Whitehousec7526662006-03-20 12:30:04 -05001589
1590 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1591 if (dent) {
1592 if (IS_ERR(dent))
1593 return PTR_ERR(dent);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001594 if (ip) {
1595 if (be64_to_cpu(dent->de_inum.no_addr) != ip->i_no_addr)
1596 goto out;
1597 if (be64_to_cpu(dent->de_inum.no_formal_ino) !=
1598 ip->i_no_formal_ino)
1599 goto out;
1600 if (unlikely(IF2DT(ip->i_inode.i_mode) !=
1601 be16_to_cpu(dent->de_type))) {
1602 gfs2_consist_inode(GFS2_I(dir));
1603 ret = -EIO;
1604 goto out;
1605 }
1606 }
1607 ret = 0;
1608out:
Steven Whitehousec7526662006-03-20 12:30:04 -05001609 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001610 }
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001611 return ret;
Steven Whitehousec7526662006-03-20 12:30:04 -05001612}
1613
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001614/**
1615 * dir_new_leaf - Add a new leaf onto hash chain
1616 * @inode: The directory
1617 * @name: The name we are adding
1618 *
1619 * This adds a new dir leaf onto an existing leaf when there is not
1620 * enough space to add a new dir entry. This is a last resort after
1621 * we've expanded the hash table to max size and also split existing
1622 * leaf blocks, so it will only occur for very large directories.
1623 *
1624 * The dist parameter is set to 1 for leaf blocks directly attached
1625 * to the hash table, 2 for one layer of indirection, 3 for two layers
1626 * etc. We are thus able to tell the difference between an old leaf
1627 * with dist set to zero (i.e. "don't know") and a new one where we
1628 * set this information for debug/fsck purposes.
1629 *
1630 * Returns: 0 on success, or -ve on error
1631 */
1632
Steven Whitehousec7526662006-03-20 12:30:04 -05001633static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1634{
1635 struct buffer_head *bh, *obh;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001636 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001637 struct gfs2_leaf *leaf, *oleaf;
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001638 u32 dist = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001639 int error;
Steven Whitehousec7526662006-03-20 12:30:04 -05001640 u32 index;
1641 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001642
Steven Whitehouse9a004502008-02-01 09:23:44 +00001643 index = name->hash >> (32 - ip->i_depth);
Steven Whitehousec7526662006-03-20 12:30:04 -05001644 error = get_first_leaf(ip, index, &obh);
1645 if (error)
1646 return error;
1647 do {
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001648 dist++;
Steven Whitehousec7526662006-03-20 12:30:04 -05001649 oleaf = (struct gfs2_leaf *)obh->b_data;
1650 bn = be64_to_cpu(oleaf->lf_next);
1651 if (!bn)
1652 break;
1653 brelse(obh);
1654 error = get_leaf(ip, bn, &obh);
1655 if (error)
1656 return error;
1657 } while(1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001658
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001659 gfs2_trans_add_meta(ip->i_gl, obh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001660
1661 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1662 if (!leaf) {
1663 brelse(obh);
1664 return -ENOSPC;
1665 }
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001666 leaf->lf_dist = cpu_to_be32(dist);
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04001667 oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
Steven Whitehousec7526662006-03-20 12:30:04 -05001668 brelse(bh);
1669 brelse(obh);
1670
1671 error = gfs2_meta_inode_buffer(ip, &bh);
1672 if (error)
1673 return error;
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001674 gfs2_trans_add_meta(ip->i_gl, bh);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001675 gfs2_add_inode_blocks(&ip->i_inode, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001676 gfs2_dinode_out(ip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001677 brelse(bh);
1678 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001679}
1680
Steven Whitehouse44aaada2014-02-07 11:23:22 +00001681static u16 gfs2_inode_ra_len(const struct gfs2_inode *ip)
1682{
1683 u64 where = ip->i_no_addr + 1;
1684 if (ip->i_eattr == where)
1685 return 1;
1686 return 0;
1687}
1688
David Teiglandb3b94fa2006-01-16 16:50:04 +00001689/**
1690 * gfs2_dir_add - Add new filename into directory
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001691 * @inode: The directory inode
1692 * @name: The new name
1693 * @nip: The GFS2 inode to be linked in to the directory
1694 * @da: The directory addition info
1695 *
1696 * If the call to gfs2_diradd_alloc_required resulted in there being
1697 * no need to allocate any new directory blocks, then it will contain
1698 * a pointer to the directory entry and the bh in which it resides. We
1699 * can use that without having to repeat the search. If there was no
1700 * free space, then we must now create more space.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001701 *
1702 * Returns: 0 on success, error code on failure
1703 */
1704
Steven Whitehousec7526662006-03-20 12:30:04 -05001705int gfs2_dir_add(struct inode *inode, const struct qstr *name,
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001706 const struct gfs2_inode *nip, struct gfs2_diradd *da)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001707{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001708 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001709 struct buffer_head *bh = da->bh;
1710 struct gfs2_dirent *dent = da->dent;
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001711 struct timespec tv;
Steven Whitehousec7526662006-03-20 12:30:04 -05001712 struct gfs2_leaf *leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001713 int error;
1714
Steven Whitehousec7526662006-03-20 12:30:04 -05001715 while(1) {
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001716 if (da->bh == NULL) {
1717 dent = gfs2_dirent_search(inode, name,
1718 gfs2_dirent_find_space, &bh);
1719 }
Steven Whitehousec7526662006-03-20 12:30:04 -05001720 if (dent) {
1721 if (IS_ERR(dent))
1722 return PTR_ERR(dent);
1723 dent = gfs2_init_dirent(inode, dent, name, bh);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001724 gfs2_inum_out(nip, dent);
Steven Whitehouse3d6ecb72011-05-09 13:30:08 +01001725 dent->de_type = cpu_to_be16(IF2DT(nip->i_inode.i_mode));
Steven Whitehouse44aaada2014-02-07 11:23:22 +00001726 dent->de_rahead = cpu_to_be16(gfs2_inode_ra_len(nip));
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001727 tv = CURRENT_TIME;
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001728 if (ip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001729 leaf = (struct gfs2_leaf *)bh->b_data;
Marcin Slusarzbb16b342008-02-13 00:06:10 +01001730 be16_add_cpu(&leaf->lf_entries, 1);
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001731 leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
1732 leaf->lf_sec = cpu_to_be64(tv.tv_sec);
Steven Whitehousec7526662006-03-20 12:30:04 -05001733 }
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001734 da->dent = NULL;
1735 da->bh = NULL;
Steven Whitehousec7526662006-03-20 12:30:04 -05001736 brelse(bh);
Steven Whitehousead6203f22008-11-03 13:59:19 +00001737 ip->i_entries++;
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001738 ip->i_inode.i_mtime = ip->i_inode.i_ctime = tv;
Steven Whitehouse3d6ecb72011-05-09 13:30:08 +01001739 if (S_ISDIR(nip->i_inode.i_mode))
1740 inc_nlink(&ip->i_inode);
Bob Peterson343cd8f2012-11-12 13:04:54 -05001741 mark_inode_dirty(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001742 error = 0;
1743 break;
1744 }
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001745 if (!(ip->i_diskflags & GFS2_DIF_EXHASH)) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001746 error = dir_make_exhash(inode);
1747 if (error)
1748 break;
1749 continue;
1750 }
1751 error = dir_split_leaf(inode, name);
1752 if (error == 0)
1753 continue;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001754 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001755 break;
Steven Whitehouse9a004502008-02-01 09:23:44 +00001756 if (ip->i_depth < GFS2_DIR_MAX_DEPTH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001757 error = dir_double_exhash(ip);
1758 if (error)
1759 break;
1760 error = dir_split_leaf(inode, name);
Steven Whitehousee90deff2006-03-29 19:02:15 -05001761 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001762 break;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001763 if (error == 0)
1764 continue;
Steven Whitehousec7526662006-03-20 12:30:04 -05001765 }
1766 error = dir_new_leaf(inode, name);
1767 if (!error)
1768 continue;
1769 error = -ENOSPC;
1770 break;
1771 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001772 return error;
1773}
1774
Steven Whitehousec7526662006-03-20 12:30:04 -05001775
David Teiglandb3b94fa2006-01-16 16:50:04 +00001776/**
1777 * gfs2_dir_del - Delete a directory entry
1778 * @dip: The GFS2 inode
1779 * @filename: The filename
1780 *
1781 * Returns: 0 on success, error code on failure
1782 */
1783
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001784int gfs2_dir_del(struct gfs2_inode *dip, const struct dentry *dentry)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001785{
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001786 const struct qstr *name = &dentry->d_name;
Steven Whitehousec7526662006-03-20 12:30:04 -05001787 struct gfs2_dirent *dent, *prev = NULL;
1788 struct buffer_head *bh;
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001789 struct timespec tv = CURRENT_TIME;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001790
Steven Whitehousec7526662006-03-20 12:30:04 -05001791 /* Returns _either_ the entry (if its first in block) or the
1792 previous entry otherwise */
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001793 dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001794 if (!dent) {
1795 gfs2_consist_inode(dip);
1796 return -EIO;
1797 }
1798 if (IS_ERR(dent)) {
1799 gfs2_consist_inode(dip);
1800 return PTR_ERR(dent);
1801 }
1802 /* If not first in block, adjust pointers accordingly */
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001803 if (gfs2_dirent_find(dent, name, NULL) == 0) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001804 prev = dent;
1805 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1806 }
1807
1808 dirent_del(dip, bh, prev, dent);
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001809 if (dip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001810 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1811 u16 entries = be16_to_cpu(leaf->lf_entries);
1812 if (!entries)
1813 gfs2_consist_inode(dip);
1814 leaf->lf_entries = cpu_to_be16(--entries);
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001815 leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
1816 leaf->lf_sec = cpu_to_be64(tv.tv_sec);
Steven Whitehousec7526662006-03-20 12:30:04 -05001817 }
Steven Whitehouseed386502006-04-07 16:28:07 -04001818 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001819
Steven Whitehousead6203f22008-11-03 13:59:19 +00001820 if (!dip->i_entries)
Steven Whitehousec7526662006-03-20 12:30:04 -05001821 gfs2_consist_inode(dip);
Steven Whitehousead6203f22008-11-03 13:59:19 +00001822 dip->i_entries--;
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001823 dip->i_inode.i_mtime = dip->i_inode.i_ctime = tv;
David Howellse36cb0b2015-01-29 12:02:35 +00001824 if (d_is_dir(dentry))
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001825 drop_nlink(&dip->i_inode);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001826 mark_inode_dirty(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001827
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001828 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001829}
1830
David Teiglandb3b94fa2006-01-16 16:50:04 +00001831/**
1832 * gfs2_dir_mvino - Change inode number of directory entry
1833 * @dip: The GFS2 inode
1834 * @filename:
1835 * @new_inode:
1836 *
1837 * This routine changes the inode number of a directory entry. It's used
1838 * by rename to change ".." when a directory is moved.
1839 * Assumes a glock is held on dvp.
1840 *
1841 * Returns: errno
1842 */
1843
Steven Whitehousec7526662006-03-20 12:30:04 -05001844int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001845 const struct gfs2_inode *nip, unsigned int new_type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001846{
Steven Whitehousec7526662006-03-20 12:30:04 -05001847 struct buffer_head *bh;
1848 struct gfs2_dirent *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001849 int error;
1850
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001851 dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001852 if (!dent) {
1853 gfs2_consist_inode(dip);
1854 return -EIO;
1855 }
1856 if (IS_ERR(dent))
1857 return PTR_ERR(dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001858
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001859 gfs2_trans_add_meta(dip->i_gl, bh);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001860 gfs2_inum_out(nip, dent);
Steven Whitehousec7526662006-03-20 12:30:04 -05001861 dent->de_type = cpu_to_be16(new_type);
1862
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001863 if (dip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001864 brelse(bh);
1865 error = gfs2_meta_inode_buffer(dip, &bh);
1866 if (error)
1867 return error;
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001868 gfs2_trans_add_meta(dip->i_gl, bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001869 }
1870
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001871 dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001872 gfs2_dinode_out(dip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001873 brelse(bh);
1874 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001875}
1876
1877/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001878 * leaf_dealloc - Deallocate a directory leaf
1879 * @dip: the directory
1880 * @index: the hash table offset in the directory
1881 * @len: the number of pointers to this leaf
1882 * @leaf_no: the leaf number
Bob Petersonec038c82011-03-22 13:55:23 -04001883 * @leaf_bh: buffer_head for the starting leaf
Bob Petersond24a7a42011-03-22 13:54:03 -04001884 * last_dealloc: 1 if this is the final dealloc for the leaf, else 0
David Teiglandb3b94fa2006-01-16 16:50:04 +00001885 *
1886 * Returns: errno
1887 */
1888
Steven Whitehousecd915492006-09-04 12:49:07 -04001889static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
Bob Petersonec038c82011-03-22 13:55:23 -04001890 u64 leaf_no, struct buffer_head *leaf_bh,
1891 int last_dealloc)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001892{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001893 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001894 struct gfs2_leaf *tmp_leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001895 struct gfs2_rgrp_list rlist;
1896 struct buffer_head *bh, *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001897 u64 blk, nblk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001898 unsigned int rg_blocks = 0, l_blocks = 0;
1899 char *ht;
Steven Whitehousecd915492006-09-04 12:49:07 -04001900 unsigned int x, size = len * sizeof(u64);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001901 int error;
1902
Bob Peterson5e2f7d62012-04-04 22:11:16 -04001903 error = gfs2_rindex_update(sdp);
1904 if (error)
1905 return error;
1906
David Teiglandb3b94fa2006-01-16 16:50:04 +00001907 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1908
Joe Perches8be04b92013-06-19 12:15:53 -07001909 ht = kzalloc(size, GFP_NOFS | __GFP_NOWARN);
Bob Petersone8830d82013-05-30 09:48:56 -04001910 if (ht == NULL)
Oleg Drokin7456a372015-02-01 22:59:54 -05001911 ht = __vmalloc(size, GFP_NOFS | __GFP_NOWARN | __GFP_ZERO,
1912 PAGE_KERNEL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001913 if (!ht)
1914 return -ENOMEM;
1915
Eric W. Biedermanf4108a62013-01-31 17:49:26 -08001916 error = gfs2_quota_hold(dip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001917 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -04001918 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001919
David Teiglandb3b94fa2006-01-16 16:50:04 +00001920 /* Count the number of leaves */
Bob Petersonec038c82011-03-22 13:55:23 -04001921 bh = leaf_bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001922
Steven Whitehousec7526662006-03-20 12:30:04 -05001923 for (blk = leaf_no; blk; blk = nblk) {
Bob Petersonec038c82011-03-22 13:55:23 -04001924 if (blk != leaf_no) {
1925 error = get_leaf(dip, blk, &bh);
1926 if (error)
1927 goto out_rlist;
1928 }
Steven Whitehousec7526662006-03-20 12:30:04 -05001929 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1930 nblk = be64_to_cpu(tmp_leaf->lf_next);
Bob Petersonec038c82011-03-22 13:55:23 -04001931 if (blk != leaf_no)
1932 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001933
Steven Whitehouse70b0c362011-09-02 16:08:09 +01001934 gfs2_rlist_add(dip, &rlist, blk);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001935 l_blocks++;
1936 }
1937
Bob Petersonfe6c9912008-01-28 11:13:02 -06001938 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001939
1940 for (x = 0; x < rlist.rl_rgrps; x++) {
1941 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001942 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001943 rg_blocks += rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001944 }
1945
1946 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1947 if (error)
1948 goto out_rlist;
1949
1950 error = gfs2_trans_begin(sdp,
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001951 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
David Teiglandb3b94fa2006-01-16 16:50:04 +00001952 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1953 if (error)
1954 goto out_rg_gunlock;
1955
Bob Petersonec038c82011-03-22 13:55:23 -04001956 bh = leaf_bh;
1957
Steven Whitehousec7526662006-03-20 12:30:04 -05001958 for (blk = leaf_no; blk; blk = nblk) {
Bob Petersonec038c82011-03-22 13:55:23 -04001959 if (blk != leaf_no) {
1960 error = get_leaf(dip, blk, &bh);
1961 if (error)
1962 goto out_end_trans;
1963 }
Steven Whitehousec7526662006-03-20 12:30:04 -05001964 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1965 nblk = be64_to_cpu(tmp_leaf->lf_next);
Bob Petersonec038c82011-03-22 13:55:23 -04001966 if (blk != leaf_no)
1967 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001968
1969 gfs2_free_meta(dip, blk, 1);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001970 gfs2_add_inode_blocks(&dip->i_inode, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001971 }
1972
Steven Whitehousecd915492006-09-04 12:49:07 -04001973 error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001974 if (error != size) {
1975 if (error >= 0)
1976 error = -EIO;
1977 goto out_end_trans;
1978 }
1979
1980 error = gfs2_meta_inode_buffer(dip, &dibh);
1981 if (error)
1982 goto out_end_trans;
1983
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001984 gfs2_trans_add_meta(dip->i_gl, dibh);
Bob Petersond24a7a42011-03-22 13:54:03 -04001985 /* On the last dealloc, make this a regular file in case we crash.
1986 (We don't want to free these blocks a second time.) */
1987 if (last_dealloc)
1988 dip->i_inode.i_mode = S_IFREG;
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001989 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001990 brelse(dibh);
1991
Steven Whitehousea91ea692006-09-04 12:04:26 -04001992out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001993 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001994out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001995 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001996out_rlist:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001997 gfs2_rlist_free(&rlist);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001998 gfs2_quota_unhold(dip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001999out:
Al Viro3cdcf632014-11-20 05:18:38 +00002000 kvfree(ht);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002001 return error;
2002}
2003
2004/**
2005 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
2006 * @dip: the directory
2007 *
2008 * Dealloc all on-disk directory leaves to FREEMETA state
2009 * Change on-disk inode type to "regular file"
2010 *
2011 * Returns: errno
2012 */
2013
2014int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
2015{
Bob Peterson556bb172011-03-22 13:56:37 -04002016 struct buffer_head *bh;
2017 struct gfs2_leaf *leaf;
2018 u32 hsize, len;
Bob Peterson556bb172011-03-22 13:56:37 -04002019 u32 index = 0, next_index;
2020 __be64 *lp;
2021 u64 leaf_no;
2022 int error = 0, last;
2023
2024 hsize = 1 << dip->i_depth;
Bob Peterson556bb172011-03-22 13:56:37 -04002025
Steven Whitehouse17d539f2011-06-15 10:29:37 +01002026 lp = gfs2_dir_get_hash_table(dip);
2027 if (IS_ERR(lp))
2028 return PTR_ERR(lp);
Bob Peterson556bb172011-03-22 13:56:37 -04002029
2030 while (index < hsize) {
Steven Whitehouse17d539f2011-06-15 10:29:37 +01002031 leaf_no = be64_to_cpu(lp[index]);
Bob Peterson556bb172011-03-22 13:56:37 -04002032 if (leaf_no) {
2033 error = get_leaf(dip, leaf_no, &bh);
2034 if (error)
2035 goto out;
2036 leaf = (struct gfs2_leaf *)bh->b_data;
2037 len = 1 << (dip->i_depth - be16_to_cpu(leaf->lf_depth));
2038
2039 next_index = (index & ~(len - 1)) + len;
2040 last = ((next_index >= hsize) ? 1 : 0);
2041 error = leaf_dealloc(dip, index, len, leaf_no, bh,
2042 last);
2043 brelse(bh);
2044 if (error)
2045 goto out;
2046 index = next_index;
2047 } else
2048 index++;
2049 }
2050
2051 if (index != hsize) {
2052 gfs2_consist_inode(dip);
2053 error = -EIO;
2054 }
2055
2056out:
Bob Peterson556bb172011-03-22 13:56:37 -04002057
2058 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002059}
2060
2061/**
2062 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
2063 * @ip: the file being written to
2064 * @filname: the filename that's going to be added
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00002065 * @da: The structure to return dir alloc info
David Teiglandb3b94fa2006-01-16 16:50:04 +00002066 *
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00002067 * Returns: 0 if ok, -ve on error
David Teiglandb3b94fa2006-01-16 16:50:04 +00002068 */
2069
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00002070int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name,
2071 struct gfs2_diradd *da)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002072{
Steven Whitehouse22b5a6c2014-01-08 11:05:29 +00002073 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00002074 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse22b5a6c2014-01-08 11:05:29 +00002075 const unsigned int extra = sizeof(struct gfs2_dinode) - sizeof(struct gfs2_leaf);
Steven Whitehousec7526662006-03-20 12:30:04 -05002076 struct gfs2_dirent *dent;
2077 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002078
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00002079 da->nr_blocks = 0;
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00002080 da->bh = NULL;
2081 da->dent = NULL;
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00002082
Steven Whitehousec7526662006-03-20 12:30:04 -05002083 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
Steven Whitehouseed386502006-04-07 16:28:07 -04002084 if (!dent) {
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00002085 da->nr_blocks = sdp->sd_max_dirres;
Steven Whitehouse22b5a6c2014-01-08 11:05:29 +00002086 if (!(ip->i_diskflags & GFS2_DIF_EXHASH) &&
2087 (GFS2_DIRENT_SIZE(name->len) < extra))
2088 da->nr_blocks = 1;
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00002089 return 0;
Steven Whitehouseed386502006-04-07 16:28:07 -04002090 }
Steven Whitehousec7526662006-03-20 12:30:04 -05002091 if (IS_ERR(dent))
2092 return PTR_ERR(dent);
Bob Peterson19aeb5a62014-09-29 08:52:04 -04002093
2094 if (da->save_loc) {
2095 da->bh = bh;
2096 da->dent = dent;
2097 } else {
2098 brelse(bh);
2099 }
Steven Whitehousec7526662006-03-20 12:30:04 -05002100 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002101}
2102