blob: ffc1beff6703e65fa40fa512222fec585cedf649 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
4 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License v.2.
8 */
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 *
39 * dip->i_di.di_flags & GFS2_DIF_EXHASH is true
40 *
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
56#include <linux/sched.h>
57#include <linux/slab.h>
58#include <linux/spinlock.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000059#include <linux/buffer_head.h>
60#include <linux/sort.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050061#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050062#include <linux/crc32.h>
Steven Whitehousefe1bded2006-04-18 10:09:15 -040063#include <linux/vmalloc.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000064
65#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050066#include "lm_interface.h"
67#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000068#include "dir.h"
69#include "glock.h"
70#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000071#include "meta_io.h"
72#include "quota.h"
73#include "rgrp.h"
74#include "trans.h"
Steven Whitehousee13940b2006-01-30 13:31:50 +000075#include "bmap.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050076#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000077
78#define IS_LEAF 1 /* Hashed (leaf) directory */
79#define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
80
81#if 1
82#define gfs2_disk_hash2offset(h) (((uint64_t)(h)) >> 1)
83#define gfs2_dir_offset2hash(p) ((uint32_t)(((uint64_t)(p)) << 1))
84#else
85#define gfs2_disk_hash2offset(h) (((uint64_t)(h)))
86#define gfs2_dir_offset2hash(p) ((uint32_t)(((uint64_t)(p))))
87#endif
88
89typedef int (*leaf_call_t) (struct gfs2_inode *dip,
90 uint32_t index, uint32_t len, uint64_t leaf_no,
91 void *data);
92
Steven Whitehouse61e085a2006-04-24 10:07:13 -040093
94int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, uint64_t block,
95 struct buffer_head **bhp)
Steven Whitehousee13940b2006-01-30 13:31:50 +000096{
97 struct buffer_head *bh;
Steven Whitehousee13940b2006-01-30 13:31:50 +000098
Steven Whitehouse61e085a2006-04-24 10:07:13 -040099 bh = gfs2_meta_new(ip->i_gl, block);
100 gfs2_trans_add_bh(ip->i_gl, bh, 1);
101 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
102 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
Steven Whitehousee13940b2006-01-30 13:31:50 +0000103 *bhp = bh;
104 return 0;
105}
106
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400107static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, uint64_t block,
108 struct buffer_head **bhp)
109{
110 struct buffer_head *bh;
111 int error;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000112
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400113 error = gfs2_meta_read(ip->i_gl, block, DIO_START | DIO_WAIT, &bh);
114 if (error)
115 return error;
116 if (gfs2_metatype_check(ip->i_sbd, bh, GFS2_METATYPE_JD)) {
117 brelse(bh);
118 return -EIO;
119 }
120 *bhp = bh;
121 return 0;
122}
Steven Whitehousee13940b2006-01-30 13:31:50 +0000123
124static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
125 unsigned int offset, unsigned int size)
126
127{
128 struct buffer_head *dibh;
129 int error;
130
131 error = gfs2_meta_inode_buffer(ip, &dibh);
132 if (error)
133 return error;
134
135 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehousec7526662006-03-20 12:30:04 -0500136 memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000137 if (ip->i_di.di_size < offset + size)
138 ip->i_di.di_size = offset + size;
139 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
140 gfs2_dinode_out(&ip->i_di, dibh->b_data);
141
142 brelse(dibh);
143
144 return size;
145}
146
147
148
149/**
150 * gfs2_dir_write_data - Write directory information to the inode
151 * @ip: The GFS2 inode
152 * @buf: The buffer containing information to be written
153 * @offset: The file offset to start writing at
154 * @size: The amount of data to write
155 *
156 * Returns: The number of bytes correctly written or error code
157 */
158static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
159 uint64_t offset, unsigned int size)
160{
161 struct gfs2_sbd *sdp = ip->i_sbd;
162 struct buffer_head *dibh;
163 uint64_t lblock, dblock;
164 uint32_t extlen = 0;
165 unsigned int o;
166 int copied = 0;
167 int error = 0;
168
169 if (!size)
170 return 0;
171
172 if (gfs2_is_stuffed(ip) &&
173 offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500174 return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
175 size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000176
177 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
178 return -EINVAL;
179
180 if (gfs2_is_stuffed(ip)) {
181 error = gfs2_unstuff_dinode(ip, NULL, NULL);
182 if (error)
Steven Whitehousec7526662006-03-20 12:30:04 -0500183 return error;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000184 }
185
186 lblock = offset;
187 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
188
189 while (copied < size) {
190 unsigned int amount;
191 struct buffer_head *bh;
192 int new;
193
194 amount = size - copied;
195 if (amount > sdp->sd_sb.sb_bsize - o)
196 amount = sdp->sd_sb.sb_bsize - o;
197
198 if (!extlen) {
199 new = 1;
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500200 error = gfs2_block_map(ip, lblock, &new, &dblock,
201 &extlen);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000202 if (error)
203 goto fail;
204 error = -EIO;
205 if (gfs2_assert_withdraw(sdp, dblock))
206 goto fail;
207 }
208
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400209 if (amount == sdp->sd_jbsize || new)
210 error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
211 else
212 error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
213
Steven Whitehousee13940b2006-01-30 13:31:50 +0000214 if (error)
215 goto fail;
216
217 gfs2_trans_add_bh(ip->i_gl, bh, 1);
218 memcpy(bh->b_data + o, buf, amount);
219 brelse(bh);
220 if (error)
221 goto fail;
222
223 copied += amount;
224 lblock++;
225 dblock++;
226 extlen--;
227
228 o = sizeof(struct gfs2_meta_header);
229 }
230
231out:
232 error = gfs2_meta_inode_buffer(ip, &dibh);
233 if (error)
234 return error;
235
236 if (ip->i_di.di_size < offset + copied)
237 ip->i_di.di_size = offset + copied;
238 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
239
240 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
241 gfs2_dinode_out(&ip->i_di, dibh->b_data);
242 brelse(dibh);
243
244 return copied;
245fail:
246 if (copied)
247 goto out;
248 return error;
249}
250
251static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf,
Steven Whitehousec7526662006-03-20 12:30:04 -0500252 unsigned int offset, unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000253{
254 struct buffer_head *dibh;
255 int error;
256
257 error = gfs2_meta_inode_buffer(ip, &dibh);
258 if (!error) {
259 offset += sizeof(struct gfs2_dinode);
260 memcpy(buf, dibh->b_data + offset, size);
261 brelse(dibh);
262 }
263
264 return (error) ? error : size;
265}
266
267
268/**
269 * gfs2_dir_read_data - Read a data from a directory inode
270 * @ip: The GFS2 Inode
271 * @buf: The buffer to place result into
272 * @offset: File offset to begin jdata_readng from
273 * @size: Amount of data to transfer
274 *
275 * Returns: The amount of data actually copied or the error
276 */
277static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf,
278 uint64_t offset, unsigned int size)
279{
280 struct gfs2_sbd *sdp = ip->i_sbd;
281 uint64_t lblock, dblock;
282 uint32_t extlen = 0;
283 unsigned int o;
284 int copied = 0;
285 int error = 0;
286
287 if (offset >= ip->i_di.di_size)
288 return 0;
289
290 if ((offset + size) > ip->i_di.di_size)
291 size = ip->i_di.di_size - offset;
292
293 if (!size)
294 return 0;
295
296 if (gfs2_is_stuffed(ip))
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500297 return gfs2_dir_read_stuffed(ip, buf, (unsigned int)offset,
298 size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000299
300 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
301 return -EINVAL;
302
303 lblock = offset;
304 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
305
306 while (copied < size) {
307 unsigned int amount;
308 struct buffer_head *bh;
309 int new;
310
311 amount = size - copied;
312 if (amount > sdp->sd_sb.sb_bsize - o)
313 amount = sdp->sd_sb.sb_bsize - o;
314
315 if (!extlen) {
316 new = 0;
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500317 error = gfs2_block_map(ip, lblock, &new, &dblock,
318 &extlen);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000319 if (error)
320 goto fail;
321 }
322
323 if (extlen > 1)
324 gfs2_meta_ra(ip->i_gl, dblock, extlen);
325
326 if (dblock) {
Steven Whitehouse61e085a2006-04-24 10:07:13 -0400327 if (new)
328 error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
329 else
330 error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000331 if (error)
332 goto fail;
333 dblock++;
334 extlen--;
335 } else
336 bh = NULL;
337
338 memcpy(buf, bh->b_data + o, amount);
339 brelse(bh);
340 if (error)
341 goto fail;
342
343 copied += amount;
344 lblock++;
345
346 o = sizeof(struct gfs2_meta_header);
347 }
348
349 return copied;
350fail:
351 return (copied) ? copied : error;
352}
353
Steven Whitehousec7526662006-03-20 12:30:04 -0500354typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500355 const struct qstr *name,
356 void *opaque);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000357
Steven Whitehousec7526662006-03-20 12:30:04 -0500358static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
359 const struct qstr *name, int ret)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000360{
Steven Whitehousec7526662006-03-20 12:30:04 -0500361 if (dent->de_inum.no_addr != 0 &&
362 be32_to_cpu(dent->de_hash) == name->hash &&
363 be16_to_cpu(dent->de_name_len) == name->len &&
364 memcmp((char *)(dent+1), name->name, name->len) == 0)
365 return ret;
366 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000367}
368
Steven Whitehousec7526662006-03-20 12:30:04 -0500369static int gfs2_dirent_find(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500370 const struct qstr *name,
371 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500372{
373 return __gfs2_dirent_find(dent, name, 1);
374}
375
376static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500377 const struct qstr *name,
378 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500379{
380 return __gfs2_dirent_find(dent, name, 2);
381}
382
383/*
384 * name->name holds ptr to start of block.
385 * name->len holds size of block.
386 */
387static int gfs2_dirent_last(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500388 const struct qstr *name,
389 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500390{
391 const char *start = name->name;
392 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
393 if (name->len == (end - start))
394 return 1;
395 return 0;
396}
397
398static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500399 const struct qstr *name,
400 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500401{
402 unsigned required = GFS2_DIRENT_SIZE(name->len);
403 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
404 unsigned totlen = be16_to_cpu(dent->de_rec_len);
405
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500406 if (!dent->de_inum.no_addr)
407 actual = GFS2_DIRENT_SIZE(0);
Steven Whitehousec7526662006-03-20 12:30:04 -0500408 if ((totlen - actual) >= required)
409 return 1;
410 return 0;
411}
412
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500413struct dirent_gather {
414 const struct gfs2_dirent **pdent;
415 unsigned offset;
416};
417
418static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
419 const struct qstr *name,
420 void *opaque)
421{
422 struct dirent_gather *g = opaque;
423 if (dent->de_inum.no_addr) {
424 g->pdent[g->offset++] = dent;
425 }
426 return 0;
427}
428
Steven Whitehousec7526662006-03-20 12:30:04 -0500429/*
430 * Other possible things to check:
431 * - Inode located within filesystem size (and on valid block)
432 * - Valid directory entry type
433 * Not sure how heavy-weight we want to make this... could also check
434 * hash is correct for example, but that would take a lot of extra time.
435 * For now the most important thing is to check that the various sizes
436 * are correct.
437 */
438static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
439 unsigned int size, unsigned int len, int first)
440{
441 const char *msg = "gfs2_dirent too small";
442 if (unlikely(size < sizeof(struct gfs2_dirent)))
443 goto error;
444 msg = "gfs2_dirent misaligned";
445 if (unlikely(offset & 0x7))
446 goto error;
447 msg = "gfs2_dirent points beyond end of block";
448 if (unlikely(offset + size > len))
449 goto error;
450 msg = "zero inode number";
451 if (unlikely(!first && !dent->de_inum.no_addr))
452 goto error;
453 msg = "name length is greater than space in dirent";
454 if (dent->de_inum.no_addr &&
455 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
456 size))
457 goto error;
458 return 0;
459error:
460 printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
461 first ? "first in block" : "not first in block");
462 return -EIO;
463}
464
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500465static int gfs2_dirent_offset(const void *buf)
Steven Whitehousec7526662006-03-20 12:30:04 -0500466{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500467 const struct gfs2_meta_header *h = buf;
468 int offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500469
470 BUG_ON(buf == NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500471
Steven Whitehousee3167de2006-03-30 15:46:23 -0500472 switch(be32_to_cpu(h->mh_type)) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500473 case GFS2_METATYPE_LF:
474 offset = sizeof(struct gfs2_leaf);
475 break;
476 case GFS2_METATYPE_DI:
477 offset = sizeof(struct gfs2_dinode);
478 break;
479 default:
480 goto wrong_type;
481 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500482 return offset;
483wrong_type:
484 printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
Steven Whitehousee3167de2006-03-30 15:46:23 -0500485 be32_to_cpu(h->mh_type));
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500486 return -1;
487}
Steven Whitehousec7526662006-03-20 12:30:04 -0500488
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500489static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode,
490 void *buf,
491 unsigned int len, gfs2_dscan_t scan,
492 const struct qstr *name,
493 void *opaque)
494{
495 struct gfs2_dirent *dent, *prev;
496 unsigned offset;
497 unsigned size;
498 int ret = 0;
499
500 ret = gfs2_dirent_offset(buf);
501 if (ret < 0)
502 goto consist_inode;
503
504 offset = ret;
Steven Whitehousec7526662006-03-20 12:30:04 -0500505 prev = NULL;
506 dent = (struct gfs2_dirent *)(buf + offset);
507 size = be16_to_cpu(dent->de_rec_len);
508 if (gfs2_check_dirent(dent, offset, size, len, 1))
509 goto consist_inode;
510 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500511 ret = scan(dent, name, opaque);
Steven Whitehousec7526662006-03-20 12:30:04 -0500512 if (ret)
513 break;
514 offset += size;
515 if (offset == len)
516 break;
517 prev = dent;
518 dent = (struct gfs2_dirent *)(buf + offset);
519 size = be16_to_cpu(dent->de_rec_len);
520 if (gfs2_check_dirent(dent, offset, size, len, 0))
521 goto consist_inode;
522 } while(1);
523
524 switch(ret) {
525 case 0:
526 return NULL;
527 case 1:
528 return dent;
529 case 2:
530 return prev ? prev : dent;
531 default:
532 BUG_ON(ret > 0);
533 return ERR_PTR(ret);
534 }
535
Steven Whitehousec7526662006-03-20 12:30:04 -0500536consist_inode:
537 gfs2_consist_inode(inode->u.generic_ip);
538 return ERR_PTR(-EIO);
539}
540
541
David Teiglandb3b94fa2006-01-16 16:50:04 +0000542/**
543 * dirent_first - Return the first dirent
544 * @dip: the directory
545 * @bh: The buffer
546 * @dent: Pointer to list of dirents
547 *
548 * return first dirent whether bh points to leaf or stuffed dinode
549 *
550 * Returns: IS_LEAF, IS_DINODE, or -errno
551 */
552
553static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
554 struct gfs2_dirent **dent)
555{
556 struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data;
557
Steven Whitehousee3167de2006-03-30 15:46:23 -0500558 if (be32_to_cpu(h->mh_type) == GFS2_METATYPE_LF) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000559 if (gfs2_meta_check(dip->i_sbd, bh))
560 return -EIO;
561 *dent = (struct gfs2_dirent *)(bh->b_data +
562 sizeof(struct gfs2_leaf));
563 return IS_LEAF;
564 } else {
565 if (gfs2_metatype_check(dip->i_sbd, bh, GFS2_METATYPE_DI))
566 return -EIO;
567 *dent = (struct gfs2_dirent *)(bh->b_data +
568 sizeof(struct gfs2_dinode));
569 return IS_DINODE;
570 }
571}
572
573/**
574 * dirent_next - Next dirent
575 * @dip: the directory
576 * @bh: The buffer
577 * @dent: Pointer to list of dirents
578 *
579 * Returns: 0 on success, error code otherwise
580 */
581
582static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
583 struct gfs2_dirent **dent)
584{
585 struct gfs2_dirent *tmp, *cur;
586 char *bh_end;
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000587 uint16_t cur_rec_len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000588
589 cur = *dent;
590 bh_end = bh->b_data + bh->b_size;
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000591 cur_rec_len = be16_to_cpu(cur->de_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000592
593 if ((char *)cur + cur_rec_len >= bh_end) {
594 if ((char *)cur + cur_rec_len > bh_end) {
595 gfs2_consist_inode(dip);
596 return -EIO;
597 }
598 return -ENOENT;
599 }
600
601 tmp = (struct gfs2_dirent *)((char *)cur + cur_rec_len);
602
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000603 if ((char *)tmp + be16_to_cpu(tmp->de_rec_len) > bh_end) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000604 gfs2_consist_inode(dip);
605 return -EIO;
606 }
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000607
608 if (cur_rec_len == 0) {
609 gfs2_consist_inode(dip);
610 return -EIO;
611 }
612
David Teiglandb3b94fa2006-01-16 16:50:04 +0000613 /* Only the first dent could ever have de_inum.no_addr == 0 */
614 if (!tmp->de_inum.no_addr) {
615 gfs2_consist_inode(dip);
616 return -EIO;
617 }
618
619 *dent = tmp;
620
621 return 0;
622}
623
624/**
625 * dirent_del - Delete a dirent
626 * @dip: The GFS2 inode
627 * @bh: The buffer
628 * @prev: The previous dirent
629 * @cur: The current dirent
630 *
631 */
632
633static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
634 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
635{
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000636 uint16_t cur_rec_len, prev_rec_len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000637
638 if (!cur->de_inum.no_addr) {
639 gfs2_consist_inode(dip);
640 return;
641 }
642
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000643 gfs2_trans_add_bh(dip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000644
645 /* If there is no prev entry, this is the first entry in the block.
646 The de_rec_len is already as big as it needs to be. Just zero
647 out the inode number and return. */
648
649 if (!prev) {
650 cur->de_inum.no_addr = 0; /* No endianess worries */
651 return;
652 }
653
654 /* Combine this dentry with the previous one. */
655
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000656 prev_rec_len = be16_to_cpu(prev->de_rec_len);
657 cur_rec_len = be16_to_cpu(cur->de_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000658
659 if ((char *)prev + prev_rec_len != (char *)cur)
660 gfs2_consist_inode(dip);
661 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
662 gfs2_consist_inode(dip);
663
664 prev_rec_len += cur_rec_len;
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000665 prev->de_rec_len = cpu_to_be16(prev_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000666}
667
Steven Whitehousec7526662006-03-20 12:30:04 -0500668/*
669 * Takes a dent from which to grab space as an argument. Returns the
670 * newly created dent.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000671 */
Steven Whitehousec7526662006-03-20 12:30:04 -0500672struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
673 struct gfs2_dirent *dent,
674 const struct qstr *name,
675 struct buffer_head *bh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000676{
Steven Whitehousec7526662006-03-20 12:30:04 -0500677 struct gfs2_inode *ip = inode->u.generic_ip;
678 struct gfs2_dirent *ndent;
679 unsigned offset = 0, totlen;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000680
Steven Whitehousec7526662006-03-20 12:30:04 -0500681 if (dent->de_inum.no_addr)
682 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
683 totlen = be16_to_cpu(dent->de_rec_len);
684 BUG_ON(offset + name->len > totlen);
685 gfs2_trans_add_bh(ip->i_gl, bh, 1);
686 ndent = (struct gfs2_dirent *)((char *)dent + offset);
687 dent->de_rec_len = cpu_to_be16(offset);
688 gfs2_qstr2dirent(name, totlen - offset, ndent);
689 return ndent;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000690}
691
Steven Whitehousec7526662006-03-20 12:30:04 -0500692static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
693 struct buffer_head *bh,
694 const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000695{
696 struct gfs2_dirent *dent;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500697 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
698 gfs2_dirent_find_space, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500699 if (!dent || IS_ERR(dent))
700 return dent;
701 return gfs2_init_dirent(inode, dent, name, bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000702}
703
704static int get_leaf(struct gfs2_inode *dip, uint64_t leaf_no,
705 struct buffer_head **bhp)
706{
707 int error;
708
709 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_START | DIO_WAIT, bhp);
710 if (!error && gfs2_metatype_check(dip->i_sbd, *bhp, GFS2_METATYPE_LF))
711 error = -EIO;
712
713 return error;
714}
715
716/**
717 * get_leaf_nr - Get a leaf number associated with the index
718 * @dip: The GFS2 inode
719 * @index:
720 * @leaf_out:
721 *
722 * Returns: 0 on success, error code otherwise
723 */
724
725static int get_leaf_nr(struct gfs2_inode *dip, uint32_t index,
726 uint64_t *leaf_out)
727{
728 uint64_t leaf_no;
729 int error;
730
Steven Whitehousee13940b2006-01-30 13:31:50 +0000731 error = gfs2_dir_read_data(dip, (char *)&leaf_no,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000732 index * sizeof(uint64_t),
733 sizeof(uint64_t));
734 if (error != sizeof(uint64_t))
735 return (error < 0) ? error : -EIO;
736
737 *leaf_out = be64_to_cpu(leaf_no);
738
739 return 0;
740}
741
742static int get_first_leaf(struct gfs2_inode *dip, uint32_t index,
743 struct buffer_head **bh_out)
744{
745 uint64_t leaf_no;
746 int error;
747
748 error = get_leaf_nr(dip, index, &leaf_no);
749 if (!error)
750 error = get_leaf(dip, leaf_no, bh_out);
751
752 return error;
753}
754
Steven Whitehousec7526662006-03-20 12:30:04 -0500755static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
756 const struct qstr *name,
757 gfs2_dscan_t scan,
758 struct buffer_head **pbh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000759{
Steven Whitehousec7526662006-03-20 12:30:04 -0500760 struct buffer_head *bh;
761 struct gfs2_dirent *dent;
762 struct gfs2_inode *ip = inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000763 int error;
764
Steven Whitehousec7526662006-03-20 12:30:04 -0500765 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
766 struct gfs2_leaf *leaf;
767 unsigned hsize = 1 << ip->i_di.di_depth;
768 unsigned index;
769 u64 ln;
770 if (hsize * sizeof(u64) != ip->i_di.di_size) {
771 gfs2_consist_inode(ip);
772 return ERR_PTR(-EIO);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000773 }
774
Steven Whitehousec7526662006-03-20 12:30:04 -0500775 index = name->hash >> (32 - ip->i_di.di_depth);
776 error = get_first_leaf(ip, index, &bh);
777 if (error)
778 return ERR_PTR(error);
779 do {
780 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500781 scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500782 if (dent)
783 goto got_dent;
784 leaf = (struct gfs2_leaf *)bh->b_data;
785 ln = be64_to_cpu(leaf->lf_next);
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400786 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -0500787 if (!ln)
788 break;
789 error = get_leaf(ip, ln, &bh);
790 } while(!error);
791
792 return error ? ERR_PTR(error) : NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000793 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000794
Steven Whitehousec7526662006-03-20 12:30:04 -0500795 error = gfs2_meta_inode_buffer(ip, &bh);
796 if (error)
797 return ERR_PTR(error);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500798 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500799got_dent:
Steven Whitehousef4154ea2006-04-11 14:49:06 -0400800 if (unlikely(dent == NULL || IS_ERR(dent))) {
Steven Whitehouseed386502006-04-07 16:28:07 -0400801 brelse(bh);
802 bh = NULL;
803 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500804 *pbh = bh;
805 return dent;
806}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000807
Steven Whitehousec7526662006-03-20 12:30:04 -0500808static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
809{
810 struct gfs2_inode *ip = inode->u.generic_ip;
811 u64 bn = gfs2_alloc_meta(ip);
812 struct buffer_head *bh = gfs2_meta_new(ip->i_gl, bn);
813 struct gfs2_leaf *leaf;
814 struct gfs2_dirent *dent;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500815 struct qstr name = { .name = "", .len = 0, .hash = 0 };
Steven Whitehousec7526662006-03-20 12:30:04 -0500816 if (!bh)
817 return NULL;
818 gfs2_trans_add_bh(ip->i_gl, bh, 1);
819 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
820 leaf = (struct gfs2_leaf *)bh->b_data;
821 leaf->lf_depth = cpu_to_be16(depth);
822 leaf->lf_entries = cpu_to_be16(0);
823 leaf->lf_dirent_format = cpu_to_be16(GFS2_FORMAT_DE);
824 leaf->lf_next = cpu_to_be64(0);
825 memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
826 dent = (struct gfs2_dirent *)(leaf+1);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500827 gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
Steven Whitehousec7526662006-03-20 12:30:04 -0500828 *pbh = bh;
829 return leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000830}
831
832/**
833 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
834 * @dip: The GFS2 inode
835 *
836 * Returns: 0 on success, error code otherwise
837 */
838
Steven Whitehousec7526662006-03-20 12:30:04 -0500839static int dir_make_exhash(struct inode *inode)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000840{
Steven Whitehousec7526662006-03-20 12:30:04 -0500841 struct gfs2_inode *dip = inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000842 struct gfs2_sbd *sdp = dip->i_sbd;
843 struct gfs2_dirent *dent;
Steven Whitehousec7526662006-03-20 12:30:04 -0500844 struct qstr args;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000845 struct buffer_head *bh, *dibh;
846 struct gfs2_leaf *leaf;
847 int y;
848 uint32_t x;
849 uint64_t *lp, bn;
850 int error;
851
852 error = gfs2_meta_inode_buffer(dip, &dibh);
853 if (error)
854 return error;
855
David Teiglandb3b94fa2006-01-16 16:50:04 +0000856 /* Turn over a new leaf */
857
Steven Whitehousec7526662006-03-20 12:30:04 -0500858 leaf = new_leaf(inode, &bh, 0);
859 if (!leaf)
860 return -ENOSPC;
861 bn = bh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000862
863 gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000864 leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries);
865
866 /* Copy dirents */
867
868 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
869 sizeof(struct gfs2_dinode));
870
871 /* Find last entry */
872
873 x = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500874 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
875 sizeof(struct gfs2_leaf);
876 args.name = bh->b_data;
877 dent = gfs2_dirent_scan(dip->i_vnode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500878 gfs2_dirent_last, &args, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500879 if (!dent) {
880 brelse(bh);
881 brelse(dibh);
882 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000883 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500884 if (IS_ERR(dent)) {
885 brelse(bh);
886 brelse(dibh);
887 return PTR_ERR(dent);
888 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000889
890 /* Adjust the last dirent's record length
891 (Remember that dent still points to the last entry.) */
892
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000893 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000894 sizeof(struct gfs2_dinode) -
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000895 sizeof(struct gfs2_leaf));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000896
897 brelse(bh);
898
899 /* We're done with the new leaf block, now setup the new
900 hash table. */
901
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000902 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000903 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
904
905 lp = (uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode));
906
907 for (x = sdp->sd_hash_ptrs; x--; lp++)
908 *lp = cpu_to_be64(bn);
909
910 dip->i_di.di_size = sdp->sd_sb.sb_bsize / 2;
911 dip->i_di.di_blocks++;
912 dip->i_di.di_flags |= GFS2_DIF_EXHASH;
913 dip->i_di.di_payload_format = 0;
914
915 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
916 dip->i_di.di_depth = y;
917
918 gfs2_dinode_out(&dip->i_di, dibh->b_data);
919
920 brelse(dibh);
921
922 return 0;
923}
924
925/**
926 * dir_split_leaf - Split a leaf block into two
927 * @dip: The GFS2 inode
928 * @index:
929 * @leaf_no:
930 *
931 * Returns: 0 on success, error code on failure
932 */
933
Steven Whitehousec7526662006-03-20 12:30:04 -0500934static int dir_split_leaf(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000935{
Steven Whitehousec7526662006-03-20 12:30:04 -0500936 struct gfs2_inode *dip = inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000937 struct buffer_head *nbh, *obh, *dibh;
938 struct gfs2_leaf *nleaf, *oleaf;
939 struct gfs2_dirent *dent, *prev = NULL, *next = NULL, *new;
940 uint32_t start, len, half_len, divider;
Steven Whitehousec7526662006-03-20 12:30:04 -0500941 uint64_t bn, *lp, leaf_no;
942 uint32_t index;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000943 int x, moved = 0;
944 int error;
945
Steven Whitehousec7526662006-03-20 12:30:04 -0500946 index = name->hash >> (32 - dip->i_di.di_depth);
947 error = get_leaf_nr(dip, index, &leaf_no);
948 if (error)
949 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000950
951 /* Get the old leaf block */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000952 error = get_leaf(dip, leaf_no, &obh);
953 if (error)
Steven Whitehousee90deff2006-03-29 19:02:15 -0500954 return error;
955
956 oleaf = (struct gfs2_leaf *)obh->b_data;
957 if (dip->i_di.di_depth == be16_to_cpu(oleaf->lf_depth)) {
958 brelse(obh);
959 return 1; /* can't split */
960 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000961
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000962 gfs2_trans_add_bh(dip->i_gl, obh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000963
Steven Whitehousec7526662006-03-20 12:30:04 -0500964 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
965 if (!nleaf) {
966 brelse(obh);
967 return -ENOSPC;
968 }
969 bn = nbh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000970
Steven Whitehousec7526662006-03-20 12:30:04 -0500971 /* Compute the start and len of leaf pointers in the hash table. */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000972 len = 1 << (dip->i_di.di_depth - be16_to_cpu(oleaf->lf_depth));
973 half_len = len >> 1;
974 if (!half_len) {
Steven Whitehousee90deff2006-03-29 19:02:15 -0500975 printk(KERN_WARNING "di_depth %u lf_depth %u index %u\n", dip->i_di.di_depth, be16_to_cpu(oleaf->lf_depth), index);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000976 gfs2_consist_inode(dip);
977 error = -EIO;
978 goto fail_brelse;
979 }
980
981 start = (index & ~(len - 1));
982
983 /* Change the pointers.
984 Don't bother distinguishing stuffed from non-stuffed.
985 This code is complicated enough already. */
Steven Whitehousec7526662006-03-20 12:30:04 -0500986 lp = kmalloc(half_len * sizeof(uint64_t), GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000987 /* Change the pointers */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000988 for (x = 0; x < half_len; x++)
989 lp[x] = cpu_to_be64(bn);
990
Steven Whitehousee13940b2006-01-30 13:31:50 +0000991 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(uint64_t),
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500992 half_len * sizeof(uint64_t));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000993 if (error != half_len * sizeof(uint64_t)) {
994 if (error >= 0)
995 error = -EIO;
996 goto fail_lpfree;
997 }
998
999 kfree(lp);
1000
1001 /* Compute the divider */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001002 divider = (start + half_len) << (32 - dip->i_di.di_depth);
1003
1004 /* Copy the entries */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001005 dirent_first(dip, obh, &dent);
1006
1007 do {
1008 next = dent;
1009 if (dirent_next(dip, obh, &next))
1010 next = NULL;
1011
1012 if (dent->de_inum.no_addr &&
1013 be32_to_cpu(dent->de_hash) < divider) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001014 struct qstr str;
1015 str.name = (char*)(dent+1);
1016 str.len = be16_to_cpu(dent->de_name_len);
1017 str.hash = be32_to_cpu(dent->de_hash);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001018 new = gfs2_dirent_alloc(inode, nbh, &str);
Steven Whitehousec7526662006-03-20 12:30:04 -05001019 if (IS_ERR(new)) {
1020 error = PTR_ERR(new);
1021 break;
1022 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001023
1024 new->de_inum = dent->de_inum; /* No endian worries */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001025 new->de_type = dent->de_type; /* No endian worries */
Steven Whitehousec7526662006-03-20 12:30:04 -05001026 nleaf->lf_entries = cpu_to_be16(be16_to_cpu(nleaf->lf_entries)+1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001027
1028 dirent_del(dip, obh, prev, dent);
1029
1030 if (!oleaf->lf_entries)
1031 gfs2_consist_inode(dip);
Steven Whitehousec7526662006-03-20 12:30:04 -05001032 oleaf->lf_entries = cpu_to_be16(be16_to_cpu(oleaf->lf_entries)-1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001033
1034 if (!prev)
1035 prev = dent;
1036
1037 moved = 1;
Steven Whitehousec7526662006-03-20 12:30:04 -05001038 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001039 prev = dent;
Steven Whitehousec7526662006-03-20 12:30:04 -05001040 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001041 dent = next;
Steven Whitehousec7526662006-03-20 12:30:04 -05001042 } while (dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001043
Steven Whitehousec7526662006-03-20 12:30:04 -05001044 oleaf->lf_depth = nleaf->lf_depth;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001045
1046 error = gfs2_meta_inode_buffer(dip, &dibh);
1047 if (!gfs2_assert_withdraw(dip->i_sbd, !error)) {
1048 dip->i_di.di_blocks++;
1049 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1050 brelse(dibh);
1051 }
1052
1053 brelse(obh);
1054 brelse(nbh);
1055
1056 return error;
1057
Steven Whitehousee90deff2006-03-29 19:02:15 -05001058fail_lpfree:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001059 kfree(lp);
1060
Steven Whitehousee90deff2006-03-29 19:02:15 -05001061fail_brelse:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001062 brelse(obh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001063 brelse(nbh);
1064 return error;
1065}
1066
1067/**
1068 * dir_double_exhash - Double size of ExHash table
1069 * @dip: The GFS2 dinode
1070 *
1071 * Returns: 0 on success, error code on failure
1072 */
1073
1074static int dir_double_exhash(struct gfs2_inode *dip)
1075{
1076 struct gfs2_sbd *sdp = dip->i_sbd;
1077 struct buffer_head *dibh;
1078 uint32_t hsize;
1079 uint64_t *buf;
1080 uint64_t *from, *to;
1081 uint64_t block;
1082 int x;
1083 int error = 0;
1084
1085 hsize = 1 << dip->i_di.di_depth;
1086 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1087 gfs2_consist_inode(dip);
1088 return -EIO;
1089 }
1090
1091 /* Allocate both the "from" and "to" buffers in one big chunk */
1092
1093 buf = kcalloc(3, sdp->sd_hash_bsize, GFP_KERNEL | __GFP_NOFAIL);
1094
1095 for (block = dip->i_di.di_size >> sdp->sd_hash_bsize_shift; block--;) {
Steven Whitehousee13940b2006-01-30 13:31:50 +00001096 error = gfs2_dir_read_data(dip, (char *)buf,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001097 block * sdp->sd_hash_bsize,
1098 sdp->sd_hash_bsize);
1099 if (error != sdp->sd_hash_bsize) {
1100 if (error >= 0)
1101 error = -EIO;
1102 goto fail;
1103 }
1104
1105 from = buf;
1106 to = (uint64_t *)((char *)buf + sdp->sd_hash_bsize);
1107
1108 for (x = sdp->sd_hash_ptrs; x--; from++) {
1109 *to++ = *from; /* No endianess worries */
1110 *to++ = *from;
1111 }
1112
Steven Whitehousee13940b2006-01-30 13:31:50 +00001113 error = gfs2_dir_write_data(dip,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001114 (char *)buf + sdp->sd_hash_bsize,
1115 block * sdp->sd_sb.sb_bsize,
1116 sdp->sd_sb.sb_bsize);
1117 if (error != sdp->sd_sb.sb_bsize) {
1118 if (error >= 0)
1119 error = -EIO;
1120 goto fail;
1121 }
1122 }
1123
1124 kfree(buf);
1125
1126 error = gfs2_meta_inode_buffer(dip, &dibh);
1127 if (!gfs2_assert_withdraw(sdp, !error)) {
1128 dip->i_di.di_depth++;
1129 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1130 brelse(dibh);
1131 }
1132
1133 return error;
1134
1135 fail:
1136 kfree(buf);
1137
1138 return error;
1139}
1140
1141/**
1142 * compare_dents - compare directory entries by hash value
1143 * @a: first dent
1144 * @b: second dent
1145 *
1146 * When comparing the hash entries of @a to @b:
1147 * gt: returns 1
1148 * lt: returns -1
1149 * eq: returns 0
1150 */
1151
1152static int compare_dents(const void *a, const void *b)
1153{
1154 struct gfs2_dirent *dent_a, *dent_b;
1155 uint32_t hash_a, hash_b;
1156 int ret = 0;
1157
1158 dent_a = *(struct gfs2_dirent **)a;
Steven Whitehousec7526662006-03-20 12:30:04 -05001159 hash_a = be32_to_cpu(dent_a->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001160
1161 dent_b = *(struct gfs2_dirent **)b;
Steven Whitehousec7526662006-03-20 12:30:04 -05001162 hash_b = be32_to_cpu(dent_b->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001163
1164 if (hash_a > hash_b)
1165 ret = 1;
1166 else if (hash_a < hash_b)
1167 ret = -1;
1168 else {
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001169 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1170 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001171
1172 if (len_a > len_b)
1173 ret = 1;
1174 else if (len_a < len_b)
1175 ret = -1;
1176 else
1177 ret = memcmp((char *)(dent_a + 1),
1178 (char *)(dent_b + 1),
1179 len_a);
1180 }
1181
1182 return ret;
1183}
1184
1185/**
1186 * do_filldir_main - read out directory entries
1187 * @dip: The GFS2 inode
1188 * @offset: The offset in the file to read from
1189 * @opaque: opaque data to pass to filldir
1190 * @filldir: The function to pass entries to
1191 * @darr: an array of struct gfs2_dirent pointers to read
1192 * @entries: the number of entries in darr
1193 * @copied: pointer to int that's non-zero if a entry has been copied out
1194 *
1195 * Jump through some hoops to make sure that if there are hash collsions,
1196 * they are read out at the beginning of a buffer. We want to minimize
1197 * the possibility that they will fall into different readdir buffers or
1198 * that someone will want to seek to that location.
1199 *
1200 * Returns: errno, >0 on exception from filldir
1201 */
1202
1203static int do_filldir_main(struct gfs2_inode *dip, uint64_t *offset,
1204 void *opaque, gfs2_filldir_t filldir,
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001205 const struct gfs2_dirent **darr, uint32_t entries,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001206 int *copied)
1207{
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001208 const struct gfs2_dirent *dent, *dent_next;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001209 struct gfs2_inum inum;
1210 uint64_t off, off_next;
1211 unsigned int x, y;
1212 int run = 0;
1213 int error = 0;
1214
1215 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1216
1217 dent_next = darr[0];
1218 off_next = be32_to_cpu(dent_next->de_hash);
1219 off_next = gfs2_disk_hash2offset(off_next);
1220
1221 for (x = 0, y = 1; x < entries; x++, y++) {
1222 dent = dent_next;
1223 off = off_next;
1224
1225 if (y < entries) {
1226 dent_next = darr[y];
1227 off_next = be32_to_cpu(dent_next->de_hash);
1228 off_next = gfs2_disk_hash2offset(off_next);
1229
1230 if (off < *offset)
1231 continue;
1232 *offset = off;
1233
1234 if (off_next == off) {
1235 if (*copied && !run)
1236 return 1;
1237 run = 1;
1238 } else
1239 run = 0;
1240 } else {
1241 if (off < *offset)
1242 continue;
1243 *offset = off;
1244 }
1245
1246 gfs2_inum_in(&inum, (char *)&dent->de_inum);
1247
1248 error = filldir(opaque, (char *)(dent + 1),
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001249 be16_to_cpu(dent->de_name_len),
David Teiglandb3b94fa2006-01-16 16:50:04 +00001250 off, &inum,
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001251 be16_to_cpu(dent->de_type));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001252 if (error)
1253 return 1;
1254
1255 *copied = 1;
1256 }
1257
1258 /* Increment the *offset by one, so the next time we come into the
1259 do_filldir fxn, we get the next entry instead of the last one in the
1260 current leaf */
1261
1262 (*offset)++;
1263
1264 return 0;
1265}
1266
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001267static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
1268 gfs2_filldir_t filldir, int *copied,
1269 unsigned *depth, u64 leaf_no)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001270{
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001271 struct gfs2_inode *ip = inode->u.generic_ip;
1272 struct buffer_head *bh;
1273 struct gfs2_leaf *lf;
1274 unsigned entries = 0;
1275 unsigned leaves = 0;
1276 const struct gfs2_dirent **darr, *dent;
1277 struct dirent_gather g;
1278 struct buffer_head **larr;
1279 int leaf = 0;
1280 int error, i;
1281 u64 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001282
David Teiglandb3b94fa2006-01-16 16:50:04 +00001283 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001284 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001285 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001286 goto out;
1287 lf = (struct gfs2_leaf *)bh->b_data;
1288 if (leaves == 0)
1289 *depth = be16_to_cpu(lf->lf_depth);
1290 entries += be16_to_cpu(lf->lf_entries);
1291 leaves++;
1292 lfn = be64_to_cpu(lf->lf_next);
1293 brelse(bh);
1294 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001295
1296 if (!entries)
1297 return 0;
1298
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001299 error = -ENOMEM;
Steven Whitehousefe1bded2006-04-18 10:09:15 -04001300 larr = vmalloc((leaves + entries) * sizeof(void*));
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001301 if (!larr)
1302 goto out;
1303 darr = (const struct gfs2_dirent **)(larr + leaves);
1304 g.pdent = darr;
1305 g.offset = 0;
1306 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001307
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001308 do {
1309 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001310 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001311 goto out_kfree;
1312 lf = (struct gfs2_leaf *)bh->b_data;
1313 lfn = be64_to_cpu(lf->lf_next);
1314 if (lf->lf_entries) {
1315 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1316 gfs2_dirent_gather, NULL, &g);
1317 error = PTR_ERR(dent);
1318 if (IS_ERR(dent)) {
1319 goto out_kfree;
1320 }
1321 error = 0;
1322 larr[leaf++] = bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001323 } else {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001324 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001325 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001326 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001327
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001328 error = do_filldir_main(ip, offset, opaque, filldir, darr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001329 entries, copied);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001330out_kfree:
1331 for(i = 0; i < leaf; i++)
1332 brelse(larr[i]);
Steven Whitehousefe1bded2006-04-18 10:09:15 -04001333 vfree(larr);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001334out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001335 return error;
1336}
1337
1338/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001339 * dir_e_read - Reads the entries from a directory into a filldir buffer
1340 * @dip: dinode pointer
1341 * @offset: the hash of the last entry read shifted to the right once
1342 * @opaque: buffer for the filldir function to fill
1343 * @filldir: points to the filldir function to use
1344 *
1345 * Returns: errno
1346 */
1347
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001348static int dir_e_read(struct inode *inode, uint64_t *offset, void *opaque,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001349 gfs2_filldir_t filldir)
1350{
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001351 struct gfs2_inode *dip = inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001352 struct gfs2_sbd *sdp = dip->i_sbd;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001353 uint32_t hsize, len = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001354 uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
1355 uint32_t hash, index;
1356 uint64_t *lp;
1357 int copied = 0;
1358 int error = 0;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001359 unsigned depth;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001360
1361 hsize = 1 << dip->i_di.di_depth;
1362 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1363 gfs2_consist_inode(dip);
1364 return -EIO;
1365 }
1366
1367 hash = gfs2_dir_offset2hash(*offset);
1368 index = hash >> (32 - dip->i_di.di_depth);
1369
1370 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1371 if (!lp)
1372 return -ENOMEM;
1373
1374 while (index < hsize) {
1375 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1376 ht_offset = index - lp_offset;
1377
1378 if (ht_offset_cur != ht_offset) {
Steven Whitehousee13940b2006-01-30 13:31:50 +00001379 error = gfs2_dir_read_data(dip, (char *)lp,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001380 ht_offset * sizeof(uint64_t),
1381 sdp->sd_hash_bsize);
1382 if (error != sdp->sd_hash_bsize) {
1383 if (error >= 0)
1384 error = -EIO;
1385 goto out;
1386 }
1387 ht_offset_cur = ht_offset;
1388 }
1389
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001390 error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
1391 &copied, &depth,
1392 be64_to_cpu(lp[lp_offset]));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001393 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001394 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001395
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001396 len = 1 << (dip->i_di.di_depth - depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001397 index = (index & ~(len - 1)) + len;
1398 }
1399
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001400out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001401 kfree(lp);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001402 if (error > 0)
1403 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001404 return error;
1405}
1406
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001407int gfs2_dir_read(struct inode *inode, uint64_t *offset, void *opaque,
1408 gfs2_filldir_t filldir)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001409{
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001410 struct gfs2_inode *dip = inode->u.generic_ip;
1411 struct dirent_gather g;
1412 const struct gfs2_dirent **darr, *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001413 struct buffer_head *dibh;
1414 int copied = 0;
1415 int error;
1416
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001417 if (!dip->i_di.di_entries)
1418 return 0;
1419
1420 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
1421 return dir_e_read(inode, offset, opaque, filldir);
1422
David Teiglandb3b94fa2006-01-16 16:50:04 +00001423 if (!gfs2_is_stuffed(dip)) {
1424 gfs2_consist_inode(dip);
1425 return -EIO;
1426 }
1427
David Teiglandb3b94fa2006-01-16 16:50:04 +00001428 error = gfs2_meta_inode_buffer(dip, &dibh);
1429 if (error)
1430 return error;
1431
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001432 error = -ENOMEM;
1433 darr = kmalloc(dip->i_di.di_entries * sizeof(struct gfs2_dirent *),
1434 GFP_KERNEL);
1435 if (darr) {
1436 g.pdent = darr;
1437 g.offset = 0;
1438 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1439 gfs2_dirent_gather, NULL, &g);
1440 if (IS_ERR(dent)) {
1441 error = PTR_ERR(dent);
1442 goto out;
1443 }
1444 error = do_filldir_main(dip, offset, opaque, filldir, darr,
1445 dip->i_di.di_entries, &copied);
1446out:
1447 kfree(darr);
1448 }
1449
David Teiglandb3b94fa2006-01-16 16:50:04 +00001450 if (error > 0)
1451 error = 0;
1452
1453 brelse(dibh);
1454
1455 return error;
1456}
1457
David Teiglandb3b94fa2006-01-16 16:50:04 +00001458/**
1459 * gfs2_dir_search - Search a directory
1460 * @dip: The GFS2 inode
1461 * @filename:
1462 * @inode:
1463 *
1464 * This routine searches a directory for a file or another directory.
1465 * Assumes a glock is held on dip.
1466 *
1467 * Returns: errno
1468 */
1469
Steven Whitehousec7526662006-03-20 12:30:04 -05001470int gfs2_dir_search(struct inode *dir, const struct qstr *name,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001471 struct gfs2_inum *inum, unsigned int *type)
1472{
Steven Whitehousec7526662006-03-20 12:30:04 -05001473 struct buffer_head *bh;
1474 struct gfs2_dirent *dent;
1475
1476 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1477 if (dent) {
1478 if (IS_ERR(dent))
1479 return PTR_ERR(dent);
1480 if (inum)
1481 gfs2_inum_in(inum, (char *)&dent->de_inum);
1482 if (type)
1483 *type = be16_to_cpu(dent->de_type);
1484 brelse(bh);
1485 return 0;
1486 }
1487 return -ENOENT;
1488}
1489
1490static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1491{
1492 struct buffer_head *bh, *obh;
1493 struct gfs2_inode *ip = inode->u.generic_ip;
1494 struct gfs2_leaf *leaf, *oleaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001495 int error;
Steven Whitehousec7526662006-03-20 12:30:04 -05001496 u32 index;
1497 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001498
Steven Whitehousec7526662006-03-20 12:30:04 -05001499 index = name->hash >> (32 - ip->i_di.di_depth);
1500 error = get_first_leaf(ip, index, &obh);
1501 if (error)
1502 return error;
1503 do {
1504 oleaf = (struct gfs2_leaf *)obh->b_data;
1505 bn = be64_to_cpu(oleaf->lf_next);
1506 if (!bn)
1507 break;
1508 brelse(obh);
1509 error = get_leaf(ip, bn, &obh);
1510 if (error)
1511 return error;
1512 } while(1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001513
Steven Whitehousec7526662006-03-20 12:30:04 -05001514 gfs2_trans_add_bh(ip->i_gl, obh, 1);
1515
1516 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1517 if (!leaf) {
1518 brelse(obh);
1519 return -ENOSPC;
1520 }
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04001521 oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
Steven Whitehousec7526662006-03-20 12:30:04 -05001522 brelse(bh);
1523 brelse(obh);
1524
1525 error = gfs2_meta_inode_buffer(ip, &bh);
1526 if (error)
1527 return error;
1528 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1529 ip->i_di.di_blocks++;
1530 gfs2_dinode_out(&ip->i_di, bh->b_data);
1531 brelse(bh);
1532 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001533}
1534
1535/**
1536 * gfs2_dir_add - Add new filename into directory
1537 * @dip: The GFS2 inode
1538 * @filename: The new name
1539 * @inode: The inode number of the entry
1540 * @type: The type of the entry
1541 *
1542 * Returns: 0 on success, error code on failure
1543 */
1544
Steven Whitehousec7526662006-03-20 12:30:04 -05001545int gfs2_dir_add(struct inode *inode, const struct qstr *name,
1546 const struct gfs2_inum *inum, unsigned type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001547{
Steven Whitehousec7526662006-03-20 12:30:04 -05001548 struct gfs2_inode *ip = inode->u.generic_ip;
1549 struct buffer_head *bh;
1550 struct gfs2_dirent *dent;
1551 struct gfs2_leaf *leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001552 int error;
1553
Steven Whitehousec7526662006-03-20 12:30:04 -05001554 while(1) {
1555 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
1556 &bh);
1557 if (dent) {
1558 if (IS_ERR(dent))
1559 return PTR_ERR(dent);
1560 dent = gfs2_init_dirent(inode, dent, name, bh);
1561 gfs2_inum_out(inum, (char *)&dent->de_inum);
1562 dent->de_type = cpu_to_be16(type);
1563 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
1564 leaf = (struct gfs2_leaf *)bh->b_data;
1565 leaf->lf_entries = cpu_to_be16(be16_to_cpu(leaf->lf_entries) + 1);
1566 }
1567 brelse(bh);
1568 error = gfs2_meta_inode_buffer(ip, &bh);
1569 if (error)
1570 break;
1571 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1572 ip->i_di.di_entries++;
1573 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
1574 gfs2_dinode_out(&ip->i_di, bh->b_data);
1575 brelse(bh);
1576 error = 0;
1577 break;
1578 }
1579 if (!(ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
1580 error = dir_make_exhash(inode);
1581 if (error)
1582 break;
1583 continue;
1584 }
1585 error = dir_split_leaf(inode, name);
1586 if (error == 0)
1587 continue;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001588 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001589 break;
1590 if (ip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) {
1591 error = dir_double_exhash(ip);
1592 if (error)
1593 break;
1594 error = dir_split_leaf(inode, name);
Steven Whitehousee90deff2006-03-29 19:02:15 -05001595 if (error < 0)
Steven Whitehousec7526662006-03-20 12:30:04 -05001596 break;
Steven Whitehousee90deff2006-03-29 19:02:15 -05001597 if (error == 0)
1598 continue;
Steven Whitehousec7526662006-03-20 12:30:04 -05001599 }
1600 error = dir_new_leaf(inode, name);
1601 if (!error)
1602 continue;
1603 error = -ENOSPC;
1604 break;
1605 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001606 return error;
1607}
1608
Steven Whitehousec7526662006-03-20 12:30:04 -05001609
David Teiglandb3b94fa2006-01-16 16:50:04 +00001610/**
1611 * gfs2_dir_del - Delete a directory entry
1612 * @dip: The GFS2 inode
1613 * @filename: The filename
1614 *
1615 * Returns: 0 on success, error code on failure
1616 */
1617
Steven Whitehousec7526662006-03-20 12:30:04 -05001618int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001619{
Steven Whitehousec7526662006-03-20 12:30:04 -05001620 struct gfs2_dirent *dent, *prev = NULL;
1621 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001622 int error;
1623
Steven Whitehousec7526662006-03-20 12:30:04 -05001624 /* Returns _either_ the entry (if its first in block) or the
1625 previous entry otherwise */
1626 dent = gfs2_dirent_search(dip->i_vnode, name, gfs2_dirent_prev, &bh);
1627 if (!dent) {
1628 gfs2_consist_inode(dip);
1629 return -EIO;
1630 }
1631 if (IS_ERR(dent)) {
1632 gfs2_consist_inode(dip);
1633 return PTR_ERR(dent);
1634 }
1635 /* If not first in block, adjust pointers accordingly */
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001636 if (gfs2_dirent_find(dent, name, NULL) == 0) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001637 prev = dent;
1638 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1639 }
1640
1641 dirent_del(dip, bh, prev, dent);
1642 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1643 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1644 u16 entries = be16_to_cpu(leaf->lf_entries);
1645 if (!entries)
1646 gfs2_consist_inode(dip);
1647 leaf->lf_entries = cpu_to_be16(--entries);
Steven Whitehousec7526662006-03-20 12:30:04 -05001648 }
Steven Whitehouseed386502006-04-07 16:28:07 -04001649 brelse(bh);
Steven Whitehousec7526662006-03-20 12:30:04 -05001650
1651 error = gfs2_meta_inode_buffer(dip, &bh);
1652 if (error)
1653 return error;
1654
1655 if (!dip->i_di.di_entries)
1656 gfs2_consist_inode(dip);
1657 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1658 dip->i_di.di_entries--;
1659 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1660 gfs2_dinode_out(&dip->i_di, bh->b_data);
1661 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001662
1663 return error;
1664}
1665
David Teiglandb3b94fa2006-01-16 16:50:04 +00001666/**
1667 * gfs2_dir_mvino - Change inode number of directory entry
1668 * @dip: The GFS2 inode
1669 * @filename:
1670 * @new_inode:
1671 *
1672 * This routine changes the inode number of a directory entry. It's used
1673 * by rename to change ".." when a directory is moved.
1674 * Assumes a glock is held on dvp.
1675 *
1676 * Returns: errno
1677 */
1678
Steven Whitehousec7526662006-03-20 12:30:04 -05001679int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001680 struct gfs2_inum *inum, unsigned int new_type)
1681{
Steven Whitehousec7526662006-03-20 12:30:04 -05001682 struct buffer_head *bh;
1683 struct gfs2_dirent *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001684 int error;
1685
Steven Whitehousec7526662006-03-20 12:30:04 -05001686 dent = gfs2_dirent_search(dip->i_vnode, filename, gfs2_dirent_find, &bh);
1687 if (!dent) {
1688 gfs2_consist_inode(dip);
1689 return -EIO;
1690 }
1691 if (IS_ERR(dent))
1692 return PTR_ERR(dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001693
Steven Whitehousec7526662006-03-20 12:30:04 -05001694 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1695 gfs2_inum_out(inum, (char *)&dent->de_inum);
1696 dent->de_type = cpu_to_be16(new_type);
1697
1698 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1699 brelse(bh);
1700 error = gfs2_meta_inode_buffer(dip, &bh);
1701 if (error)
1702 return error;
1703 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1704 }
1705
1706 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1707 gfs2_dinode_out(&dip->i_di, bh->b_data);
1708 brelse(bh);
1709 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001710}
1711
1712/**
1713 * foreach_leaf - call a function for each leaf in a directory
1714 * @dip: the directory
1715 * @lc: the function to call for each each
1716 * @data: private data to pass to it
1717 *
1718 * Returns: errno
1719 */
1720
1721static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
1722{
1723 struct gfs2_sbd *sdp = dip->i_sbd;
1724 struct buffer_head *bh;
Steven Whitehousec7526662006-03-20 12:30:04 -05001725 struct gfs2_leaf *leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001726 uint32_t hsize, len;
1727 uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
1728 uint32_t index = 0;
1729 uint64_t *lp;
1730 uint64_t leaf_no;
1731 int error = 0;
1732
1733 hsize = 1 << dip->i_di.di_depth;
1734 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1735 gfs2_consist_inode(dip);
1736 return -EIO;
1737 }
1738
1739 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1740 if (!lp)
1741 return -ENOMEM;
1742
1743 while (index < hsize) {
1744 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1745 ht_offset = index - lp_offset;
1746
1747 if (ht_offset_cur != ht_offset) {
Steven Whitehousee13940b2006-01-30 13:31:50 +00001748 error = gfs2_dir_read_data(dip, (char *)lp,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001749 ht_offset * sizeof(uint64_t),
1750 sdp->sd_hash_bsize);
1751 if (error != sdp->sd_hash_bsize) {
1752 if (error >= 0)
1753 error = -EIO;
1754 goto out;
1755 }
1756 ht_offset_cur = ht_offset;
1757 }
1758
1759 leaf_no = be64_to_cpu(lp[lp_offset]);
1760 if (leaf_no) {
1761 error = get_leaf(dip, leaf_no, &bh);
1762 if (error)
1763 goto out;
Steven Whitehousec7526662006-03-20 12:30:04 -05001764 leaf = (struct gfs2_leaf *)bh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001765 brelse(bh);
1766
Steven Whitehousec7526662006-03-20 12:30:04 -05001767 len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001768
1769 error = lc(dip, index, len, leaf_no, data);
1770 if (error)
1771 goto out;
1772
1773 index = (index & ~(len - 1)) + len;
1774 } else
1775 index++;
1776 }
1777
1778 if (index != hsize) {
1779 gfs2_consist_inode(dip);
1780 error = -EIO;
1781 }
1782
1783 out:
1784 kfree(lp);
1785
1786 return error;
1787}
1788
1789/**
1790 * leaf_dealloc - Deallocate a directory leaf
1791 * @dip: the directory
1792 * @index: the hash table offset in the directory
1793 * @len: the number of pointers to this leaf
1794 * @leaf_no: the leaf number
1795 * @data: not used
1796 *
1797 * Returns: errno
1798 */
1799
1800static int leaf_dealloc(struct gfs2_inode *dip, uint32_t index, uint32_t len,
1801 uint64_t leaf_no, void *data)
1802{
1803 struct gfs2_sbd *sdp = dip->i_sbd;
Steven Whitehousec7526662006-03-20 12:30:04 -05001804 struct gfs2_leaf *tmp_leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001805 struct gfs2_rgrp_list rlist;
1806 struct buffer_head *bh, *dibh;
Steven Whitehousec7526662006-03-20 12:30:04 -05001807 uint64_t blk, nblk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001808 unsigned int rg_blocks = 0, l_blocks = 0;
1809 char *ht;
1810 unsigned int x, size = len * sizeof(uint64_t);
1811 int error;
1812
1813 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1814
1815 ht = kzalloc(size, GFP_KERNEL);
1816 if (!ht)
1817 return -ENOMEM;
1818
1819 gfs2_alloc_get(dip);
1820
1821 error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1822 if (error)
1823 goto out;
1824
1825 error = gfs2_rindex_hold(sdp, &dip->i_alloc.al_ri_gh);
1826 if (error)
1827 goto out_qs;
1828
1829 /* Count the number of leaves */
1830
Steven Whitehousec7526662006-03-20 12:30:04 -05001831 for (blk = leaf_no; blk; blk = nblk) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001832 error = get_leaf(dip, blk, &bh);
1833 if (error)
1834 goto out_rlist;
Steven Whitehousec7526662006-03-20 12:30:04 -05001835 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1836 nblk = be64_to_cpu(tmp_leaf->lf_next);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001837 brelse(bh);
1838
1839 gfs2_rlist_add(sdp, &rlist, blk);
1840 l_blocks++;
1841 }
1842
1843 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1844
1845 for (x = 0; x < rlist.rl_rgrps; x++) {
1846 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001847 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001848 rg_blocks += rgd->rd_ri.ri_length;
1849 }
1850
1851 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1852 if (error)
1853 goto out_rlist;
1854
1855 error = gfs2_trans_begin(sdp,
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001856 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
David Teiglandb3b94fa2006-01-16 16:50:04 +00001857 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1858 if (error)
1859 goto out_rg_gunlock;
1860
Steven Whitehousec7526662006-03-20 12:30:04 -05001861 for (blk = leaf_no; blk; blk = nblk) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001862 error = get_leaf(dip, blk, &bh);
1863 if (error)
1864 goto out_end_trans;
Steven Whitehousec7526662006-03-20 12:30:04 -05001865 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1866 nblk = be64_to_cpu(tmp_leaf->lf_next);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001867 brelse(bh);
1868
1869 gfs2_free_meta(dip, blk, 1);
1870
1871 if (!dip->i_di.di_blocks)
1872 gfs2_consist_inode(dip);
1873 dip->i_di.di_blocks--;
1874 }
1875
Steven Whitehousee13940b2006-01-30 13:31:50 +00001876 error = gfs2_dir_write_data(dip, ht, index * sizeof(uint64_t), size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001877 if (error != size) {
1878 if (error >= 0)
1879 error = -EIO;
1880 goto out_end_trans;
1881 }
1882
1883 error = gfs2_meta_inode_buffer(dip, &dibh);
1884 if (error)
1885 goto out_end_trans;
1886
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001887 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001888 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1889 brelse(dibh);
1890
1891 out_end_trans:
1892 gfs2_trans_end(sdp);
1893
1894 out_rg_gunlock:
1895 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1896
1897 out_rlist:
1898 gfs2_rlist_free(&rlist);
1899 gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh);
1900
1901 out_qs:
1902 gfs2_quota_unhold(dip);
1903
1904 out:
1905 gfs2_alloc_put(dip);
1906 kfree(ht);
1907
1908 return error;
1909}
1910
1911/**
1912 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1913 * @dip: the directory
1914 *
1915 * Dealloc all on-disk directory leaves to FREEMETA state
1916 * Change on-disk inode type to "regular file"
1917 *
1918 * Returns: errno
1919 */
1920
1921int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
1922{
1923 struct gfs2_sbd *sdp = dip->i_sbd;
1924 struct buffer_head *bh;
1925 int error;
1926
1927 /* Dealloc on-disk leaves to FREEMETA state */
1928 error = foreach_leaf(dip, leaf_dealloc, NULL);
1929 if (error)
1930 return error;
1931
1932 /* Make this a regular file in case we crash.
1933 (We don't want to free these blocks a second time.) */
1934
1935 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1936 if (error)
1937 return error;
1938
1939 error = gfs2_meta_inode_buffer(dip, &bh);
1940 if (!error) {
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001941 gfs2_trans_add_bh(dip->i_gl, bh, 1);
Steven Whitehouse568f4c92006-02-27 12:00:42 -05001942 ((struct gfs2_dinode *)bh->b_data)->di_mode =
1943 cpu_to_be32(S_IFREG);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001944 brelse(bh);
1945 }
1946
1947 gfs2_trans_end(sdp);
1948
1949 return error;
1950}
1951
1952/**
1953 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
1954 * @ip: the file being written to
1955 * @filname: the filename that's going to be added
David Teiglandb3b94fa2006-01-16 16:50:04 +00001956 *
Steven Whitehousec7526662006-03-20 12:30:04 -05001957 * Returns: 1 if alloc required, 0 if not, -ve on error
David Teiglandb3b94fa2006-01-16 16:50:04 +00001958 */
1959
Steven Whitehouse4d8012b2006-04-12 17:39:45 -04001960int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001961{
Steven Whitehousec7526662006-03-20 12:30:04 -05001962 struct gfs2_dirent *dent;
1963 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001964
Steven Whitehousec7526662006-03-20 12:30:04 -05001965 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
Steven Whitehouseed386502006-04-07 16:28:07 -04001966 if (!dent) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001967 return 1;
Steven Whitehouseed386502006-04-07 16:28:07 -04001968 }
Steven Whitehousec7526662006-03-20 12:30:04 -05001969 if (IS_ERR(dent))
1970 return PTR_ERR(dent);
1971 brelse(bh);
1972 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001973}
1974