blob: ba3438553f33a73d68b9f87d93ed684e7b8c5aee [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/*
11* 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 block
46* pointer in the array that points to the same leaf. In fact, when a directory
47* is first converted from linear to exhash, all of the pointers point to the
48* 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*/
55
56#include <linux/sched.h>
57#include <linux/slab.h>
58#include <linux/spinlock.h>
59#include <linux/completion.h>
60#include <linux/buffer_head.h>
61#include <linux/sort.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050062#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050063#include <linux/crc32.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000064#include <asm/semaphore.h>
65
66#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050067#include "lm_interface.h"
68#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000069#include "dir.h"
70#include "glock.h"
71#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000072#include "meta_io.h"
73#include "quota.h"
74#include "rgrp.h"
75#include "trans.h"
Steven Whitehousee13940b2006-01-30 13:31:50 +000076#include "bmap.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050077#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000078
79#define IS_LEAF 1 /* Hashed (leaf) directory */
80#define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
81
82#if 1
83#define gfs2_disk_hash2offset(h) (((uint64_t)(h)) >> 1)
84#define gfs2_dir_offset2hash(p) ((uint32_t)(((uint64_t)(p)) << 1))
85#else
86#define gfs2_disk_hash2offset(h) (((uint64_t)(h)))
87#define gfs2_dir_offset2hash(p) ((uint32_t)(((uint64_t)(p))))
88#endif
89
90typedef int (*leaf_call_t) (struct gfs2_inode *dip,
91 uint32_t index, uint32_t len, uint64_t leaf_no,
92 void *data);
93
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000094int gfs2_dir_get_buffer(struct gfs2_inode *ip, uint64_t block, int new,
95 struct buffer_head **bhp)
Steven Whitehousee13940b2006-01-30 13:31:50 +000096{
97 struct buffer_head *bh;
98 int error = 0;
99
100 if (new) {
101 bh = gfs2_meta_new(ip->i_gl, block);
102 gfs2_trans_add_bh(ip->i_gl, bh, 1);
103 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
104 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
105 } else {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500106 error = gfs2_meta_read(ip->i_gl, block, DIO_START | DIO_WAIT,
107 &bh);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000108 if (error)
109 return error;
110 if (gfs2_metatype_check(ip->i_sbd, bh, GFS2_METATYPE_JD)) {
111 brelse(bh);
112 return -EIO;
113 }
114 }
115
116 *bhp = bh;
117 return 0;
118}
119
120
121
122static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
123 unsigned int offset, unsigned int size)
124
125{
126 struct buffer_head *dibh;
127 int error;
128
129 error = gfs2_meta_inode_buffer(ip, &dibh);
130 if (error)
131 return error;
132
133 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
Steven Whitehousec7526662006-03-20 12:30:04 -0500134 memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000135 if (ip->i_di.di_size < offset + size)
136 ip->i_di.di_size = offset + size;
137 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
138 gfs2_dinode_out(&ip->i_di, dibh->b_data);
139
140 brelse(dibh);
141
142 return size;
143}
144
145
146
147/**
148 * gfs2_dir_write_data - Write directory information to the inode
149 * @ip: The GFS2 inode
150 * @buf: The buffer containing information to be written
151 * @offset: The file offset to start writing at
152 * @size: The amount of data to write
153 *
154 * Returns: The number of bytes correctly written or error code
155 */
156static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
157 uint64_t offset, unsigned int size)
158{
159 struct gfs2_sbd *sdp = ip->i_sbd;
160 struct buffer_head *dibh;
161 uint64_t lblock, dblock;
162 uint32_t extlen = 0;
163 unsigned int o;
164 int copied = 0;
165 int error = 0;
166
167 if (!size)
168 return 0;
169
170 if (gfs2_is_stuffed(ip) &&
171 offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500172 return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
173 size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000174
175 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
176 return -EINVAL;
177
178 if (gfs2_is_stuffed(ip)) {
179 error = gfs2_unstuff_dinode(ip, NULL, NULL);
180 if (error)
Steven Whitehousec7526662006-03-20 12:30:04 -0500181 return error;
Steven Whitehousee13940b2006-01-30 13:31:50 +0000182 }
183
184 lblock = offset;
185 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
186
187 while (copied < size) {
188 unsigned int amount;
189 struct buffer_head *bh;
190 int new;
191
192 amount = size - copied;
193 if (amount > sdp->sd_sb.sb_bsize - o)
194 amount = sdp->sd_sb.sb_bsize - o;
195
196 if (!extlen) {
197 new = 1;
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500198 error = gfs2_block_map(ip, lblock, &new, &dblock,
199 &extlen);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000200 if (error)
201 goto fail;
202 error = -EIO;
203 if (gfs2_assert_withdraw(sdp, dblock))
204 goto fail;
205 }
206
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500207 error = gfs2_dir_get_buffer(ip, dblock,
208 (amount == sdp->sd_jbsize) ?
209 1 : new, &bh);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000210 if (error)
211 goto fail;
212
213 gfs2_trans_add_bh(ip->i_gl, bh, 1);
214 memcpy(bh->b_data + o, buf, amount);
215 brelse(bh);
216 if (error)
217 goto fail;
218
219 copied += amount;
220 lblock++;
221 dblock++;
222 extlen--;
223
224 o = sizeof(struct gfs2_meta_header);
225 }
226
227out:
228 error = gfs2_meta_inode_buffer(ip, &dibh);
229 if (error)
230 return error;
231
232 if (ip->i_di.di_size < offset + copied)
233 ip->i_di.di_size = offset + copied;
234 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
235
236 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
237 gfs2_dinode_out(&ip->i_di, dibh->b_data);
238 brelse(dibh);
239
240 return copied;
241fail:
242 if (copied)
243 goto out;
244 return error;
245}
246
247static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf,
Steven Whitehousec7526662006-03-20 12:30:04 -0500248 unsigned int offset, unsigned int size)
Steven Whitehousee13940b2006-01-30 13:31:50 +0000249{
250 struct buffer_head *dibh;
251 int error;
252
253 error = gfs2_meta_inode_buffer(ip, &dibh);
254 if (!error) {
255 offset += sizeof(struct gfs2_dinode);
256 memcpy(buf, dibh->b_data + offset, size);
257 brelse(dibh);
258 }
259
260 return (error) ? error : size;
261}
262
263
264/**
265 * gfs2_dir_read_data - Read a data from a directory inode
266 * @ip: The GFS2 Inode
267 * @buf: The buffer to place result into
268 * @offset: File offset to begin jdata_readng from
269 * @size: Amount of data to transfer
270 *
271 * Returns: The amount of data actually copied or the error
272 */
273static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf,
274 uint64_t offset, unsigned int size)
275{
276 struct gfs2_sbd *sdp = ip->i_sbd;
277 uint64_t lblock, dblock;
278 uint32_t extlen = 0;
279 unsigned int o;
280 int copied = 0;
281 int error = 0;
282
283 if (offset >= ip->i_di.di_size)
284 return 0;
285
286 if ((offset + size) > ip->i_di.di_size)
287 size = ip->i_di.di_size - offset;
288
289 if (!size)
290 return 0;
291
292 if (gfs2_is_stuffed(ip))
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500293 return gfs2_dir_read_stuffed(ip, buf, (unsigned int)offset,
294 size);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000295
296 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
297 return -EINVAL;
298
299 lblock = offset;
300 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
301
302 while (copied < size) {
303 unsigned int amount;
304 struct buffer_head *bh;
305 int new;
306
307 amount = size - copied;
308 if (amount > sdp->sd_sb.sb_bsize - o)
309 amount = sdp->sd_sb.sb_bsize - o;
310
311 if (!extlen) {
312 new = 0;
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500313 error = gfs2_block_map(ip, lblock, &new, &dblock,
314 &extlen);
Steven Whitehousee13940b2006-01-30 13:31:50 +0000315 if (error)
316 goto fail;
317 }
318
319 if (extlen > 1)
320 gfs2_meta_ra(ip->i_gl, dblock, extlen);
321
322 if (dblock) {
323 error = gfs2_dir_get_buffer(ip, dblock, new, &bh);
324 if (error)
325 goto fail;
326 dblock++;
327 extlen--;
328 } else
329 bh = NULL;
330
331 memcpy(buf, bh->b_data + o, amount);
332 brelse(bh);
333 if (error)
334 goto fail;
335
336 copied += amount;
337 lblock++;
338
339 o = sizeof(struct gfs2_meta_header);
340 }
341
342 return copied;
343fail:
344 return (copied) ? copied : error;
345}
346
Steven Whitehousec7526662006-03-20 12:30:04 -0500347typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500348 const struct qstr *name,
349 void *opaque);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000350
Steven Whitehousec7526662006-03-20 12:30:04 -0500351static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
352 const struct qstr *name, int ret)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000353{
Steven Whitehousec7526662006-03-20 12:30:04 -0500354 if (dent->de_inum.no_addr != 0 &&
355 be32_to_cpu(dent->de_hash) == name->hash &&
356 be16_to_cpu(dent->de_name_len) == name->len &&
357 memcmp((char *)(dent+1), name->name, name->len) == 0)
358 return ret;
359 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000360}
361
Steven Whitehousec7526662006-03-20 12:30:04 -0500362static int gfs2_dirent_find(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500363 const struct qstr *name,
364 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500365{
366 return __gfs2_dirent_find(dent, name, 1);
367}
368
369static int gfs2_dirent_prev(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, 2);
374}
375
376/*
377 * name->name holds ptr to start of block.
378 * name->len holds size of block.
379 */
380static int gfs2_dirent_last(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500381 const struct qstr *name,
382 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500383{
384 const char *start = name->name;
385 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
386 if (name->len == (end - start))
387 return 1;
388 return 0;
389}
390
391static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500392 const struct qstr *name,
393 void *opaque)
Steven Whitehousec7526662006-03-20 12:30:04 -0500394{
395 unsigned required = GFS2_DIRENT_SIZE(name->len);
396 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
397 unsigned totlen = be16_to_cpu(dent->de_rec_len);
398
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500399 if (!dent->de_inum.no_addr)
400 actual = GFS2_DIRENT_SIZE(0);
Steven Whitehousec7526662006-03-20 12:30:04 -0500401 if ((totlen - actual) >= required)
402 return 1;
403 return 0;
404}
405
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500406struct dirent_gather {
407 const struct gfs2_dirent **pdent;
408 unsigned offset;
409};
410
411static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
412 const struct qstr *name,
413 void *opaque)
414{
415 struct dirent_gather *g = opaque;
416 if (dent->de_inum.no_addr) {
417 g->pdent[g->offset++] = dent;
418 }
419 return 0;
420}
421
Steven Whitehousec7526662006-03-20 12:30:04 -0500422/*
423 * Other possible things to check:
424 * - Inode located within filesystem size (and on valid block)
425 * - Valid directory entry type
426 * Not sure how heavy-weight we want to make this... could also check
427 * hash is correct for example, but that would take a lot of extra time.
428 * For now the most important thing is to check that the various sizes
429 * are correct.
430 */
431static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
432 unsigned int size, unsigned int len, int first)
433{
434 const char *msg = "gfs2_dirent too small";
435 if (unlikely(size < sizeof(struct gfs2_dirent)))
436 goto error;
437 msg = "gfs2_dirent misaligned";
438 if (unlikely(offset & 0x7))
439 goto error;
440 msg = "gfs2_dirent points beyond end of block";
441 if (unlikely(offset + size > len))
442 goto error;
443 msg = "zero inode number";
444 if (unlikely(!first && !dent->de_inum.no_addr))
445 goto error;
446 msg = "name length is greater than space in dirent";
447 if (dent->de_inum.no_addr &&
448 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
449 size))
450 goto error;
451 return 0;
452error:
453 printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
454 first ? "first in block" : "not first in block");
455 return -EIO;
456}
457
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500458static int gfs2_dirent_offset(const void *buf)
Steven Whitehousec7526662006-03-20 12:30:04 -0500459{
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500460 const struct gfs2_meta_header *h = buf;
461 int offset;
Steven Whitehousec7526662006-03-20 12:30:04 -0500462
463 BUG_ON(buf == NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500464
465 switch(be16_to_cpu(h->mh_type)) {
466 case GFS2_METATYPE_LF:
467 offset = sizeof(struct gfs2_leaf);
468 break;
469 case GFS2_METATYPE_DI:
470 offset = sizeof(struct gfs2_dinode);
471 break;
472 default:
473 goto wrong_type;
474 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500475 return offset;
476wrong_type:
477 printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
478 be16_to_cpu(h->mh_type));
479 return -1;
480}
Steven Whitehousec7526662006-03-20 12:30:04 -0500481
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500482static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode,
483 void *buf,
484 unsigned int len, gfs2_dscan_t scan,
485 const struct qstr *name,
486 void *opaque)
487{
488 struct gfs2_dirent *dent, *prev;
489 unsigned offset;
490 unsigned size;
491 int ret = 0;
492
493 ret = gfs2_dirent_offset(buf);
494 if (ret < 0)
495 goto consist_inode;
496
497 offset = ret;
Steven Whitehousec7526662006-03-20 12:30:04 -0500498 prev = NULL;
499 dent = (struct gfs2_dirent *)(buf + offset);
500 size = be16_to_cpu(dent->de_rec_len);
501 if (gfs2_check_dirent(dent, offset, size, len, 1))
502 goto consist_inode;
503 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500504 ret = scan(dent, name, opaque);
Steven Whitehousec7526662006-03-20 12:30:04 -0500505 if (ret)
506 break;
507 offset += size;
508 if (offset == len)
509 break;
510 prev = dent;
511 dent = (struct gfs2_dirent *)(buf + offset);
512 size = be16_to_cpu(dent->de_rec_len);
513 if (gfs2_check_dirent(dent, offset, size, len, 0))
514 goto consist_inode;
515 } while(1);
516
517 switch(ret) {
518 case 0:
519 return NULL;
520 case 1:
521 return dent;
522 case 2:
523 return prev ? prev : dent;
524 default:
525 BUG_ON(ret > 0);
526 return ERR_PTR(ret);
527 }
528
Steven Whitehousec7526662006-03-20 12:30:04 -0500529consist_inode:
530 gfs2_consist_inode(inode->u.generic_ip);
531 return ERR_PTR(-EIO);
532}
533
534
David Teiglandb3b94fa2006-01-16 16:50:04 +0000535/**
536 * dirent_first - Return the first dirent
537 * @dip: the directory
538 * @bh: The buffer
539 * @dent: Pointer to list of dirents
540 *
541 * return first dirent whether bh points to leaf or stuffed dinode
542 *
543 * Returns: IS_LEAF, IS_DINODE, or -errno
544 */
545
546static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
547 struct gfs2_dirent **dent)
548{
549 struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data;
550
551 if (be16_to_cpu(h->mh_type) == GFS2_METATYPE_LF) {
552 if (gfs2_meta_check(dip->i_sbd, bh))
553 return -EIO;
554 *dent = (struct gfs2_dirent *)(bh->b_data +
555 sizeof(struct gfs2_leaf));
556 return IS_LEAF;
557 } else {
558 if (gfs2_metatype_check(dip->i_sbd, bh, GFS2_METATYPE_DI))
559 return -EIO;
560 *dent = (struct gfs2_dirent *)(bh->b_data +
561 sizeof(struct gfs2_dinode));
562 return IS_DINODE;
563 }
564}
565
566/**
567 * dirent_next - Next dirent
568 * @dip: the directory
569 * @bh: The buffer
570 * @dent: Pointer to list of dirents
571 *
572 * Returns: 0 on success, error code otherwise
573 */
574
575static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
576 struct gfs2_dirent **dent)
577{
578 struct gfs2_dirent *tmp, *cur;
579 char *bh_end;
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000580 uint16_t cur_rec_len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000581
582 cur = *dent;
583 bh_end = bh->b_data + bh->b_size;
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000584 cur_rec_len = be16_to_cpu(cur->de_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000585
586 if ((char *)cur + cur_rec_len >= bh_end) {
587 if ((char *)cur + cur_rec_len > bh_end) {
588 gfs2_consist_inode(dip);
589 return -EIO;
590 }
591 return -ENOENT;
592 }
593
594 tmp = (struct gfs2_dirent *)((char *)cur + cur_rec_len);
595
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000596 if ((char *)tmp + be16_to_cpu(tmp->de_rec_len) > bh_end) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000597 gfs2_consist_inode(dip);
598 return -EIO;
599 }
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000600
601 if (cur_rec_len == 0) {
602 gfs2_consist_inode(dip);
603 return -EIO;
604 }
605
David Teiglandb3b94fa2006-01-16 16:50:04 +0000606 /* Only the first dent could ever have de_inum.no_addr == 0 */
607 if (!tmp->de_inum.no_addr) {
608 gfs2_consist_inode(dip);
609 return -EIO;
610 }
611
612 *dent = tmp;
613
614 return 0;
615}
616
617/**
618 * dirent_del - Delete a dirent
619 * @dip: The GFS2 inode
620 * @bh: The buffer
621 * @prev: The previous dirent
622 * @cur: The current dirent
623 *
624 */
625
626static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
627 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
628{
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000629 uint16_t cur_rec_len, prev_rec_len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000630
631 if (!cur->de_inum.no_addr) {
632 gfs2_consist_inode(dip);
633 return;
634 }
635
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000636 gfs2_trans_add_bh(dip->i_gl, bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000637
638 /* If there is no prev entry, this is the first entry in the block.
639 The de_rec_len is already as big as it needs to be. Just zero
640 out the inode number and return. */
641
642 if (!prev) {
643 cur->de_inum.no_addr = 0; /* No endianess worries */
644 return;
645 }
646
647 /* Combine this dentry with the previous one. */
648
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000649 prev_rec_len = be16_to_cpu(prev->de_rec_len);
650 cur_rec_len = be16_to_cpu(cur->de_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000651
652 if ((char *)prev + prev_rec_len != (char *)cur)
653 gfs2_consist_inode(dip);
654 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
655 gfs2_consist_inode(dip);
656
657 prev_rec_len += cur_rec_len;
Steven Whitehousefc69d0d2006-02-13 16:21:47 +0000658 prev->de_rec_len = cpu_to_be16(prev_rec_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000659}
660
Steven Whitehousec7526662006-03-20 12:30:04 -0500661/*
662 * Takes a dent from which to grab space as an argument. Returns the
663 * newly created dent.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000664 */
Steven Whitehousec7526662006-03-20 12:30:04 -0500665struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
666 struct gfs2_dirent *dent,
667 const struct qstr *name,
668 struct buffer_head *bh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000669{
Steven Whitehousec7526662006-03-20 12:30:04 -0500670 struct gfs2_inode *ip = inode->u.generic_ip;
671 struct gfs2_dirent *ndent;
672 unsigned offset = 0, totlen;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000673
Steven Whitehousec7526662006-03-20 12:30:04 -0500674 if (dent->de_inum.no_addr)
675 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
676 totlen = be16_to_cpu(dent->de_rec_len);
677 BUG_ON(offset + name->len > totlen);
678 gfs2_trans_add_bh(ip->i_gl, bh, 1);
679 ndent = (struct gfs2_dirent *)((char *)dent + offset);
680 dent->de_rec_len = cpu_to_be16(offset);
681 gfs2_qstr2dirent(name, totlen - offset, ndent);
682 return ndent;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000683}
684
Steven Whitehousec7526662006-03-20 12:30:04 -0500685static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
686 struct buffer_head *bh,
687 const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000688{
689 struct gfs2_dirent *dent;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500690 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
691 gfs2_dirent_find_space, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500692 if (!dent || IS_ERR(dent))
693 return dent;
694 return gfs2_init_dirent(inode, dent, name, bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000695}
696
697static int get_leaf(struct gfs2_inode *dip, uint64_t leaf_no,
698 struct buffer_head **bhp)
699{
700 int error;
701
702 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_START | DIO_WAIT, bhp);
703 if (!error && gfs2_metatype_check(dip->i_sbd, *bhp, GFS2_METATYPE_LF))
704 error = -EIO;
705
706 return error;
707}
708
709/**
710 * get_leaf_nr - Get a leaf number associated with the index
711 * @dip: The GFS2 inode
712 * @index:
713 * @leaf_out:
714 *
715 * Returns: 0 on success, error code otherwise
716 */
717
718static int get_leaf_nr(struct gfs2_inode *dip, uint32_t index,
719 uint64_t *leaf_out)
720{
721 uint64_t leaf_no;
722 int error;
723
Steven Whitehousee13940b2006-01-30 13:31:50 +0000724 error = gfs2_dir_read_data(dip, (char *)&leaf_no,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000725 index * sizeof(uint64_t),
726 sizeof(uint64_t));
727 if (error != sizeof(uint64_t))
728 return (error < 0) ? error : -EIO;
729
730 *leaf_out = be64_to_cpu(leaf_no);
731
732 return 0;
733}
734
735static int get_first_leaf(struct gfs2_inode *dip, uint32_t index,
736 struct buffer_head **bh_out)
737{
738 uint64_t leaf_no;
739 int error;
740
741 error = get_leaf_nr(dip, index, &leaf_no);
742 if (!error)
743 error = get_leaf(dip, leaf_no, bh_out);
744
745 return error;
746}
747
Steven Whitehousec7526662006-03-20 12:30:04 -0500748static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
749 const struct qstr *name,
750 gfs2_dscan_t scan,
751 struct buffer_head **pbh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000752{
Steven Whitehousec7526662006-03-20 12:30:04 -0500753 struct buffer_head *bh;
754 struct gfs2_dirent *dent;
755 struct gfs2_inode *ip = inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000756 int error;
757
Steven Whitehousec7526662006-03-20 12:30:04 -0500758 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
759 struct gfs2_leaf *leaf;
760 unsigned hsize = 1 << ip->i_di.di_depth;
761 unsigned index;
762 u64 ln;
763 if (hsize * sizeof(u64) != ip->i_di.di_size) {
764 gfs2_consist_inode(ip);
765 return ERR_PTR(-EIO);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000766 }
767
Steven Whitehousec7526662006-03-20 12:30:04 -0500768 index = name->hash >> (32 - ip->i_di.di_depth);
769 error = get_first_leaf(ip, index, &bh);
770 if (error)
771 return ERR_PTR(error);
772 do {
773 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500774 scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500775 if (dent)
776 goto got_dent;
777 leaf = (struct gfs2_leaf *)bh->b_data;
778 ln = be64_to_cpu(leaf->lf_next);
779 brelse(bh);
780 if (!ln)
781 break;
782 error = get_leaf(ip, ln, &bh);
783 } while(!error);
784
785 return error ? ERR_PTR(error) : NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000786 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000787
Steven Whitehousec7526662006-03-20 12:30:04 -0500788 error = gfs2_meta_inode_buffer(ip, &bh);
789 if (error)
790 return ERR_PTR(error);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500791 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500792got_dent:
793 *pbh = bh;
794 return dent;
795}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000796
Steven Whitehousec7526662006-03-20 12:30:04 -0500797static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
798{
799 struct gfs2_inode *ip = inode->u.generic_ip;
800 u64 bn = gfs2_alloc_meta(ip);
801 struct buffer_head *bh = gfs2_meta_new(ip->i_gl, bn);
802 struct gfs2_leaf *leaf;
803 struct gfs2_dirent *dent;
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500804 struct qstr name = { .name = "", .len = 0, .hash = 0 };
Steven Whitehousec7526662006-03-20 12:30:04 -0500805 if (!bh)
806 return NULL;
807 gfs2_trans_add_bh(ip->i_gl, bh, 1);
808 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
809 leaf = (struct gfs2_leaf *)bh->b_data;
810 leaf->lf_depth = cpu_to_be16(depth);
811 leaf->lf_entries = cpu_to_be16(0);
812 leaf->lf_dirent_format = cpu_to_be16(GFS2_FORMAT_DE);
813 leaf->lf_next = cpu_to_be64(0);
814 memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
815 dent = (struct gfs2_dirent *)(leaf+1);
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500816 gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
Steven Whitehousec7526662006-03-20 12:30:04 -0500817 *pbh = bh;
818 return leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000819}
820
821/**
822 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
823 * @dip: The GFS2 inode
824 *
825 * Returns: 0 on success, error code otherwise
826 */
827
Steven Whitehousec7526662006-03-20 12:30:04 -0500828static int dir_make_exhash(struct inode *inode)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000829{
Steven Whitehousec7526662006-03-20 12:30:04 -0500830 struct gfs2_inode *dip = inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000831 struct gfs2_sbd *sdp = dip->i_sbd;
832 struct gfs2_dirent *dent;
Steven Whitehousec7526662006-03-20 12:30:04 -0500833 struct qstr args;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000834 struct buffer_head *bh, *dibh;
835 struct gfs2_leaf *leaf;
836 int y;
837 uint32_t x;
838 uint64_t *lp, bn;
839 int error;
840
841 error = gfs2_meta_inode_buffer(dip, &dibh);
842 if (error)
843 return error;
844
David Teiglandb3b94fa2006-01-16 16:50:04 +0000845 /* Turn over a new leaf */
846
Steven Whitehousec7526662006-03-20 12:30:04 -0500847 leaf = new_leaf(inode, &bh, 0);
848 if (!leaf)
849 return -ENOSPC;
850 bn = bh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000851
852 gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000853 leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries);
854
855 /* Copy dirents */
856
857 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
858 sizeof(struct gfs2_dinode));
859
860 /* Find last entry */
861
862 x = 0;
Steven Whitehousec7526662006-03-20 12:30:04 -0500863 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
864 sizeof(struct gfs2_leaf);
865 args.name = bh->b_data;
866 dent = gfs2_dirent_scan(dip->i_vnode, bh->b_data, bh->b_size,
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500867 gfs2_dirent_last, &args, NULL);
Steven Whitehousec7526662006-03-20 12:30:04 -0500868 if (!dent) {
869 brelse(bh);
870 brelse(dibh);
871 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000872 }
Steven Whitehousec7526662006-03-20 12:30:04 -0500873 if (IS_ERR(dent)) {
874 brelse(bh);
875 brelse(dibh);
876 return PTR_ERR(dent);
877 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000878
879 /* Adjust the last dirent's record length
880 (Remember that dent still points to the last entry.) */
881
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000882 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
David Teiglandb3b94fa2006-01-16 16:50:04 +0000883 sizeof(struct gfs2_dinode) -
Steven Whitehouse4dd651a2006-02-14 15:56:44 +0000884 sizeof(struct gfs2_leaf));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000885
886 brelse(bh);
887
888 /* We're done with the new leaf block, now setup the new
889 hash table. */
890
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000891 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000892 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
893
894 lp = (uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode));
895
896 for (x = sdp->sd_hash_ptrs; x--; lp++)
897 *lp = cpu_to_be64(bn);
898
899 dip->i_di.di_size = sdp->sd_sb.sb_bsize / 2;
900 dip->i_di.di_blocks++;
901 dip->i_di.di_flags |= GFS2_DIF_EXHASH;
902 dip->i_di.di_payload_format = 0;
903
904 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
905 dip->i_di.di_depth = y;
906
907 gfs2_dinode_out(&dip->i_di, dibh->b_data);
908
909 brelse(dibh);
910
911 return 0;
912}
913
914/**
915 * dir_split_leaf - Split a leaf block into two
916 * @dip: The GFS2 inode
917 * @index:
918 * @leaf_no:
919 *
920 * Returns: 0 on success, error code on failure
921 */
922
Steven Whitehousec7526662006-03-20 12:30:04 -0500923static int dir_split_leaf(struct inode *inode, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000924{
Steven Whitehousec7526662006-03-20 12:30:04 -0500925 struct gfs2_inode *dip = inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000926 struct buffer_head *nbh, *obh, *dibh;
927 struct gfs2_leaf *nleaf, *oleaf;
928 struct gfs2_dirent *dent, *prev = NULL, *next = NULL, *new;
929 uint32_t start, len, half_len, divider;
Steven Whitehousec7526662006-03-20 12:30:04 -0500930 uint64_t bn, *lp, leaf_no;
931 uint32_t index;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000932 int x, moved = 0;
933 int error;
934
Steven Whitehousec7526662006-03-20 12:30:04 -0500935 index = name->hash >> (32 - dip->i_di.di_depth);
936 error = get_leaf_nr(dip, index, &leaf_no);
937 if (error)
938 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000939
940 /* Get the old leaf block */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000941 error = get_leaf(dip, leaf_no, &obh);
942 if (error)
943 goto fail;
944
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000945 gfs2_trans_add_bh(dip->i_gl, obh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000946 oleaf = (struct gfs2_leaf *)obh->b_data;
947
Steven Whitehousec7526662006-03-20 12:30:04 -0500948 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
949 if (!nleaf) {
950 brelse(obh);
951 return -ENOSPC;
952 }
953 bn = nbh->b_blocknr;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000954
Steven Whitehousec7526662006-03-20 12:30:04 -0500955 /* Compute the start and len of leaf pointers in the hash table. */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000956 len = 1 << (dip->i_di.di_depth - be16_to_cpu(oleaf->lf_depth));
957 half_len = len >> 1;
958 if (!half_len) {
959 gfs2_consist_inode(dip);
960 error = -EIO;
961 goto fail_brelse;
962 }
963
964 start = (index & ~(len - 1));
965
966 /* Change the pointers.
967 Don't bother distinguishing stuffed from non-stuffed.
968 This code is complicated enough already. */
Steven Whitehousec7526662006-03-20 12:30:04 -0500969 lp = kmalloc(half_len * sizeof(uint64_t), GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000970 /* Change the pointers */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000971 for (x = 0; x < half_len; x++)
972 lp[x] = cpu_to_be64(bn);
973
Steven Whitehousee13940b2006-01-30 13:31:50 +0000974 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(uint64_t),
Steven Whitehouse71b86f52006-03-28 14:14:04 -0500975 half_len * sizeof(uint64_t));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000976 if (error != half_len * sizeof(uint64_t)) {
977 if (error >= 0)
978 error = -EIO;
979 goto fail_lpfree;
980 }
981
982 kfree(lp);
983
984 /* Compute the divider */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000985 divider = (start + half_len) << (32 - dip->i_di.di_depth);
986
987 /* Copy the entries */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000988 dirent_first(dip, obh, &dent);
989
990 do {
991 next = dent;
992 if (dirent_next(dip, obh, &next))
993 next = NULL;
994
995 if (dent->de_inum.no_addr &&
996 be32_to_cpu(dent->de_hash) < divider) {
Steven Whitehousec7526662006-03-20 12:30:04 -0500997 struct qstr str;
998 str.name = (char*)(dent+1);
999 str.len = be16_to_cpu(dent->de_name_len);
1000 str.hash = be32_to_cpu(dent->de_hash);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001001 new = gfs2_dirent_alloc(inode, nbh, &str);
Steven Whitehousec7526662006-03-20 12:30:04 -05001002 if (IS_ERR(new)) {
1003 error = PTR_ERR(new);
1004 break;
1005 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001006
1007 new->de_inum = dent->de_inum; /* No endian worries */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001008 new->de_type = dent->de_type; /* No endian worries */
Steven Whitehousec7526662006-03-20 12:30:04 -05001009 nleaf->lf_entries = cpu_to_be16(be16_to_cpu(nleaf->lf_entries)+1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001010
1011 dirent_del(dip, obh, prev, dent);
1012
1013 if (!oleaf->lf_entries)
1014 gfs2_consist_inode(dip);
Steven Whitehousec7526662006-03-20 12:30:04 -05001015 oleaf->lf_entries = cpu_to_be16(be16_to_cpu(oleaf->lf_entries)-1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001016
1017 if (!prev)
1018 prev = dent;
1019
1020 moved = 1;
Steven Whitehousec7526662006-03-20 12:30:04 -05001021 } else {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001022 prev = dent;
Steven Whitehousec7526662006-03-20 12:30:04 -05001023 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001024 dent = next;
Steven Whitehousec7526662006-03-20 12:30:04 -05001025 } while (dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001026
Steven Whitehousec7526662006-03-20 12:30:04 -05001027 oleaf->lf_depth = nleaf->lf_depth;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001028
1029 error = gfs2_meta_inode_buffer(dip, &dibh);
1030 if (!gfs2_assert_withdraw(dip->i_sbd, !error)) {
1031 dip->i_di.di_blocks++;
1032 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1033 brelse(dibh);
1034 }
1035
1036 brelse(obh);
1037 brelse(nbh);
1038
1039 return error;
1040
1041 fail_lpfree:
1042 kfree(lp);
1043
1044 fail_brelse:
1045 brelse(obh);
1046
1047 fail:
1048 brelse(nbh);
1049 return error;
1050}
1051
1052/**
1053 * dir_double_exhash - Double size of ExHash table
1054 * @dip: The GFS2 dinode
1055 *
1056 * Returns: 0 on success, error code on failure
1057 */
1058
1059static int dir_double_exhash(struct gfs2_inode *dip)
1060{
1061 struct gfs2_sbd *sdp = dip->i_sbd;
1062 struct buffer_head *dibh;
1063 uint32_t hsize;
1064 uint64_t *buf;
1065 uint64_t *from, *to;
1066 uint64_t block;
1067 int x;
1068 int error = 0;
1069
1070 hsize = 1 << dip->i_di.di_depth;
1071 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1072 gfs2_consist_inode(dip);
1073 return -EIO;
1074 }
1075
1076 /* Allocate both the "from" and "to" buffers in one big chunk */
1077
1078 buf = kcalloc(3, sdp->sd_hash_bsize, GFP_KERNEL | __GFP_NOFAIL);
1079
1080 for (block = dip->i_di.di_size >> sdp->sd_hash_bsize_shift; block--;) {
Steven Whitehousee13940b2006-01-30 13:31:50 +00001081 error = gfs2_dir_read_data(dip, (char *)buf,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001082 block * sdp->sd_hash_bsize,
1083 sdp->sd_hash_bsize);
1084 if (error != sdp->sd_hash_bsize) {
1085 if (error >= 0)
1086 error = -EIO;
1087 goto fail;
1088 }
1089
1090 from = buf;
1091 to = (uint64_t *)((char *)buf + sdp->sd_hash_bsize);
1092
1093 for (x = sdp->sd_hash_ptrs; x--; from++) {
1094 *to++ = *from; /* No endianess worries */
1095 *to++ = *from;
1096 }
1097
Steven Whitehousee13940b2006-01-30 13:31:50 +00001098 error = gfs2_dir_write_data(dip,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001099 (char *)buf + sdp->sd_hash_bsize,
1100 block * sdp->sd_sb.sb_bsize,
1101 sdp->sd_sb.sb_bsize);
1102 if (error != sdp->sd_sb.sb_bsize) {
1103 if (error >= 0)
1104 error = -EIO;
1105 goto fail;
1106 }
1107 }
1108
1109 kfree(buf);
1110
1111 error = gfs2_meta_inode_buffer(dip, &dibh);
1112 if (!gfs2_assert_withdraw(sdp, !error)) {
1113 dip->i_di.di_depth++;
1114 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1115 brelse(dibh);
1116 }
1117
1118 return error;
1119
1120 fail:
1121 kfree(buf);
1122
1123 return error;
1124}
1125
1126/**
1127 * compare_dents - compare directory entries by hash value
1128 * @a: first dent
1129 * @b: second dent
1130 *
1131 * When comparing the hash entries of @a to @b:
1132 * gt: returns 1
1133 * lt: returns -1
1134 * eq: returns 0
1135 */
1136
1137static int compare_dents(const void *a, const void *b)
1138{
1139 struct gfs2_dirent *dent_a, *dent_b;
1140 uint32_t hash_a, hash_b;
1141 int ret = 0;
1142
1143 dent_a = *(struct gfs2_dirent **)a;
Steven Whitehousec7526662006-03-20 12:30:04 -05001144 hash_a = be32_to_cpu(dent_a->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001145
1146 dent_b = *(struct gfs2_dirent **)b;
Steven Whitehousec7526662006-03-20 12:30:04 -05001147 hash_b = be32_to_cpu(dent_b->de_hash);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001148
1149 if (hash_a > hash_b)
1150 ret = 1;
1151 else if (hash_a < hash_b)
1152 ret = -1;
1153 else {
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001154 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1155 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001156
1157 if (len_a > len_b)
1158 ret = 1;
1159 else if (len_a < len_b)
1160 ret = -1;
1161 else
1162 ret = memcmp((char *)(dent_a + 1),
1163 (char *)(dent_b + 1),
1164 len_a);
1165 }
1166
1167 return ret;
1168}
1169
1170/**
1171 * do_filldir_main - read out directory entries
1172 * @dip: The GFS2 inode
1173 * @offset: The offset in the file to read from
1174 * @opaque: opaque data to pass to filldir
1175 * @filldir: The function to pass entries to
1176 * @darr: an array of struct gfs2_dirent pointers to read
1177 * @entries: the number of entries in darr
1178 * @copied: pointer to int that's non-zero if a entry has been copied out
1179 *
1180 * Jump through some hoops to make sure that if there are hash collsions,
1181 * they are read out at the beginning of a buffer. We want to minimize
1182 * the possibility that they will fall into different readdir buffers or
1183 * that someone will want to seek to that location.
1184 *
1185 * Returns: errno, >0 on exception from filldir
1186 */
1187
1188static int do_filldir_main(struct gfs2_inode *dip, uint64_t *offset,
1189 void *opaque, gfs2_filldir_t filldir,
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001190 const struct gfs2_dirent **darr, uint32_t entries,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001191 int *copied)
1192{
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001193 const struct gfs2_dirent *dent, *dent_next;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001194 struct gfs2_inum inum;
1195 uint64_t off, off_next;
1196 unsigned int x, y;
1197 int run = 0;
1198 int error = 0;
1199
1200 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1201
1202 dent_next = darr[0];
1203 off_next = be32_to_cpu(dent_next->de_hash);
1204 off_next = gfs2_disk_hash2offset(off_next);
1205
1206 for (x = 0, y = 1; x < entries; x++, y++) {
1207 dent = dent_next;
1208 off = off_next;
1209
1210 if (y < entries) {
1211 dent_next = darr[y];
1212 off_next = be32_to_cpu(dent_next->de_hash);
1213 off_next = gfs2_disk_hash2offset(off_next);
1214
1215 if (off < *offset)
1216 continue;
1217 *offset = off;
1218
1219 if (off_next == off) {
1220 if (*copied && !run)
1221 return 1;
1222 run = 1;
1223 } else
1224 run = 0;
1225 } else {
1226 if (off < *offset)
1227 continue;
1228 *offset = off;
1229 }
1230
1231 gfs2_inum_in(&inum, (char *)&dent->de_inum);
1232
1233 error = filldir(opaque, (char *)(dent + 1),
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001234 be16_to_cpu(dent->de_name_len),
David Teiglandb3b94fa2006-01-16 16:50:04 +00001235 off, &inum,
Steven Whitehouse4dd651a2006-02-14 15:56:44 +00001236 be16_to_cpu(dent->de_type));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001237 if (error)
1238 return 1;
1239
1240 *copied = 1;
1241 }
1242
1243 /* Increment the *offset by one, so the next time we come into the
1244 do_filldir fxn, we get the next entry instead of the last one in the
1245 current leaf */
1246
1247 (*offset)++;
1248
1249 return 0;
1250}
1251
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001252static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
1253 gfs2_filldir_t filldir, int *copied,
1254 unsigned *depth, u64 leaf_no)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001255{
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001256 struct gfs2_inode *ip = inode->u.generic_ip;
1257 struct buffer_head *bh;
1258 struct gfs2_leaf *lf;
1259 unsigned entries = 0;
1260 unsigned leaves = 0;
1261 const struct gfs2_dirent **darr, *dent;
1262 struct dirent_gather g;
1263 struct buffer_head **larr;
1264 int leaf = 0;
1265 int error, i;
1266 u64 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001267
David Teiglandb3b94fa2006-01-16 16:50:04 +00001268 do {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001269 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001270 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001271 goto out;
1272 lf = (struct gfs2_leaf *)bh->b_data;
1273 if (leaves == 0)
1274 *depth = be16_to_cpu(lf->lf_depth);
1275 entries += be16_to_cpu(lf->lf_entries);
1276 leaves++;
1277 lfn = be64_to_cpu(lf->lf_next);
1278 brelse(bh);
1279 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001280
1281 if (!entries)
1282 return 0;
1283
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001284 error = -ENOMEM;
1285 larr = kmalloc((leaves + entries) * sizeof(void*), GFP_KERNEL);
1286 if (!larr)
1287 goto out;
1288 darr = (const struct gfs2_dirent **)(larr + leaves);
1289 g.pdent = darr;
1290 g.offset = 0;
1291 lfn = leaf_no;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001292
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001293 do {
1294 error = get_leaf(ip, lfn, &bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001295 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001296 goto out_kfree;
1297 lf = (struct gfs2_leaf *)bh->b_data;
1298 lfn = be64_to_cpu(lf->lf_next);
1299 if (lf->lf_entries) {
1300 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1301 gfs2_dirent_gather, NULL, &g);
1302 error = PTR_ERR(dent);
1303 if (IS_ERR(dent)) {
1304 goto out_kfree;
1305 }
1306 error = 0;
1307 larr[leaf++] = bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001308 } else {
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001309 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001310 }
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001311 } while(lfn);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001312
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001313 error = do_filldir_main(ip, offset, opaque, filldir, darr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001314 entries, copied);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001315out_kfree:
1316 for(i = 0; i < leaf; i++)
1317 brelse(larr[i]);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001318 kfree(larr);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001319out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001320 return error;
1321}
1322
1323/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001324 * dir_e_read - Reads the entries from a directory into a filldir buffer
1325 * @dip: dinode pointer
1326 * @offset: the hash of the last entry read shifted to the right once
1327 * @opaque: buffer for the filldir function to fill
1328 * @filldir: points to the filldir function to use
1329 *
1330 * Returns: errno
1331 */
1332
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001333static int dir_e_read(struct inode *inode, uint64_t *offset, void *opaque,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001334 gfs2_filldir_t filldir)
1335{
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001336 struct gfs2_inode *dip = inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001337 struct gfs2_sbd *sdp = dip->i_sbd;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001338 uint32_t hsize, len = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001339 uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
1340 uint32_t hash, index;
1341 uint64_t *lp;
1342 int copied = 0;
1343 int error = 0;
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001344 unsigned depth;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001345
1346 hsize = 1 << dip->i_di.di_depth;
1347 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1348 gfs2_consist_inode(dip);
1349 return -EIO;
1350 }
1351
1352 hash = gfs2_dir_offset2hash(*offset);
1353 index = hash >> (32 - dip->i_di.di_depth);
1354
1355 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1356 if (!lp)
1357 return -ENOMEM;
1358
1359 while (index < hsize) {
1360 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1361 ht_offset = index - lp_offset;
1362
1363 if (ht_offset_cur != ht_offset) {
Steven Whitehousee13940b2006-01-30 13:31:50 +00001364 error = gfs2_dir_read_data(dip, (char *)lp,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001365 ht_offset * sizeof(uint64_t),
1366 sdp->sd_hash_bsize);
1367 if (error != sdp->sd_hash_bsize) {
1368 if (error >= 0)
1369 error = -EIO;
1370 goto out;
1371 }
1372 ht_offset_cur = ht_offset;
1373 }
1374
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001375 error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
1376 &copied, &depth,
1377 be64_to_cpu(lp[lp_offset]));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001378 if (error)
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001379 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001380
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001381 len = 1 << (dip->i_di.di_depth - depth);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001382 index = (index & ~(len - 1)) + len;
1383 }
1384
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001385out:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001386 kfree(lp);
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001387 if (error > 0)
1388 error = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001389 return error;
1390}
1391
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001392int gfs2_dir_read(struct inode *inode, uint64_t *offset, void *opaque,
1393 gfs2_filldir_t filldir)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001394{
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001395 struct gfs2_inode *dip = inode->u.generic_ip;
1396 struct dirent_gather g;
1397 const struct gfs2_dirent **darr, *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001398 struct buffer_head *dibh;
1399 int copied = 0;
1400 int error;
1401
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001402 if (!dip->i_di.di_entries)
1403 return 0;
1404
1405 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
1406 return dir_e_read(inode, offset, opaque, filldir);
1407
David Teiglandb3b94fa2006-01-16 16:50:04 +00001408 if (!gfs2_is_stuffed(dip)) {
1409 gfs2_consist_inode(dip);
1410 return -EIO;
1411 }
1412
David Teiglandb3b94fa2006-01-16 16:50:04 +00001413 error = gfs2_meta_inode_buffer(dip, &dibh);
1414 if (error)
1415 return error;
1416
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001417 error = -ENOMEM;
1418 darr = kmalloc(dip->i_di.di_entries * sizeof(struct gfs2_dirent *),
1419 GFP_KERNEL);
1420 if (darr) {
1421 g.pdent = darr;
1422 g.offset = 0;
1423 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1424 gfs2_dirent_gather, NULL, &g);
1425 if (IS_ERR(dent)) {
1426 error = PTR_ERR(dent);
1427 goto out;
1428 }
1429 error = do_filldir_main(dip, offset, opaque, filldir, darr,
1430 dip->i_di.di_entries, &copied);
1431out:
1432 kfree(darr);
1433 }
1434
David Teiglandb3b94fa2006-01-16 16:50:04 +00001435 if (error > 0)
1436 error = 0;
1437
1438 brelse(dibh);
1439
1440 return error;
1441}
1442
David Teiglandb3b94fa2006-01-16 16:50:04 +00001443/**
1444 * gfs2_dir_search - Search a directory
1445 * @dip: The GFS2 inode
1446 * @filename:
1447 * @inode:
1448 *
1449 * This routine searches a directory for a file or another directory.
1450 * Assumes a glock is held on dip.
1451 *
1452 * Returns: errno
1453 */
1454
Steven Whitehousec7526662006-03-20 12:30:04 -05001455int gfs2_dir_search(struct inode *dir, const struct qstr *name,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001456 struct gfs2_inum *inum, unsigned int *type)
1457{
Steven Whitehousec7526662006-03-20 12:30:04 -05001458 struct buffer_head *bh;
1459 struct gfs2_dirent *dent;
1460
1461 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1462 if (dent) {
1463 if (IS_ERR(dent))
1464 return PTR_ERR(dent);
1465 if (inum)
1466 gfs2_inum_in(inum, (char *)&dent->de_inum);
1467 if (type)
1468 *type = be16_to_cpu(dent->de_type);
1469 brelse(bh);
1470 return 0;
1471 }
1472 return -ENOENT;
1473}
1474
1475static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1476{
1477 struct buffer_head *bh, *obh;
1478 struct gfs2_inode *ip = inode->u.generic_ip;
1479 struct gfs2_leaf *leaf, *oleaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001480 int error;
Steven Whitehousec7526662006-03-20 12:30:04 -05001481 u32 index;
1482 u64 bn;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001483
Steven Whitehousec7526662006-03-20 12:30:04 -05001484 index = name->hash >> (32 - ip->i_di.di_depth);
1485 error = get_first_leaf(ip, index, &obh);
1486 if (error)
1487 return error;
1488 do {
1489 oleaf = (struct gfs2_leaf *)obh->b_data;
1490 bn = be64_to_cpu(oleaf->lf_next);
1491 if (!bn)
1492 break;
1493 brelse(obh);
1494 error = get_leaf(ip, bn, &obh);
1495 if (error)
1496 return error;
1497 } while(1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001498
Steven Whitehousec7526662006-03-20 12:30:04 -05001499 gfs2_trans_add_bh(ip->i_gl, obh, 1);
1500
1501 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1502 if (!leaf) {
1503 brelse(obh);
1504 return -ENOSPC;
1505 }
1506 oleaf->lf_next = cpu_to_be64(bn);
1507 brelse(bh);
1508 brelse(obh);
1509
1510 error = gfs2_meta_inode_buffer(ip, &bh);
1511 if (error)
1512 return error;
1513 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1514 ip->i_di.di_blocks++;
1515 gfs2_dinode_out(&ip->i_di, bh->b_data);
1516 brelse(bh);
1517 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001518}
1519
1520/**
1521 * gfs2_dir_add - Add new filename into directory
1522 * @dip: The GFS2 inode
1523 * @filename: The new name
1524 * @inode: The inode number of the entry
1525 * @type: The type of the entry
1526 *
1527 * Returns: 0 on success, error code on failure
1528 */
1529
Steven Whitehousec7526662006-03-20 12:30:04 -05001530int gfs2_dir_add(struct inode *inode, const struct qstr *name,
1531 const struct gfs2_inum *inum, unsigned type)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001532{
Steven Whitehousec7526662006-03-20 12:30:04 -05001533 struct gfs2_inode *ip = inode->u.generic_ip;
1534 struct buffer_head *bh;
1535 struct gfs2_dirent *dent;
1536 struct gfs2_leaf *leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001537 int error;
1538
Steven Whitehousec7526662006-03-20 12:30:04 -05001539 while(1) {
1540 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
1541 &bh);
1542 if (dent) {
1543 if (IS_ERR(dent))
1544 return PTR_ERR(dent);
1545 dent = gfs2_init_dirent(inode, dent, name, bh);
1546 gfs2_inum_out(inum, (char *)&dent->de_inum);
1547 dent->de_type = cpu_to_be16(type);
1548 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
1549 leaf = (struct gfs2_leaf *)bh->b_data;
1550 leaf->lf_entries = cpu_to_be16(be16_to_cpu(leaf->lf_entries) + 1);
1551 }
1552 brelse(bh);
1553 error = gfs2_meta_inode_buffer(ip, &bh);
1554 if (error)
1555 break;
1556 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1557 ip->i_di.di_entries++;
1558 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
1559 gfs2_dinode_out(&ip->i_di, bh->b_data);
1560 brelse(bh);
1561 error = 0;
1562 break;
1563 }
1564 if (!(ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
1565 error = dir_make_exhash(inode);
1566 if (error)
1567 break;
1568 continue;
1569 }
1570 error = dir_split_leaf(inode, name);
1571 if (error == 0)
1572 continue;
1573 if (error != -ENOSPC)
1574 break;
1575 if (ip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) {
1576 error = dir_double_exhash(ip);
1577 if (error)
1578 break;
1579 error = dir_split_leaf(inode, name);
1580 if (error)
1581 break;
1582 continue;
1583 }
1584 error = dir_new_leaf(inode, name);
1585 if (!error)
1586 continue;
1587 error = -ENOSPC;
1588 break;
1589 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001590 return error;
1591}
1592
Steven Whitehousec7526662006-03-20 12:30:04 -05001593
David Teiglandb3b94fa2006-01-16 16:50:04 +00001594/**
1595 * gfs2_dir_del - Delete a directory entry
1596 * @dip: The GFS2 inode
1597 * @filename: The filename
1598 *
1599 * Returns: 0 on success, error code on failure
1600 */
1601
Steven Whitehousec7526662006-03-20 12:30:04 -05001602int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001603{
Steven Whitehousec7526662006-03-20 12:30:04 -05001604 struct gfs2_dirent *dent, *prev = NULL;
1605 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001606 int error;
1607
Steven Whitehousec7526662006-03-20 12:30:04 -05001608 /* Returns _either_ the entry (if its first in block) or the
1609 previous entry otherwise */
1610 dent = gfs2_dirent_search(dip->i_vnode, name, gfs2_dirent_prev, &bh);
1611 if (!dent) {
1612 gfs2_consist_inode(dip);
1613 return -EIO;
1614 }
1615 if (IS_ERR(dent)) {
1616 gfs2_consist_inode(dip);
1617 return PTR_ERR(dent);
1618 }
1619 /* If not first in block, adjust pointers accordingly */
Steven Whitehouse71b86f52006-03-28 14:14:04 -05001620 if (gfs2_dirent_find(dent, name, NULL) == 0) {
Steven Whitehousec7526662006-03-20 12:30:04 -05001621 prev = dent;
1622 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1623 }
1624
1625 dirent_del(dip, bh, prev, dent);
1626 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1627 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1628 u16 entries = be16_to_cpu(leaf->lf_entries);
1629 if (!entries)
1630 gfs2_consist_inode(dip);
1631 leaf->lf_entries = cpu_to_be16(--entries);
1632 brelse(bh);
1633 }
1634
1635 error = gfs2_meta_inode_buffer(dip, &bh);
1636 if (error)
1637 return error;
1638
1639 if (!dip->i_di.di_entries)
1640 gfs2_consist_inode(dip);
1641 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1642 dip->i_di.di_entries--;
1643 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1644 gfs2_dinode_out(&dip->i_di, bh->b_data);
1645 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001646
1647 return error;
1648}
1649
David Teiglandb3b94fa2006-01-16 16:50:04 +00001650/**
1651 * gfs2_dir_mvino - Change inode number of directory entry
1652 * @dip: The GFS2 inode
1653 * @filename:
1654 * @new_inode:
1655 *
1656 * This routine changes the inode number of a directory entry. It's used
1657 * by rename to change ".." when a directory is moved.
1658 * Assumes a glock is held on dvp.
1659 *
1660 * Returns: errno
1661 */
1662
Steven Whitehousec7526662006-03-20 12:30:04 -05001663int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001664 struct gfs2_inum *inum, unsigned int new_type)
1665{
Steven Whitehousec7526662006-03-20 12:30:04 -05001666 struct buffer_head *bh;
1667 struct gfs2_dirent *dent;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001668 int error;
1669
Steven Whitehousec7526662006-03-20 12:30:04 -05001670 dent = gfs2_dirent_search(dip->i_vnode, filename, gfs2_dirent_find, &bh);
1671 if (!dent) {
1672 gfs2_consist_inode(dip);
1673 return -EIO;
1674 }
1675 if (IS_ERR(dent))
1676 return PTR_ERR(dent);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001677
Steven Whitehousec7526662006-03-20 12:30:04 -05001678 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1679 gfs2_inum_out(inum, (char *)&dent->de_inum);
1680 dent->de_type = cpu_to_be16(new_type);
1681
1682 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1683 brelse(bh);
1684 error = gfs2_meta_inode_buffer(dip, &bh);
1685 if (error)
1686 return error;
1687 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1688 }
1689
1690 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1691 gfs2_dinode_out(&dip->i_di, bh->b_data);
1692 brelse(bh);
1693 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001694}
1695
1696/**
1697 * foreach_leaf - call a function for each leaf in a directory
1698 * @dip: the directory
1699 * @lc: the function to call for each each
1700 * @data: private data to pass to it
1701 *
1702 * Returns: errno
1703 */
1704
1705static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
1706{
1707 struct gfs2_sbd *sdp = dip->i_sbd;
1708 struct buffer_head *bh;
Steven Whitehousec7526662006-03-20 12:30:04 -05001709 struct gfs2_leaf *leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001710 uint32_t hsize, len;
1711 uint32_t ht_offset, lp_offset, ht_offset_cur = -1;
1712 uint32_t index = 0;
1713 uint64_t *lp;
1714 uint64_t leaf_no;
1715 int error = 0;
1716
1717 hsize = 1 << dip->i_di.di_depth;
1718 if (hsize * sizeof(uint64_t) != dip->i_di.di_size) {
1719 gfs2_consist_inode(dip);
1720 return -EIO;
1721 }
1722
1723 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1724 if (!lp)
1725 return -ENOMEM;
1726
1727 while (index < hsize) {
1728 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1729 ht_offset = index - lp_offset;
1730
1731 if (ht_offset_cur != ht_offset) {
Steven Whitehousee13940b2006-01-30 13:31:50 +00001732 error = gfs2_dir_read_data(dip, (char *)lp,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001733 ht_offset * sizeof(uint64_t),
1734 sdp->sd_hash_bsize);
1735 if (error != sdp->sd_hash_bsize) {
1736 if (error >= 0)
1737 error = -EIO;
1738 goto out;
1739 }
1740 ht_offset_cur = ht_offset;
1741 }
1742
1743 leaf_no = be64_to_cpu(lp[lp_offset]);
1744 if (leaf_no) {
1745 error = get_leaf(dip, leaf_no, &bh);
1746 if (error)
1747 goto out;
Steven Whitehousec7526662006-03-20 12:30:04 -05001748 leaf = (struct gfs2_leaf *)bh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001749 brelse(bh);
1750
Steven Whitehousec7526662006-03-20 12:30:04 -05001751 len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001752
1753 error = lc(dip, index, len, leaf_no, data);
1754 if (error)
1755 goto out;
1756
1757 index = (index & ~(len - 1)) + len;
1758 } else
1759 index++;
1760 }
1761
1762 if (index != hsize) {
1763 gfs2_consist_inode(dip);
1764 error = -EIO;
1765 }
1766
1767 out:
1768 kfree(lp);
1769
1770 return error;
1771}
1772
1773/**
1774 * leaf_dealloc - Deallocate a directory leaf
1775 * @dip: the directory
1776 * @index: the hash table offset in the directory
1777 * @len: the number of pointers to this leaf
1778 * @leaf_no: the leaf number
1779 * @data: not used
1780 *
1781 * Returns: errno
1782 */
1783
1784static int leaf_dealloc(struct gfs2_inode *dip, uint32_t index, uint32_t len,
1785 uint64_t leaf_no, void *data)
1786{
1787 struct gfs2_sbd *sdp = dip->i_sbd;
Steven Whitehousec7526662006-03-20 12:30:04 -05001788 struct gfs2_leaf *tmp_leaf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001789 struct gfs2_rgrp_list rlist;
1790 struct buffer_head *bh, *dibh;
Steven Whitehousec7526662006-03-20 12:30:04 -05001791 uint64_t blk, nblk;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001792 unsigned int rg_blocks = 0, l_blocks = 0;
1793 char *ht;
1794 unsigned int x, size = len * sizeof(uint64_t);
1795 int error;
1796
1797 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1798
1799 ht = kzalloc(size, GFP_KERNEL);
1800 if (!ht)
1801 return -ENOMEM;
1802
1803 gfs2_alloc_get(dip);
1804
1805 error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1806 if (error)
1807 goto out;
1808
1809 error = gfs2_rindex_hold(sdp, &dip->i_alloc.al_ri_gh);
1810 if (error)
1811 goto out_qs;
1812
1813 /* Count the number of leaves */
1814
Steven Whitehousec7526662006-03-20 12:30:04 -05001815 for (blk = leaf_no; blk; blk = nblk) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001816 error = get_leaf(dip, blk, &bh);
1817 if (error)
1818 goto out_rlist;
Steven Whitehousec7526662006-03-20 12:30:04 -05001819 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1820 nblk = be64_to_cpu(tmp_leaf->lf_next);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001821 brelse(bh);
1822
1823 gfs2_rlist_add(sdp, &rlist, blk);
1824 l_blocks++;
1825 }
1826
1827 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1828
1829 for (x = 0; x < rlist.rl_rgrps; x++) {
1830 struct gfs2_rgrpd *rgd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001831 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001832 rg_blocks += rgd->rd_ri.ri_length;
1833 }
1834
1835 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1836 if (error)
1837 goto out_rlist;
1838
1839 error = gfs2_trans_begin(sdp,
Steven Whitehouse5c676f62006-02-27 17:23:27 -05001840 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
David Teiglandb3b94fa2006-01-16 16:50:04 +00001841 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1842 if (error)
1843 goto out_rg_gunlock;
1844
Steven Whitehousec7526662006-03-20 12:30:04 -05001845 for (blk = leaf_no; blk; blk = nblk) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001846 error = get_leaf(dip, blk, &bh);
1847 if (error)
1848 goto out_end_trans;
Steven Whitehousec7526662006-03-20 12:30:04 -05001849 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1850 nblk = be64_to_cpu(tmp_leaf->lf_next);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001851 brelse(bh);
1852
1853 gfs2_free_meta(dip, blk, 1);
1854
1855 if (!dip->i_di.di_blocks)
1856 gfs2_consist_inode(dip);
1857 dip->i_di.di_blocks--;
1858 }
1859
Steven Whitehousee13940b2006-01-30 13:31:50 +00001860 error = gfs2_dir_write_data(dip, ht, index * sizeof(uint64_t), size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001861 if (error != size) {
1862 if (error >= 0)
1863 error = -EIO;
1864 goto out_end_trans;
1865 }
1866
1867 error = gfs2_meta_inode_buffer(dip, &dibh);
1868 if (error)
1869 goto out_end_trans;
1870
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001871 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001872 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1873 brelse(dibh);
1874
1875 out_end_trans:
1876 gfs2_trans_end(sdp);
1877
1878 out_rg_gunlock:
1879 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1880
1881 out_rlist:
1882 gfs2_rlist_free(&rlist);
1883 gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh);
1884
1885 out_qs:
1886 gfs2_quota_unhold(dip);
1887
1888 out:
1889 gfs2_alloc_put(dip);
1890 kfree(ht);
1891
1892 return error;
1893}
1894
1895/**
1896 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1897 * @dip: the directory
1898 *
1899 * Dealloc all on-disk directory leaves to FREEMETA state
1900 * Change on-disk inode type to "regular file"
1901 *
1902 * Returns: errno
1903 */
1904
1905int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
1906{
1907 struct gfs2_sbd *sdp = dip->i_sbd;
1908 struct buffer_head *bh;
1909 int error;
1910
1911 /* Dealloc on-disk leaves to FREEMETA state */
1912 error = foreach_leaf(dip, leaf_dealloc, NULL);
1913 if (error)
1914 return error;
1915
1916 /* Make this a regular file in case we crash.
1917 (We don't want to free these blocks a second time.) */
1918
1919 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1920 if (error)
1921 return error;
1922
1923 error = gfs2_meta_inode_buffer(dip, &bh);
1924 if (!error) {
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +00001925 gfs2_trans_add_bh(dip->i_gl, bh, 1);
Steven Whitehouse568f4c92006-02-27 12:00:42 -05001926 ((struct gfs2_dinode *)bh->b_data)->di_mode =
1927 cpu_to_be32(S_IFREG);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001928 brelse(bh);
1929 }
1930
1931 gfs2_trans_end(sdp);
1932
1933 return error;
1934}
1935
1936/**
1937 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
1938 * @ip: the file being written to
1939 * @filname: the filename that's going to be added
David Teiglandb3b94fa2006-01-16 16:50:04 +00001940 *
Steven Whitehousec7526662006-03-20 12:30:04 -05001941 * Returns: 1 if alloc required, 0 if not, -ve on error
David Teiglandb3b94fa2006-01-16 16:50:04 +00001942 */
1943
Steven Whitehousec7526662006-03-20 12:30:04 -05001944int gfs2_diradd_alloc_required(struct inode *inode,
1945 const struct qstr *name)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001946{
Steven Whitehousec7526662006-03-20 12:30:04 -05001947 struct gfs2_dirent *dent;
1948 struct buffer_head *bh;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001949
Steven Whitehousec7526662006-03-20 12:30:04 -05001950 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
1951 if (!dent)
1952 return 1;
1953 if (IS_ERR(dent))
1954 return PTR_ERR(dent);
1955 brelse(bh);
1956 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001957}
1958