blob: 0cb4c1557f20c87a44f74b4ac7d29f9a700de05a [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
9
10/*
Steven Whitehouse61e085a2006-04-24 10:07:13 -040011 * Implements Extendible Hashing as described in:
12 * "Extendible Hashing" by Fagin, et al in
13 * __ACM Trans. on Database Systems__, Sept 1979.
14 *
15 *
16 * Here's the layout of dirents which is essentially the same as that of ext2
17 * within a single block. The field de_name_len is the number of bytes
18 * actually required for the name (no null terminator). The field de_rec_len
19 * is the number of bytes allocated to the dirent. The offset of the next
20 * dirent in the block is (dirent + dirent->de_rec_len). When a dirent is
21 * deleted, the preceding dirent inherits its allocated space, ie
22 * prev->de_rec_len += deleted->de_rec_len. Since the next dirent is obtained
23 * by adding de_rec_len to the current dirent, this essentially causes the
24 * deleted dirent to get jumped over when iterating through all the dirents.
25 *
26 * When deleting the first dirent in a block, there is no previous dirent so
27 * the field de_ino is set to zero to designate it as deleted. When allocating
28 * a dirent, gfs2_dirent_alloc iterates through the dirents in a block. If the
29 * first dirent has (de_ino == 0) and de_rec_len is large enough, this first
30 * dirent is allocated. Otherwise it must go through all the 'used' dirents
31 * searching for one in which the amount of total space minus the amount of
32 * used space will provide enough space for the new dirent.
33 *
34 * There are two types of blocks in which dirents reside. In a stuffed dinode,
35 * the dirents begin at offset sizeof(struct gfs2_dinode) from the beginning of
36 * the block. In leaves, they begin at offset sizeof(struct gfs2_leaf) from the
37 * beginning of the leaf block. The dirents reside in leaves when
38 *
Steven Whitehouse383f01f2008-11-04 10:05:22 +000039 * dip->i_diskflags & GFS2_DIF_EXHASH is true
Steven Whitehouse61e085a2006-04-24 10:07:13 -040040 *
41 * Otherwise, the dirents are "linear", within a single stuffed dinode block.
42 *
43 * When the dirents are in leaves, the actual contents of the directory file are
44 * used as an array of 64-bit block pointers pointing to the leaf blocks. The
45 * dirents are NOT in the directory file itself. There can be more than one
46 * block pointer in the array that points to the same leaf. In fact, when a
47 * directory is first converted from linear to exhash, all of the pointers
48 * point to the same leaf.
49 *
50 * When a leaf is completely full, the size of the hash table can be
51 * doubled unless it is already at the maximum size which is hard coded into
52 * GFS2_DIR_MAX_DEPTH. After that, leaves are chained together in a linked list,
53 * but never before the maximum hash table size has been reached.
54 */
David Teiglandb3b94fa2006-01-16 16:50:04 +000055
David Teiglandb3b94fa2006-01-16 16:50:04 +000056#include <linux/slab.h>
57#include <linux/spinlock.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000058#include <linux/buffer_head.h>
59#include <linux/sort.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050060#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050061#include <linux/crc32.h>
Steven Whitehousefe1bded2006-04-18 10:09:15 -040062#include <linux/vmalloc.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000063
64#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050065#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000066#include "dir.h"
67#include "glock.h"
68#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000069#include "meta_io.h"
70#include "quota.h"
71#include "rgrp.h"
72#include "trans.h"
Steven Whitehousee13940b2006-01-30 13:31:50 +000073#include "bmap.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050074#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000075
76#define IS_LEAF 1 /* Hashed (leaf) directory */
77#define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
78
Bob Petersondfe4d342011-10-27 12:16:06 -040079#define MAX_RA_BLOCKS 32 /* max read-ahead blocks */
80
Steven Whitehousecd915492006-09-04 12:49:07 -040081#define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
82#define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
David Teiglandb3b94fa2006-01-16 16:50:04 +000083
Steven Whitehouse8d123582010-09-17 12:30:23 +010084struct qstr gfs2_qdot __read_mostly;
85struct qstr gfs2_qdotdot __read_mostly;
86
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -040087typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
88 const struct qstr *name, void *opaque);
David Teiglandb3b94fa2006-01-16 16:50:04 +000089
Steven Whitehousecd915492006-09-04 12:49:07 -040090int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
Steven Whitehouse61e085a2006-04-24 10:07:13 -040091 struct buffer_head **bhp)
Steven Whitehousee13940b2006-01-30 13:31:50 +000092{
93 struct buffer_head *bh;
Steven Whitehousee13940b2006-01-30 13:31:50 +000094
Steven Whitehouse61e085a2006-04-24 10:07:13 -040095 bh = gfs2_meta_new(ip->i_gl, block);
Steven Whitehouse350a9b02012-12-14 12:36:02 +000096 gfs2_trans_add_meta(ip->i_gl, bh);
Steven Whitehouse61e085a2006-04-24 10:07:13 -040097 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
98 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
Steven Whitehousee13940b2006-01-30 13:31:50 +000099 *bhp = bh;
100 return 0;
101}
102
Steven Whitehousecd915492006-09-04 12:49:07 -0400103static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, u64 block,
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400104 struct buffer_head **bhp)
105{
106 struct buffer_head *bh;
107 int error;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000108
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400109 error = gfs2_meta_read(ip->i_gl, block, DIO_WAIT, &bh);
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400110 if (error)
111 return error;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400112 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) {
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400113 brelse(bh);
114 return -EIO;
115 }
116 *bhp = bh;
117 return 0;
118}
Steven Whitehousee13940b2006-01-30 13:31:50 +0000119
120static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
121 unsigned int offset, unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000122{
123 struct buffer_head *dibh;
124 int error;
125
126 error = gfs2_meta_inode_buffer(ip, &dibh);
127 if (error)
128 return error;
129
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000130 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500131 memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100132 if (ip->i_inode.i_size < offset + size)
133 i_size_write(&ip->i_inode, offset + size);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100134 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500135 gfs2_dinode_out(ip, dibh->b_data);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000136
137 brelse(dibh);
138
139 return size;
140}
141
142
143
144/**
145 * gfs2_dir_write_data - Write directory information to the inode
146 * @ip: The GFS2 inode
147 * @buf: The buffer containing information to be written
148 * @offset: The file offset to start writing at
149 * @size: The amount of data to write
150 *
151 * Returns: The number of bytes correctly written or error code
152 */
153static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
Steven Whitehousecd915492006-09-04 12:49:07 -0400154 u64 offset, unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000155{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400156 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000157 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400158 u64 lblock, dblock;
159 u32 extlen = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000160 unsigned int o;
161 int copied = 0;
162 int error = 0;
Steven Whitehouse9b8c81d2008-02-22 16:09:31 +0000163 int new = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000164
165 if (!size)
166 return 0;
167
168 if (gfs2_is_stuffed(ip) &&
169 offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500170 return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
171 size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000172
173 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
174 return -EINVAL;
175
176 if (gfs2_is_stuffed(ip)) {
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400177 error = gfs2_unstuff_dinode(ip, NULL);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000178 if (error)
Steven Whitehousec7526662006-03-20 12:30:04 -0500179 return error;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000180 }
181
182 lblock = offset;
183 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
184
185 while (copied < size) {
186 unsigned int amount;
187 struct buffer_head *bh;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000188
189 amount = size - copied;
190 if (amount > sdp->sd_sb.sb_bsize - o)
191 amount = sdp->sd_sb.sb_bsize - o;
192
193 if (!extlen) {
194 new = 1;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400195 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
Steven Whitehousefd88de562006-05-05 16:59:11 -0400196 &dblock, &extlen);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000197 if (error)
198 goto fail;
199 error = -EIO;
200 if (gfs2_assert_withdraw(sdp, dblock))
201 goto fail;
202 }
203
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400204 if (amount == sdp->sd_jbsize || new)
205 error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
206 else
207 error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
208
Steven Whitehousee13940b2006-01-30 13:31:50 +0000209 if (error)
210 goto fail;
211
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000212 gfs2_trans_add_meta(ip->i_gl, bh);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000213 memcpy(bh->b_data + o, buf, amount);
214 brelse(bh);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000215
Steven Whitehouse899bb262006-08-01 15:28:57 -0400216 buf += amount;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000217 copied += amount;
218 lblock++;
219 dblock++;
220 extlen--;
221
222 o = sizeof(struct gfs2_meta_header);
223 }
224
225out:
226 error = gfs2_meta_inode_buffer(ip, &dibh);
227 if (error)
228 return error;
229
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100230 if (ip->i_inode.i_size < offset + copied)
231 i_size_write(&ip->i_inode, offset + copied);
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +0100232 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000233
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000234 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500235 gfs2_dinode_out(ip, dibh->b_data);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000236 brelse(dibh);
237
238 return copied;
239fail:
240 if (copied)
241 goto out;
242 return error;
243}
244
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100245static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, __be64 *buf,
246 unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000247{
248 struct buffer_head *dibh;
249 int error;
250
251 error = gfs2_meta_inode_buffer(ip, &dibh);
252 if (!error) {
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100253 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000254 brelse(dibh);
255 }
256
257 return (error) ? error : size;
258}
259
260
261/**
262 * gfs2_dir_read_data - Read a data from a directory inode
263 * @ip: The GFS2 Inode
264 * @buf: The buffer to place result into
Steven Whitehousee13940b2006-01-30 13:31:50 +0000265 * @size: Amount of data to transfer
266 *
267 * Returns: The amount of data actually copied or the error
268 */
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100269static int gfs2_dir_read_data(struct gfs2_inode *ip, __be64 *buf,
270 unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000271{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400272 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehousecd915492006-09-04 12:49:07 -0400273 u64 lblock, dblock;
274 u32 extlen = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000275 unsigned int o;
276 int copied = 0;
277 int error = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000278
279 if (gfs2_is_stuffed(ip))
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100280 return gfs2_dir_read_stuffed(ip, buf, size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000281
282 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
283 return -EINVAL;
284
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100285 lblock = 0;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000286 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
287
288 while (copied < size) {
289 unsigned int amount;
290 struct buffer_head *bh;
291 int new;
292
293 amount = size - copied;
294 if (amount > sdp->sd_sb.sb_bsize - o)
295 amount = sdp->sd_sb.sb_bsize - o;
296
297 if (!extlen) {
298 new = 0;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400299 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
Steven Whitehousefd88de562006-05-05 16:59:11 -0400300 &dblock, &extlen);
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400301 if (error || !dblock)
302 goto fail;
303 BUG_ON(extlen < 1);
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400304 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
Adrian Bunkb7d8ac32006-10-19 16:02:07 +0200305 } else {
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400306 error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, &bh);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000307 if (error)
308 goto fail;
309 }
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400310 error = gfs2_metatype_check(sdp, bh, GFS2_METATYPE_JD);
311 if (error) {
312 brelse(bh);
313 goto fail;
314 }
315 dblock++;
316 extlen--;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000317 memcpy(buf, bh->b_data + o, amount);
318 brelse(bh);
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100319 buf += (amount/sizeof(__be64));
Steven Whitehousee13940b2006-01-30 13:31:50 +0000320 copied += amount;
321 lblock++;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000322 o = sizeof(struct gfs2_meta_header);
323 }
324
325 return copied;
326fail:
327 return (copied) ? copied : error;
328}
329
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100330/**
331 * gfs2_dir_get_hash_table - Get pointer to the dir hash table
332 * @ip: The inode in question
333 *
334 * Returns: The hash table or an error
335 */
336
337static __be64 *gfs2_dir_get_hash_table(struct gfs2_inode *ip)
338{
339 struct inode *inode = &ip->i_inode;
340 int ret;
341 u32 hsize;
342 __be64 *hc;
343
344 BUG_ON(!(ip->i_diskflags & GFS2_DIF_EXHASH));
345
346 hc = ip->i_hash_cache;
347 if (hc)
348 return hc;
349
350 hsize = 1 << ip->i_depth;
351 hsize *= sizeof(__be64);
352 if (hsize != i_size_read(&ip->i_inode)) {
353 gfs2_consist_inode(ip);
354 return ERR_PTR(-EIO);
355 }
356
Bob Petersone8830d82013-05-30 09:48:56 -0400357 hc = kmalloc(hsize, GFP_NOFS | __GFP_NOWARN);
358 if (hc == NULL)
359 hc = __vmalloc(hsize, GFP_NOFS, PAGE_KERNEL);
360
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100361 if (hc == NULL)
362 return ERR_PTR(-ENOMEM);
363
Steven Whitehouse4c28d332011-07-26 09:17:28 +0100364 ret = gfs2_dir_read_data(ip, hc, hsize);
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100365 if (ret < 0) {
Bob Petersone8830d82013-05-30 09:48:56 -0400366 if (is_vmalloc_addr(hc))
367 vfree(hc);
368 else
369 kfree(hc);
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100370 return ERR_PTR(ret);
371 }
372
373 spin_lock(&inode->i_lock);
Bob Petersone8830d82013-05-30 09:48:56 -0400374 if (ip->i_hash_cache) {
375 if (is_vmalloc_addr(hc))
376 vfree(hc);
377 else
378 kfree(hc);
379 } else {
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100380 ip->i_hash_cache = hc;
Bob Petersone8830d82013-05-30 09:48:56 -0400381 }
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100382 spin_unlock(&inode->i_lock);
383
384 return ip->i_hash_cache;
385}
386
387/**
388 * gfs2_dir_hash_inval - Invalidate dir hash
389 * @ip: The directory inode
390 *
391 * Must be called with an exclusive glock, or during glock invalidation.
392 */
393void gfs2_dir_hash_inval(struct gfs2_inode *ip)
394{
395 __be64 *hc = ip->i_hash_cache;
396 ip->i_hash_cache = NULL;
Bob Petersone8830d82013-05-30 09:48:56 -0400397 if (is_vmalloc_addr(hc))
398 vfree(hc);
399 else
400 kfree(hc);
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100401}
402
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500403static inline int gfs2_dirent_sentinel(const struct gfs2_dirent *dent)
404{
405 return dent->de_inum.no_addr == 0 || dent->de_inum.no_formal_ino == 0;
406}
407
Steven Whitehousec7526662006-03-20 12:30:04 -0500408static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
409 const struct qstr *name, int ret)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000410{
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500411 if (!gfs2_dirent_sentinel(dent) &&
Steven Whitehousec7526662006-03-20 12:30:04 -0500412 be32_to_cpu(dent->de_hash) == name->hash &&
413 be16_to_cpu(dent->de_name_len) == name->len &&
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400414 memcmp(dent+1, name->name, name->len) == 0)
Steven Whitehousec7526662006-03-20 12:30:04 -0500415 return ret;
416 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000417}
418
Steven Whitehousec7526662006-03-20 12:30:04 -0500419static int gfs2_dirent_find(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500420 const struct qstr *name,
421 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500422{
423 return __gfs2_dirent_find(dent, name, 1);
424}
425
426static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500427 const struct qstr *name,
428 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500429{
430 return __gfs2_dirent_find(dent, name, 2);
431}
432
433/*
434 * name->name holds ptr to start of block.
435 * name->len holds size of block.
436 */
437static int gfs2_dirent_last(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500438 const struct qstr *name,
439 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500440{
441 const char *start = name->name;
442 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
443 if (name->len == (end - start))
444 return 1;
445 return 0;
446}
447
448static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500449 const struct qstr *name,
450 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500451{
452 unsigned required = GFS2_DIRENT_SIZE(name->len);
453 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
454 unsigned totlen = be16_to_cpu(dent->de_rec_len);
455
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500456 if (gfs2_dirent_sentinel(dent))
Bob Peterson728a7562010-07-14 18:12:26 -0400457 actual = 0;
Jan Engelhardtc53921242006-09-05 14:30:40 +0200458 if (totlen - actual >= required)
Steven Whitehousec7526662006-03-20 12:30:04 -0500459 return 1;
460 return 0;
461}
462
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500463struct dirent_gather {
464 const struct gfs2_dirent **pdent;
465 unsigned offset;
466};
467
468static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
469 const struct qstr *name,
470 void *opaque)
471{
472 struct dirent_gather *g = opaque;
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500473 if (!gfs2_dirent_sentinel(dent)) {
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500474 g->pdent[g->offset++] = dent;
475 }
476 return 0;
477}
478
Steven Whitehousec7526662006-03-20 12:30:04 -0500479/*
480 * Other possible things to check:
481 * - Inode located within filesystem size (and on valid block)
482 * - Valid directory entry type
483 * Not sure how heavy-weight we want to make this... could also check
484 * hash is correct for example, but that would take a lot of extra time.
485 * For now the most important thing is to check that the various sizes
486 * are correct.
487 */
488static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
489 unsigned int size, unsigned int len, int first)
490{
491 const char *msg = "gfs2_dirent too small";
492 if (unlikely(size < sizeof(struct gfs2_dirent)))
493 goto error;
494 msg = "gfs2_dirent misaligned";
495 if (unlikely(offset & 0x7))
496 goto error;
497 msg = "gfs2_dirent points beyond end of block";
498 if (unlikely(offset + size > len))
499 goto error;
500 msg = "zero inode number";
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500501 if (unlikely(!first && gfs2_dirent_sentinel(dent)))
Steven Whitehousec7526662006-03-20 12:30:04 -0500502 goto error;
503 msg = "name length is greater than space in dirent";
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500504 if (!gfs2_dirent_sentinel(dent) &&
Steven Whitehousec7526662006-03-20 12:30:04 -0500505 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
506 size))
507 goto error;
508 return 0;
509error:
510 printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
511 first ? "first in block" : "not first in block");
512 return -EIO;
513}
514
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500515static int gfs2_dirent_offset(const void *buf)
Steven Whitehousec7526662006-03-20 12:30:04 -0500516{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500517 const struct gfs2_meta_header *h = buf;
518 int offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500519
520 BUG_ON(buf == NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500521
Steven Whitehousee3167de2006-03-30 15:46:23 -0500522 switch(be32_to_cpu(h->mh_type)) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500523 case GFS2_METATYPE_LF:
524 offset = sizeof(struct gfs2_leaf);
525 break;
526 case GFS2_METATYPE_DI:
527 offset = sizeof(struct gfs2_dinode);
528 break;
529 default:
530 goto wrong_type;
531 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500532 return offset;
533wrong_type:
534 printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
Steven Whitehousee3167de2006-03-30 15:46:23 -0500535 be32_to_cpu(h->mh_type));
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500536 return -1;
537}
Steven Whitehousec7526662006-03-20 12:30:04 -0500538
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400539static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500540 unsigned int len, gfs2_dscan_t scan,
541 const struct qstr *name,
542 void *opaque)
543{
544 struct gfs2_dirent *dent, *prev;
545 unsigned offset;
546 unsigned size;
547 int ret = 0;
548
549 ret = gfs2_dirent_offset(buf);
550 if (ret < 0)
551 goto consist_inode;
552
553 offset = ret;
Steven Whitehousec7526662006-03-20 12:30:04 -0500554 prev = NULL;
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400555 dent = buf + offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500556 size = be16_to_cpu(dent->de_rec_len);
557 if (gfs2_check_dirent(dent, offset, size, len, 1))
558 goto consist_inode;
559 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500560 ret = scan(dent, name, opaque);
Steven Whitehousec7526662006-03-20 12:30:04 -0500561 if (ret)
562 break;
563 offset += size;
564 if (offset == len)
565 break;
566 prev = dent;
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400567 dent = buf + offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500568 size = be16_to_cpu(dent->de_rec_len);
569 if (gfs2_check_dirent(dent, offset, size, len, 0))
570 goto consist_inode;
571 } while(1);
572
573 switch(ret) {
574 case 0:
575 return NULL;
576 case 1:
577 return dent;
578 case 2:
579 return prev ? prev : dent;
580 default:
581 BUG_ON(ret > 0);
582 return ERR_PTR(ret);
583 }
584
Steven Whitehousec7526662006-03-20 12:30:04 -0500585consist_inode:
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400586 gfs2_consist_inode(GFS2_I(inode));
Steven Whitehousec7526662006-03-20 12:30:04 -0500587 return ERR_PTR(-EIO);
588}
589
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400590static int dirent_check_reclen(struct gfs2_inode *dip,
591 const struct gfs2_dirent *d, const void *end_p)
592{
593 const void *ptr = d;
594 u16 rec_len = be16_to_cpu(d->de_rec_len);
595
596 if (unlikely(rec_len < sizeof(struct gfs2_dirent)))
597 goto broken;
598 ptr += rec_len;
599 if (ptr < end_p)
600 return rec_len;
601 if (ptr == end_p)
602 return -ENOENT;
603broken:
604 gfs2_consist_inode(dip);
605 return -EIO;
606}
607
David Teiglandb3b94fa2006-01-16 16:50:04 +0000608/**
609 * dirent_next - Next dirent
610 * @dip: the directory
611 * @bh: The buffer
612 * @dent: Pointer to list of dirents
613 *
614 * Returns: 0 on success, error code otherwise
615 */
616
617static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
618 struct gfs2_dirent **dent)
619{
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400620 struct gfs2_dirent *cur = *dent, *tmp;
621 char *bh_end = bh->b_data + bh->b_size;
622 int ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000623
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400624 ret = dirent_check_reclen(dip, cur, bh_end);
625 if (ret < 0)
626 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000627
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400628 tmp = (void *)cur + ret;
629 ret = dirent_check_reclen(dip, tmp, bh_end);
630 if (ret == -EIO)
631 return ret;
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000632
David Teiglandb3b94fa2006-01-16 16:50:04 +0000633 /* Only the first dent could ever have de_inum.no_addr == 0 */
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500634 if (gfs2_dirent_sentinel(tmp)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000635 gfs2_consist_inode(dip);
636 return -EIO;
637 }
638
639 *dent = tmp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000640 return 0;
641}
642
643/**
644 * dirent_del - Delete a dirent
645 * @dip: The GFS2 inode
646 * @bh: The buffer
647 * @prev: The previous dirent
648 * @cur: The current dirent
649 *
650 */
651
652static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
653 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
654{
Steven Whitehousecd915492006-09-04 12:49:07 -0400655 u16 cur_rec_len, prev_rec_len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000656
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500657 if (gfs2_dirent_sentinel(cur)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000658 gfs2_consist_inode(dip);
659 return;
660 }
661
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000662 gfs2_trans_add_meta(dip->i_gl, bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000663
664 /* If there is no prev entry, this is the first entry in the block.
665 The de_rec_len is already as big as it needs to be. Just zero
666 out the inode number and return. */
667
668 if (!prev) {
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500669 cur->de_inum.no_addr = 0;
670 cur->de_inum.no_formal_ino = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000671 return;
672 }
673
674 /* Combine this dentry with the previous one. */
675
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000676 prev_rec_len = be16_to_cpu(prev->de_rec_len);
677 cur_rec_len = be16_to_cpu(cur->de_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000678
679 if ((char *)prev + prev_rec_len != (char *)cur)
680 gfs2_consist_inode(dip);
681 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
682 gfs2_consist_inode(dip);
683
684 prev_rec_len += cur_rec_len;
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000685 prev->de_rec_len = cpu_to_be16(prev_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000686}
687
Steven Whitehousec7526662006-03-20 12:30:04 -0500688/*
689 * Takes a dent from which to grab space as an argument. Returns the
690 * newly created dent.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000691 */
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400692static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
693 struct gfs2_dirent *dent,
694 const struct qstr *name,
695 struct buffer_head *bh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000696{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400697 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -0500698 struct gfs2_dirent *ndent;
699 unsigned offset = 0, totlen;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000700
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -0500701 if (!gfs2_dirent_sentinel(dent))
Steven Whitehousec7526662006-03-20 12:30:04 -0500702 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
703 totlen = be16_to_cpu(dent->de_rec_len);
704 BUG_ON(offset + name->len > totlen);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000705 gfs2_trans_add_meta(ip->i_gl, bh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500706 ndent = (struct gfs2_dirent *)((char *)dent + offset);
707 dent->de_rec_len = cpu_to_be16(offset);
708 gfs2_qstr2dirent(name, totlen - offset, ndent);
709 return ndent;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000710}
711
Steven Whitehousec7526662006-03-20 12:30:04 -0500712static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
713 struct buffer_head *bh,
714 const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000715{
716 struct gfs2_dirent *dent;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400717 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500718 gfs2_dirent_find_space, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500719 if (!dent || IS_ERR(dent))
720 return dent;
721 return gfs2_init_dirent(inode, dent, name, bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000722}
723
Steven Whitehousecd915492006-09-04 12:49:07 -0400724static int get_leaf(struct gfs2_inode *dip, u64 leaf_no,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000725 struct buffer_head **bhp)
726{
727 int error;
728
Steven Whitehouse7276b3b2006-09-21 17:05:23 -0400729 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, bhp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400730 if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
731 /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000732 error = -EIO;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400733 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000734
735 return error;
736}
737
738/**
739 * get_leaf_nr - Get a leaf number associated with the index
740 * @dip: The GFS2 inode
741 * @index:
742 * @leaf_out:
743 *
744 * Returns: 0 on success, error code otherwise
745 */
746
Steven Whitehousecd915492006-09-04 12:49:07 -0400747static int get_leaf_nr(struct gfs2_inode *dip, u32 index,
748 u64 *leaf_out)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000749{
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100750 __be64 *hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000751
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100752 hash = gfs2_dir_get_hash_table(dip);
753 if (IS_ERR(hash))
754 return PTR_ERR(hash);
755 *leaf_out = be64_to_cpu(*(hash + index));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000756 return 0;
757}
758
Steven Whitehousecd915492006-09-04 12:49:07 -0400759static int get_first_leaf(struct gfs2_inode *dip, u32 index,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000760 struct buffer_head **bh_out)
761{
Steven Whitehousecd915492006-09-04 12:49:07 -0400762 u64 leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000763 int error;
764
765 error = get_leaf_nr(dip, index, &leaf_no);
766 if (!error)
767 error = get_leaf(dip, leaf_no, bh_out);
768
769 return error;
770}
771
Steven Whitehousec7526662006-03-20 12:30:04 -0500772static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
773 const struct qstr *name,
774 gfs2_dscan_t scan,
775 struct buffer_head **pbh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000776{
Steven Whitehousec7526662006-03-20 12:30:04 -0500777 struct buffer_head *bh;
778 struct gfs2_dirent *dent;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400779 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000780 int error;
781
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000782 if (ip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500783 struct gfs2_leaf *leaf;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000784 unsigned hsize = 1 << ip->i_depth;
Steven Whitehousec7526662006-03-20 12:30:04 -0500785 unsigned index;
786 u64 ln;
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100787 if (hsize * sizeof(u64) != i_size_read(inode)) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500788 gfs2_consist_inode(ip);
789 return ERR_PTR(-EIO);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000790 }
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400791
Steven Whitehouse9a004502008-02-01 09:23:44 +0000792 index = name->hash >> (32 - ip->i_depth);
Steven Whitehousec7526662006-03-20 12:30:04 -0500793 error = get_first_leaf(ip, index, &bh);
794 if (error)
795 return ERR_PTR(error);
796 do {
797 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500798 scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500799 if (dent)
800 goto got_dent;
801 leaf = (struct gfs2_leaf *)bh->b_data;
802 ln = be64_to_cpu(leaf->lf_next);
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400803 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500804 if (!ln)
805 break;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400806
Steven Whitehousec7526662006-03-20 12:30:04 -0500807 error = get_leaf(ip, ln, &bh);
808 } while(!error);
809
810 return error ? ERR_PTR(error) : NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000811 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000812
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400813
Steven Whitehousec7526662006-03-20 12:30:04 -0500814 error = gfs2_meta_inode_buffer(ip, &bh);
815 if (error)
816 return ERR_PTR(error);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500817 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500818got_dent:
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400819 if (unlikely(dent == NULL || IS_ERR(dent))) {
Steven Whitehouseed386502006-04-07 16:28:07 -0400820 brelse(bh);
821 bh = NULL;
822 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500823 *pbh = bh;
824 return dent;
825}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000826
Steven Whitehousec7526662006-03-20 12:30:04 -0500827static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
828{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400829 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehouseb45e41d2008-02-06 10:11:15 +0000830 unsigned int n = 1;
Steven Whitehouse09010972009-05-20 10:48:47 +0100831 u64 bn;
832 int error;
833 struct buffer_head *bh;
Steven Whitehousec7526662006-03-20 12:30:04 -0500834 struct gfs2_leaf *leaf;
835 struct gfs2_dirent *dent;
Linus Torvalds26fe5752012-05-10 13:14:12 -0700836 struct qstr name = { .name = "" };
Steven Whitehouse09010972009-05-20 10:48:47 +0100837
Bob Peterson6e87ed02011-11-18 10:58:32 -0500838 error = gfs2_alloc_blocks(ip, &bn, &n, 0, NULL);
Steven Whitehouse09010972009-05-20 10:48:47 +0100839 if (error)
840 return NULL;
841 bh = gfs2_meta_new(ip->i_gl, bn);
Steven Whitehousec7526662006-03-20 12:30:04 -0500842 if (!bh)
843 return NULL;
Steven Whitehouse09010972009-05-20 10:48:47 +0100844
Steven Whitehouse5731be52008-02-01 13:16:55 +0000845 gfs2_trans_add_unrevoke(GFS2_SB(inode), bn, 1);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000846 gfs2_trans_add_meta(ip->i_gl, bh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500847 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
848 leaf = (struct gfs2_leaf *)bh->b_data;
849 leaf->lf_depth = cpu_to_be16(depth);
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400850 leaf->lf_entries = 0;
Al Viroa2d7d022006-10-14 16:49:30 +0100851 leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -0400852 leaf->lf_next = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500853 memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
854 dent = (struct gfs2_dirent *)(leaf+1);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500855 gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
Steven Whitehousec7526662006-03-20 12:30:04 -0500856 *pbh = bh;
857 return leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000858}
859
860/**
861 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
862 * @dip: The GFS2 inode
863 *
864 * Returns: 0 on success, error code otherwise
865 */
866
Steven Whitehousec7526662006-03-20 12:30:04 -0500867static int dir_make_exhash(struct inode *inode)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000868{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400869 struct gfs2_inode *dip = GFS2_I(inode);
870 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000871 struct gfs2_dirent *dent;
Steven Whitehousec7526662006-03-20 12:30:04 -0500872 struct qstr args;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000873 struct buffer_head *bh, *dibh;
874 struct gfs2_leaf *leaf;
875 int y;
Steven Whitehousecd915492006-09-04 12:49:07 -0400876 u32 x;
Al Virob44b84d2006-10-14 10:46:30 -0400877 __be64 *lp;
878 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000879 int error;
880
881 error = gfs2_meta_inode_buffer(dip, &dibh);
882 if (error)
883 return error;
884
David Teiglandb3b94fa2006-01-16 16:50:04 +0000885 /* Turn over a new leaf */
886
Steven Whitehousec7526662006-03-20 12:30:04 -0500887 leaf = new_leaf(inode, &bh, 0);
888 if (!leaf)
889 return -ENOSPC;
890 bn = bh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000891
Steven Whitehousead6203f2008-11-03 13:59:19 +0000892 gfs2_assert(sdp, dip->i_entries < (1 << 16));
893 leaf->lf_entries = cpu_to_be16(dip->i_entries);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000894
895 /* Copy dirents */
896
897 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
898 sizeof(struct gfs2_dinode));
899
900 /* Find last entry */
901
902 x = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500903 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
904 sizeof(struct gfs2_leaf);
905 args.name = bh->b_data;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400906 dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500907 gfs2_dirent_last, &args, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500908 if (!dent) {
909 brelse(bh);
910 brelse(dibh);
911 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000912 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500913 if (IS_ERR(dent)) {
914 brelse(bh);
915 brelse(dibh);
916 return PTR_ERR(dent);
917 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000918
919 /* Adjust the last dirent's record length
920 (Remember that dent still points to the last entry.) */
921
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000922 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000923 sizeof(struct gfs2_dinode) -
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000924 sizeof(struct gfs2_leaf));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000925
926 brelse(bh);
927
928 /* We're done with the new leaf block, now setup the new
929 hash table. */
930
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000931 gfs2_trans_add_meta(dip->i_gl, dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000932 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
933
Al Virob44b84d2006-10-14 10:46:30 -0400934 lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000935
936 for (x = sdp->sd_hash_ptrs; x--; lp++)
937 *lp = cpu_to_be64(bn);
938
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100939 i_size_write(inode, sdp->sd_sb.sb_bsize / 2);
Steven Whitehouse77658aa2008-02-12 14:17:27 +0000940 gfs2_add_inode_blocks(&dip->i_inode, 1);
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000941 dip->i_diskflags |= GFS2_DIF_EXHASH;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000942
943 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000944 dip->i_depth = y;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000945
Steven Whitehouse539e5d62006-10-31 15:07:05 -0500946 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000947
948 brelse(dibh);
949
950 return 0;
951}
952
953/**
954 * dir_split_leaf - Split a leaf block into two
955 * @dip: The GFS2 inode
956 * @index:
957 * @leaf_no:
958 *
959 * Returns: 0 on success, error code on failure
960 */
961
Steven Whitehousec7526662006-03-20 12:30:04 -0500962static int dir_split_leaf(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000963{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400964 struct gfs2_inode *dip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000965 struct buffer_head *nbh, *obh, *dibh;
966 struct gfs2_leaf *nleaf, *oleaf;
Steven Whitehouse4da3c642006-07-11 13:19:13 -0400967 struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
Steven Whitehousecd915492006-09-04 12:49:07 -0400968 u32 start, len, half_len, divider;
Al Virob44b84d2006-10-14 10:46:30 -0400969 u64 bn, leaf_no;
970 __be64 *lp;
Steven Whitehousecd915492006-09-04 12:49:07 -0400971 u32 index;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000972 int x, moved = 0;
973 int error;
974
Steven Whitehouse9a004502008-02-01 09:23:44 +0000975 index = name->hash >> (32 - dip->i_depth);
Steven Whitehousec7526662006-03-20 12:30:04 -0500976 error = get_leaf_nr(dip, index, &leaf_no);
977 if (error)
978 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000979
980 /* Get the old leaf block */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000981 error = get_leaf(dip, leaf_no, &obh);
982 if (error)
Steven Whitehousee90deff2006-03-29 19:02:15 -0500983 return error;
984
985 oleaf = (struct gfs2_leaf *)obh->b_data;
Steven Whitehouse9a004502008-02-01 09:23:44 +0000986 if (dip->i_depth == be16_to_cpu(oleaf->lf_depth)) {
Steven Whitehousee90deff2006-03-29 19:02:15 -0500987 brelse(obh);
988 return 1; /* can't split */
989 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000990
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000991 gfs2_trans_add_meta(dip->i_gl, obh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000992
Steven Whitehousec7526662006-03-20 12:30:04 -0500993 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
994 if (!nleaf) {
995 brelse(obh);
996 return -ENOSPC;
997 }
998 bn = nbh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000999
Steven Whitehousec7526662006-03-20 12:30:04 -05001000 /* Compute the start and len of leaf pointers in the hash table. */
Steven Whitehouse9a004502008-02-01 09:23:44 +00001001 len = 1 << (dip->i_depth - be16_to_cpu(oleaf->lf_depth));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001002 half_len = len >> 1;
1003 if (!half_len) {
Steven Whitehouse9a004502008-02-01 09:23:44 +00001004 printk(KERN_WARNING "i_depth %u lf_depth %u index %u\n", dip->i_depth, be16_to_cpu(oleaf->lf_depth), index);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001005 gfs2_consist_inode(dip);
1006 error = -EIO;
1007 goto fail_brelse;
1008 }
1009
1010 start = (index & ~(len - 1));
1011
1012 /* Change the pointers.
1013 Don't bother distinguishing stuffed from non-stuffed.
1014 This code is complicated enough already. */
David Rientjes4244b522010-07-20 19:45:03 -07001015 lp = kmalloc(half_len * sizeof(__be64), GFP_NOFS);
1016 if (!lp) {
1017 error = -ENOMEM;
1018 goto fail_brelse;
1019 }
1020
David Teiglandb3b94fa2006-01-16 16:50:04 +00001021 /* Change the pointers */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001022 for (x = 0; x < half_len; x++)
1023 lp[x] = cpu_to_be64(bn);
1024
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001025 gfs2_dir_hash_inval(dip);
1026
Steven Whitehousecd915492006-09-04 12:49:07 -04001027 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
1028 half_len * sizeof(u64));
1029 if (error != half_len * sizeof(u64)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001030 if (error >= 0)
1031 error = -EIO;
1032 goto fail_lpfree;
1033 }
1034
1035 kfree(lp);
1036
1037 /* Compute the divider */
Steven Whitehouse9a004502008-02-01 09:23:44 +00001038 divider = (start + half_len) << (32 - dip->i_depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001039
1040 /* Copy the entries */
Steven Whitehouse15793432009-11-06 11:06:37 +00001041 dent = (struct gfs2_dirent *)(obh->b_data + sizeof(struct gfs2_leaf));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001042
1043 do {
1044 next = dent;
1045 if (dirent_next(dip, obh, &next))
1046 next = NULL;
1047
Steven Whitehouse5e7d65c2006-11-17 12:27:44 -05001048 if (!gfs2_dirent_sentinel(dent) &&
David Teiglandb3b94fa2006-01-16 16:50:04 +00001049 be32_to_cpu(dent->de_hash) < divider) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001050 struct qstr str;
1051 str.name = (char*)(dent+1);
1052 str.len = be16_to_cpu(dent->de_name_len);
1053 str.hash = be32_to_cpu(dent->de_hash);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001054 new = gfs2_dirent_alloc(inode, nbh, &str);
Steven Whitehousec7526662006-03-20 12:30:04 -05001055 if (IS_ERR(new)) {
1056 error = PTR_ERR(new);
1057 break;
1058 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001059
1060 new->de_inum = dent->de_inum; /* No endian worries */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001061 new->de_type = dent->de_type; /* No endian worries */
Marcin Slusarzbb16b342008-02-13 00:06:10 +01001062 be16_add_cpu(&nleaf->lf_entries, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001063
1064 dirent_del(dip, obh, prev, dent);
1065
1066 if (!oleaf->lf_entries)
1067 gfs2_consist_inode(dip);
Marcin Slusarzbb16b342008-02-13 00:06:10 +01001068 be16_add_cpu(&oleaf->lf_entries, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001069
1070 if (!prev)
1071 prev = dent;
1072
1073 moved = 1;
Steven Whitehousec7526662006-03-20 12:30:04 -05001074 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001075 prev = dent;
Steven Whitehousec7526662006-03-20 12:30:04 -05001076 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001077 dent = next;
Steven Whitehousec7526662006-03-20 12:30:04 -05001078 } while (dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001079
Steven Whitehousec7526662006-03-20 12:30:04 -05001080 oleaf->lf_depth = nleaf->lf_depth;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001081
1082 error = gfs2_meta_inode_buffer(dip, &dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001083 if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001084 gfs2_trans_add_meta(dip->i_gl, dibh);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001085 gfs2_add_inode_blocks(&dip->i_inode, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001086 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001087 brelse(dibh);
1088 }
1089
1090 brelse(obh);
1091 brelse(nbh);
1092
1093 return error;
1094
Steven Whitehousee90deff2006-03-29 19:02:15 -05001095fail_lpfree:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001096 kfree(lp);
1097
Steven Whitehousee90deff2006-03-29 19:02:15 -05001098fail_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001099 brelse(obh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001100 brelse(nbh);
1101 return error;
1102}
1103
1104/**
1105 * dir_double_exhash - Double size of ExHash table
1106 * @dip: The GFS2 dinode
1107 *
1108 * Returns: 0 on success, error code on failure
1109 */
1110
1111static int dir_double_exhash(struct gfs2_inode *dip)
1112{
David Teiglandb3b94fa2006-01-16 16:50:04 +00001113 struct buffer_head *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001114 u32 hsize;
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001115 u32 hsize_bytes;
1116 __be64 *hc;
1117 __be64 *hc2, *h;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001118 int x;
1119 int error = 0;
1120
Steven Whitehouse9a004502008-02-01 09:23:44 +00001121 hsize = 1 << dip->i_depth;
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001122 hsize_bytes = hsize * sizeof(__be64);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001123
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001124 hc = gfs2_dir_get_hash_table(dip);
1125 if (IS_ERR(hc))
1126 return PTR_ERR(hc);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001127
Bob Peterson512cbf02013-06-14 08:39:18 -04001128 hc2 = kmalloc(hsize_bytes * 2, GFP_NOFS | __GFP_NOWARN);
Bob Petersone8830d82013-05-30 09:48:56 -04001129 if (hc2 == NULL)
1130 hc2 = __vmalloc(hsize_bytes * 2, GFP_NOFS, PAGE_KERNEL);
1131
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001132 if (!hc2)
David Rientjes4244b522010-07-20 19:45:03 -07001133 return -ENOMEM;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001134
Bob Peterson512cbf02013-06-14 08:39:18 -04001135 h = hc2;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001136 error = gfs2_meta_inode_buffer(dip, &dibh);
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001137 if (error)
1138 goto out_kfree;
1139
1140 for (x = 0; x < hsize; x++) {
1141 *h++ = *hc;
1142 *h++ = *hc;
1143 hc++;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001144 }
1145
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001146 error = gfs2_dir_write_data(dip, (char *)hc2, 0, hsize_bytes * 2);
1147 if (error != (hsize_bytes * 2))
1148 goto fail;
1149
1150 gfs2_dir_hash_inval(dip);
1151 dip->i_hash_cache = hc2;
1152 dip->i_depth++;
1153 gfs2_dinode_out(dip, dibh->b_data);
1154 brelse(dibh);
1155 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001156
Steven Whitehousea91ea692006-09-04 12:04:26 -04001157fail:
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001158 /* Replace original hash table & size */
1159 gfs2_dir_write_data(dip, (char *)hc, 0, hsize_bytes);
1160 i_size_write(&dip->i_inode, hsize_bytes);
1161 gfs2_dinode_out(dip, dibh->b_data);
1162 brelse(dibh);
1163out_kfree:
Bob Petersone8830d82013-05-30 09:48:56 -04001164 if (is_vmalloc_addr(hc2))
1165 vfree(hc2);
1166 else
1167 kfree(hc2);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001168 return error;
1169}
1170
1171/**
1172 * compare_dents - compare directory entries by hash value
1173 * @a: first dent
1174 * @b: second dent
1175 *
1176 * When comparing the hash entries of @a to @b:
1177 * gt: returns 1
1178 * lt: returns -1
1179 * eq: returns 0
1180 */
1181
1182static int compare_dents(const void *a, const void *b)
1183{
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001184 const struct gfs2_dirent *dent_a, *dent_b;
Steven Whitehousecd915492006-09-04 12:49:07 -04001185 u32 hash_a, hash_b;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001186 int ret = 0;
1187
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001188 dent_a = *(const struct gfs2_dirent **)a;
Steven Whitehousec7526662006-03-20 12:30:04 -05001189 hash_a = be32_to_cpu(dent_a->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001190
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001191 dent_b = *(const struct gfs2_dirent **)b;
Steven Whitehousec7526662006-03-20 12:30:04 -05001192 hash_b = be32_to_cpu(dent_b->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001193
1194 if (hash_a > hash_b)
1195 ret = 1;
1196 else if (hash_a < hash_b)
1197 ret = -1;
1198 else {
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001199 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1200 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001201
1202 if (len_a > len_b)
1203 ret = 1;
1204 else if (len_a < len_b)
1205 ret = -1;
1206 else
Steven Whitehouse2bdbc5d2006-09-05 09:34:20 -04001207 ret = memcmp(dent_a + 1, dent_b + 1, len_a);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001208 }
1209
1210 return ret;
1211}
1212
1213/**
1214 * do_filldir_main - read out directory entries
1215 * @dip: The GFS2 inode
Al Virod81a8ef2013-05-16 14:14:48 -04001216 * @ctx: what to feed the entries to
David Teiglandb3b94fa2006-01-16 16:50:04 +00001217 * @darr: an array of struct gfs2_dirent pointers to read
1218 * @entries: the number of entries in darr
1219 * @copied: pointer to int that's non-zero if a entry has been copied out
1220 *
1221 * Jump through some hoops to make sure that if there are hash collsions,
1222 * they are read out at the beginning of a buffer. We want to minimize
1223 * the possibility that they will fall into different readdir buffers or
1224 * that someone will want to seek to that location.
1225 *
Al Virod81a8ef2013-05-16 14:14:48 -04001226 * Returns: errno, >0 if the actor tells you to stop
David Teiglandb3b94fa2006-01-16 16:50:04 +00001227 */
1228
Al Virod81a8ef2013-05-16 14:14:48 -04001229static int do_filldir_main(struct gfs2_inode *dip, struct dir_context *ctx,
Steven Whitehousecd915492006-09-04 12:49:07 -04001230 const struct gfs2_dirent **darr, u32 entries,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001231 int *copied)
1232{
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001233 const struct gfs2_dirent *dent, *dent_next;
Steven Whitehousecd915492006-09-04 12:49:07 -04001234 u64 off, off_next;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001235 unsigned int x, y;
1236 int run = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001237
1238 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1239
1240 dent_next = darr[0];
1241 off_next = be32_to_cpu(dent_next->de_hash);
1242 off_next = gfs2_disk_hash2offset(off_next);
1243
1244 for (x = 0, y = 1; x < entries; x++, y++) {
1245 dent = dent_next;
1246 off = off_next;
1247
1248 if (y < entries) {
1249 dent_next = darr[y];
1250 off_next = be32_to_cpu(dent_next->de_hash);
1251 off_next = gfs2_disk_hash2offset(off_next);
1252
Al Virod81a8ef2013-05-16 14:14:48 -04001253 if (off < ctx->pos)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001254 continue;
Al Virod81a8ef2013-05-16 14:14:48 -04001255 ctx->pos = off;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001256
1257 if (off_next == off) {
1258 if (*copied && !run)
1259 return 1;
1260 run = 1;
1261 } else
1262 run = 0;
1263 } else {
Al Virod81a8ef2013-05-16 14:14:48 -04001264 if (off < ctx->pos)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001265 continue;
Al Virod81a8ef2013-05-16 14:14:48 -04001266 ctx->pos = off;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001267 }
1268
Al Virod81a8ef2013-05-16 14:14:48 -04001269 if (!dir_emit(ctx, (const char *)(dent + 1),
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001270 be16_to_cpu(dent->de_name_len),
Al Virod81a8ef2013-05-16 14:14:48 -04001271 be64_to_cpu(dent->de_inum.no_addr),
1272 be16_to_cpu(dent->de_type)))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001273 return 1;
1274
1275 *copied = 1;
1276 }
1277
Al Virod81a8ef2013-05-16 14:14:48 -04001278 /* Increment the ctx->pos by one, so the next time we come into the
David Teiglandb3b94fa2006-01-16 16:50:04 +00001279 do_filldir fxn, we get the next entry instead of the last one in the
1280 current leaf */
1281
Al Virod81a8ef2013-05-16 14:14:48 -04001282 ctx->pos++;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001283
1284 return 0;
1285}
1286
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001287static void *gfs2_alloc_sort_buffer(unsigned size)
1288{
1289 void *ptr = NULL;
1290
1291 if (size < KMALLOC_MAX_SIZE)
1292 ptr = kmalloc(size, GFP_NOFS | __GFP_NOWARN);
1293 if (!ptr)
1294 ptr = __vmalloc(size, GFP_NOFS, PAGE_KERNEL);
1295 return ptr;
1296}
1297
1298static void gfs2_free_sort_buffer(void *ptr)
1299{
1300 if (is_vmalloc_addr(ptr))
1301 vfree(ptr);
1302 else
1303 kfree(ptr);
1304}
1305
Al Virod81a8ef2013-05-16 14:14:48 -04001306static int gfs2_dir_read_leaf(struct inode *inode, struct dir_context *ctx,
1307 int *copied, unsigned *depth,
Steven Whitehouse3699e3a2007-01-17 15:09:20 +00001308 u64 leaf_no)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001309{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001310 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001311 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001312 struct buffer_head *bh;
1313 struct gfs2_leaf *lf;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001314 unsigned entries = 0, entries2 = 0;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001315 unsigned leaves = 0;
1316 const struct gfs2_dirent **darr, *dent;
1317 struct dirent_gather g;
1318 struct buffer_head **larr;
1319 int leaf = 0;
1320 int error, i;
1321 u64 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001322
David Teiglandb3b94fa2006-01-16 16:50:04 +00001323 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001324 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001325 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001326 goto out;
1327 lf = (struct gfs2_leaf *)bh->b_data;
1328 if (leaves == 0)
1329 *depth = be16_to_cpu(lf->lf_depth);
1330 entries += be16_to_cpu(lf->lf_entries);
1331 leaves++;
1332 lfn = be64_to_cpu(lf->lf_next);
1333 brelse(bh);
1334 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001335
1336 if (!entries)
1337 return 0;
1338
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001339 error = -ENOMEM;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001340 /*
1341 * The extra 99 entries are not normally used, but are a buffer
1342 * zone in case the number of entries in the leaf is corrupt.
1343 * 99 is the maximum number of entries that can fit in a single
1344 * leaf block.
1345 */
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001346 larr = gfs2_alloc_sort_buffer((leaves + entries + 99) * sizeof(void *));
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001347 if (!larr)
1348 goto out;
1349 darr = (const struct gfs2_dirent **)(larr + leaves);
1350 g.pdent = darr;
1351 g.offset = 0;
1352 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001353
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001354 do {
1355 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001356 if (error)
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001357 goto out_free;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001358 lf = (struct gfs2_leaf *)bh->b_data;
1359 lfn = be64_to_cpu(lf->lf_next);
1360 if (lf->lf_entries) {
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001361 entries2 += be16_to_cpu(lf->lf_entries);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001362 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1363 gfs2_dirent_gather, NULL, &g);
1364 error = PTR_ERR(dent);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001365 if (IS_ERR(dent))
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001366 goto out_free;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001367 if (entries2 != g.offset) {
akpm@linux-foundation.orgf391a4e2007-04-25 21:08:02 -07001368 fs_warn(sdp, "Number of entries corrupt in dir "
1369 "leaf %llu, entries2 (%u) != "
1370 "g.offset (%u)\n",
1371 (unsigned long long)bh->b_blocknr,
1372 entries2, g.offset);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001373
1374 error = -EIO;
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001375 goto out_free;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001376 }
1377 error = 0;
1378 larr[leaf++] = bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001379 } else {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001380 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001381 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001382 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001383
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001384 BUG_ON(entries2 != entries);
Al Virod81a8ef2013-05-16 14:14:48 -04001385 error = do_filldir_main(ip, ctx, darr, entries, copied);
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001386out_free:
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001387 for(i = 0; i < leaf; i++)
1388 brelse(larr[i]);
Steven Whitehoused2a97a42010-07-28 17:56:23 +01001389 gfs2_free_sort_buffer(larr);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001390out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001391 return error;
1392}
1393
Steven Whitehouse79c4c372011-11-09 13:46:06 +00001394/**
1395 * gfs2_dir_readahead - Issue read-ahead requests for leaf blocks.
Bob Petersondfe4d342011-10-27 12:16:06 -04001396 *
1397 * Note: we can't calculate each index like dir_e_read can because we don't
1398 * have the leaf, and therefore we don't have the depth, and therefore we
1399 * don't have the length. So we have to just read enough ahead to make up
Steven Whitehouse79c4c372011-11-09 13:46:06 +00001400 * for the loss of information.
1401 */
Bob Petersondfe4d342011-10-27 12:16:06 -04001402static void gfs2_dir_readahead(struct inode *inode, unsigned hsize, u32 index,
1403 struct file_ra_state *f_ra)
1404{
1405 struct gfs2_inode *ip = GFS2_I(inode);
1406 struct gfs2_glock *gl = ip->i_gl;
1407 struct buffer_head *bh;
1408 u64 blocknr = 0, last;
1409 unsigned count;
1410
1411 /* First check if we've already read-ahead for the whole range. */
Steven Whitehouse79c4c372011-11-09 13:46:06 +00001412 if (index + MAX_RA_BLOCKS < f_ra->start)
Bob Petersondfe4d342011-10-27 12:16:06 -04001413 return;
1414
1415 f_ra->start = max((pgoff_t)index, f_ra->start);
1416 for (count = 0; count < MAX_RA_BLOCKS; count++) {
1417 if (f_ra->start >= hsize) /* if exceeded the hash table */
1418 break;
1419
1420 last = blocknr;
1421 blocknr = be64_to_cpu(ip->i_hash_cache[f_ra->start]);
1422 f_ra->start++;
1423 if (blocknr == last)
1424 continue;
1425
1426 bh = gfs2_getbuf(gl, blocknr, 1);
1427 if (trylock_buffer(bh)) {
1428 if (buffer_uptodate(bh)) {
1429 unlock_buffer(bh);
1430 brelse(bh);
1431 continue;
1432 }
1433 bh->b_end_io = end_buffer_read_sync;
1434 submit_bh(READA | REQ_META, bh);
1435 continue;
1436 }
1437 brelse(bh);
1438 }
1439}
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001440
David Teiglandb3b94fa2006-01-16 16:50:04 +00001441/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001442 * dir_e_read - Reads the entries from a directory into a filldir buffer
1443 * @dip: dinode pointer
Al Virod81a8ef2013-05-16 14:14:48 -04001444 * @ctx: actor to feed the entries to
David Teiglandb3b94fa2006-01-16 16:50:04 +00001445 *
1446 * Returns: errno
1447 */
1448
Al Virod81a8ef2013-05-16 14:14:48 -04001449static int dir_e_read(struct inode *inode, struct dir_context *ctx,
1450 struct file_ra_state *f_ra)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001451{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001452 struct gfs2_inode *dip = GFS2_I(inode);
Steven Whitehousecd915492006-09-04 12:49:07 -04001453 u32 hsize, len = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -04001454 u32 hash, index;
Al Virob44b84d2006-10-14 10:46:30 -04001455 __be64 *lp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001456 int copied = 0;
1457 int error = 0;
Steven Whitehouse4da3c642006-07-11 13:19:13 -04001458 unsigned depth = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001459
Steven Whitehouse9a004502008-02-01 09:23:44 +00001460 hsize = 1 << dip->i_depth;
Al Virod81a8ef2013-05-16 14:14:48 -04001461 hash = gfs2_dir_offset2hash(ctx->pos);
Steven Whitehouse9a004502008-02-01 09:23:44 +00001462 index = hash >> (32 - dip->i_depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001463
Steven Whitehouse79c4c372011-11-09 13:46:06 +00001464 if (dip->i_hash_cache == NULL)
Bob Petersondfe4d342011-10-27 12:16:06 -04001465 f_ra->start = 0;
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001466 lp = gfs2_dir_get_hash_table(dip);
1467 if (IS_ERR(lp))
1468 return PTR_ERR(lp);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001469
Bob Petersondfe4d342011-10-27 12:16:06 -04001470 gfs2_dir_readahead(inode, hsize, index, f_ra);
1471
David Teiglandb3b94fa2006-01-16 16:50:04 +00001472 while (index < hsize) {
Al Virod81a8ef2013-05-16 14:14:48 -04001473 error = gfs2_dir_read_leaf(inode, ctx,
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001474 &copied, &depth,
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001475 be64_to_cpu(lp[index]));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001476 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001477 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001478
Steven Whitehouse9a004502008-02-01 09:23:44 +00001479 len = 1 << (dip->i_depth - depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001480 index = (index & ~(len - 1)) + len;
1481 }
1482
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001483 if (error > 0)
1484 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001485 return error;
1486}
1487
Al Virod81a8ef2013-05-16 14:14:48 -04001488int gfs2_dir_read(struct inode *inode, struct dir_context *ctx,
1489 struct file_ra_state *f_ra)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001490{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001491 struct gfs2_inode *dip = GFS2_I(inode);
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001492 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001493 struct dirent_gather g;
1494 const struct gfs2_dirent **darr, *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001495 struct buffer_head *dibh;
1496 int copied = 0;
1497 int error;
1498
Steven Whitehousead6203f2008-11-03 13:59:19 +00001499 if (!dip->i_entries)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001500 return 0;
1501
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001502 if (dip->i_diskflags & GFS2_DIF_EXHASH)
Al Virod81a8ef2013-05-16 14:14:48 -04001503 return dir_e_read(inode, ctx, f_ra);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001504
David Teiglandb3b94fa2006-01-16 16:50:04 +00001505 if (!gfs2_is_stuffed(dip)) {
1506 gfs2_consist_inode(dip);
1507 return -EIO;
1508 }
1509
David Teiglandb3b94fa2006-01-16 16:50:04 +00001510 error = gfs2_meta_inode_buffer(dip, &dibh);
1511 if (error)
1512 return error;
1513
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001514 error = -ENOMEM;
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001515 /* 96 is max number of dirents which can be stuffed into an inode */
Josef Bacik16c5f062008-04-09 09:33:41 -04001516 darr = kmalloc(96 * sizeof(struct gfs2_dirent *), GFP_NOFS);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001517 if (darr) {
1518 g.pdent = darr;
1519 g.offset = 0;
1520 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1521 gfs2_dirent_gather, NULL, &g);
1522 if (IS_ERR(dent)) {
1523 error = PTR_ERR(dent);
1524 goto out;
1525 }
Steven Whitehousead6203f2008-11-03 13:59:19 +00001526 if (dip->i_entries != g.offset) {
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001527 fs_warn(sdp, "Number of entries corrupt in dir %llu, "
Steven Whitehousead6203f2008-11-03 13:59:19 +00001528 "ip->i_entries (%u) != g.offset (%u)\n",
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001529 (unsigned long long)dip->i_no_addr,
Steven Whitehousead6203f2008-11-03 13:59:19 +00001530 dip->i_entries,
Steven Whitehousebdd19a22007-04-18 09:38:42 +01001531 g.offset);
1532 error = -EIO;
1533 goto out;
1534 }
Al Virod81a8ef2013-05-16 14:14:48 -04001535 error = do_filldir_main(dip, ctx, darr,
Steven Whitehousead6203f2008-11-03 13:59:19 +00001536 dip->i_entries, &copied);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001537out:
1538 kfree(darr);
1539 }
1540
David Teiglandb3b94fa2006-01-16 16:50:04 +00001541 if (error > 0)
1542 error = 0;
1543
1544 brelse(dibh);
1545
1546 return error;
1547}
1548
David Teiglandb3b94fa2006-01-16 16:50:04 +00001549/**
1550 * gfs2_dir_search - Search a directory
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +01001551 * @dip: The GFS2 dir inode
1552 * @name: The name we are looking up
1553 * @fail_on_exist: Fail if the name exists rather than looking it up
David Teiglandb3b94fa2006-01-16 16:50:04 +00001554 *
1555 * This routine searches a directory for a file or another directory.
1556 * Assumes a glock is held on dip.
1557 *
1558 * Returns: errno
1559 */
1560
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +01001561struct inode *gfs2_dir_search(struct inode *dir, const struct qstr *name,
1562 bool fail_on_exist)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001563{
Steven Whitehousec7526662006-03-20 12:30:04 -05001564 struct buffer_head *bh;
1565 struct gfs2_dirent *dent;
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +01001566 u64 addr, formal_ino;
1567 u16 dtype;
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001568
1569 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1570 if (dent) {
1571 if (IS_ERR(dent))
David Howellse231c2e2008-02-07 00:15:26 -08001572 return ERR_CAST(dent);
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +01001573 dtype = be16_to_cpu(dent->de_type);
1574 addr = be64_to_cpu(dent->de_inum.no_addr);
1575 formal_ino = be64_to_cpu(dent->de_inum.no_formal_ino);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001576 brelse(bh);
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +01001577 if (fail_on_exist)
1578 return ERR_PTR(-EEXIST);
1579 return gfs2_inode_lookup(dir->i_sb, dtype, addr, formal_ino, 0);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001580 }
1581 return ERR_PTR(-ENOENT);
1582}
1583
1584int gfs2_dir_check(struct inode *dir, const struct qstr *name,
1585 const struct gfs2_inode *ip)
1586{
1587 struct buffer_head *bh;
1588 struct gfs2_dirent *dent;
1589 int ret = -ENOENT;
Steven Whitehousec7526662006-03-20 12:30:04 -05001590
1591 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1592 if (dent) {
1593 if (IS_ERR(dent))
1594 return PTR_ERR(dent);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001595 if (ip) {
1596 if (be64_to_cpu(dent->de_inum.no_addr) != ip->i_no_addr)
1597 goto out;
1598 if (be64_to_cpu(dent->de_inum.no_formal_ino) !=
1599 ip->i_no_formal_ino)
1600 goto out;
1601 if (unlikely(IF2DT(ip->i_inode.i_mode) !=
1602 be16_to_cpu(dent->de_type))) {
1603 gfs2_consist_inode(GFS2_I(dir));
1604 ret = -EIO;
1605 goto out;
1606 }
1607 }
1608 ret = 0;
1609out:
Steven Whitehousec7526662006-03-20 12:30:04 -05001610 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001611 }
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001612 return ret;
Steven Whitehousec7526662006-03-20 12:30:04 -05001613}
1614
1615static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1616{
1617 struct buffer_head *bh, *obh;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001618 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001619 struct gfs2_leaf *leaf, *oleaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001620 int error;
Steven Whitehousec7526662006-03-20 12:30:04 -05001621 u32 index;
1622 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001623
Steven Whitehouse9a004502008-02-01 09:23:44 +00001624 index = name->hash >> (32 - ip->i_depth);
Steven Whitehousec7526662006-03-20 12:30:04 -05001625 error = get_first_leaf(ip, index, &obh);
1626 if (error)
1627 return error;
1628 do {
1629 oleaf = (struct gfs2_leaf *)obh->b_data;
1630 bn = be64_to_cpu(oleaf->lf_next);
1631 if (!bn)
1632 break;
1633 brelse(obh);
1634 error = get_leaf(ip, bn, &obh);
1635 if (error)
1636 return error;
1637 } while(1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001638
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001639 gfs2_trans_add_meta(ip->i_gl, obh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001640
1641 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1642 if (!leaf) {
1643 brelse(obh);
1644 return -ENOSPC;
1645 }
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04001646 oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
Steven Whitehousec7526662006-03-20 12:30:04 -05001647 brelse(bh);
1648 brelse(obh);
1649
1650 error = gfs2_meta_inode_buffer(ip, &bh);
1651 if (error)
1652 return error;
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001653 gfs2_trans_add_meta(ip->i_gl, bh);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001654 gfs2_add_inode_blocks(&ip->i_inode, 1);
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001655 gfs2_dinode_out(ip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001656 brelse(bh);
1657 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001658}
1659
1660/**
1661 * gfs2_dir_add - Add new filename into directory
1662 * @dip: The GFS2 inode
1663 * @filename: The new name
1664 * @inode: The inode number of the entry
1665 * @type: The type of the entry
1666 *
1667 * Returns: 0 on success, error code on failure
1668 */
1669
Steven Whitehousec7526662006-03-20 12:30:04 -05001670int gfs2_dir_add(struct inode *inode, const struct qstr *name,
Steven Whitehouse3d6ecb72011-05-09 13:30:08 +01001671 const struct gfs2_inode *nip)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001672{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001673 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001674 struct buffer_head *bh;
1675 struct gfs2_dirent *dent;
1676 struct gfs2_leaf *leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001677 int error;
1678
Steven Whitehousec7526662006-03-20 12:30:04 -05001679 while(1) {
1680 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
1681 &bh);
1682 if (dent) {
1683 if (IS_ERR(dent))
1684 return PTR_ERR(dent);
1685 dent = gfs2_init_dirent(inode, dent, name, bh);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001686 gfs2_inum_out(nip, dent);
Steven Whitehouse3d6ecb72011-05-09 13:30:08 +01001687 dent->de_type = cpu_to_be16(IF2DT(nip->i_inode.i_mode));
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001688 if (ip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001689 leaf = (struct gfs2_leaf *)bh->b_data;
Marcin Slusarzbb16b342008-02-13 00:06:10 +01001690 be16_add_cpu(&leaf->lf_entries, 1);
Steven Whitehousec7526662006-03-20 12:30:04 -05001691 }
1692 brelse(bh);
Steven Whitehousead6203f2008-11-03 13:59:19 +00001693 ip->i_entries++;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001694 ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehouse3d6ecb72011-05-09 13:30:08 +01001695 if (S_ISDIR(nip->i_inode.i_mode))
1696 inc_nlink(&ip->i_inode);
Bob Peterson343cd8f2012-11-12 13:04:54 -05001697 mark_inode_dirty(inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001698 error = 0;
1699 break;
1700 }
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001701 if (!(ip->i_diskflags & GFS2_DIF_EXHASH)) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001702 error = dir_make_exhash(inode);
1703 if (error)
1704 break;
1705 continue;
1706 }
1707 error = dir_split_leaf(inode, name);
1708 if (error == 0)
1709 continue;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001710 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001711 break;
Steven Whitehouse9a004502008-02-01 09:23:44 +00001712 if (ip->i_depth < GFS2_DIR_MAX_DEPTH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001713 error = dir_double_exhash(ip);
1714 if (error)
1715 break;
1716 error = dir_split_leaf(inode, name);
Steven Whitehousee90deff2006-03-29 19:02:15 -05001717 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001718 break;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001719 if (error == 0)
1720 continue;
Steven Whitehousec7526662006-03-20 12:30:04 -05001721 }
1722 error = dir_new_leaf(inode, name);
1723 if (!error)
1724 continue;
1725 error = -ENOSPC;
1726 break;
1727 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001728 return error;
1729}
1730
Steven Whitehousec7526662006-03-20 12:30:04 -05001731
David Teiglandb3b94fa2006-01-16 16:50:04 +00001732/**
1733 * gfs2_dir_del - Delete a directory entry
1734 * @dip: The GFS2 inode
1735 * @filename: The filename
1736 *
1737 * Returns: 0 on success, error code on failure
1738 */
1739
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001740int gfs2_dir_del(struct gfs2_inode *dip, const struct dentry *dentry)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001741{
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001742 const struct qstr *name = &dentry->d_name;
Steven Whitehousec7526662006-03-20 12:30:04 -05001743 struct gfs2_dirent *dent, *prev = NULL;
1744 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001745
Steven Whitehousec7526662006-03-20 12:30:04 -05001746 /* Returns _either_ the entry (if its first in block) or the
1747 previous entry otherwise */
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001748 dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001749 if (!dent) {
1750 gfs2_consist_inode(dip);
1751 return -EIO;
1752 }
1753 if (IS_ERR(dent)) {
1754 gfs2_consist_inode(dip);
1755 return PTR_ERR(dent);
1756 }
1757 /* If not first in block, adjust pointers accordingly */
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001758 if (gfs2_dirent_find(dent, name, NULL) == 0) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001759 prev = dent;
1760 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1761 }
1762
1763 dirent_del(dip, bh, prev, dent);
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001764 if (dip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001765 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1766 u16 entries = be16_to_cpu(leaf->lf_entries);
1767 if (!entries)
1768 gfs2_consist_inode(dip);
1769 leaf->lf_entries = cpu_to_be16(--entries);
Steven Whitehousec7526662006-03-20 12:30:04 -05001770 }
Steven Whitehouseed386502006-04-07 16:28:07 -04001771 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001772
Steven Whitehousead6203f2008-11-03 13:59:19 +00001773 if (!dip->i_entries)
Steven Whitehousec7526662006-03-20 12:30:04 -05001774 gfs2_consist_inode(dip);
Steven Whitehousead6203f2008-11-03 13:59:19 +00001775 dip->i_entries--;
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001776 dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001777 if (S_ISDIR(dentry->d_inode->i_mode))
1778 drop_nlink(&dip->i_inode);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001779 mark_inode_dirty(&dip->i_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001780
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001781 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001782}
1783
David Teiglandb3b94fa2006-01-16 16:50:04 +00001784/**
1785 * gfs2_dir_mvino - Change inode number of directory entry
1786 * @dip: The GFS2 inode
1787 * @filename:
1788 * @new_inode:
1789 *
1790 * This routine changes the inode number of a directory entry. It's used
1791 * by rename to change ".." when a directory is moved.
1792 * Assumes a glock is held on dvp.
1793 *
1794 * Returns: errno
1795 */
1796
Steven Whitehousec7526662006-03-20 12:30:04 -05001797int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001798 const struct gfs2_inode *nip, unsigned int new_type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001799{
Steven Whitehousec7526662006-03-20 12:30:04 -05001800 struct buffer_head *bh;
1801 struct gfs2_dirent *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001802 int error;
1803
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001804 dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001805 if (!dent) {
1806 gfs2_consist_inode(dip);
1807 return -EIO;
1808 }
1809 if (IS_ERR(dent))
1810 return PTR_ERR(dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001811
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001812 gfs2_trans_add_meta(dip->i_gl, bh);
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001813 gfs2_inum_out(nip, dent);
Steven Whitehousec7526662006-03-20 12:30:04 -05001814 dent->de_type = cpu_to_be16(new_type);
1815
Steven Whitehouse383f01f2008-11-04 10:05:22 +00001816 if (dip->i_diskflags & GFS2_DIF_EXHASH) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001817 brelse(bh);
1818 error = gfs2_meta_inode_buffer(dip, &bh);
1819 if (error)
1820 return error;
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001821 gfs2_trans_add_meta(dip->i_gl, bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001822 }
1823
Steven Whitehouse4bd91ba2007-06-05 09:39:18 +01001824 dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME;
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001825 gfs2_dinode_out(dip, bh->b_data);
Steven Whitehousec7526662006-03-20 12:30:04 -05001826 brelse(bh);
1827 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001828}
1829
1830/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001831 * leaf_dealloc - Deallocate a directory leaf
1832 * @dip: the directory
1833 * @index: the hash table offset in the directory
1834 * @len: the number of pointers to this leaf
1835 * @leaf_no: the leaf number
Bob Petersonec038c82011-03-22 13:55:23 -04001836 * @leaf_bh: buffer_head for the starting leaf
Bob Petersond24a7a42011-03-22 13:54:03 -04001837 * last_dealloc: 1 if this is the final dealloc for the leaf, else 0
David Teiglandb3b94fa2006-01-16 16:50:04 +00001838 *
1839 * Returns: errno
1840 */
1841
Steven Whitehousecd915492006-09-04 12:49:07 -04001842static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
Bob Petersonec038c82011-03-22 13:55:23 -04001843 u64 leaf_no, struct buffer_head *leaf_bh,
1844 int last_dealloc)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001845{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001846 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehousec7526662006-03-20 12:30:04 -05001847 struct gfs2_leaf *tmp_leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001848 struct gfs2_rgrp_list rlist;
1849 struct buffer_head *bh, *dibh;
Steven Whitehousecd915492006-09-04 12:49:07 -04001850 u64 blk, nblk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001851 unsigned int rg_blocks = 0, l_blocks = 0;
1852 char *ht;
Steven Whitehousecd915492006-09-04 12:49:07 -04001853 unsigned int x, size = len * sizeof(u64);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001854 int error;
1855
Bob Peterson5e2f7d62012-04-04 22:11:16 -04001856 error = gfs2_rindex_update(sdp);
1857 if (error)
1858 return error;
1859
David Teiglandb3b94fa2006-01-16 16:50:04 +00001860 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1861
Josef Bacik16c5f062008-04-09 09:33:41 -04001862 ht = kzalloc(size, GFP_NOFS);
Bob Petersone8830d82013-05-30 09:48:56 -04001863 if (ht == NULL)
1864 ht = vzalloc(size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001865 if (!ht)
1866 return -ENOMEM;
1867
Eric W. Biedermanf4108a62013-01-31 17:49:26 -08001868 error = gfs2_quota_hold(dip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001869 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -04001870 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001871
David Teiglandb3b94fa2006-01-16 16:50:04 +00001872 /* Count the number of leaves */
Bob Petersonec038c82011-03-22 13:55:23 -04001873 bh = leaf_bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001874
Steven Whitehousec7526662006-03-20 12:30:04 -05001875 for (blk = leaf_no; blk; blk = nblk) {
Bob Petersonec038c82011-03-22 13:55:23 -04001876 if (blk != leaf_no) {
1877 error = get_leaf(dip, blk, &bh);
1878 if (error)
1879 goto out_rlist;
1880 }
Steven Whitehousec7526662006-03-20 12:30:04 -05001881 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1882 nblk = be64_to_cpu(tmp_leaf->lf_next);
Bob Petersonec038c82011-03-22 13:55:23 -04001883 if (blk != leaf_no)
1884 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001885
Steven Whitehouse70b0c362011-09-02 16:08:09 +01001886 gfs2_rlist_add(dip, &rlist, blk);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001887 l_blocks++;
1888 }
1889
Bob Petersonfe6c9912008-01-28 11:13:02 -06001890 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001891
1892 for (x = 0; x < rlist.rl_rgrps; x++) {
1893 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001894 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +01001895 rg_blocks += rgd->rd_length;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001896 }
1897
1898 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1899 if (error)
1900 goto out_rlist;
1901
1902 error = gfs2_trans_begin(sdp,
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001903 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
David Teiglandb3b94fa2006-01-16 16:50:04 +00001904 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1905 if (error)
1906 goto out_rg_gunlock;
1907
Bob Petersonec038c82011-03-22 13:55:23 -04001908 bh = leaf_bh;
1909
Steven Whitehousec7526662006-03-20 12:30:04 -05001910 for (blk = leaf_no; blk; blk = nblk) {
Bob Petersonec038c82011-03-22 13:55:23 -04001911 if (blk != leaf_no) {
1912 error = get_leaf(dip, blk, &bh);
1913 if (error)
1914 goto out_end_trans;
1915 }
Steven Whitehousec7526662006-03-20 12:30:04 -05001916 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1917 nblk = be64_to_cpu(tmp_leaf->lf_next);
Bob Petersonec038c82011-03-22 13:55:23 -04001918 if (blk != leaf_no)
1919 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001920
1921 gfs2_free_meta(dip, blk, 1);
Steven Whitehouse77658aa2008-02-12 14:17:27 +00001922 gfs2_add_inode_blocks(&dip->i_inode, -1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001923 }
1924
Steven Whitehousecd915492006-09-04 12:49:07 -04001925 error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001926 if (error != size) {
1927 if (error >= 0)
1928 error = -EIO;
1929 goto out_end_trans;
1930 }
1931
1932 error = gfs2_meta_inode_buffer(dip, &dibh);
1933 if (error)
1934 goto out_end_trans;
1935
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001936 gfs2_trans_add_meta(dip->i_gl, dibh);
Bob Petersond24a7a42011-03-22 13:54:03 -04001937 /* On the last dealloc, make this a regular file in case we crash.
1938 (We don't want to free these blocks a second time.) */
1939 if (last_dealloc)
1940 dip->i_inode.i_mode = S_IFREG;
Steven Whitehouse539e5d62006-10-31 15:07:05 -05001941 gfs2_dinode_out(dip, dibh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001942 brelse(dibh);
1943
Steven Whitehousea91ea692006-09-04 12:04:26 -04001944out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001945 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001946out_rg_gunlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001947 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001948out_rlist:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001949 gfs2_rlist_free(&rlist);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001950 gfs2_quota_unhold(dip);
Cyrill Gorcunov182fe5a2008-03-03 21:54:21 +03001951out:
Bob Petersone8830d82013-05-30 09:48:56 -04001952 if (is_vmalloc_addr(ht))
1953 vfree(ht);
1954 else
1955 kfree(ht);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001956 return error;
1957}
1958
1959/**
1960 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1961 * @dip: the directory
1962 *
1963 * Dealloc all on-disk directory leaves to FREEMETA state
1964 * Change on-disk inode type to "regular file"
1965 *
1966 * Returns: errno
1967 */
1968
1969int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
1970{
Bob Peterson556bb172011-03-22 13:56:37 -04001971 struct buffer_head *bh;
1972 struct gfs2_leaf *leaf;
1973 u32 hsize, len;
Bob Peterson556bb172011-03-22 13:56:37 -04001974 u32 index = 0, next_index;
1975 __be64 *lp;
1976 u64 leaf_no;
1977 int error = 0, last;
1978
1979 hsize = 1 << dip->i_depth;
Bob Peterson556bb172011-03-22 13:56:37 -04001980
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001981 lp = gfs2_dir_get_hash_table(dip);
1982 if (IS_ERR(lp))
1983 return PTR_ERR(lp);
Bob Peterson556bb172011-03-22 13:56:37 -04001984
1985 while (index < hsize) {
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001986 leaf_no = be64_to_cpu(lp[index]);
Bob Peterson556bb172011-03-22 13:56:37 -04001987 if (leaf_no) {
1988 error = get_leaf(dip, leaf_no, &bh);
1989 if (error)
1990 goto out;
1991 leaf = (struct gfs2_leaf *)bh->b_data;
1992 len = 1 << (dip->i_depth - be16_to_cpu(leaf->lf_depth));
1993
1994 next_index = (index & ~(len - 1)) + len;
1995 last = ((next_index >= hsize) ? 1 : 0);
1996 error = leaf_dealloc(dip, index, len, leaf_no, bh,
1997 last);
1998 brelse(bh);
1999 if (error)
2000 goto out;
2001 index = next_index;
2002 } else
2003 index++;
2004 }
2005
2006 if (index != hsize) {
2007 gfs2_consist_inode(dip);
2008 error = -EIO;
2009 }
2010
2011out:
Bob Peterson556bb172011-03-22 13:56:37 -04002012
2013 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002014}
2015
2016/**
2017 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
2018 * @ip: the file being written to
2019 * @filname: the filename that's going to be added
David Teiglandb3b94fa2006-01-16 16:50:04 +00002020 *
Steven Whitehousec7526662006-03-20 12:30:04 -05002021 * Returns: 1 if alloc required, 0 if not, -ve on error
David Teiglandb3b94fa2006-01-16 16:50:04 +00002022 */
2023
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04002024int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002025{
Steven Whitehousec7526662006-03-20 12:30:04 -05002026 struct gfs2_dirent *dent;
2027 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002028
Steven Whitehousec7526662006-03-20 12:30:04 -05002029 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
Steven Whitehouseed386502006-04-07 16:28:07 -04002030 if (!dent) {
Steven Whitehousec7526662006-03-20 12:30:04 -05002031 return 1;
Steven Whitehouseed386502006-04-07 16:28:07 -04002032 }
Steven Whitehousec7526662006-03-20 12:30:04 -05002033 if (IS_ERR(dent))
2034 return PTR_ERR(dent);
2035 brelse(bh);
2036 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002037}
2038