[PATCH] 2.6.18: sb1250-mac: Broadcom PHY support
[linux-2.6/kvm.git] / fs / gfs2 / dir.c
blobe24af28b1a121e556ce19770682c4c9ae4df5fe1
1 /*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
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 version 2.
8 */
11 * Implements Extendible Hashing as described in:
12 * "Extendible Hashing" by Fagin, et al in
13 * __ACM Trans. on Database Systems__, Sept 1979.
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.
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.
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
39 * dip->i_di.di_flags & GFS2_DIF_EXHASH is true
41 * Otherwise, the dirents are "linear", within a single stuffed dinode block.
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.
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.
56 #include <linux/sched.h>
57 #include <linux/slab.h>
58 #include <linux/spinlock.h>
59 #include <linux/buffer_head.h>
60 #include <linux/sort.h>
61 #include <linux/gfs2_ondisk.h>
62 #include <linux/crc32.h>
63 #include <linux/vmalloc.h>
64 #include <linux/lm_interface.h>
66 #include "gfs2.h"
67 #include "incore.h"
68 #include "dir.h"
69 #include "glock.h"
70 #include "inode.h"
71 #include "meta_io.h"
72 #include "quota.h"
73 #include "rgrp.h"
74 #include "trans.h"
75 #include "bmap.h"
76 #include "util.h"
78 #define IS_LEAF 1 /* Hashed (leaf) directory */
79 #define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
81 #define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
82 #define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
84 typedef int (*leaf_call_t) (struct gfs2_inode *dip, u32 index, u32 len,
85 u64 leaf_no, void *data);
86 typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
87 const struct qstr *name, void *opaque);
90 int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
91 struct buffer_head **bhp)
93 struct buffer_head *bh;
95 bh = gfs2_meta_new(ip->i_gl, block);
96 gfs2_trans_add_bh(ip->i_gl, bh, 1);
97 gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
98 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
99 *bhp = bh;
100 return 0;
103 static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, u64 block,
104 struct buffer_head **bhp)
106 struct buffer_head *bh;
107 int error;
109 error = gfs2_meta_read(ip->i_gl, block, DIO_WAIT, &bh);
110 if (error)
111 return error;
112 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) {
113 brelse(bh);
114 return -EIO;
116 *bhp = bh;
117 return 0;
120 static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
121 unsigned int offset, unsigned int size)
123 struct buffer_head *dibh;
124 int error;
126 error = gfs2_meta_inode_buffer(ip, &dibh);
127 if (error)
128 return error;
130 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
131 memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
132 if (ip->i_di.di_size < offset + size)
133 ip->i_di.di_size = offset + size;
134 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
135 gfs2_dinode_out(&ip->i_di, dibh->b_data);
137 brelse(dibh);
139 return size;
145 * gfs2_dir_write_data - Write directory information to the inode
146 * @ip: The GFS2 inode
147 * @buf: The buffer containing information to be written
148 * @offset: The file offset to start writing at
149 * @size: The amount of data to write
151 * Returns: The number of bytes correctly written or error code
153 static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
154 u64 offset, unsigned int size)
156 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
157 struct buffer_head *dibh;
158 u64 lblock, dblock;
159 u32 extlen = 0;
160 unsigned int o;
161 int copied = 0;
162 int error = 0;
164 if (!size)
165 return 0;
167 if (gfs2_is_stuffed(ip) &&
168 offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
169 return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
170 size);
172 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
173 return -EINVAL;
175 if (gfs2_is_stuffed(ip)) {
176 error = gfs2_unstuff_dinode(ip, NULL);
177 if (error)
178 return error;
181 lblock = offset;
182 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
184 while (copied < size) {
185 unsigned int amount;
186 struct buffer_head *bh;
187 int new = 0;
189 amount = size - copied;
190 if (amount > sdp->sd_sb.sb_bsize - o)
191 amount = sdp->sd_sb.sb_bsize - o;
193 if (!extlen) {
194 new = 1;
195 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
196 &dblock, &extlen);
197 if (error)
198 goto fail;
199 error = -EIO;
200 if (gfs2_assert_withdraw(sdp, dblock))
201 goto fail;
204 if (amount == sdp->sd_jbsize || new)
205 error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
206 else
207 error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
209 if (error)
210 goto fail;
212 gfs2_trans_add_bh(ip->i_gl, bh, 1);
213 memcpy(bh->b_data + o, buf, amount);
214 brelse(bh);
216 buf += amount;
217 copied += amount;
218 lblock++;
219 dblock++;
220 extlen--;
222 o = sizeof(struct gfs2_meta_header);
225 out:
226 error = gfs2_meta_inode_buffer(ip, &dibh);
227 if (error)
228 return error;
230 if (ip->i_di.di_size < offset + copied)
231 ip->i_di.di_size = offset + copied;
232 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
234 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
235 gfs2_dinode_out(&ip->i_di, dibh->b_data);
236 brelse(dibh);
238 return copied;
239 fail:
240 if (copied)
241 goto out;
242 return error;
245 static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf,
246 u64 offset, unsigned int size)
248 struct buffer_head *dibh;
249 int error;
251 error = gfs2_meta_inode_buffer(ip, &dibh);
252 if (!error) {
253 offset += sizeof(struct gfs2_dinode);
254 memcpy(buf, dibh->b_data + offset, size);
255 brelse(dibh);
258 return (error) ? error : size;
263 * gfs2_dir_read_data - Read a data from a directory inode
264 * @ip: The GFS2 Inode
265 * @buf: The buffer to place result into
266 * @offset: File offset to begin jdata_readng from
267 * @size: Amount of data to transfer
269 * Returns: The amount of data actually copied or the error
271 static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf, u64 offset,
272 unsigned int size, unsigned ra)
274 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
275 u64 lblock, dblock;
276 u32 extlen = 0;
277 unsigned int o;
278 int copied = 0;
279 int error = 0;
281 if (offset >= ip->i_di.di_size)
282 return 0;
284 if (offset + size > ip->i_di.di_size)
285 size = ip->i_di.di_size - offset;
287 if (!size)
288 return 0;
290 if (gfs2_is_stuffed(ip))
291 return gfs2_dir_read_stuffed(ip, buf, offset, size);
293 if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
294 return -EINVAL;
296 lblock = offset;
297 o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
299 while (copied < size) {
300 unsigned int amount;
301 struct buffer_head *bh;
302 int new;
304 amount = size - copied;
305 if (amount > sdp->sd_sb.sb_bsize - o)
306 amount = sdp->sd_sb.sb_bsize - o;
308 if (!extlen) {
309 new = 0;
310 error = gfs2_extent_map(&ip->i_inode, lblock, &new,
311 &dblock, &extlen);
312 if (error || !dblock)
313 goto fail;
314 BUG_ON(extlen < 1);
315 if (!ra)
316 extlen = 1;
317 bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
318 } else {
319 error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, &bh);
320 if (error)
321 goto fail;
323 error = gfs2_metatype_check(sdp, bh, GFS2_METATYPE_JD);
324 if (error) {
325 brelse(bh);
326 goto fail;
328 dblock++;
329 extlen--;
330 memcpy(buf, bh->b_data + o, amount);
331 brelse(bh);
332 buf += amount;
333 copied += amount;
334 lblock++;
335 o = sizeof(struct gfs2_meta_header);
338 return copied;
339 fail:
340 return (copied) ? copied : error;
343 static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
344 const struct qstr *name, int ret)
346 if (dent->de_inum.no_addr != 0 &&
347 be32_to_cpu(dent->de_hash) == name->hash &&
348 be16_to_cpu(dent->de_name_len) == name->len &&
349 memcmp(dent+1, name->name, name->len) == 0)
350 return ret;
351 return 0;
354 static int gfs2_dirent_find(const struct gfs2_dirent *dent,
355 const struct qstr *name,
356 void *opaque)
358 return __gfs2_dirent_find(dent, name, 1);
361 static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
362 const struct qstr *name,
363 void *opaque)
365 return __gfs2_dirent_find(dent, name, 2);
369 * name->name holds ptr to start of block.
370 * name->len holds size of block.
372 static int gfs2_dirent_last(const struct gfs2_dirent *dent,
373 const struct qstr *name,
374 void *opaque)
376 const char *start = name->name;
377 const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
378 if (name->len == (end - start))
379 return 1;
380 return 0;
383 static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
384 const struct qstr *name,
385 void *opaque)
387 unsigned required = GFS2_DIRENT_SIZE(name->len);
388 unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
389 unsigned totlen = be16_to_cpu(dent->de_rec_len);
391 if (!dent->de_inum.no_addr)
392 actual = GFS2_DIRENT_SIZE(0);
393 if (totlen - actual >= required)
394 return 1;
395 return 0;
398 struct dirent_gather {
399 const struct gfs2_dirent **pdent;
400 unsigned offset;
403 static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
404 const struct qstr *name,
405 void *opaque)
407 struct dirent_gather *g = opaque;
408 if (dent->de_inum.no_addr) {
409 g->pdent[g->offset++] = dent;
411 return 0;
415 * Other possible things to check:
416 * - Inode located within filesystem size (and on valid block)
417 * - Valid directory entry type
418 * Not sure how heavy-weight we want to make this... could also check
419 * hash is correct for example, but that would take a lot of extra time.
420 * For now the most important thing is to check that the various sizes
421 * are correct.
423 static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
424 unsigned int size, unsigned int len, int first)
426 const char *msg = "gfs2_dirent too small";
427 if (unlikely(size < sizeof(struct gfs2_dirent)))
428 goto error;
429 msg = "gfs2_dirent misaligned";
430 if (unlikely(offset & 0x7))
431 goto error;
432 msg = "gfs2_dirent points beyond end of block";
433 if (unlikely(offset + size > len))
434 goto error;
435 msg = "zero inode number";
436 if (unlikely(!first && !dent->de_inum.no_addr))
437 goto error;
438 msg = "name length is greater than space in dirent";
439 if (dent->de_inum.no_addr &&
440 unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
441 size))
442 goto error;
443 return 0;
444 error:
445 printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
446 first ? "first in block" : "not first in block");
447 return -EIO;
450 static int gfs2_dirent_offset(const void *buf)
452 const struct gfs2_meta_header *h = buf;
453 int offset;
455 BUG_ON(buf == NULL);
457 switch(be32_to_cpu(h->mh_type)) {
458 case GFS2_METATYPE_LF:
459 offset = sizeof(struct gfs2_leaf);
460 break;
461 case GFS2_METATYPE_DI:
462 offset = sizeof(struct gfs2_dinode);
463 break;
464 default:
465 goto wrong_type;
467 return offset;
468 wrong_type:
469 printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
470 be32_to_cpu(h->mh_type));
471 return -1;
474 static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf,
475 unsigned int len, gfs2_dscan_t scan,
476 const struct qstr *name,
477 void *opaque)
479 struct gfs2_dirent *dent, *prev;
480 unsigned offset;
481 unsigned size;
482 int ret = 0;
484 ret = gfs2_dirent_offset(buf);
485 if (ret < 0)
486 goto consist_inode;
488 offset = ret;
489 prev = NULL;
490 dent = buf + offset;
491 size = be16_to_cpu(dent->de_rec_len);
492 if (gfs2_check_dirent(dent, offset, size, len, 1))
493 goto consist_inode;
494 do {
495 ret = scan(dent, name, opaque);
496 if (ret)
497 break;
498 offset += size;
499 if (offset == len)
500 break;
501 prev = dent;
502 dent = buf + offset;
503 size = be16_to_cpu(dent->de_rec_len);
504 if (gfs2_check_dirent(dent, offset, size, len, 0))
505 goto consist_inode;
506 } while(1);
508 switch(ret) {
509 case 0:
510 return NULL;
511 case 1:
512 return dent;
513 case 2:
514 return prev ? prev : dent;
515 default:
516 BUG_ON(ret > 0);
517 return ERR_PTR(ret);
520 consist_inode:
521 gfs2_consist_inode(GFS2_I(inode));
522 return ERR_PTR(-EIO);
527 * dirent_first - Return the first dirent
528 * @dip: the directory
529 * @bh: The buffer
530 * @dent: Pointer to list of dirents
532 * return first dirent whether bh points to leaf or stuffed dinode
534 * Returns: IS_LEAF, IS_DINODE, or -errno
537 static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
538 struct gfs2_dirent **dent)
540 struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data;
542 if (be32_to_cpu(h->mh_type) == GFS2_METATYPE_LF) {
543 if (gfs2_meta_check(GFS2_SB(&dip->i_inode), bh))
544 return -EIO;
545 *dent = (struct gfs2_dirent *)(bh->b_data +
546 sizeof(struct gfs2_leaf));
547 return IS_LEAF;
548 } else {
549 if (gfs2_metatype_check(GFS2_SB(&dip->i_inode), bh, GFS2_METATYPE_DI))
550 return -EIO;
551 *dent = (struct gfs2_dirent *)(bh->b_data +
552 sizeof(struct gfs2_dinode));
553 return IS_DINODE;
557 static int dirent_check_reclen(struct gfs2_inode *dip,
558 const struct gfs2_dirent *d, const void *end_p)
560 const void *ptr = d;
561 u16 rec_len = be16_to_cpu(d->de_rec_len);
563 if (unlikely(rec_len < sizeof(struct gfs2_dirent)))
564 goto broken;
565 ptr += rec_len;
566 if (ptr < end_p)
567 return rec_len;
568 if (ptr == end_p)
569 return -ENOENT;
570 broken:
571 gfs2_consist_inode(dip);
572 return -EIO;
576 * dirent_next - Next dirent
577 * @dip: the directory
578 * @bh: The buffer
579 * @dent: Pointer to list of dirents
581 * Returns: 0 on success, error code otherwise
584 static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
585 struct gfs2_dirent **dent)
587 struct gfs2_dirent *cur = *dent, *tmp;
588 char *bh_end = bh->b_data + bh->b_size;
589 int ret;
591 ret = dirent_check_reclen(dip, cur, bh_end);
592 if (ret < 0)
593 return ret;
595 tmp = (void *)cur + ret;
596 ret = dirent_check_reclen(dip, tmp, bh_end);
597 if (ret == -EIO)
598 return ret;
600 /* Only the first dent could ever have de_inum.no_addr == 0 */
601 if (!tmp->de_inum.no_addr) {
602 gfs2_consist_inode(dip);
603 return -EIO;
606 *dent = tmp;
607 return 0;
611 * dirent_del - Delete a dirent
612 * @dip: The GFS2 inode
613 * @bh: The buffer
614 * @prev: The previous dirent
615 * @cur: The current dirent
619 static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
620 struct gfs2_dirent *prev, struct gfs2_dirent *cur)
622 u16 cur_rec_len, prev_rec_len;
624 if (!cur->de_inum.no_addr) {
625 gfs2_consist_inode(dip);
626 return;
629 gfs2_trans_add_bh(dip->i_gl, bh, 1);
631 /* If there is no prev entry, this is the first entry in the block.
632 The de_rec_len is already as big as it needs to be. Just zero
633 out the inode number and return. */
635 if (!prev) {
636 cur->de_inum.no_addr = 0; /* No endianess worries */
637 return;
640 /* Combine this dentry with the previous one. */
642 prev_rec_len = be16_to_cpu(prev->de_rec_len);
643 cur_rec_len = be16_to_cpu(cur->de_rec_len);
645 if ((char *)prev + prev_rec_len != (char *)cur)
646 gfs2_consist_inode(dip);
647 if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
648 gfs2_consist_inode(dip);
650 prev_rec_len += cur_rec_len;
651 prev->de_rec_len = cpu_to_be16(prev_rec_len);
655 * Takes a dent from which to grab space as an argument. Returns the
656 * newly created dent.
658 static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
659 struct gfs2_dirent *dent,
660 const struct qstr *name,
661 struct buffer_head *bh)
663 struct gfs2_inode *ip = GFS2_I(inode);
664 struct gfs2_dirent *ndent;
665 unsigned offset = 0, totlen;
667 if (dent->de_inum.no_addr)
668 offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
669 totlen = be16_to_cpu(dent->de_rec_len);
670 BUG_ON(offset + name->len > totlen);
671 gfs2_trans_add_bh(ip->i_gl, bh, 1);
672 ndent = (struct gfs2_dirent *)((char *)dent + offset);
673 dent->de_rec_len = cpu_to_be16(offset);
674 gfs2_qstr2dirent(name, totlen - offset, ndent);
675 return ndent;
678 static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
679 struct buffer_head *bh,
680 const struct qstr *name)
682 struct gfs2_dirent *dent;
683 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
684 gfs2_dirent_find_space, name, NULL);
685 if (!dent || IS_ERR(dent))
686 return dent;
687 return gfs2_init_dirent(inode, dent, name, bh);
690 static int get_leaf(struct gfs2_inode *dip, u64 leaf_no,
691 struct buffer_head **bhp)
693 int error;
695 error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, bhp);
696 if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
697 /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
698 error = -EIO;
701 return error;
705 * get_leaf_nr - Get a leaf number associated with the index
706 * @dip: The GFS2 inode
707 * @index:
708 * @leaf_out:
710 * Returns: 0 on success, error code otherwise
713 static int get_leaf_nr(struct gfs2_inode *dip, u32 index,
714 u64 *leaf_out)
716 u64 leaf_no;
717 int error;
719 error = gfs2_dir_read_data(dip, (char *)&leaf_no,
720 index * sizeof(u64),
721 sizeof(u64), 0);
722 if (error != sizeof(u64))
723 return (error < 0) ? error : -EIO;
725 *leaf_out = be64_to_cpu(leaf_no);
727 return 0;
730 static int get_first_leaf(struct gfs2_inode *dip, u32 index,
731 struct buffer_head **bh_out)
733 u64 leaf_no;
734 int error;
736 error = get_leaf_nr(dip, index, &leaf_no);
737 if (!error)
738 error = get_leaf(dip, leaf_no, bh_out);
740 return error;
743 static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
744 const struct qstr *name,
745 gfs2_dscan_t scan,
746 struct buffer_head **pbh)
748 struct buffer_head *bh;
749 struct gfs2_dirent *dent;
750 struct gfs2_inode *ip = GFS2_I(inode);
751 int error;
753 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
754 struct gfs2_leaf *leaf;
755 unsigned hsize = 1 << ip->i_di.di_depth;
756 unsigned index;
757 u64 ln;
758 if (hsize * sizeof(u64) != ip->i_di.di_size) {
759 gfs2_consist_inode(ip);
760 return ERR_PTR(-EIO);
763 index = name->hash >> (32 - ip->i_di.di_depth);
764 error = get_first_leaf(ip, index, &bh);
765 if (error)
766 return ERR_PTR(error);
767 do {
768 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
769 scan, name, NULL);
770 if (dent)
771 goto got_dent;
772 leaf = (struct gfs2_leaf *)bh->b_data;
773 ln = be64_to_cpu(leaf->lf_next);
774 brelse(bh);
775 if (!ln)
776 break;
778 error = get_leaf(ip, ln, &bh);
779 } while(!error);
781 return error ? ERR_PTR(error) : NULL;
785 error = gfs2_meta_inode_buffer(ip, &bh);
786 if (error)
787 return ERR_PTR(error);
788 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
789 got_dent:
790 if (unlikely(dent == NULL || IS_ERR(dent))) {
791 brelse(bh);
792 bh = NULL;
794 *pbh = bh;
795 return dent;
798 static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
800 struct gfs2_inode *ip = GFS2_I(inode);
801 u64 bn = gfs2_alloc_meta(ip);
802 struct buffer_head *bh = gfs2_meta_new(ip->i_gl, bn);
803 struct gfs2_leaf *leaf;
804 struct gfs2_dirent *dent;
805 struct qstr name = { .name = "", .len = 0, .hash = 0 };
806 if (!bh)
807 return NULL;
809 gfs2_trans_add_bh(ip->i_gl, bh, 1);
810 gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
811 leaf = (struct gfs2_leaf *)bh->b_data;
812 leaf->lf_depth = cpu_to_be16(depth);
813 leaf->lf_entries = 0;
814 leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE);
815 leaf->lf_next = 0;
816 memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
817 dent = (struct gfs2_dirent *)(leaf+1);
818 gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
819 *pbh = bh;
820 return leaf;
824 * dir_make_exhash - Convert a stuffed directory into an ExHash directory
825 * @dip: The GFS2 inode
827 * Returns: 0 on success, error code otherwise
830 static int dir_make_exhash(struct inode *inode)
832 struct gfs2_inode *dip = GFS2_I(inode);
833 struct gfs2_sbd *sdp = GFS2_SB(inode);
834 struct gfs2_dirent *dent;
835 struct qstr args;
836 struct buffer_head *bh, *dibh;
837 struct gfs2_leaf *leaf;
838 int y;
839 u32 x;
840 u64 *lp, bn;
841 int error;
843 error = gfs2_meta_inode_buffer(dip, &dibh);
844 if (error)
845 return error;
847 /* Turn over a new leaf */
849 leaf = new_leaf(inode, &bh, 0);
850 if (!leaf)
851 return -ENOSPC;
852 bn = bh->b_blocknr;
854 gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16));
855 leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries);
857 /* Copy dirents */
859 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
860 sizeof(struct gfs2_dinode));
862 /* Find last entry */
864 x = 0;
865 args.len = bh->b_size - sizeof(struct gfs2_dinode) +
866 sizeof(struct gfs2_leaf);
867 args.name = bh->b_data;
868 dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
869 gfs2_dirent_last, &args, NULL);
870 if (!dent) {
871 brelse(bh);
872 brelse(dibh);
873 return -EIO;
875 if (IS_ERR(dent)) {
876 brelse(bh);
877 brelse(dibh);
878 return PTR_ERR(dent);
881 /* Adjust the last dirent's record length
882 (Remember that dent still points to the last entry.) */
884 dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
885 sizeof(struct gfs2_dinode) -
886 sizeof(struct gfs2_leaf));
888 brelse(bh);
890 /* We're done with the new leaf block, now setup the new
891 hash table. */
893 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
894 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
896 lp = (u64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
898 for (x = sdp->sd_hash_ptrs; x--; lp++)
899 *lp = cpu_to_be64(bn);
901 dip->i_di.di_size = sdp->sd_sb.sb_bsize / 2;
902 dip->i_di.di_blocks++;
903 dip->i_di.di_flags |= GFS2_DIF_EXHASH;
904 dip->i_di.di_payload_format = 0;
906 for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
907 dip->i_di.di_depth = y;
909 gfs2_dinode_out(&dip->i_di, dibh->b_data);
911 brelse(dibh);
913 return 0;
917 * dir_split_leaf - Split a leaf block into two
918 * @dip: The GFS2 inode
919 * @index:
920 * @leaf_no:
922 * Returns: 0 on success, error code on failure
925 static int dir_split_leaf(struct inode *inode, const struct qstr *name)
927 struct gfs2_inode *dip = GFS2_I(inode);
928 struct buffer_head *nbh, *obh, *dibh;
929 struct gfs2_leaf *nleaf, *oleaf;
930 struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
931 u32 start, len, half_len, divider;
932 u64 bn, *lp, leaf_no;
933 u32 index;
934 int x, moved = 0;
935 int error;
937 index = name->hash >> (32 - dip->i_di.di_depth);
938 error = get_leaf_nr(dip, index, &leaf_no);
939 if (error)
940 return error;
942 /* Get the old leaf block */
943 error = get_leaf(dip, leaf_no, &obh);
944 if (error)
945 return error;
947 oleaf = (struct gfs2_leaf *)obh->b_data;
948 if (dip->i_di.di_depth == be16_to_cpu(oleaf->lf_depth)) {
949 brelse(obh);
950 return 1; /* can't split */
953 gfs2_trans_add_bh(dip->i_gl, obh, 1);
955 nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
956 if (!nleaf) {
957 brelse(obh);
958 return -ENOSPC;
960 bn = nbh->b_blocknr;
962 /* Compute the start and len of leaf pointers in the hash table. */
963 len = 1 << (dip->i_di.di_depth - be16_to_cpu(oleaf->lf_depth));
964 half_len = len >> 1;
965 if (!half_len) {
966 printk(KERN_WARNING "di_depth %u lf_depth %u index %u\n", dip->i_di.di_depth, be16_to_cpu(oleaf->lf_depth), index);
967 gfs2_consist_inode(dip);
968 error = -EIO;
969 goto fail_brelse;
972 start = (index & ~(len - 1));
974 /* Change the pointers.
975 Don't bother distinguishing stuffed from non-stuffed.
976 This code is complicated enough already. */
977 lp = kmalloc(half_len * sizeof(u64), GFP_NOFS | __GFP_NOFAIL);
978 /* Change the pointers */
979 for (x = 0; x < half_len; x++)
980 lp[x] = cpu_to_be64(bn);
982 error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
983 half_len * sizeof(u64));
984 if (error != half_len * sizeof(u64)) {
985 if (error >= 0)
986 error = -EIO;
987 goto fail_lpfree;
990 kfree(lp);
992 /* Compute the divider */
993 divider = (start + half_len) << (32 - dip->i_di.di_depth);
995 /* Copy the entries */
996 dirent_first(dip, obh, &dent);
998 do {
999 next = dent;
1000 if (dirent_next(dip, obh, &next))
1001 next = NULL;
1003 if (dent->de_inum.no_addr &&
1004 be32_to_cpu(dent->de_hash) < divider) {
1005 struct qstr str;
1006 str.name = (char*)(dent+1);
1007 str.len = be16_to_cpu(dent->de_name_len);
1008 str.hash = be32_to_cpu(dent->de_hash);
1009 new = gfs2_dirent_alloc(inode, nbh, &str);
1010 if (IS_ERR(new)) {
1011 error = PTR_ERR(new);
1012 break;
1015 new->de_inum = dent->de_inum; /* No endian worries */
1016 new->de_type = dent->de_type; /* No endian worries */
1017 nleaf->lf_entries = cpu_to_be16(be16_to_cpu(nleaf->lf_entries)+1);
1019 dirent_del(dip, obh, prev, dent);
1021 if (!oleaf->lf_entries)
1022 gfs2_consist_inode(dip);
1023 oleaf->lf_entries = cpu_to_be16(be16_to_cpu(oleaf->lf_entries)-1);
1025 if (!prev)
1026 prev = dent;
1028 moved = 1;
1029 } else {
1030 prev = dent;
1032 dent = next;
1033 } while (dent);
1035 oleaf->lf_depth = nleaf->lf_depth;
1037 error = gfs2_meta_inode_buffer(dip, &dibh);
1038 if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
1039 dip->i_di.di_blocks++;
1040 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1041 brelse(dibh);
1044 brelse(obh);
1045 brelse(nbh);
1047 return error;
1049 fail_lpfree:
1050 kfree(lp);
1052 fail_brelse:
1053 brelse(obh);
1054 brelse(nbh);
1055 return error;
1059 * dir_double_exhash - Double size of ExHash table
1060 * @dip: The GFS2 dinode
1062 * Returns: 0 on success, error code on failure
1065 static int dir_double_exhash(struct gfs2_inode *dip)
1067 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1068 struct buffer_head *dibh;
1069 u32 hsize;
1070 u64 *buf;
1071 u64 *from, *to;
1072 u64 block;
1073 int x;
1074 int error = 0;
1076 hsize = 1 << dip->i_di.di_depth;
1077 if (hsize * sizeof(u64) != dip->i_di.di_size) {
1078 gfs2_consist_inode(dip);
1079 return -EIO;
1082 /* Allocate both the "from" and "to" buffers in one big chunk */
1084 buf = kcalloc(3, sdp->sd_hash_bsize, GFP_KERNEL | __GFP_NOFAIL);
1086 for (block = dip->i_di.di_size >> sdp->sd_hash_bsize_shift; block--;) {
1087 error = gfs2_dir_read_data(dip, (char *)buf,
1088 block * sdp->sd_hash_bsize,
1089 sdp->sd_hash_bsize, 1);
1090 if (error != sdp->sd_hash_bsize) {
1091 if (error >= 0)
1092 error = -EIO;
1093 goto fail;
1096 from = buf;
1097 to = (u64 *)((char *)buf + sdp->sd_hash_bsize);
1099 for (x = sdp->sd_hash_ptrs; x--; from++) {
1100 *to++ = *from; /* No endianess worries */
1101 *to++ = *from;
1104 error = gfs2_dir_write_data(dip,
1105 (char *)buf + sdp->sd_hash_bsize,
1106 block * sdp->sd_sb.sb_bsize,
1107 sdp->sd_sb.sb_bsize);
1108 if (error != sdp->sd_sb.sb_bsize) {
1109 if (error >= 0)
1110 error = -EIO;
1111 goto fail;
1115 kfree(buf);
1117 error = gfs2_meta_inode_buffer(dip, &dibh);
1118 if (!gfs2_assert_withdraw(sdp, !error)) {
1119 dip->i_di.di_depth++;
1120 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1121 brelse(dibh);
1124 return error;
1126 fail:
1127 kfree(buf);
1128 return error;
1132 * compare_dents - compare directory entries by hash value
1133 * @a: first dent
1134 * @b: second dent
1136 * When comparing the hash entries of @a to @b:
1137 * gt: returns 1
1138 * lt: returns -1
1139 * eq: returns 0
1142 static int compare_dents(const void *a, const void *b)
1144 const struct gfs2_dirent *dent_a, *dent_b;
1145 u32 hash_a, hash_b;
1146 int ret = 0;
1148 dent_a = *(const struct gfs2_dirent **)a;
1149 hash_a = be32_to_cpu(dent_a->de_hash);
1151 dent_b = *(const struct gfs2_dirent **)b;
1152 hash_b = be32_to_cpu(dent_b->de_hash);
1154 if (hash_a > hash_b)
1155 ret = 1;
1156 else if (hash_a < hash_b)
1157 ret = -1;
1158 else {
1159 unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
1160 unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
1162 if (len_a > len_b)
1163 ret = 1;
1164 else if (len_a < len_b)
1165 ret = -1;
1166 else
1167 ret = memcmp(dent_a + 1, dent_b + 1, len_a);
1170 return ret;
1174 * do_filldir_main - read out directory entries
1175 * @dip: The GFS2 inode
1176 * @offset: The offset in the file to read from
1177 * @opaque: opaque data to pass to filldir
1178 * @filldir: The function to pass entries to
1179 * @darr: an array of struct gfs2_dirent pointers to read
1180 * @entries: the number of entries in darr
1181 * @copied: pointer to int that's non-zero if a entry has been copied out
1183 * Jump through some hoops to make sure that if there are hash collsions,
1184 * they are read out at the beginning of a buffer. We want to minimize
1185 * the possibility that they will fall into different readdir buffers or
1186 * that someone will want to seek to that location.
1188 * Returns: errno, >0 on exception from filldir
1191 static int do_filldir_main(struct gfs2_inode *dip, u64 *offset,
1192 void *opaque, gfs2_filldir_t filldir,
1193 const struct gfs2_dirent **darr, u32 entries,
1194 int *copied)
1196 const struct gfs2_dirent *dent, *dent_next;
1197 struct gfs2_inum inum;
1198 u64 off, off_next;
1199 unsigned int x, y;
1200 int run = 0;
1201 int error = 0;
1203 sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
1205 dent_next = darr[0];
1206 off_next = be32_to_cpu(dent_next->de_hash);
1207 off_next = gfs2_disk_hash2offset(off_next);
1209 for (x = 0, y = 1; x < entries; x++, y++) {
1210 dent = dent_next;
1211 off = off_next;
1213 if (y < entries) {
1214 dent_next = darr[y];
1215 off_next = be32_to_cpu(dent_next->de_hash);
1216 off_next = gfs2_disk_hash2offset(off_next);
1218 if (off < *offset)
1219 continue;
1220 *offset = off;
1222 if (off_next == off) {
1223 if (*copied && !run)
1224 return 1;
1225 run = 1;
1226 } else
1227 run = 0;
1228 } else {
1229 if (off < *offset)
1230 continue;
1231 *offset = off;
1234 gfs2_inum_in(&inum, (char *)&dent->de_inum);
1236 error = filldir(opaque, (const char *)(dent + 1),
1237 be16_to_cpu(dent->de_name_len),
1238 off, &inum,
1239 be16_to_cpu(dent->de_type));
1240 if (error)
1241 return 1;
1243 *copied = 1;
1246 /* Increment the *offset by one, so the next time we come into the
1247 do_filldir fxn, we get the next entry instead of the last one in the
1248 current leaf */
1250 (*offset)++;
1252 return 0;
1255 static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
1256 gfs2_filldir_t filldir, int *copied,
1257 unsigned *depth, u64 leaf_no)
1259 struct gfs2_inode *ip = GFS2_I(inode);
1260 struct buffer_head *bh;
1261 struct gfs2_leaf *lf;
1262 unsigned entries = 0;
1263 unsigned leaves = 0;
1264 const struct gfs2_dirent **darr, *dent;
1265 struct dirent_gather g;
1266 struct buffer_head **larr;
1267 int leaf = 0;
1268 int error, i;
1269 u64 lfn = leaf_no;
1271 do {
1272 error = get_leaf(ip, lfn, &bh);
1273 if (error)
1274 goto out;
1275 lf = (struct gfs2_leaf *)bh->b_data;
1276 if (leaves == 0)
1277 *depth = be16_to_cpu(lf->lf_depth);
1278 entries += be16_to_cpu(lf->lf_entries);
1279 leaves++;
1280 lfn = be64_to_cpu(lf->lf_next);
1281 brelse(bh);
1282 } while(lfn);
1284 if (!entries)
1285 return 0;
1287 error = -ENOMEM;
1288 larr = vmalloc((leaves + entries) * sizeof(void *));
1289 if (!larr)
1290 goto out;
1291 darr = (const struct gfs2_dirent **)(larr + leaves);
1292 g.pdent = darr;
1293 g.offset = 0;
1294 lfn = leaf_no;
1296 do {
1297 error = get_leaf(ip, lfn, &bh);
1298 if (error)
1299 goto out_kfree;
1300 lf = (struct gfs2_leaf *)bh->b_data;
1301 lfn = be64_to_cpu(lf->lf_next);
1302 if (lf->lf_entries) {
1303 dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
1304 gfs2_dirent_gather, NULL, &g);
1305 error = PTR_ERR(dent);
1306 if (IS_ERR(dent)) {
1307 goto out_kfree;
1309 error = 0;
1310 larr[leaf++] = bh;
1311 } else {
1312 brelse(bh);
1314 } while(lfn);
1316 error = do_filldir_main(ip, offset, opaque, filldir, darr,
1317 entries, copied);
1318 out_kfree:
1319 for(i = 0; i < leaf; i++)
1320 brelse(larr[i]);
1321 vfree(larr);
1322 out:
1323 return error;
1327 * dir_e_read - Reads the entries from a directory into a filldir buffer
1328 * @dip: dinode pointer
1329 * @offset: the hash of the last entry read shifted to the right once
1330 * @opaque: buffer for the filldir function to fill
1331 * @filldir: points to the filldir function to use
1333 * Returns: errno
1336 static int dir_e_read(struct inode *inode, u64 *offset, void *opaque,
1337 gfs2_filldir_t filldir)
1339 struct gfs2_inode *dip = GFS2_I(inode);
1340 struct gfs2_sbd *sdp = GFS2_SB(inode);
1341 u32 hsize, len = 0;
1342 u32 ht_offset, lp_offset, ht_offset_cur = -1;
1343 u32 hash, index;
1344 u64 *lp;
1345 int copied = 0;
1346 int error = 0;
1347 unsigned depth = 0;
1349 hsize = 1 << dip->i_di.di_depth;
1350 if (hsize * sizeof(u64) != dip->i_di.di_size) {
1351 gfs2_consist_inode(dip);
1352 return -EIO;
1355 hash = gfs2_dir_offset2hash(*offset);
1356 index = hash >> (32 - dip->i_di.di_depth);
1358 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1359 if (!lp)
1360 return -ENOMEM;
1362 while (index < hsize) {
1363 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1364 ht_offset = index - lp_offset;
1366 if (ht_offset_cur != ht_offset) {
1367 error = gfs2_dir_read_data(dip, (char *)lp,
1368 ht_offset * sizeof(u64),
1369 sdp->sd_hash_bsize, 1);
1370 if (error != sdp->sd_hash_bsize) {
1371 if (error >= 0)
1372 error = -EIO;
1373 goto out;
1375 ht_offset_cur = ht_offset;
1378 error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
1379 &copied, &depth,
1380 be64_to_cpu(lp[lp_offset]));
1381 if (error)
1382 break;
1384 len = 1 << (dip->i_di.di_depth - depth);
1385 index = (index & ~(len - 1)) + len;
1388 out:
1389 kfree(lp);
1390 if (error > 0)
1391 error = 0;
1392 return error;
1395 int gfs2_dir_read(struct inode *inode, u64 *offset, void *opaque,
1396 gfs2_filldir_t filldir)
1398 struct gfs2_inode *dip = GFS2_I(inode);
1399 struct dirent_gather g;
1400 const struct gfs2_dirent **darr, *dent;
1401 struct buffer_head *dibh;
1402 int copied = 0;
1403 int error;
1405 if (!dip->i_di.di_entries)
1406 return 0;
1408 if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
1409 return dir_e_read(inode, offset, opaque, filldir);
1411 if (!gfs2_is_stuffed(dip)) {
1412 gfs2_consist_inode(dip);
1413 return -EIO;
1416 error = gfs2_meta_inode_buffer(dip, &dibh);
1417 if (error)
1418 return error;
1420 error = -ENOMEM;
1421 darr = kmalloc(dip->i_di.di_entries * sizeof(struct gfs2_dirent *),
1422 GFP_KERNEL);
1423 if (darr) {
1424 g.pdent = darr;
1425 g.offset = 0;
1426 dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
1427 gfs2_dirent_gather, NULL, &g);
1428 if (IS_ERR(dent)) {
1429 error = PTR_ERR(dent);
1430 goto out;
1432 error = do_filldir_main(dip, offset, opaque, filldir, darr,
1433 dip->i_di.di_entries, &copied);
1434 out:
1435 kfree(darr);
1438 if (error > 0)
1439 error = 0;
1441 brelse(dibh);
1443 return error;
1447 * gfs2_dir_search - Search a directory
1448 * @dip: The GFS2 inode
1449 * @filename:
1450 * @inode:
1452 * This routine searches a directory for a file or another directory.
1453 * Assumes a glock is held on dip.
1455 * Returns: errno
1458 int gfs2_dir_search(struct inode *dir, const struct qstr *name,
1459 struct gfs2_inum *inum, unsigned int *type)
1461 struct buffer_head *bh;
1462 struct gfs2_dirent *dent;
1464 dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
1465 if (dent) {
1466 if (IS_ERR(dent))
1467 return PTR_ERR(dent);
1468 if (inum)
1469 gfs2_inum_in(inum, (char *)&dent->de_inum);
1470 if (type)
1471 *type = be16_to_cpu(dent->de_type);
1472 brelse(bh);
1473 return 0;
1475 return -ENOENT;
1478 static int dir_new_leaf(struct inode *inode, const struct qstr *name)
1480 struct buffer_head *bh, *obh;
1481 struct gfs2_inode *ip = GFS2_I(inode);
1482 struct gfs2_leaf *leaf, *oleaf;
1483 int error;
1484 u32 index;
1485 u64 bn;
1487 index = name->hash >> (32 - ip->i_di.di_depth);
1488 error = get_first_leaf(ip, index, &obh);
1489 if (error)
1490 return error;
1491 do {
1492 oleaf = (struct gfs2_leaf *)obh->b_data;
1493 bn = be64_to_cpu(oleaf->lf_next);
1494 if (!bn)
1495 break;
1496 brelse(obh);
1497 error = get_leaf(ip, bn, &obh);
1498 if (error)
1499 return error;
1500 } while(1);
1502 gfs2_trans_add_bh(ip->i_gl, obh, 1);
1504 leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
1505 if (!leaf) {
1506 brelse(obh);
1507 return -ENOSPC;
1509 oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
1510 brelse(bh);
1511 brelse(obh);
1513 error = gfs2_meta_inode_buffer(ip, &bh);
1514 if (error)
1515 return error;
1516 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1517 ip->i_di.di_blocks++;
1518 gfs2_dinode_out(&ip->i_di, bh->b_data);
1519 brelse(bh);
1520 return 0;
1524 * gfs2_dir_add - Add new filename into directory
1525 * @dip: The GFS2 inode
1526 * @filename: The new name
1527 * @inode: The inode number of the entry
1528 * @type: The type of the entry
1530 * Returns: 0 on success, error code on failure
1533 int gfs2_dir_add(struct inode *inode, const struct qstr *name,
1534 const struct gfs2_inum *inum, unsigned type)
1536 struct gfs2_inode *ip = GFS2_I(inode);
1537 struct buffer_head *bh;
1538 struct gfs2_dirent *dent;
1539 struct gfs2_leaf *leaf;
1540 int error;
1542 while(1) {
1543 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
1544 &bh);
1545 if (dent) {
1546 if (IS_ERR(dent))
1547 return PTR_ERR(dent);
1548 dent = gfs2_init_dirent(inode, dent, name, bh);
1549 gfs2_inum_out(inum, (char *)&dent->de_inum);
1550 dent->de_type = cpu_to_be16(type);
1551 if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
1552 leaf = (struct gfs2_leaf *)bh->b_data;
1553 leaf->lf_entries = cpu_to_be16(be16_to_cpu(leaf->lf_entries) + 1);
1555 brelse(bh);
1556 error = gfs2_meta_inode_buffer(ip, &bh);
1557 if (error)
1558 break;
1559 gfs2_trans_add_bh(ip->i_gl, bh, 1);
1560 ip->i_di.di_entries++;
1561 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
1562 gfs2_dinode_out(&ip->i_di, bh->b_data);
1563 brelse(bh);
1564 error = 0;
1565 break;
1567 if (!(ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
1568 error = dir_make_exhash(inode);
1569 if (error)
1570 break;
1571 continue;
1573 error = dir_split_leaf(inode, name);
1574 if (error == 0)
1575 continue;
1576 if (error < 0)
1577 break;
1578 if (ip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) {
1579 error = dir_double_exhash(ip);
1580 if (error)
1581 break;
1582 error = dir_split_leaf(inode, name);
1583 if (error < 0)
1584 break;
1585 if (error == 0)
1586 continue;
1588 error = dir_new_leaf(inode, name);
1589 if (!error)
1590 continue;
1591 error = -ENOSPC;
1592 break;
1594 return error;
1599 * gfs2_dir_del - Delete a directory entry
1600 * @dip: The GFS2 inode
1601 * @filename: The filename
1603 * Returns: 0 on success, error code on failure
1606 int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name)
1608 struct gfs2_dirent *dent, *prev = NULL;
1609 struct buffer_head *bh;
1610 int error;
1612 /* Returns _either_ the entry (if its first in block) or the
1613 previous entry otherwise */
1614 dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
1615 if (!dent) {
1616 gfs2_consist_inode(dip);
1617 return -EIO;
1619 if (IS_ERR(dent)) {
1620 gfs2_consist_inode(dip);
1621 return PTR_ERR(dent);
1623 /* If not first in block, adjust pointers accordingly */
1624 if (gfs2_dirent_find(dent, name, NULL) == 0) {
1625 prev = dent;
1626 dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
1629 dirent_del(dip, bh, prev, dent);
1630 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1631 struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
1632 u16 entries = be16_to_cpu(leaf->lf_entries);
1633 if (!entries)
1634 gfs2_consist_inode(dip);
1635 leaf->lf_entries = cpu_to_be16(--entries);
1637 brelse(bh);
1639 error = gfs2_meta_inode_buffer(dip, &bh);
1640 if (error)
1641 return error;
1643 if (!dip->i_di.di_entries)
1644 gfs2_consist_inode(dip);
1645 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1646 dip->i_di.di_entries--;
1647 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1648 gfs2_dinode_out(&dip->i_di, bh->b_data);
1649 brelse(bh);
1650 mark_inode_dirty(&dip->i_inode);
1652 return error;
1656 * gfs2_dir_mvino - Change inode number of directory entry
1657 * @dip: The GFS2 inode
1658 * @filename:
1659 * @new_inode:
1661 * This routine changes the inode number of a directory entry. It's used
1662 * by rename to change ".." when a directory is moved.
1663 * Assumes a glock is held on dvp.
1665 * Returns: errno
1668 int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
1669 struct gfs2_inum *inum, unsigned int new_type)
1671 struct buffer_head *bh;
1672 struct gfs2_dirent *dent;
1673 int error;
1675 dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
1676 if (!dent) {
1677 gfs2_consist_inode(dip);
1678 return -EIO;
1680 if (IS_ERR(dent))
1681 return PTR_ERR(dent);
1683 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1684 gfs2_inum_out(inum, (char *)&dent->de_inum);
1685 dent->de_type = cpu_to_be16(new_type);
1687 if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
1688 brelse(bh);
1689 error = gfs2_meta_inode_buffer(dip, &bh);
1690 if (error)
1691 return error;
1692 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1695 dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
1696 gfs2_dinode_out(&dip->i_di, bh->b_data);
1697 brelse(bh);
1698 return 0;
1702 * foreach_leaf - call a function for each leaf in a directory
1703 * @dip: the directory
1704 * @lc: the function to call for each each
1705 * @data: private data to pass to it
1707 * Returns: errno
1710 static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
1712 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1713 struct buffer_head *bh;
1714 struct gfs2_leaf *leaf;
1715 u32 hsize, len;
1716 u32 ht_offset, lp_offset, ht_offset_cur = -1;
1717 u32 index = 0;
1718 u64 *lp;
1719 u64 leaf_no;
1720 int error = 0;
1722 hsize = 1 << dip->i_di.di_depth;
1723 if (hsize * sizeof(u64) != dip->i_di.di_size) {
1724 gfs2_consist_inode(dip);
1725 return -EIO;
1728 lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
1729 if (!lp)
1730 return -ENOMEM;
1732 while (index < hsize) {
1733 lp_offset = index & (sdp->sd_hash_ptrs - 1);
1734 ht_offset = index - lp_offset;
1736 if (ht_offset_cur != ht_offset) {
1737 error = gfs2_dir_read_data(dip, (char *)lp,
1738 ht_offset * sizeof(u64),
1739 sdp->sd_hash_bsize, 1);
1740 if (error != sdp->sd_hash_bsize) {
1741 if (error >= 0)
1742 error = -EIO;
1743 goto out;
1745 ht_offset_cur = ht_offset;
1748 leaf_no = be64_to_cpu(lp[lp_offset]);
1749 if (leaf_no) {
1750 error = get_leaf(dip, leaf_no, &bh);
1751 if (error)
1752 goto out;
1753 leaf = (struct gfs2_leaf *)bh->b_data;
1754 len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth));
1755 brelse(bh);
1757 error = lc(dip, index, len, leaf_no, data);
1758 if (error)
1759 goto out;
1761 index = (index & ~(len - 1)) + len;
1762 } else
1763 index++;
1766 if (index != hsize) {
1767 gfs2_consist_inode(dip);
1768 error = -EIO;
1771 out:
1772 kfree(lp);
1774 return error;
1778 * leaf_dealloc - Deallocate a directory leaf
1779 * @dip: the directory
1780 * @index: the hash table offset in the directory
1781 * @len: the number of pointers to this leaf
1782 * @leaf_no: the leaf number
1783 * @data: not used
1785 * Returns: errno
1788 static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
1789 u64 leaf_no, void *data)
1791 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1792 struct gfs2_leaf *tmp_leaf;
1793 struct gfs2_rgrp_list rlist;
1794 struct buffer_head *bh, *dibh;
1795 u64 blk, nblk;
1796 unsigned int rg_blocks = 0, l_blocks = 0;
1797 char *ht;
1798 unsigned int x, size = len * sizeof(u64);
1799 int error;
1801 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1803 ht = kzalloc(size, GFP_KERNEL);
1804 if (!ht)
1805 return -ENOMEM;
1807 gfs2_alloc_get(dip);
1809 error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1810 if (error)
1811 goto out;
1813 error = gfs2_rindex_hold(sdp, &dip->i_alloc.al_ri_gh);
1814 if (error)
1815 goto out_qs;
1817 /* Count the number of leaves */
1819 for (blk = leaf_no; blk; blk = nblk) {
1820 error = get_leaf(dip, blk, &bh);
1821 if (error)
1822 goto out_rlist;
1823 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1824 nblk = be64_to_cpu(tmp_leaf->lf_next);
1825 brelse(bh);
1827 gfs2_rlist_add(sdp, &rlist, blk);
1828 l_blocks++;
1831 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
1833 for (x = 0; x < rlist.rl_rgrps; x++) {
1834 struct gfs2_rgrpd *rgd;
1835 rgd = rlist.rl_ghs[x].gh_gl->gl_object;
1836 rg_blocks += rgd->rd_ri.ri_length;
1839 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1840 if (error)
1841 goto out_rlist;
1843 error = gfs2_trans_begin(sdp,
1844 rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
1845 RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
1846 if (error)
1847 goto out_rg_gunlock;
1849 for (blk = leaf_no; blk; blk = nblk) {
1850 error = get_leaf(dip, blk, &bh);
1851 if (error)
1852 goto out_end_trans;
1853 tmp_leaf = (struct gfs2_leaf *)bh->b_data;
1854 nblk = be64_to_cpu(tmp_leaf->lf_next);
1855 brelse(bh);
1857 gfs2_free_meta(dip, blk, 1);
1859 if (!dip->i_di.di_blocks)
1860 gfs2_consist_inode(dip);
1861 dip->i_di.di_blocks--;
1864 error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
1865 if (error != size) {
1866 if (error >= 0)
1867 error = -EIO;
1868 goto out_end_trans;
1871 error = gfs2_meta_inode_buffer(dip, &dibh);
1872 if (error)
1873 goto out_end_trans;
1875 gfs2_trans_add_bh(dip->i_gl, dibh, 1);
1876 gfs2_dinode_out(&dip->i_di, dibh->b_data);
1877 brelse(dibh);
1879 out_end_trans:
1880 gfs2_trans_end(sdp);
1881 out_rg_gunlock:
1882 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1883 out_rlist:
1884 gfs2_rlist_free(&rlist);
1885 gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh);
1886 out_qs:
1887 gfs2_quota_unhold(dip);
1888 out:
1889 gfs2_alloc_put(dip);
1890 kfree(ht);
1891 return error;
1895 * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
1896 * @dip: the directory
1898 * Dealloc all on-disk directory leaves to FREEMETA state
1899 * Change on-disk inode type to "regular file"
1901 * Returns: errno
1904 int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
1906 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
1907 struct buffer_head *bh;
1908 int error;
1910 /* Dealloc on-disk leaves to FREEMETA state */
1911 error = foreach_leaf(dip, leaf_dealloc, NULL);
1912 if (error)
1913 return error;
1915 /* Make this a regular file in case we crash.
1916 (We don't want to free these blocks a second time.) */
1918 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1919 if (error)
1920 return error;
1922 error = gfs2_meta_inode_buffer(dip, &bh);
1923 if (!error) {
1924 gfs2_trans_add_bh(dip->i_gl, bh, 1);
1925 ((struct gfs2_dinode *)bh->b_data)->di_mode =
1926 cpu_to_be32(S_IFREG);
1927 brelse(bh);
1930 gfs2_trans_end(sdp);
1932 return error;
1936 * gfs2_diradd_alloc_required - find if adding entry will require an allocation
1937 * @ip: the file being written to
1938 * @filname: the filename that's going to be added
1940 * Returns: 1 if alloc required, 0 if not, -ve on error
1943 int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
1945 struct gfs2_dirent *dent;
1946 struct buffer_head *bh;
1948 dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
1949 if (!dent) {
1950 return 1;
1952 if (IS_ERR(dent))
1953 return PTR_ERR(dent);
1954 brelse(bh);
1955 return 0;