blob: c0f2875c946c9cdb0f601768876bcaa43afbe733 [file] [log] [blame]
Thomas Gleixner7336d0e2019-05-31 01:09:56 -07001// SPDX-License-Identifier: GPL-2.0-only
David Teiglandb3b94fa2006-01-16 16:50:04 +00002/*
3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04004 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00005 */
6
7/*
Steven Whitehouse61e085a2006-04-24 10:07:13 -04008 * Implements Extendible Hashing as described in:
9 * "Extendible Hashing" by Fagin, et al in
10 * __ACM Trans. on Database Systems__, Sept 1979.
11 *
12 *
13 * Here's the layout of dirents which is essentially the same as that of ext2
14 * within a single block. The field de_name_len is the number of bytes
15 * actually required for the name (no null terminator). The field de_rec_len
16 * is the number of bytes allocated to the dirent. The offset of the next
17 * dirent in the block is (dirent + dirent->de_rec_len). When a dirent is
18 * deleted, the preceding dirent inherits its allocated space, ie
19 * prev->de_rec_len += deleted->de_rec_len. Since the next dirent is obtained
20 * by adding de_rec_len to the current dirent, this essentially causes the
21 * deleted dirent to get jumped over when iterating through all the dirents.
22 *
23 * When deleting the first dirent in a block, there is no previous dirent so
24 * the field de_ino is set to zero to designate it as deleted. When allocating
25 * a dirent, gfs2_dirent_alloc iterates through the dirents in a block. If the
26 * first dirent has (de_ino == 0) and de_rec_len is large enough, this first
27 * dirent is allocated. Otherwise it must go through all the 'used' dirents
28 * searching for one in which the amount of total space minus the amount of
29 * used space will provide enough space for the new dirent.
30 *
31 * There are two types of blocks in which dirents reside. In a stuffed dinode,
32 * the dirents begin at offset sizeof(struct gfs2_dinode) from the beginning of
33 * the block. In leaves, they begin at offset sizeof(struct gfs2_leaf) from the
34 * beginning of the leaf block. The dirents reside in leaves when
35 *
Steven Whitehouse383f01f2008-11-04 10:05:22 +000036 * dip->i_diskflags & GFS2_DIF_EXHASH is true
Steven Whitehouse61e085a2006-04-24 10:07:13 -040037 *
38 * Otherwise, the dirents are "linear", within a single stuffed dinode block.
39 *
40 * When the dirents are in leaves, the actual contents of the directory file are
41 * used as an array of 64-bit block pointers pointing to the leaf blocks. The
42 * dirents are NOT in the directory file itself. There can be more than one
43 * block pointer in the array that points to the same leaf. In fact, when a
44 * directory is first converted from linear to exhash, all of the pointers
45 * point to the same leaf.
46 *
47 * When a leaf is completely full, the size of the hash table can be
48 * doubled unless it is already at the maximum size which is hard coded into
49 * GFS2_DIR_MAX_DEPTH. After that, leaves are chained together in a linked list,
50 * but never before the maximum hash table size has been reached.
51 */
David Teiglandb3b94fa2006-01-16 16:50:04 +000052
Joe Perchesd77d1b52014-03-06 12:10:45 -080053#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54
David Teiglandb3b94fa2006-01-16 16:50:04 +000055#include <linux/slab.h>
56#include <linux/spinlock.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000057#include <linux/buffer_head.h>
58#include <linux/sort.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050059#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050060#include <linux/crc32.h>
Steven Whitehousefe1bded2006-04-18 10:09:15 -040061#include <linux/vmalloc.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -060062#include <linux/bio.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000063
64#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050065#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000066#include "dir.h"
67#include "glock.h"
68#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000069#include "meta_io.h"
70#include "quota.h"
71#include "rgrp.h"
72#include "trans.h"
Steven Whitehousee13940b2006-01-30 13:31:50 +000073#include "bmap.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050074#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000075
Bob Petersondfe4d342011-10-27 12:16:06 -040076#define MAX_RA_BLOCKS 32 /* max read-ahead blocks */
77
Steven Whitehousecd915492006-09-04 12:49:07 -040078#define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
79#define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
Benjamin Marzinski471f3db2015-12-01 08:46:55 -060080#define GFS2_HASH_INDEX_MASK 0xffffc000
81#define GFS2_USE_HASH_FLAG 0x2000
David Teiglandb3b94fa2006-01-16 16:50:04 +000082
Steven Whitehouse8d123582010-09-17 12:30:23 +010083struct qstr gfs2_qdot __read_mostly;
84struct qstr gfs2_qdotdot __read_mostly;
85
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -040086typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
87 const struct qstr *name, void *opaque);
David Teiglandb3b94fa2006-01-16 16:50:04 +000088
Steven Whitehousecd915492006-09-04 12:49:07 -040089int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
Steven Whitehouse61e085a2006-04-24 10:07:13 -040090 struct buffer_head **bhp)
Steven Whitehousee13940b2006-01-30 13:31:50 +000091{
92 struct buffer_head *bh;
Steven Whitehousee13940b2006-01-30 13:31:50 +000093
Steven Whitehouse61e085a2006-04-24 10:07:13 -040094 bh = gfs2_meta_new(ip->i_gl, block);
Steven Whitehouse350a9b02012-12-14 12:36:02 +000095 gfs2_trans_add_meta(ip->i_gl, bh);
Steven Whitehouse61e085a2006-04-24 10:07:13 -040096 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
97 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
Steven Whitehousee13940b2006-01-30 13:31:50 +000098 *bhp = bh;
99 return 0;
100}
101
Steven Whitehousecd915492006-09-04 12:49:07 -0400102static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, u64 block,
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400103 struct buffer_head **bhp)
104{
105 struct buffer_head *bh;
106 int error;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000107
Andreas Gruenbacherc8d57702015-11-11 15:00:35 -0600108 error = gfs2_meta_read(ip->i_gl, block, DIO_WAIT, 0, &bh);
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400109 if (error)
110 return error;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400111 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) {
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400112 brelse(bh);
113 return -EIO;
114 }
115 *bhp = bh;
116 return 0;
117}
Steven Whitehousee13940b2006-01-30 13:31:50 +0000118
119static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
120 unsigned int offset, unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000121{
122 struct buffer_head *dibh;
123 int error;
124
125 error = gfs2_meta_inode_buffer(ip, &dibh);
126 if (error)
127 return error;
128
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000129 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500130 memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100131 if (ip->i_inode.i_size < offset + size)
132 i_size_write(&ip->i_inode, offset + size);
Deepa Dinamani078cd822016-09-14 07:48:04 -0700133 ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500134 gfs2_dinode_out(ip, dibh->b_data);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000135
136 brelse(dibh);
137
138 return size;
139}
140
141
142
143/**
144 * gfs2_dir_write_data - Write directory information to the inode
145 * @ip: The GFS2 inode
146 * @buf: The buffer containing information to be written
147 * @offset: The file offset to start writing at
148 * @size: The amount of data to write
149 *
150 * Returns: The number of bytes correctly written or error code
151 */
152static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
Steven Whitehousecd915492006-09-04 12:49:07 -0400153 u64 offset, unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000154{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400155 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000156 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400157 u64 lblock, dblock;
158 u32 extlen = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000159 unsigned int o;
160 int copied = 0;
161 int error = 0;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000162 int new = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000163
164 if (!size)
165 return 0;
166
Andreas Gruenbacher235628c2017-11-14 16:53:12 +0100167 if (gfs2_is_stuffed(ip) && offset + size <= gfs2_max_stuffed_size(ip))
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500168 return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
169 size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000170
171 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
172 return -EINVAL;
173
174 if (gfs2_is_stuffed(ip)) {
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400175 error = gfs2_unstuff_dinode(ip, NULL);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000176 if (error)
Steven Whitehousec7526662006-03-20 12:30:04 -0500177 return error;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000178 }
179
180 lblock = offset;
181 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
182
183 while (copied < size) {
184 unsigned int amount;
185 struct buffer_head *bh;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000186
187 amount = size - copied;
188 if (amount > sdp->sd_sb.sb_bsize - o)
189 amount = sdp->sd_sb.sb_bsize - o;
190
191 if (!extlen) {
192 new = 1;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400193 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
Steven Whitehousefd88de562006-05-05 16:59:11 -0400194 &dblock, &extlen);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000195 if (error)
196 goto fail;
197 error = -EIO;
198 if (gfs2_assert_withdraw(sdp, dblock))
199 goto fail;
200 }
201
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400202 if (amount == sdp->sd_jbsize || new)
203 error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
204 else
205 error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
206
Steven Whitehousee13940b2006-01-30 13:31:50 +0000207 if (error)
208 goto fail;
209
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000210 gfs2_trans_add_meta(ip->i_gl, bh);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000211 memcpy(bh->b_data + o, buf, amount);
212 brelse(bh);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000213
Steven Whitehouse899bb262006-08-01 15:28:57 -0400214 buf += amount;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000215 copied += amount;
216 lblock++;
217 dblock++;
218 extlen--;
219
220 o = sizeof(struct gfs2_meta_header);
221 }
222
223out:
224 error = gfs2_meta_inode_buffer(ip, &dibh);
225 if (error)
226 return error;
227
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100228 if (ip->i_inode.i_size < offset + copied)
229 i_size_write(&ip->i_inode, offset + copied);
Deepa Dinamani078cd822016-09-14 07:48:04 -0700230 ip->i_inode.i_mtime = ip->i_inode.i_ctime = current_time(&ip->i_inode);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000231
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000232 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500233 gfs2_dinode_out(ip, dibh->b_data);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000234 brelse(dibh);
235
236 return copied;
237fail:
238 if (copied)
239 goto out;
240 return error;
241}
242
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100243static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, __be64 *buf,
244 unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000245{
246 struct buffer_head *dibh;
247 int error;
248
249 error = gfs2_meta_inode_buffer(ip, &dibh);
250 if (!error) {
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100251 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000252 brelse(dibh);
253 }
254
255 return (error) ? error : size;
256}
257
258
259/**
260 * gfs2_dir_read_data - Read a data from a directory inode
261 * @ip: The GFS2 Inode
262 * @buf: The buffer to place result into
Steven Whitehousee13940b2006-01-30 13:31:50 +0000263 * @size: Amount of data to transfer
264 *
265 * Returns: The amount of data actually copied or the error
266 */
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100267static int gfs2_dir_read_data(struct gfs2_inode *ip, __be64 *buf,
268 unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000269{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400270 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehousecd915492006-09-04 12:49:07 -0400271 u64 lblock, dblock;
272 u32 extlen = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000273 unsigned int o;
274 int copied = 0;
275 int error = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000276
277 if (gfs2_is_stuffed(ip))
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100278 return gfs2_dir_read_stuffed(ip, buf, size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000279
280 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
281 return -EINVAL;
282
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100283 lblock = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000284 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
285
286 while (copied < size) {
287 unsigned int amount;
288 struct buffer_head *bh;
289 int new;
290
291 amount = size - copied;
292 if (amount > sdp->sd_sb.sb_bsize - o)
293 amount = sdp->sd_sb.sb_bsize - o;
294
295 if (!extlen) {
296 new = 0;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400297 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
Steven Whitehousefd88de562006-05-05 16:59:11 -0400298 &dblock, &extlen);
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400299 if (error || !dblock)
300 goto fail;
301 BUG_ON(extlen < 1);
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400302 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
Adrian Bunkb7d8ac32006-10-19 16:02:07 +0200303 } else {
Andreas Gruenbacherc8d57702015-11-11 15:00:35 -0600304 error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, 0, &bh);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000305 if (error)
306 goto fail;
307 }
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400308 error = gfs2_metatype_check(sdp, bh, GFS2_METATYPE_JD);
309 if (error) {
310 brelse(bh);
311 goto fail;
312 }
313 dblock++;
314 extlen--;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000315 memcpy(buf, bh->b_data + o, amount);
316 brelse(bh);
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100317 buf += (amount/sizeof(__be64));
Steven Whitehousee13940b2006-01-30 13:31:50 +0000318 copied += amount;
319 lblock++;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000320 o = sizeof(struct gfs2_meta_header);
321 }
322
323 return copied;
324fail:
325 return (copied) ? copied : error;
326}
327
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100328/**
329 * gfs2_dir_get_hash_table - Get pointer to the dir hash table
330 * @ip: The inode in question
331 *
332 * Returns: The hash table or an error
333 */
334
335static __be64 *gfs2_dir_get_hash_table(struct gfs2_inode *ip)
336{
337 struct inode *inode = &ip->i_inode;
338 int ret;
339 u32 hsize;
340 __be64 *hc;
341
342 BUG_ON(!(ip->i_diskflags & GFS2_DIF_EXHASH));
343
344 hc = ip->i_hash_cache;
345 if (hc)
346 return hc;
347
Fabian Frederick47a9a522016-08-02 12:05:27 -0500348 hsize = BIT(ip->i_depth);
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100349 hsize *= sizeof(__be64);
350 if (hsize != i_size_read(&ip->i_inode)) {
351 gfs2_consist_inode(ip);
352 return ERR_PTR(-EIO);
353 }
354
Bob Petersone8830d82013-05-30 09:48:56 -0400355 hc = kmalloc(hsize, GFP_NOFS | __GFP_NOWARN);
356 if (hc == NULL)
Christoph Hellwig88dca4c2020-06-01 21:51:40 -0700357 hc = __vmalloc(hsize, GFP_NOFS);
Bob Petersone8830d82013-05-30 09:48:56 -0400358
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100359 if (hc == NULL)
360 return ERR_PTR(-ENOMEM);
361
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100362 ret = gfs2_dir_read_data(ip, hc, hsize);
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100363 if (ret < 0) {
Al Viro3cdcf632014-11-20 05:18:38 +0000364 kvfree(hc);
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100365 return ERR_PTR(ret);
366 }
367
368 spin_lock(&inode->i_lock);
Al Viro9265f1d2014-11-20 05:19:47 +0000369 if (likely(!ip->i_hash_cache)) {
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100370 ip->i_hash_cache = hc;
Al Viro9265f1d2014-11-20 05:19:47 +0000371 hc = NULL;
372 }
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100373 spin_unlock(&inode->i_lock);
Al Viro9265f1d2014-11-20 05:19:47 +0000374 kvfree(hc);
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100375
376 return ip->i_hash_cache;
377}
378
379/**
380 * gfs2_dir_hash_inval - Invalidate dir hash
381 * @ip: The directory inode
382 *
383 * Must be called with an exclusive glock, or during glock invalidation.
384 */
385void gfs2_dir_hash_inval(struct gfs2_inode *ip)
386{
Bob Petersonc36b97e2015-10-29 10:03:41 -0500387 __be64 *hc;
388
389 spin_lock(&ip->i_inode.i_lock);
390 hc = ip->i_hash_cache;
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100391 ip->i_hash_cache = NULL;
Bob Petersonc36b97e2015-10-29 10:03:41 -0500392 spin_unlock(&ip->i_inode.i_lock);
393
Al Viro3cdcf632014-11-20 05:18:38 +0000394 kvfree(hc);
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100395}
396
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500397static inline int gfs2_dirent_sentinel(const struct gfs2_dirent *dent)
398{
399 return dent->de_inum.no_addr == 0 || dent->de_inum.no_formal_ino == 0;
400}
401
Steven Whitehousec7526662006-03-20 12:30:04 -0500402static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
403 const struct qstr *name, int ret)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000404{
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500405 if (!gfs2_dirent_sentinel(dent) &&
Steven Whitehousec7526662006-03-20 12:30:04 -0500406 be32_to_cpu(dent->de_hash) == name->hash &&
407 be16_to_cpu(dent->de_name_len) == name->len &&
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400408 memcmp(dent+1, name->name, name->len) == 0)
Steven Whitehousec7526662006-03-20 12:30:04 -0500409 return ret;
410 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000411}
412
Steven Whitehousec7526662006-03-20 12:30:04 -0500413static int gfs2_dirent_find(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500414 const struct qstr *name,
415 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500416{
417 return __gfs2_dirent_find(dent, name, 1);
418}
419
420static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500421 const struct qstr *name,
422 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500423{
424 return __gfs2_dirent_find(dent, name, 2);
425}
426
427/*
428 * name->name holds ptr to start of block.
429 * name->len holds size of block.
430 */
431static int gfs2_dirent_last(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500432 const struct qstr *name,
433 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500434{
435 const char *start = name->name;
436 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
437 if (name->len == (end - start))
438 return 1;
439 return 0;
440}
441
Benjamin Marzinski34017472015-12-01 08:30:34 -0600442/* Look for the dirent that contains the offset specified in data. Once we
443 * find that dirent, there must be space available there for the new dirent */
444static int gfs2_dirent_find_offset(const struct gfs2_dirent *dent,
445 const struct qstr *name,
446 void *ptr)
447{
448 unsigned required = GFS2_DIRENT_SIZE(name->len);
449 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
450 unsigned totlen = be16_to_cpu(dent->de_rec_len);
451
452 if (ptr < (void *)dent || ptr >= (void *)dent + totlen)
453 return 0;
454 if (gfs2_dirent_sentinel(dent))
455 actual = 0;
456 if (ptr < (void *)dent + actual)
457 return -1;
458 if ((void *)dent + totlen >= ptr + required)
459 return 1;
460 return -1;
461}
462
Steven Whitehousec7526662006-03-20 12:30:04 -0500463static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500464 const struct qstr *name,
465 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500466{
467 unsigned required = GFS2_DIRENT_SIZE(name->len);
468 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
469 unsigned totlen = be16_to_cpu(dent->de_rec_len);
470
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500471 if (gfs2_dirent_sentinel(dent))
Bob Peterson728a7562010-07-14 18:12:26 -0400472 actual = 0;
Jan Engelhardtc53921242006-09-05 14:30:40 +0200473 if (totlen - actual >= required)
Steven Whitehousec7526662006-03-20 12:30:04 -0500474 return 1;
475 return 0;
476}
477
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500478struct dirent_gather {
479 const struct gfs2_dirent **pdent;
480 unsigned offset;
481};
482
483static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
484 const struct qstr *name,
485 void *opaque)
486{
487 struct dirent_gather *g = opaque;
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500488 if (!gfs2_dirent_sentinel(dent)) {
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500489 g->pdent[g->offset++] = dent;
490 }
491 return 0;
492}
493
Steven Whitehousec7526662006-03-20 12:30:04 -0500494/*
495 * Other possible things to check:
496 * - Inode located within filesystem size (and on valid block)
497 * - Valid directory entry type
498 * Not sure how heavy-weight we want to make this... could also check
499 * hash is correct for example, but that would take a lot of extra time.
500 * For now the most important thing is to check that the various sizes
501 * are correct.
502 */
Bob Petersone54c78a2018-10-03 08:47:36 -0500503static int gfs2_check_dirent(struct gfs2_sbd *sdp,
504 struct gfs2_dirent *dent, unsigned int offset,
Steven Whitehousec7526662006-03-20 12:30:04 -0500505 unsigned int size, unsigned int len, int first)
506{
507 const char *msg = "gfs2_dirent too small";
508 if (unlikely(size < sizeof(struct gfs2_dirent)))
509 goto error;
510 msg = "gfs2_dirent misaligned";
511 if (unlikely(offset & 0x7))
512 goto error;
513 msg = "gfs2_dirent points beyond end of block";
514 if (unlikely(offset + size > len))
515 goto error;
516 msg = "zero inode number";
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500517 if (unlikely(!first && gfs2_dirent_sentinel(dent)))
Steven Whitehousec7526662006-03-20 12:30:04 -0500518 goto error;
519 msg = "name length is greater than space in dirent";
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500520 if (!gfs2_dirent_sentinel(dent) &&
Steven Whitehousec7526662006-03-20 12:30:04 -0500521 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
522 size))
523 goto error;
524 return 0;
525error:
Bob Petersone54c78a2018-10-03 08:47:36 -0500526 fs_warn(sdp, "%s: %s (%s)\n",
Joe Perchesd77d1b52014-03-06 12:10:45 -0800527 __func__, msg, first ? "first in block" : "not first in block");
Steven Whitehousec7526662006-03-20 12:30:04 -0500528 return -EIO;
529}
530
Bob Petersone54c78a2018-10-03 08:47:36 -0500531static int gfs2_dirent_offset(struct gfs2_sbd *sdp, const void *buf)
Steven Whitehousec7526662006-03-20 12:30:04 -0500532{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500533 const struct gfs2_meta_header *h = buf;
534 int offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500535
536 BUG_ON(buf == NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500537
Steven Whitehousee3167de2006-03-30 15:46:23 -0500538 switch(be32_to_cpu(h->mh_type)) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500539 case GFS2_METATYPE_LF:
540 offset = sizeof(struct gfs2_leaf);
541 break;
542 case GFS2_METATYPE_DI:
543 offset = sizeof(struct gfs2_dinode);
544 break;
545 default:
546 goto wrong_type;
547 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500548 return offset;
549wrong_type:
Bob Petersone54c78a2018-10-03 08:47:36 -0500550 fs_warn(sdp, "%s: wrong block type %u\n", __func__,
551 be32_to_cpu(h->mh_type));
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500552 return -1;
553}
Steven Whitehousec7526662006-03-20 12:30:04 -0500554
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400555static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500556 unsigned int len, gfs2_dscan_t scan,
557 const struct qstr *name,
558 void *opaque)
559{
560 struct gfs2_dirent *dent, *prev;
561 unsigned offset;
562 unsigned size;
563 int ret = 0;
564
Bob Petersone54c78a2018-10-03 08:47:36 -0500565 ret = gfs2_dirent_offset(GFS2_SB(inode), buf);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500566 if (ret < 0)
567 goto consist_inode;
568
569 offset = ret;
Steven Whitehousec7526662006-03-20 12:30:04 -0500570 prev = NULL;
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400571 dent = buf + offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500572 size = be16_to_cpu(dent->de_rec_len);
Bob Petersone54c78a2018-10-03 08:47:36 -0500573 if (gfs2_check_dirent(GFS2_SB(inode), dent, offset, size, len, 1))
Steven Whitehousec7526662006-03-20 12:30:04 -0500574 goto consist_inode;
575 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500576 ret = scan(dent, name, opaque);
Steven Whitehousec7526662006-03-20 12:30:04 -0500577 if (ret)
578 break;
579 offset += size;
580 if (offset == len)
581 break;
582 prev = dent;
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400583 dent = buf + offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500584 size = be16_to_cpu(dent->de_rec_len);
Bob Petersone54c78a2018-10-03 08:47:36 -0500585 if (gfs2_check_dirent(GFS2_SB(inode), dent, offset, size,
586 len, 0))
Steven Whitehousec7526662006-03-20 12:30:04 -0500587 goto consist_inode;
588 } while(1);
589
590 switch(ret) {
591 case 0:
592 return NULL;
593 case 1:
594 return dent;
595 case 2:
596 return prev ? prev : dent;
597 default:
598 BUG_ON(ret > 0);
599 return ERR_PTR(ret);
600 }
601
Steven Whitehousec7526662006-03-20 12:30:04 -0500602consist_inode:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400603 gfs2_consist_inode(GFS2_I(inode));
Steven Whitehousec7526662006-03-20 12:30:04 -0500604 return ERR_PTR(-EIO);
605}
606
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400607static int dirent_check_reclen(struct gfs2_inode *dip,
608 const struct gfs2_dirent *d, const void *end_p)
609{
610 const void *ptr = d;
611 u16 rec_len = be16_to_cpu(d->de_rec_len);
612
613 if (unlikely(rec_len < sizeof(struct gfs2_dirent)))
614 goto broken;
615 ptr += rec_len;
616 if (ptr < end_p)
617 return rec_len;
618 if (ptr == end_p)
619 return -ENOENT;
620broken:
621 gfs2_consist_inode(dip);
622 return -EIO;
623}
624
David Teiglandb3b94fa2006-01-16 16:50:04 +0000625/**
626 * dirent_next - Next dirent
627 * @dip: the directory
628 * @bh: The buffer
629 * @dent: Pointer to list of dirents
630 *
631 * Returns: 0 on success, error code otherwise
632 */
633
634static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
635 struct gfs2_dirent **dent)
636{
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400637 struct gfs2_dirent *cur = *dent, *tmp;
638 char *bh_end = bh->b_data + bh->b_size;
639 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000640
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400641 ret = dirent_check_reclen(dip, cur, bh_end);
642 if (ret < 0)
643 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000644
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400645 tmp = (void *)cur + ret;
646 ret = dirent_check_reclen(dip, tmp, bh_end);
647 if (ret == -EIO)
648 return ret;
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000649
David Teiglandb3b94fa2006-01-16 16:50:04 +0000650 /* Only the first dent could ever have de_inum.no_addr == 0 */
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500651 if (gfs2_dirent_sentinel(tmp)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000652 gfs2_consist_inode(dip);
653 return -EIO;
654 }
655
656 *dent = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000657 return 0;
658}
659
660/**
661 * dirent_del - Delete a dirent
662 * @dip: The GFS2 inode
663 * @bh: The buffer
664 * @prev: The previous dirent
665 * @cur: The current dirent
666 *
667 */
668
669static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
670 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
671{
Steven Whitehousecd915492006-09-04 12:49:07 -0400672 u16 cur_rec_len, prev_rec_len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000673
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500674 if (gfs2_dirent_sentinel(cur)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000675 gfs2_consist_inode(dip);
676 return;
677 }
678
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000679 gfs2_trans_add_meta(dip->i_gl, bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000680
681 /* If there is no prev entry, this is the first entry in the block.
682 The de_rec_len is already as big as it needs to be. Just zero
683 out the inode number and return. */
684
685 if (!prev) {
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500686 cur->de_inum.no_addr = 0;
687 cur->de_inum.no_formal_ino = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000688 return;
689 }
690
691 /* Combine this dentry with the previous one. */
692
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000693 prev_rec_len = be16_to_cpu(prev->de_rec_len);
694 cur_rec_len = be16_to_cpu(cur->de_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000695
696 if ((char *)prev + prev_rec_len != (char *)cur)
697 gfs2_consist_inode(dip);
698 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
699 gfs2_consist_inode(dip);
700
701 prev_rec_len += cur_rec_len;
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000702 prev->de_rec_len = cpu_to_be16(prev_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000703}
704
Benjamin Marzinski34017472015-12-01 08:30:34 -0600705
706static struct gfs2_dirent *do_init_dirent(struct inode *inode,
707 struct gfs2_dirent *dent,
708 const struct qstr *name,
709 struct buffer_head *bh,
710 unsigned offset)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000711{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400712 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -0500713 struct gfs2_dirent *ndent;
Benjamin Marzinski34017472015-12-01 08:30:34 -0600714 unsigned totlen;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000715
Steven Whitehousec7526662006-03-20 12:30:04 -0500716 totlen = be16_to_cpu(dent->de_rec_len);
717 BUG_ON(offset + name->len > totlen);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000718 gfs2_trans_add_meta(ip->i_gl, bh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500719 ndent = (struct gfs2_dirent *)((char *)dent + offset);
720 dent->de_rec_len = cpu_to_be16(offset);
721 gfs2_qstr2dirent(name, totlen - offset, ndent);
722 return ndent;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000723}
724
Benjamin Marzinski34017472015-12-01 08:30:34 -0600725
726/*
727 * Takes a dent from which to grab space as an argument. Returns the
728 * newly created dent.
729 */
730static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
731 struct gfs2_dirent *dent,
732 const struct qstr *name,
733 struct buffer_head *bh)
734{
735 unsigned offset = 0;
736
737 if (!gfs2_dirent_sentinel(dent))
738 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
739 return do_init_dirent(inode, dent, name, bh, offset);
740}
741
742static struct gfs2_dirent *gfs2_dirent_split_alloc(struct inode *inode,
743 struct buffer_head *bh,
744 const struct qstr *name,
745 void *ptr)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000746{
747 struct gfs2_dirent *dent;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400748 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
Benjamin Marzinski34017472015-12-01 08:30:34 -0600749 gfs2_dirent_find_offset, name, ptr);
Kefeng Wang15a798f2019-06-05 22:24:24 +0800750 if (IS_ERR_OR_NULL(dent))
Steven Whitehousec7526662006-03-20 12:30:04 -0500751 return dent;
Benjamin Marzinski34017472015-12-01 08:30:34 -0600752 return do_init_dirent(inode, dent, name, bh,
753 (unsigned)(ptr - (void *)dent));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000754}
755
Steven Whitehousecd915492006-09-04 12:49:07 -0400756static int get_leaf(struct gfs2_inode *dip, u64 leaf_no,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000757 struct buffer_head **bhp)
758{
759 int error;
760
Andreas Gruenbacherc8d57702015-11-11 15:00:35 -0600761 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, 0, bhp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400762 if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
Joe Perchesd77d1b52014-03-06 12:10:45 -0800763 /* pr_info("block num=%llu\n", leaf_no); */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000764 error = -EIO;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400765 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000766
767 return error;
768}
769
770/**
771 * get_leaf_nr - Get a leaf number associated with the index
772 * @dip: The GFS2 inode
773 * @index:
774 * @leaf_out:
775 *
776 * Returns: 0 on success, error code otherwise
777 */
778
Steven Whitehousecd915492006-09-04 12:49:07 -0400779static int get_leaf_nr(struct gfs2_inode *dip, u32 index,
780 u64 *leaf_out)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000781{
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100782 __be64 *hash;
Arnd Bergmann287980e2016-05-27 23:23:25 +0200783 int error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000784
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100785 hash = gfs2_dir_get_hash_table(dip);
Arnd Bergmann287980e2016-05-27 23:23:25 +0200786 error = PTR_ERR_OR_ZERO(hash);
787
788 if (!error)
789 *leaf_out = be64_to_cpu(*(hash + index));
790
791 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000792}
793
Steven Whitehousecd915492006-09-04 12:49:07 -0400794static int get_first_leaf(struct gfs2_inode *dip, u32 index,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000795 struct buffer_head **bh_out)
796{
Steven Whitehousecd915492006-09-04 12:49:07 -0400797 u64 leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000798 int error;
799
800 error = get_leaf_nr(dip, index, &leaf_no);
Arnd Bergmann287980e2016-05-27 23:23:25 +0200801 if (!error)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000802 error = get_leaf(dip, leaf_no, bh_out);
803
804 return error;
805}
806
Steven Whitehousec7526662006-03-20 12:30:04 -0500807static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
808 const struct qstr *name,
809 gfs2_dscan_t scan,
810 struct buffer_head **pbh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000811{
Steven Whitehousec7526662006-03-20 12:30:04 -0500812 struct buffer_head *bh;
813 struct gfs2_dirent *dent;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400814 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000815 int error;
816
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000817 if (ip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500818 struct gfs2_leaf *leaf;
Fabian Frederick47a9a522016-08-02 12:05:27 -0500819 unsigned int hsize = BIT(ip->i_depth);
820 unsigned int index;
Steven Whitehousec7526662006-03-20 12:30:04 -0500821 u64 ln;
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100822 if (hsize * sizeof(u64) != i_size_read(inode)) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500823 gfs2_consist_inode(ip);
824 return ERR_PTR(-EIO);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000825 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400826
Steven Whitehouse9a004502008-02-01 09:23:44 +0000827 index = name->hash >> (32 - ip->i_depth);
Steven Whitehousec7526662006-03-20 12:30:04 -0500828 error = get_first_leaf(ip, index, &bh);
829 if (error)
830 return ERR_PTR(error);
831 do {
832 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500833 scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500834 if (dent)
835 goto got_dent;
836 leaf = (struct gfs2_leaf *)bh->b_data;
837 ln = be64_to_cpu(leaf->lf_next);
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400838 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500839 if (!ln)
840 break;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400841
Steven Whitehousec7526662006-03-20 12:30:04 -0500842 error = get_leaf(ip, ln, &bh);
843 } while(!error);
844
845 return error ? ERR_PTR(error) : NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000846 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000847
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400848
Steven Whitehousec7526662006-03-20 12:30:04 -0500849 error = gfs2_meta_inode_buffer(ip, &bh);
850 if (error)
851 return ERR_PTR(error);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500852 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500853got_dent:
Kefeng Wang15a798f2019-06-05 22:24:24 +0800854 if (IS_ERR_OR_NULL(dent)) {
Steven Whitehouseed386502006-04-07 16:28:07 -0400855 brelse(bh);
856 bh = NULL;
857 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500858 *pbh = bh;
859 return dent;
860}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000861
Steven Whitehousec7526662006-03-20 12:30:04 -0500862static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
863{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400864 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000865 unsigned int n = 1;
Steven Whitehouse09010972009-05-20 10:48:47 +0100866 u64 bn;
867 int error;
868 struct buffer_head *bh;
Steven Whitehousec7526662006-03-20 12:30:04 -0500869 struct gfs2_leaf *leaf;
870 struct gfs2_dirent *dent;
Deepa Dinamani95582b02018-05-08 19:36:02 -0700871 struct timespec64 tv = current_time(inode);
Steven Whitehouse09010972009-05-20 10:48:47 +0100872
Bob Peterson6e87ed02011-11-18 10:58:32 -0500873 error = gfs2_alloc_blocks(ip, &bn, &n, 0, NULL);
Steven Whitehouse09010972009-05-20 10:48:47 +0100874 if (error)
875 return NULL;
876 bh = gfs2_meta_new(ip->i_gl, bn);
Steven Whitehousec7526662006-03-20 12:30:04 -0500877 if (!bh)
878 return NULL;
Steven Whitehouse09010972009-05-20 10:48:47 +0100879
Andreas Gruenbacherfbb27872019-04-05 12:18:23 +0100880 gfs2_trans_remove_revoke(GFS2_SB(inode), bn, 1);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000881 gfs2_trans_add_meta(ip->i_gl, bh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500882 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
883 leaf = (struct gfs2_leaf *)bh->b_data;
884 leaf->lf_depth = cpu_to_be16(depth);
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400885 leaf->lf_entries = 0;
Al Viroa2d7d022006-10-14 16:49:30 +0100886 leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400887 leaf->lf_next = 0;
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +0000888 leaf->lf_inode = cpu_to_be64(ip->i_no_addr);
889 leaf->lf_dist = cpu_to_be32(1);
890 leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
891 leaf->lf_sec = cpu_to_be64(tv.tv_sec);
892 memset(leaf->lf_reserved2, 0, sizeof(leaf->lf_reserved2));
Steven Whitehousec7526662006-03-20 12:30:04 -0500893 dent = (struct gfs2_dirent *)(leaf+1);
David Howellscdf01222017-07-04 17:25:22 +0100894 gfs2_qstr2dirent(&empty_name, bh->b_size - sizeof(struct gfs2_leaf), dent);
Steven Whitehousec7526662006-03-20 12:30:04 -0500895 *pbh = bh;
896 return leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000897}
898
899/**
900 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
901 * @dip: The GFS2 inode
902 *
903 * Returns: 0 on success, error code otherwise
904 */
905
Steven Whitehousec7526662006-03-20 12:30:04 -0500906static int dir_make_exhash(struct inode *inode)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000907{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400908 struct gfs2_inode *dip = GFS2_I(inode);
909 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000910 struct gfs2_dirent *dent;
Steven Whitehousec7526662006-03-20 12:30:04 -0500911 struct qstr args;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000912 struct buffer_head *bh, *dibh;
913 struct gfs2_leaf *leaf;
914 int y;
Steven Whitehousecd915492006-09-04 12:49:07 -0400915 u32 x;
Al Virob44b84d2006-10-14 10:46:30 -0400916 __be64 *lp;
917 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000918 int error;
919
920 error = gfs2_meta_inode_buffer(dip, &dibh);
921 if (error)
922 return error;
923
David Teiglandb3b94fa2006-01-16 16:50:04 +0000924 /* Turn over a new leaf */
925
Steven Whitehousec7526662006-03-20 12:30:04 -0500926 leaf = new_leaf(inode, &bh, 0);
927 if (!leaf)
928 return -ENOSPC;
929 bn = bh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000930
Fabian Frederick47a9a522016-08-02 12:05:27 -0500931 gfs2_assert(sdp, dip->i_entries < BIT(16));
Steven Whitehousead6203f22008-11-03 13:59:19 +0000932 leaf->lf_entries = cpu_to_be16(dip->i_entries);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000933
934 /* Copy dirents */
935
936 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
937 sizeof(struct gfs2_dinode));
938
939 /* Find last entry */
940
941 x = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500942 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
943 sizeof(struct gfs2_leaf);
944 args.name = bh->b_data;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400945 dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500946 gfs2_dirent_last, &args, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500947 if (!dent) {
948 brelse(bh);
949 brelse(dibh);
950 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000951 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500952 if (IS_ERR(dent)) {
953 brelse(bh);
954 brelse(dibh);
955 return PTR_ERR(dent);
956 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000957
958 /* Adjust the last dirent's record length
959 (Remember that dent still points to the last entry.) */
960
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000961 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000962 sizeof(struct gfs2_dinode) -
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000963 sizeof(struct gfs2_leaf));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000964
965 brelse(bh);
966
967 /* We're done with the new leaf block, now setup the new
968 hash table. */
969
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000970 gfs2_trans_add_meta(dip->i_gl, dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000971 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
972
Al Virob44b84d2006-10-14 10:46:30 -0400973 lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000974
975 for (x = sdp->sd_hash_ptrs; x--; lp++)
976 *lp = cpu_to_be64(bn);
977
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100978 i_size_write(inode, sdp->sd_sb.sb_bsize / 2);
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000979 gfs2_add_inode_blocks(&dip->i_inode, 1);
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000980 dip->i_diskflags |= GFS2_DIF_EXHASH;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000981
982 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000983 dip->i_depth = y;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000984
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500985 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000986
987 brelse(dibh);
988
989 return 0;
990}
991
992/**
993 * dir_split_leaf - Split a leaf block into two
994 * @dip: The GFS2 inode
995 * @index:
996 * @leaf_no:
997 *
998 * Returns: 0 on success, error code on failure
999 */
1000
Steven Whitehousec7526662006-03-20 12:30:04 -05001001static int dir_split_leaf(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001002{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001003 struct gfs2_inode *dip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001004 struct buffer_head *nbh, *obh, *dibh;
1005 struct gfs2_leaf *nleaf, *oleaf;
Steven Whitehouse4da3c642006-07-11 13:19:13 -04001006 struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
Steven Whitehousecd915492006-09-04 12:49:07 -04001007 u32 start, len, half_len, divider;
Al Virob44b84d2006-10-14 10:46:30 -04001008 u64 bn, leaf_no;
1009 __be64 *lp;
Steven Whitehousecd915492006-09-04 12:49:07 -04001010 u32 index;
Colin Ian Kingd1b0cb92018-07-17 15:59:28 +01001011 int x;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001012 int error;
1013
Steven Whitehouse9a004502008-02-01 09:23:44 +00001014 index = name->hash >> (32 - dip->i_depth);
Steven Whitehousec7526662006-03-20 12:30:04 -05001015 error = get_leaf_nr(dip, index, &leaf_no);
Arnd Bergmann287980e2016-05-27 23:23:25 +02001016 if (error)
Steven Whitehousec7526662006-03-20 12:30:04 -05001017 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001018
1019 /* Get the old leaf block */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001020 error = get_leaf(dip, leaf_no, &obh);
1021 if (error)
Steven Whitehousee90deff2006-03-29 19:02:15 -05001022 return error;
1023
1024 oleaf = (struct gfs2_leaf *)obh->b_data;
Steven Whitehouse9a004502008-02-01 09:23:44 +00001025 if (dip->i_depth == be16_to_cpu(oleaf->lf_depth)) {
Steven Whitehousee90deff2006-03-29 19:02:15 -05001026 brelse(obh);
1027 return 1; /* can't split */
1028 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001029
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001030 gfs2_trans_add_meta(dip->i_gl, obh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001031
Steven Whitehousec7526662006-03-20 12:30:04 -05001032 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
1033 if (!nleaf) {
1034 brelse(obh);
1035 return -ENOSPC;
1036 }
1037 bn = nbh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001038
Steven Whitehousec7526662006-03-20 12:30:04 -05001039 /* Compute the start and len of leaf pointers in the hash table. */
Fabian Frederick47a9a522016-08-02 12:05:27 -05001040 len = BIT(dip->i_depth - be16_to_cpu(oleaf->lf_depth));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001041 half_len = len >> 1;
1042 if (!half_len) {
Bob Petersone54c78a2018-10-03 08:47:36 -05001043 fs_warn(GFS2_SB(inode), "i_depth %u lf_depth %u index %u\n",
Joe Perchesd77d1b52014-03-06 12:10:45 -08001044 dip->i_depth, be16_to_cpu(oleaf->lf_depth), index);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001045 gfs2_consist_inode(dip);
1046 error = -EIO;
1047 goto fail_brelse;
1048 }
1049
1050 start = (index & ~(len - 1));
1051
1052 /* Change the pointers.
1053 Don't bother distinguishing stuffed from non-stuffed.
1054 This code is complicated enough already. */
Kees Cook6da2ec52018-06-12 13:55:00 -07001055 lp = kmalloc_array(half_len, sizeof(__be64), GFP_NOFS);
David Rientjes4244b522010-07-20 19:45:03 -07001056 if (!lp) {
1057 error = -ENOMEM;
1058 goto fail_brelse;
1059 }
1060
David Teiglandb3b94fa2006-01-16 16:50:04 +00001061 /* Change the pointers */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001062 for (x = 0; x < half_len; x++)
1063 lp[x] = cpu_to_be64(bn);
1064
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001065 gfs2_dir_hash_inval(dip);
1066
Steven Whitehousecd915492006-09-04 12:49:07 -04001067 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
1068 half_len * sizeof(u64));
1069 if (error != half_len * sizeof(u64)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001070 if (error >= 0)
1071 error = -EIO;
1072 goto fail_lpfree;
1073 }
1074
1075 kfree(lp);
1076
1077 /* Compute the divider */
Steven Whitehouse9a004502008-02-01 09:23:44 +00001078 divider = (start + half_len) << (32 - dip->i_depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001079
1080 /* Copy the entries */
Steven Whitehouse15793432009-11-06 11:06:37 +00001081 dent = (struct gfs2_dirent *)(obh->b_data + sizeof(struct gfs2_leaf));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001082
1083 do {
1084 next = dent;
1085 if (dirent_next(dip, obh, &next))
1086 next = NULL;
1087
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -05001088 if (!gfs2_dirent_sentinel(dent) &&
David Teiglandb3b94fa2006-01-16 16:50:04 +00001089 be32_to_cpu(dent->de_hash) < divider) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001090 struct qstr str;
Benjamin Marzinski34017472015-12-01 08:30:34 -06001091 void *ptr = ((char *)dent - obh->b_data) + nbh->b_data;
Steven Whitehousec7526662006-03-20 12:30:04 -05001092 str.name = (char*)(dent+1);
1093 str.len = be16_to_cpu(dent->de_name_len);
1094 str.hash = be32_to_cpu(dent->de_hash);
Benjamin Marzinski34017472015-12-01 08:30:34 -06001095 new = gfs2_dirent_split_alloc(inode, nbh, &str, ptr);
Steven Whitehousec7526662006-03-20 12:30:04 -05001096 if (IS_ERR(new)) {
1097 error = PTR_ERR(new);
1098 break;
1099 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001100
1101 new->de_inum = dent->de_inum; /* No endian worries */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001102 new->de_type = dent->de_type; /* No endian worries */
Marcin Slusarzbb16b342008-02-13 00:06:10 +01001103 be16_add_cpu(&nleaf->lf_entries, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001104
1105 dirent_del(dip, obh, prev, dent);
1106
1107 if (!oleaf->lf_entries)
1108 gfs2_consist_inode(dip);
Marcin Slusarzbb16b342008-02-13 00:06:10 +01001109 be16_add_cpu(&oleaf->lf_entries, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001110
1111 if (!prev)
1112 prev = dent;
Steven Whitehousec7526662006-03-20 12:30:04 -05001113 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001114 prev = dent;
Steven Whitehousec7526662006-03-20 12:30:04 -05001115 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001116 dent = next;
Steven Whitehousec7526662006-03-20 12:30:04 -05001117 } while (dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001118
Steven Whitehousec7526662006-03-20 12:30:04 -05001119 oleaf->lf_depth = nleaf->lf_depth;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001120
1121 error = gfs2_meta_inode_buffer(dip, &dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001122 if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001123 gfs2_trans_add_meta(dip->i_gl, dibh);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001124 gfs2_add_inode_blocks(&dip->i_inode, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001125 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001126 brelse(dibh);
1127 }
1128
1129 brelse(obh);
1130 brelse(nbh);
1131
1132 return error;
1133
Steven Whitehousee90deff2006-03-29 19:02:15 -05001134fail_lpfree:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001135 kfree(lp);
1136
Steven Whitehousee90deff2006-03-29 19:02:15 -05001137fail_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001138 brelse(obh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001139 brelse(nbh);
1140 return error;
1141}
1142
1143/**
1144 * dir_double_exhash - Double size of ExHash table
1145 * @dip: The GFS2 dinode
1146 *
1147 * Returns: 0 on success, error code on failure
1148 */
1149
1150static int dir_double_exhash(struct gfs2_inode *dip)
1151{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001152 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001153 u32 hsize;
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001154 u32 hsize_bytes;
1155 __be64 *hc;
1156 __be64 *hc2, *h;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001157 int x;
1158 int error = 0;
1159
Fabian Frederick47a9a522016-08-02 12:05:27 -05001160 hsize = BIT(dip->i_depth);
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001161 hsize_bytes = hsize * sizeof(__be64);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001162
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001163 hc = gfs2_dir_get_hash_table(dip);
1164 if (IS_ERR(hc))
1165 return PTR_ERR(hc);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001166
Kees Cook6da2ec52018-06-12 13:55:00 -07001167 hc2 = kmalloc_array(hsize_bytes, 2, GFP_NOFS | __GFP_NOWARN);
Bob Petersone8830d82013-05-30 09:48:56 -04001168 if (hc2 == NULL)
Christoph Hellwig88dca4c2020-06-01 21:51:40 -07001169 hc2 = __vmalloc(hsize_bytes * 2, GFP_NOFS);
Bob Petersone8830d82013-05-30 09:48:56 -04001170
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001171 if (!hc2)
David Rientjes4244b522010-07-20 19:45:03 -07001172 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001173
Bob Peterson512cbf02013-06-14 08:39:18 -04001174 h = hc2;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001175 error = gfs2_meta_inode_buffer(dip, &dibh);
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001176 if (error)
1177 goto out_kfree;
1178
1179 for (x = 0; x < hsize; x++) {
1180 *h++ = *hc;
1181 *h++ = *hc;
1182 hc++;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001183 }
1184
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001185 error = gfs2_dir_write_data(dip, (char *)hc2, 0, hsize_bytes * 2);
1186 if (error != (hsize_bytes * 2))
1187 goto fail;
1188
1189 gfs2_dir_hash_inval(dip);
1190 dip->i_hash_cache = hc2;
1191 dip->i_depth++;
1192 gfs2_dinode_out(dip, dibh->b_data);
1193 brelse(dibh);
1194 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001195
Steven Whitehousea91ea692006-09-04 12:04:26 -04001196fail:
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001197 /* Replace original hash table & size */
1198 gfs2_dir_write_data(dip, (char *)hc, 0, hsize_bytes);
1199 i_size_write(&dip->i_inode, hsize_bytes);
1200 gfs2_dinode_out(dip, dibh->b_data);
1201 brelse(dibh);
1202out_kfree:
Al Viro3cdcf632014-11-20 05:18:38 +00001203 kvfree(hc2);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001204 return error;
1205}
1206
1207/**
1208 * compare_dents - compare directory entries by hash value
1209 * @a: first dent
1210 * @b: second dent
1211 *
1212 * When comparing the hash entries of @a to @b:
1213 * gt: returns 1
1214 * lt: returns -1
1215 * eq: returns 0
1216 */
1217
1218static int compare_dents(const void *a, const void *b)
1219{
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001220 const struct gfs2_dirent *dent_a, *dent_b;
Steven Whitehousecd915492006-09-04 12:49:07 -04001221 u32 hash_a, hash_b;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001222 int ret = 0;
1223
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001224 dent_a = *(const struct gfs2_dirent **)a;
Benjamin Marzinski471f3db2015-12-01 08:46:55 -06001225 hash_a = dent_a->de_cookie;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001226
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001227 dent_b = *(const struct gfs2_dirent **)b;
Benjamin Marzinski471f3db2015-12-01 08:46:55 -06001228 hash_b = dent_b->de_cookie;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001229
1230 if (hash_a > hash_b)
1231 ret = 1;
1232 else if (hash_a < hash_b)
1233 ret = -1;
1234 else {
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001235 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1236 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001237
1238 if (len_a > len_b)
1239 ret = 1;
1240 else if (len_a < len_b)
1241 ret = -1;
1242 else
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001243 ret = memcmp(dent_a + 1, dent_b + 1, len_a);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001244 }
1245
1246 return ret;
1247}
1248
1249/**
1250 * do_filldir_main - read out directory entries
1251 * @dip: The GFS2 inode
Al Virod81a8ef2013-05-16 14:14:48 -04001252 * @ctx: what to feed the entries to
David Teiglandb3b94fa2006-01-16 16:50:04 +00001253 * @darr: an array of struct gfs2_dirent pointers to read
1254 * @entries: the number of entries in darr
1255 * @copied: pointer to int that's non-zero if a entry has been copied out
1256 *
1257 * Jump through some hoops to make sure that if there are hash collsions,
1258 * they are read out at the beginning of a buffer. We want to minimize
1259 * the possibility that they will fall into different readdir buffers or
1260 * that someone will want to seek to that location.
1261 *
Al Virod81a8ef2013-05-16 14:14:48 -04001262 * Returns: errno, >0 if the actor tells you to stop
David Teiglandb3b94fa2006-01-16 16:50:04 +00001263 */
1264
Al Virod81a8ef2013-05-16 14:14:48 -04001265static int do_filldir_main(struct gfs2_inode *dip, struct dir_context *ctx,
Benjamin Marzinski471f3db2015-12-01 08:46:55 -06001266 struct gfs2_dirent **darr, u32 entries,
1267 u32 sort_start, int *copied)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001268{
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001269 const struct gfs2_dirent *dent, *dent_next;
Steven Whitehousecd915492006-09-04 12:49:07 -04001270 u64 off, off_next;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001271 unsigned int x, y;
1272 int run = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001273
Benjamin Marzinski471f3db2015-12-01 08:46:55 -06001274 if (sort_start < entries)
1275 sort(&darr[sort_start], entries - sort_start,
1276 sizeof(struct gfs2_dirent *), compare_dents, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001277
1278 dent_next = darr[0];
Benjamin Marzinski471f3db2015-12-01 08:46:55 -06001279 off_next = dent_next->de_cookie;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001280
1281 for (x = 0, y = 1; x < entries; x++, y++) {
1282 dent = dent_next;
1283 off = off_next;
1284
1285 if (y < entries) {
1286 dent_next = darr[y];
Benjamin Marzinski471f3db2015-12-01 08:46:55 -06001287 off_next = dent_next->de_cookie;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001288
Al Virod81a8ef2013-05-16 14:14:48 -04001289 if (off < ctx->pos)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001290 continue;
Al Virod81a8ef2013-05-16 14:14:48 -04001291 ctx->pos = off;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001292
1293 if (off_next == off) {
1294 if (*copied && !run)
1295 return 1;
1296 run = 1;
1297 } else
1298 run = 0;
1299 } else {
Al Virod81a8ef2013-05-16 14:14:48 -04001300 if (off < ctx->pos)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001301 continue;
Al Virod81a8ef2013-05-16 14:14:48 -04001302 ctx->pos = off;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001303 }
1304
Al Virod81a8ef2013-05-16 14:14:48 -04001305 if (!dir_emit(ctx, (const char *)(dent + 1),
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001306 be16_to_cpu(dent->de_name_len),
Al Virod81a8ef2013-05-16 14:14:48 -04001307 be64_to_cpu(dent->de_inum.no_addr),
1308 be16_to_cpu(dent->de_type)))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001309 return 1;
1310
1311 *copied = 1;
1312 }
1313
Al Virod81a8ef2013-05-16 14:14:48 -04001314 /* Increment the ctx->pos by one, so the next time we come into the
David Teiglandb3b94fa2006-01-16 16:50:04 +00001315 do_filldir fxn, we get the next entry instead of the last one in the
1316 current leaf */
1317
Al Virod81a8ef2013-05-16 14:14:48 -04001318 ctx->pos++;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001319
1320 return 0;
1321}
1322
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001323static void *gfs2_alloc_sort_buffer(unsigned size)
1324{
1325 void *ptr = NULL;
1326
1327 if (size < KMALLOC_MAX_SIZE)
1328 ptr = kmalloc(size, GFP_NOFS | __GFP_NOWARN);
1329 if (!ptr)
Christoph Hellwig88dca4c2020-06-01 21:51:40 -07001330 ptr = __vmalloc(size, GFP_NOFS);
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001331 return ptr;
1332}
1333
Benjamin Marzinski471f3db2015-12-01 08:46:55 -06001334
1335static int gfs2_set_cookies(struct gfs2_sbd *sdp, struct buffer_head *bh,
1336 unsigned leaf_nr, struct gfs2_dirent **darr,
1337 unsigned entries)
1338{
1339 int sort_id = -1;
1340 int i;
1341
1342 for (i = 0; i < entries; i++) {
1343 unsigned offset;
1344
1345 darr[i]->de_cookie = be32_to_cpu(darr[i]->de_hash);
1346 darr[i]->de_cookie = gfs2_disk_hash2offset(darr[i]->de_cookie);
1347
1348 if (!sdp->sd_args.ar_loccookie)
1349 continue;
1350 offset = (char *)(darr[i]) -
Bob Petersone54c78a2018-10-03 08:47:36 -05001351 (bh->b_data + gfs2_dirent_offset(sdp, bh->b_data));
Benjamin Marzinski471f3db2015-12-01 08:46:55 -06001352 offset /= GFS2_MIN_DIRENT_SIZE;
1353 offset += leaf_nr * sdp->sd_max_dents_per_leaf;
1354 if (offset >= GFS2_USE_HASH_FLAG ||
1355 leaf_nr >= GFS2_USE_HASH_FLAG) {
1356 darr[i]->de_cookie |= GFS2_USE_HASH_FLAG;
1357 if (sort_id < 0)
1358 sort_id = i;
1359 continue;
1360 }
1361 darr[i]->de_cookie &= GFS2_HASH_INDEX_MASK;
1362 darr[i]->de_cookie |= offset;
1363 }
1364 return sort_id;
1365}
1366
1367
Al Virod81a8ef2013-05-16 14:14:48 -04001368static int gfs2_dir_read_leaf(struct inode *inode, struct dir_context *ctx,
1369 int *copied, unsigned *depth,
Steven Whitehouse3699e3a2007-01-17 15:09:20 +00001370 u64 leaf_no)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001371{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001372 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001373 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001374 struct buffer_head *bh;
1375 struct gfs2_leaf *lf;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001376 unsigned entries = 0, entries2 = 0;
Benjamin Marzinski471f3db2015-12-01 08:46:55 -06001377 unsigned leaves = 0, leaf = 0, offset, sort_offset;
1378 struct gfs2_dirent **darr, *dent;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001379 struct dirent_gather g;
1380 struct buffer_head **larr;
Benjamin Marzinski471f3db2015-12-01 08:46:55 -06001381 int error, i, need_sort = 0, sort_id;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001382 u64 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001383
David Teiglandb3b94fa2006-01-16 16:50:04 +00001384 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001385 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001386 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001387 goto out;
1388 lf = (struct gfs2_leaf *)bh->b_data;
1389 if (leaves == 0)
1390 *depth = be16_to_cpu(lf->lf_depth);
1391 entries += be16_to_cpu(lf->lf_entries);
1392 leaves++;
1393 lfn = be64_to_cpu(lf->lf_next);
1394 brelse(bh);
1395 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001396
Benjamin Marzinski471f3db2015-12-01 08:46:55 -06001397 if (*depth < GFS2_DIR_MAX_DEPTH || !sdp->sd_args.ar_loccookie) {
1398 need_sort = 1;
1399 sort_offset = 0;
1400 }
1401
David Teiglandb3b94fa2006-01-16 16:50:04 +00001402 if (!entries)
1403 return 0;
1404
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001405 error = -ENOMEM;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001406 /*
1407 * The extra 99 entries are not normally used, but are a buffer
1408 * zone in case the number of entries in the leaf is corrupt.
1409 * 99 is the maximum number of entries that can fit in a single
1410 * leaf block.
1411 */
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001412 larr = gfs2_alloc_sort_buffer((leaves + entries + 99) * sizeof(void *));
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001413 if (!larr)
1414 goto out;
Benjamin Marzinski471f3db2015-12-01 08:46:55 -06001415 darr = (struct gfs2_dirent **)(larr + leaves);
1416 g.pdent = (const struct gfs2_dirent **)darr;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001417 g.offset = 0;
1418 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001419
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001420 do {
1421 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001422 if (error)
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001423 goto out_free;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001424 lf = (struct gfs2_leaf *)bh->b_data;
1425 lfn = be64_to_cpu(lf->lf_next);
1426 if (lf->lf_entries) {
Benjamin Marzinski471f3db2015-12-01 08:46:55 -06001427 offset = g.offset;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001428 entries2 += be16_to_cpu(lf->lf_entries);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001429 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1430 gfs2_dirent_gather, NULL, &g);
1431 error = PTR_ERR(dent);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001432 if (IS_ERR(dent))
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001433 goto out_free;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001434 if (entries2 != g.offset) {
akpm@linux-foundation.orgf391a4e2007-04-25 21:08:02 -07001435 fs_warn(sdp, "Number of entries corrupt in dir "
1436 "leaf %llu, entries2 (%u) != "
1437 "g.offset (%u)\n",
1438 (unsigned long long)bh->b_blocknr,
1439 entries2, g.offset);
Bob Petersond87d62b2017-05-26 08:28:56 -05001440 gfs2_consist_inode(ip);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001441 error = -EIO;
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001442 goto out_free;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001443 }
1444 error = 0;
Benjamin Marzinski471f3db2015-12-01 08:46:55 -06001445 sort_id = gfs2_set_cookies(sdp, bh, leaf, &darr[offset],
1446 be16_to_cpu(lf->lf_entries));
1447 if (!need_sort && sort_id >= 0) {
1448 need_sort = 1;
1449 sort_offset = offset + sort_id;
1450 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001451 larr[leaf++] = bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001452 } else {
Benjamin Marzinski471f3db2015-12-01 08:46:55 -06001453 larr[leaf++] = NULL;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001454 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001455 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001456 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001457
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001458 BUG_ON(entries2 != entries);
Benjamin Marzinski471f3db2015-12-01 08:46:55 -06001459 error = do_filldir_main(ip, ctx, darr, entries, need_sort ?
1460 sort_offset : entries, copied);
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001461out_free:
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001462 for(i = 0; i < leaf; i++)
Markus Elfringbccaef92019-09-03 15:10:05 +02001463 brelse(larr[i]);
Al Viro3cdcf632014-11-20 05:18:38 +00001464 kvfree(larr);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001465out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001466 return error;
1467}
1468
Steven Whitehouse79c4c372011-11-09 13:46:06 +00001469/**
1470 * gfs2_dir_readahead - Issue read-ahead requests for leaf blocks.
Bob Petersondfe4d342011-10-27 12:16:06 -04001471 *
1472 * Note: we can't calculate each index like dir_e_read can because we don't
1473 * have the leaf, and therefore we don't have the depth, and therefore we
1474 * don't have the length. So we have to just read enough ahead to make up
Steven Whitehouse79c4c372011-11-09 13:46:06 +00001475 * for the loss of information.
1476 */
Bob Petersondfe4d342011-10-27 12:16:06 -04001477static void gfs2_dir_readahead(struct inode *inode, unsigned hsize, u32 index,
1478 struct file_ra_state *f_ra)
1479{
1480 struct gfs2_inode *ip = GFS2_I(inode);
1481 struct gfs2_glock *gl = ip->i_gl;
1482 struct buffer_head *bh;
1483 u64 blocknr = 0, last;
1484 unsigned count;
1485
1486 /* First check if we've already read-ahead for the whole range. */
Steven Whitehouse79c4c372011-11-09 13:46:06 +00001487 if (index + MAX_RA_BLOCKS < f_ra->start)
Bob Petersondfe4d342011-10-27 12:16:06 -04001488 return;
1489
1490 f_ra->start = max((pgoff_t)index, f_ra->start);
1491 for (count = 0; count < MAX_RA_BLOCKS; count++) {
1492 if (f_ra->start >= hsize) /* if exceeded the hash table */
1493 break;
1494
1495 last = blocknr;
1496 blocknr = be64_to_cpu(ip->i_hash_cache[f_ra->start]);
1497 f_ra->start++;
1498 if (blocknr == last)
1499 continue;
1500
1501 bh = gfs2_getbuf(gl, blocknr, 1);
1502 if (trylock_buffer(bh)) {
1503 if (buffer_uptodate(bh)) {
1504 unlock_buffer(bh);
1505 brelse(bh);
1506 continue;
1507 }
1508 bh->b_end_io = end_buffer_read_sync;
Coly Lie477b242017-07-21 07:48:22 -05001509 submit_bh(REQ_OP_READ,
1510 REQ_RAHEAD | REQ_META | REQ_PRIO,
1511 bh);
Bob Petersondfe4d342011-10-27 12:16:06 -04001512 continue;
1513 }
1514 brelse(bh);
1515 }
1516}
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001517
David Teiglandb3b94fa2006-01-16 16:50:04 +00001518/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001519 * dir_e_read - Reads the entries from a directory into a filldir buffer
1520 * @dip: dinode pointer
Al Virod81a8ef2013-05-16 14:14:48 -04001521 * @ctx: actor to feed the entries to
David Teiglandb3b94fa2006-01-16 16:50:04 +00001522 *
1523 * Returns: errno
1524 */
1525
Al Virod81a8ef2013-05-16 14:14:48 -04001526static int dir_e_read(struct inode *inode, struct dir_context *ctx,
1527 struct file_ra_state *f_ra)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001528{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001529 struct gfs2_inode *dip = GFS2_I(inode);
Steven Whitehousecd915492006-09-04 12:49:07 -04001530 u32 hsize, len = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -04001531 u32 hash, index;
Al Virob44b84d2006-10-14 10:46:30 -04001532 __be64 *lp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001533 int copied = 0;
1534 int error = 0;
Steven Whitehouse4da3c642006-07-11 13:19:13 -04001535 unsigned depth = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001536
Fabian Frederick47a9a522016-08-02 12:05:27 -05001537 hsize = BIT(dip->i_depth);
Al Virod81a8ef2013-05-16 14:14:48 -04001538 hash = gfs2_dir_offset2hash(ctx->pos);
Steven Whitehouse9a004502008-02-01 09:23:44 +00001539 index = hash >> (32 - dip->i_depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001540
Steven Whitehouse79c4c372011-11-09 13:46:06 +00001541 if (dip->i_hash_cache == NULL)
Bob Petersondfe4d342011-10-27 12:16:06 -04001542 f_ra->start = 0;
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001543 lp = gfs2_dir_get_hash_table(dip);
1544 if (IS_ERR(lp))
1545 return PTR_ERR(lp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001546
Bob Petersondfe4d342011-10-27 12:16:06 -04001547 gfs2_dir_readahead(inode, hsize, index, f_ra);
1548
David Teiglandb3b94fa2006-01-16 16:50:04 +00001549 while (index < hsize) {
Al Virod81a8ef2013-05-16 14:14:48 -04001550 error = gfs2_dir_read_leaf(inode, ctx,
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001551 &copied, &depth,
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001552 be64_to_cpu(lp[index]));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001553 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001554 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001555
Fabian Frederick47a9a522016-08-02 12:05:27 -05001556 len = BIT(dip->i_depth - depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001557 index = (index & ~(len - 1)) + len;
1558 }
1559
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001560 if (error > 0)
1561 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001562 return error;
1563}
1564
Al Virod81a8ef2013-05-16 14:14:48 -04001565int gfs2_dir_read(struct inode *inode, struct dir_context *ctx,
1566 struct file_ra_state *f_ra)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001567{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001568 struct gfs2_inode *dip = GFS2_I(inode);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001569 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001570 struct dirent_gather g;
Benjamin Marzinski471f3db2015-12-01 08:46:55 -06001571 struct gfs2_dirent **darr, *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001572 struct buffer_head *dibh;
1573 int copied = 0;
1574 int error;
1575
Steven Whitehousead6203f22008-11-03 13:59:19 +00001576 if (!dip->i_entries)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001577 return 0;
1578
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001579 if (dip->i_diskflags & GFS2_DIF_EXHASH)
Al Virod81a8ef2013-05-16 14:14:48 -04001580 return dir_e_read(inode, ctx, f_ra);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001581
David Teiglandb3b94fa2006-01-16 16:50:04 +00001582 if (!gfs2_is_stuffed(dip)) {
1583 gfs2_consist_inode(dip);
1584 return -EIO;
1585 }
1586
David Teiglandb3b94fa2006-01-16 16:50:04 +00001587 error = gfs2_meta_inode_buffer(dip, &dibh);
1588 if (error)
1589 return error;
1590
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001591 error = -ENOMEM;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001592 /* 96 is max number of dirents which can be stuffed into an inode */
Kees Cook6da2ec52018-06-12 13:55:00 -07001593 darr = kmalloc_array(96, sizeof(struct gfs2_dirent *), GFP_NOFS);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001594 if (darr) {
Benjamin Marzinski471f3db2015-12-01 08:46:55 -06001595 g.pdent = (const struct gfs2_dirent **)darr;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001596 g.offset = 0;
1597 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1598 gfs2_dirent_gather, NULL, &g);
1599 if (IS_ERR(dent)) {
1600 error = PTR_ERR(dent);
1601 goto out;
1602 }
Steven Whitehousead6203f22008-11-03 13:59:19 +00001603 if (dip->i_entries != g.offset) {
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001604 fs_warn(sdp, "Number of entries corrupt in dir %llu, "
Steven Whitehousead6203f22008-11-03 13:59:19 +00001605 "ip->i_entries (%u) != g.offset (%u)\n",
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001606 (unsigned long long)dip->i_no_addr,
Steven Whitehousead6203f22008-11-03 13:59:19 +00001607 dip->i_entries,
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001608 g.offset);
Bob Petersond87d62b2017-05-26 08:28:56 -05001609 gfs2_consist_inode(dip);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001610 error = -EIO;
1611 goto out;
1612 }
Benjamin Marzinski471f3db2015-12-01 08:46:55 -06001613 gfs2_set_cookies(sdp, dibh, 0, darr, dip->i_entries);
Al Virod81a8ef2013-05-16 14:14:48 -04001614 error = do_filldir_main(dip, ctx, darr,
Benjamin Marzinski471f3db2015-12-01 08:46:55 -06001615 dip->i_entries, 0, &copied);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001616out:
1617 kfree(darr);
1618 }
1619
David Teiglandb3b94fa2006-01-16 16:50:04 +00001620 if (error > 0)
1621 error = 0;
1622
1623 brelse(dibh);
1624
1625 return error;
1626}
1627
David Teiglandb3b94fa2006-01-16 16:50:04 +00001628/**
1629 * gfs2_dir_search - Search a directory
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +01001630 * @dip: The GFS2 dir inode
1631 * @name: The name we are looking up
1632 * @fail_on_exist: Fail if the name exists rather than looking it up
David Teiglandb3b94fa2006-01-16 16:50:04 +00001633 *
1634 * This routine searches a directory for a file or another directory.
1635 * Assumes a glock is held on dip.
1636 *
1637 * Returns: errno
1638 */
1639
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +01001640struct inode *gfs2_dir_search(struct inode *dir, const struct qstr *name,
1641 bool fail_on_exist)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001642{
Steven Whitehousec7526662006-03-20 12:30:04 -05001643 struct buffer_head *bh;
1644 struct gfs2_dirent *dent;
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +01001645 u64 addr, formal_ino;
1646 u16 dtype;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001647
1648 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1649 if (dent) {
Andreas Gruenbacherc8d57702015-11-11 15:00:35 -06001650 struct inode *inode;
1651 u16 rahead;
1652
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001653 if (IS_ERR(dent))
David Howellse231c2e2008-02-07 00:15:26 -08001654 return ERR_CAST(dent);
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +01001655 dtype = be16_to_cpu(dent->de_type);
Andreas Gruenbacherc8d57702015-11-11 15:00:35 -06001656 rahead = be16_to_cpu(dent->de_rahead);
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +01001657 addr = be64_to_cpu(dent->de_inum.no_addr);
1658 formal_ino = be64_to_cpu(dent->de_inum.no_formal_ino);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001659 brelse(bh);
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +01001660 if (fail_on_exist)
1661 return ERR_PTR(-EEXIST);
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -05001662 inode = gfs2_inode_lookup(dir->i_sb, dtype, addr, formal_ino,
1663 GFS2_BLKST_FREE /* ignore */);
Andreas Gruenbacherc8d57702015-11-11 15:00:35 -06001664 if (!IS_ERR(inode))
1665 GFS2_I(inode)->i_rahead = rahead;
1666 return inode;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001667 }
1668 return ERR_PTR(-ENOENT);
1669}
1670
1671int gfs2_dir_check(struct inode *dir, const struct qstr *name,
1672 const struct gfs2_inode *ip)
1673{
1674 struct buffer_head *bh;
1675 struct gfs2_dirent *dent;
1676 int ret = -ENOENT;
Steven Whitehousec7526662006-03-20 12:30:04 -05001677
1678 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1679 if (dent) {
1680 if (IS_ERR(dent))
1681 return PTR_ERR(dent);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001682 if (ip) {
1683 if (be64_to_cpu(dent->de_inum.no_addr) != ip->i_no_addr)
1684 goto out;
1685 if (be64_to_cpu(dent->de_inum.no_formal_ino) !=
1686 ip->i_no_formal_ino)
1687 goto out;
1688 if (unlikely(IF2DT(ip->i_inode.i_mode) !=
1689 be16_to_cpu(dent->de_type))) {
1690 gfs2_consist_inode(GFS2_I(dir));
1691 ret = -EIO;
1692 goto out;
1693 }
1694 }
1695 ret = 0;
1696out:
Steven Whitehousec7526662006-03-20 12:30:04 -05001697 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001698 }
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001699 return ret;
Steven Whitehousec7526662006-03-20 12:30:04 -05001700}
1701
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001702/**
1703 * dir_new_leaf - Add a new leaf onto hash chain
1704 * @inode: The directory
1705 * @name: The name we are adding
1706 *
1707 * This adds a new dir leaf onto an existing leaf when there is not
1708 * enough space to add a new dir entry. This is a last resort after
1709 * we've expanded the hash table to max size and also split existing
1710 * leaf blocks, so it will only occur for very large directories.
1711 *
1712 * The dist parameter is set to 1 for leaf blocks directly attached
1713 * to the hash table, 2 for one layer of indirection, 3 for two layers
1714 * etc. We are thus able to tell the difference between an old leaf
1715 * with dist set to zero (i.e. "don't know") and a new one where we
1716 * set this information for debug/fsck purposes.
1717 *
1718 * Returns: 0 on success, or -ve on error
1719 */
1720
Steven Whitehousec7526662006-03-20 12:30:04 -05001721static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1722{
1723 struct buffer_head *bh, *obh;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001724 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001725 struct gfs2_leaf *leaf, *oleaf;
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001726 u32 dist = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001727 int error;
Steven Whitehousec7526662006-03-20 12:30:04 -05001728 u32 index;
1729 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001730
Steven Whitehouse9a004502008-02-01 09:23:44 +00001731 index = name->hash >> (32 - ip->i_depth);
Steven Whitehousec7526662006-03-20 12:30:04 -05001732 error = get_first_leaf(ip, index, &obh);
1733 if (error)
1734 return error;
1735 do {
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001736 dist++;
Steven Whitehousec7526662006-03-20 12:30:04 -05001737 oleaf = (struct gfs2_leaf *)obh->b_data;
1738 bn = be64_to_cpu(oleaf->lf_next);
1739 if (!bn)
1740 break;
1741 brelse(obh);
1742 error = get_leaf(ip, bn, &obh);
1743 if (error)
1744 return error;
1745 } while(1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001746
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001747 gfs2_trans_add_meta(ip->i_gl, obh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001748
1749 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1750 if (!leaf) {
1751 brelse(obh);
1752 return -ENOSPC;
1753 }
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001754 leaf->lf_dist = cpu_to_be32(dist);
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04001755 oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
Steven Whitehousec7526662006-03-20 12:30:04 -05001756 brelse(bh);
1757 brelse(obh);
1758
1759 error = gfs2_meta_inode_buffer(ip, &bh);
1760 if (error)
1761 return error;
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001762 gfs2_trans_add_meta(ip->i_gl, bh);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001763 gfs2_add_inode_blocks(&ip->i_inode, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001764 gfs2_dinode_out(ip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001765 brelse(bh);
1766 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001767}
1768
Steven Whitehouse44aaada2014-02-07 11:23:22 +00001769static u16 gfs2_inode_ra_len(const struct gfs2_inode *ip)
1770{
1771 u64 where = ip->i_no_addr + 1;
1772 if (ip->i_eattr == where)
1773 return 1;
1774 return 0;
1775}
1776
David Teiglandb3b94fa2006-01-16 16:50:04 +00001777/**
1778 * gfs2_dir_add - Add new filename into directory
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001779 * @inode: The directory inode
1780 * @name: The new name
1781 * @nip: The GFS2 inode to be linked in to the directory
1782 * @da: The directory addition info
1783 *
1784 * If the call to gfs2_diradd_alloc_required resulted in there being
1785 * no need to allocate any new directory blocks, then it will contain
1786 * a pointer to the directory entry and the bh in which it resides. We
1787 * can use that without having to repeat the search. If there was no
1788 * free space, then we must now create more space.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001789 *
1790 * Returns: 0 on success, error code on failure
1791 */
1792
Steven Whitehousec7526662006-03-20 12:30:04 -05001793int gfs2_dir_add(struct inode *inode, const struct qstr *name,
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001794 const struct gfs2_inode *nip, struct gfs2_diradd *da)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001795{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001796 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001797 struct buffer_head *bh = da->bh;
1798 struct gfs2_dirent *dent = da->dent;
Deepa Dinamani95582b02018-05-08 19:36:02 -07001799 struct timespec64 tv;
Steven Whitehousec7526662006-03-20 12:30:04 -05001800 struct gfs2_leaf *leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001801 int error;
1802
Steven Whitehousec7526662006-03-20 12:30:04 -05001803 while(1) {
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001804 if (da->bh == NULL) {
1805 dent = gfs2_dirent_search(inode, name,
1806 gfs2_dirent_find_space, &bh);
1807 }
Steven Whitehousec7526662006-03-20 12:30:04 -05001808 if (dent) {
1809 if (IS_ERR(dent))
1810 return PTR_ERR(dent);
1811 dent = gfs2_init_dirent(inode, dent, name, bh);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001812 gfs2_inum_out(nip, dent);
Steven Whitehouse3d6ecb72011-05-09 13:30:08 +01001813 dent->de_type = cpu_to_be16(IF2DT(nip->i_inode.i_mode));
Steven Whitehouse44aaada2014-02-07 11:23:22 +00001814 dent->de_rahead = cpu_to_be16(gfs2_inode_ra_len(nip));
Deepa Dinamani078cd822016-09-14 07:48:04 -07001815 tv = current_time(&ip->i_inode);
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001816 if (ip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001817 leaf = (struct gfs2_leaf *)bh->b_data;
Marcin Slusarzbb16b342008-02-13 00:06:10 +01001818 be16_add_cpu(&leaf->lf_entries, 1);
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001819 leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
1820 leaf->lf_sec = cpu_to_be64(tv.tv_sec);
Steven Whitehousec7526662006-03-20 12:30:04 -05001821 }
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001822 da->dent = NULL;
1823 da->bh = NULL;
Steven Whitehousec7526662006-03-20 12:30:04 -05001824 brelse(bh);
Steven Whitehousead6203f22008-11-03 13:59:19 +00001825 ip->i_entries++;
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001826 ip->i_inode.i_mtime = ip->i_inode.i_ctime = tv;
Steven Whitehouse3d6ecb72011-05-09 13:30:08 +01001827 if (S_ISDIR(nip->i_inode.i_mode))
1828 inc_nlink(&ip->i_inode);
Bob Peterson343cd8f2012-11-12 13:04:54 -05001829 mark_inode_dirty(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001830 error = 0;
1831 break;
1832 }
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001833 if (!(ip->i_diskflags & GFS2_DIF_EXHASH)) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001834 error = dir_make_exhash(inode);
1835 if (error)
1836 break;
1837 continue;
1838 }
1839 error = dir_split_leaf(inode, name);
1840 if (error == 0)
1841 continue;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001842 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001843 break;
Steven Whitehouse9a004502008-02-01 09:23:44 +00001844 if (ip->i_depth < GFS2_DIR_MAX_DEPTH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001845 error = dir_double_exhash(ip);
1846 if (error)
1847 break;
1848 error = dir_split_leaf(inode, name);
Steven Whitehousee90deff2006-03-29 19:02:15 -05001849 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001850 break;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001851 if (error == 0)
1852 continue;
Steven Whitehousec7526662006-03-20 12:30:04 -05001853 }
1854 error = dir_new_leaf(inode, name);
1855 if (!error)
1856 continue;
1857 error = -ENOSPC;
1858 break;
1859 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001860 return error;
1861}
1862
Steven Whitehousec7526662006-03-20 12:30:04 -05001863
David Teiglandb3b94fa2006-01-16 16:50:04 +00001864/**
1865 * gfs2_dir_del - Delete a directory entry
1866 * @dip: The GFS2 inode
1867 * @filename: The filename
1868 *
1869 * Returns: 0 on success, error code on failure
1870 */
1871
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001872int gfs2_dir_del(struct gfs2_inode *dip, const struct dentry *dentry)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001873{
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001874 const struct qstr *name = &dentry->d_name;
Steven Whitehousec7526662006-03-20 12:30:04 -05001875 struct gfs2_dirent *dent, *prev = NULL;
1876 struct buffer_head *bh;
Deepa Dinamani95582b02018-05-08 19:36:02 -07001877 struct timespec64 tv = current_time(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001878
Steven Whitehousec7526662006-03-20 12:30:04 -05001879 /* Returns _either_ the entry (if its first in block) or the
1880 previous entry otherwise */
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001881 dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001882 if (!dent) {
1883 gfs2_consist_inode(dip);
1884 return -EIO;
1885 }
1886 if (IS_ERR(dent)) {
1887 gfs2_consist_inode(dip);
1888 return PTR_ERR(dent);
1889 }
1890 /* If not first in block, adjust pointers accordingly */
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001891 if (gfs2_dirent_find(dent, name, NULL) == 0) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001892 prev = dent;
1893 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1894 }
1895
1896 dirent_del(dip, bh, prev, dent);
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001897 if (dip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001898 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1899 u16 entries = be16_to_cpu(leaf->lf_entries);
1900 if (!entries)
1901 gfs2_consist_inode(dip);
1902 leaf->lf_entries = cpu_to_be16(--entries);
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001903 leaf->lf_nsec = cpu_to_be32(tv.tv_nsec);
1904 leaf->lf_sec = cpu_to_be64(tv.tv_sec);
Steven Whitehousec7526662006-03-20 12:30:04 -05001905 }
Steven Whitehouseed386502006-04-07 16:28:07 -04001906 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001907
Steven Whitehousead6203f22008-11-03 13:59:19 +00001908 if (!dip->i_entries)
Steven Whitehousec7526662006-03-20 12:30:04 -05001909 gfs2_consist_inode(dip);
Steven Whitehousead6203f22008-11-03 13:59:19 +00001910 dip->i_entries--;
Steven Whitehouse01bcb0d2014-01-08 12:14:57 +00001911 dip->i_inode.i_mtime = dip->i_inode.i_ctime = tv;
David Howellse36cb0b2015-01-29 12:02:35 +00001912 if (d_is_dir(dentry))
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001913 drop_nlink(&dip->i_inode);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001914 mark_inode_dirty(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001915
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001916 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001917}
1918
David Teiglandb3b94fa2006-01-16 16:50:04 +00001919/**
1920 * gfs2_dir_mvino - Change inode number of directory entry
1921 * @dip: The GFS2 inode
1922 * @filename:
1923 * @new_inode:
1924 *
1925 * This routine changes the inode number of a directory entry. It's used
1926 * by rename to change ".." when a directory is moved.
1927 * Assumes a glock is held on dvp.
1928 *
1929 * Returns: errno
1930 */
1931
Steven Whitehousec7526662006-03-20 12:30:04 -05001932int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001933 const struct gfs2_inode *nip, unsigned int new_type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001934{
Steven Whitehousec7526662006-03-20 12:30:04 -05001935 struct buffer_head *bh;
1936 struct gfs2_dirent *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001937
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001938 dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001939 if (!dent) {
1940 gfs2_consist_inode(dip);
1941 return -EIO;
1942 }
1943 if (IS_ERR(dent))
1944 return PTR_ERR(dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001945
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001946 gfs2_trans_add_meta(dip->i_gl, bh);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001947 gfs2_inum_out(nip, dent);
Steven Whitehousec7526662006-03-20 12:30:04 -05001948 dent->de_type = cpu_to_be16(new_type);
Andreas Gruenbacher83998cc2018-02-28 12:48:53 -07001949 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001950
Deepa Dinamani078cd822016-09-14 07:48:04 -07001951 dip->i_inode.i_mtime = dip->i_inode.i_ctime = current_time(&dip->i_inode);
Andreas Gruenbacher83998cc2018-02-28 12:48:53 -07001952 mark_inode_dirty_sync(&dip->i_inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001953 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001954}
1955
1956/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001957 * leaf_dealloc - Deallocate a directory leaf
1958 * @dip: the directory
1959 * @index: the hash table offset in the directory
1960 * @len: the number of pointers to this leaf
1961 * @leaf_no: the leaf number
Bob Petersonec038c82011-03-22 13:55:23 -04001962 * @leaf_bh: buffer_head for the starting leaf
Bob Petersond24a7a42011-03-22 13:54:03 -04001963 * last_dealloc: 1 if this is the final dealloc for the leaf, else 0
David Teiglandb3b94fa2006-01-16 16:50:04 +00001964 *
1965 * Returns: errno
1966 */
1967
Steven Whitehousecd915492006-09-04 12:49:07 -04001968static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
Bob Petersonec038c82011-03-22 13:55:23 -04001969 u64 leaf_no, struct buffer_head *leaf_bh,
1970 int last_dealloc)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001971{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001972 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001973 struct gfs2_leaf *tmp_leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001974 struct gfs2_rgrp_list rlist;
1975 struct buffer_head *bh, *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001976 u64 blk, nblk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001977 unsigned int rg_blocks = 0, l_blocks = 0;
1978 char *ht;
Steven Whitehousecd915492006-09-04 12:49:07 -04001979 unsigned int x, size = len * sizeof(u64);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001980 int error;
1981
Bob Peterson5e2f7d62012-04-04 22:11:16 -04001982 error = gfs2_rindex_update(sdp);
1983 if (error)
1984 return error;
1985
David Teiglandb3b94fa2006-01-16 16:50:04 +00001986 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1987
Joe Perches8be04b92013-06-19 12:15:53 -07001988 ht = kzalloc(size, GFP_NOFS | __GFP_NOWARN);
Bob Petersone8830d82013-05-30 09:48:56 -04001989 if (ht == NULL)
Christoph Hellwig88dca4c2020-06-01 21:51:40 -07001990 ht = __vmalloc(size, GFP_NOFS | __GFP_NOWARN | __GFP_ZERO);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001991 if (!ht)
1992 return -ENOMEM;
1993
Eric W. Biedermanf4108a62013-01-31 17:49:26 -08001994 error = gfs2_quota_hold(dip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001995 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -04001996 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001997
David Teiglandb3b94fa2006-01-16 16:50:04 +00001998 /* Count the number of leaves */
Bob Petersonec038c82011-03-22 13:55:23 -04001999 bh = leaf_bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002000
Steven Whitehousec7526662006-03-20 12:30:04 -05002001 for (blk = leaf_no; blk; blk = nblk) {
Bob Petersonec038c82011-03-22 13:55:23 -04002002 if (blk != leaf_no) {
2003 error = get_leaf(dip, blk, &bh);
2004 if (error)
2005 goto out_rlist;
2006 }
Steven Whitehousec7526662006-03-20 12:30:04 -05002007 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
2008 nblk = be64_to_cpu(tmp_leaf->lf_next);
Bob Petersonec038c82011-03-22 13:55:23 -04002009 if (blk != leaf_no)
2010 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002011
Steven Whitehouse70b0c362011-09-02 16:08:09 +01002012 gfs2_rlist_add(dip, &rlist, blk);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002013 l_blocks++;
2014 }
2015
Bob Petersonc3abc292018-10-04 00:06:23 +01002016 gfs2_rlist_alloc(&rlist);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002017
2018 for (x = 0; x < rlist.rl_rgrps; x++) {
Andreas Gruenbacher6f6597ba2017-06-30 07:55:08 -05002019 struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(rlist.rl_ghs[x].gh_gl);
2020
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01002021 rg_blocks += rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002022 }
2023
2024 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
2025 if (error)
2026 goto out_rlist;
2027
2028 error = gfs2_trans_begin(sdp,
Steven Whitehouse5c676f62006-02-27 17:23:27 -05002029 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
Bob Petersoncc444572020-01-03 08:48:43 -06002030 RES_DINODE + RES_STATFS + RES_QUOTA, RES_DINODE +
2031 l_blocks);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002032 if (error)
2033 goto out_rg_gunlock;
2034
Bob Petersonec038c82011-03-22 13:55:23 -04002035 bh = leaf_bh;
2036
Steven Whitehousec7526662006-03-20 12:30:04 -05002037 for (blk = leaf_no; blk; blk = nblk) {
Andreas Gruenbacher0ddeded2018-10-04 15:36:02 +01002038 struct gfs2_rgrpd *rgd;
2039
Bob Petersonec038c82011-03-22 13:55:23 -04002040 if (blk != leaf_no) {
2041 error = get_leaf(dip, blk, &bh);
2042 if (error)
2043 goto out_end_trans;
2044 }
Steven Whitehousec7526662006-03-20 12:30:04 -05002045 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
2046 nblk = be64_to_cpu(tmp_leaf->lf_next);
Bob Petersonec038c82011-03-22 13:55:23 -04002047 if (blk != leaf_no)
2048 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002049
Andreas Gruenbacher0ddeded2018-10-04 15:36:02 +01002050 rgd = gfs2_blk2rgrpd(sdp, blk, true);
2051 gfs2_free_meta(dip, rgd, blk, 1);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00002052 gfs2_add_inode_blocks(&dip->i_inode, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002053 }
2054
Steven Whitehousecd915492006-09-04 12:49:07 -04002055 error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002056 if (error != size) {
2057 if (error >= 0)
2058 error = -EIO;
2059 goto out_end_trans;
2060 }
2061
2062 error = gfs2_meta_inode_buffer(dip, &dibh);
2063 if (error)
2064 goto out_end_trans;
2065
Steven Whitehouse350a9b02012-12-14 12:36:02 +00002066 gfs2_trans_add_meta(dip->i_gl, dibh);
Bob Petersond24a7a42011-03-22 13:54:03 -04002067 /* On the last dealloc, make this a regular file in case we crash.
2068 (We don't want to free these blocks a second time.) */
2069 if (last_dealloc)
2070 dip->i_inode.i_mode = S_IFREG;
Steven Whitehouse539e5d62006-10-31 15:07:05 -05002071 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002072 brelse(dibh);
2073
Steven Whitehousea91ea692006-09-04 12:04:26 -04002074out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00002075 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04002076out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +00002077 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -04002078out_rlist:
David Teiglandb3b94fa2006-01-16 16:50:04 +00002079 gfs2_rlist_free(&rlist);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002080 gfs2_quota_unhold(dip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03002081out:
Al Viro3cdcf632014-11-20 05:18:38 +00002082 kvfree(ht);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002083 return error;
2084}
2085
2086/**
2087 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
2088 * @dip: the directory
2089 *
2090 * Dealloc all on-disk directory leaves to FREEMETA state
2091 * Change on-disk inode type to "regular file"
2092 *
2093 * Returns: errno
2094 */
2095
2096int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
2097{
Bob Peterson556bb172011-03-22 13:56:37 -04002098 struct buffer_head *bh;
2099 struct gfs2_leaf *leaf;
2100 u32 hsize, len;
Bob Peterson556bb172011-03-22 13:56:37 -04002101 u32 index = 0, next_index;
2102 __be64 *lp;
2103 u64 leaf_no;
2104 int error = 0, last;
2105
Fabian Frederick47a9a522016-08-02 12:05:27 -05002106 hsize = BIT(dip->i_depth);
Bob Peterson556bb172011-03-22 13:56:37 -04002107
Steven Whitehouse17d539f2011-06-15 10:29:37 +01002108 lp = gfs2_dir_get_hash_table(dip);
2109 if (IS_ERR(lp))
2110 return PTR_ERR(lp);
Bob Peterson556bb172011-03-22 13:56:37 -04002111
2112 while (index < hsize) {
Steven Whitehouse17d539f2011-06-15 10:29:37 +01002113 leaf_no = be64_to_cpu(lp[index]);
Bob Peterson556bb172011-03-22 13:56:37 -04002114 if (leaf_no) {
2115 error = get_leaf(dip, leaf_no, &bh);
2116 if (error)
2117 goto out;
2118 leaf = (struct gfs2_leaf *)bh->b_data;
Fabian Frederick47a9a522016-08-02 12:05:27 -05002119 len = BIT(dip->i_depth - be16_to_cpu(leaf->lf_depth));
Bob Peterson556bb172011-03-22 13:56:37 -04002120
2121 next_index = (index & ~(len - 1)) + len;
2122 last = ((next_index >= hsize) ? 1 : 0);
2123 error = leaf_dealloc(dip, index, len, leaf_no, bh,
2124 last);
2125 brelse(bh);
2126 if (error)
2127 goto out;
2128 index = next_index;
2129 } else
2130 index++;
2131 }
2132
2133 if (index != hsize) {
2134 gfs2_consist_inode(dip);
2135 error = -EIO;
2136 }
2137
2138out:
Bob Peterson556bb172011-03-22 13:56:37 -04002139
2140 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002141}
2142
2143/**
2144 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
2145 * @ip: the file being written to
2146 * @filname: the filename that's going to be added
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00002147 * @da: The structure to return dir alloc info
David Teiglandb3b94fa2006-01-16 16:50:04 +00002148 *
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00002149 * Returns: 0 if ok, -ve on error
David Teiglandb3b94fa2006-01-16 16:50:04 +00002150 */
2151
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00002152int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name,
2153 struct gfs2_diradd *da)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002154{
Steven Whitehouse22b5a6c2014-01-08 11:05:29 +00002155 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00002156 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse22b5a6c2014-01-08 11:05:29 +00002157 const unsigned int extra = sizeof(struct gfs2_dinode) - sizeof(struct gfs2_leaf);
Steven Whitehousec7526662006-03-20 12:30:04 -05002158 struct gfs2_dirent *dent;
2159 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002160
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00002161 da->nr_blocks = 0;
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00002162 da->bh = NULL;
2163 da->dent = NULL;
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00002164
Steven Whitehousec7526662006-03-20 12:30:04 -05002165 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
Steven Whitehouseed386502006-04-07 16:28:07 -04002166 if (!dent) {
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00002167 da->nr_blocks = sdp->sd_max_dirres;
Steven Whitehouse22b5a6c2014-01-08 11:05:29 +00002168 if (!(ip->i_diskflags & GFS2_DIF_EXHASH) &&
2169 (GFS2_DIRENT_SIZE(name->len) < extra))
2170 da->nr_blocks = 1;
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00002171 return 0;
Steven Whitehouseed386502006-04-07 16:28:07 -04002172 }
Steven Whitehousec7526662006-03-20 12:30:04 -05002173 if (IS_ERR(dent))
2174 return PTR_ERR(dent);
Bob Peterson19aeb5a62014-09-29 08:52:04 -04002175
2176 if (da->save_loc) {
2177 da->bh = bh;
2178 da->dent = dent;
2179 } else {
2180 brelse(bh);
2181 }
Steven Whitehousec7526662006-03-20 12:30:04 -05002182 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002183}
2184