1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Creates, reads, walks and deletes directory-nodes
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
10 * Portions of this code from linux/fs/ext3/dir.c
12 * Copyright (C) 1992, 1993, 1994, 1995
13 * Remy Card (card@masi.ibp.fr)
14 * Laboratoire MASI - Institut Blaise pascal
15 * Universite Pierre et Marie Curie (Paris VI)
19 * linux/fs/minix/dir.c
21 * Copyright (C) 1991, 1992 Linux Torvalds
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public
25 * License as published by the Free Software Foundation; either
26 * version 2 of the License, or (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 * General Public License for more details.
33 * You should have received a copy of the GNU General Public
34 * License along with this program; if not, write to the
35 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
36 * Boston, MA 021110-1307, USA.
40 #include <linux/types.h>
41 #include <linux/slab.h>
42 #include <linux/highmem.h>
44 #define MLOG_MASK_PREFIX ML_NAMEI
45 #include <cluster/masklog.h>
52 #include "extent_map.h"
61 #include "buffer_head_io.h"
63 #define NAMEI_RA_CHUNKS 2
64 #define NAMEI_RA_BLOCKS 4
65 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
66 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
68 static unsigned char ocfs2_filetype_table[] = {
69 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
72 static int ocfs2_extend_dir(struct ocfs2_super *osb,
74 struct buffer_head *parent_fe_bh,
75 struct buffer_head **new_de_bh);
76 static int ocfs2_do_extend_dir(struct super_block *sb,
79 struct buffer_head *parent_fe_bh,
80 struct ocfs2_alloc_context *data_ac,
81 struct ocfs2_alloc_context *meta_ac,
82 struct buffer_head **new_bh);
85 * bh passed here can be an inode block or a dir data block, depending
86 * on the inode inline data flag.
88 static int ocfs2_check_dir_entry(struct inode * dir,
89 struct ocfs2_dir_entry * de,
90 struct buffer_head * bh,
93 const char *error_msg = NULL;
94 const int rlen = le16_to_cpu(de->rec_len);
96 if (rlen < OCFS2_DIR_REC_LEN(1))
97 error_msg = "rec_len is smaller than minimal";
98 else if (rlen % 4 != 0)
99 error_msg = "rec_len % 4 != 0";
100 else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
101 error_msg = "rec_len is too small for name_len";
102 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
103 error_msg = "directory entry across blocks";
105 if (error_msg != NULL)
106 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
107 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
108 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
109 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
111 return error_msg == NULL ? 1 : 0;
114 static inline int ocfs2_match(int len,
115 const char * const name,
116 struct ocfs2_dir_entry *de)
118 if (len != de->name_len)
122 return !memcmp(name, de->name, len);
126 * Returns 0 if not found, -1 on failure, and 1 on success
128 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
130 const char *name, int namelen,
131 unsigned long offset,
134 struct ocfs2_dir_entry **res_dir)
136 struct ocfs2_dir_entry *de;
137 char *dlimit, *de_buf;
144 dlimit = de_buf + bytes;
146 while (de_buf < dlimit) {
147 /* this code is executed quadratically often */
148 /* do minimal checking `by hand' */
150 de = (struct ocfs2_dir_entry *) de_buf;
152 if (de_buf + namelen <= dlimit &&
153 ocfs2_match(namelen, name, de)) {
154 /* found a match - just to be sure, do a full check */
155 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
164 /* prevent looping on a bad block */
165 de_len = le16_to_cpu(de->rec_len);
180 static struct buffer_head *ocfs2_find_entry_id(const char *name,
183 struct ocfs2_dir_entry **res_dir)
186 struct buffer_head *di_bh = NULL;
187 struct ocfs2_dinode *di;
188 struct ocfs2_inline_data *data;
190 ret = ocfs2_read_block(OCFS2_SB(dir->i_sb), OCFS2_I(dir)->ip_blkno,
191 &di_bh, OCFS2_BH_CACHED, dir);
197 di = (struct ocfs2_dinode *)di_bh->b_data;
198 data = &di->id2.i_data;
200 found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
201 data->id_data, i_size_read(dir), res_dir);
210 struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
212 struct ocfs2_dir_entry **res_dir)
214 struct super_block *sb;
215 struct buffer_head *bh_use[NAMEI_RA_SIZE];
216 struct buffer_head *bh, *ret = NULL;
217 unsigned long start, block, b;
218 int ra_max = 0; /* Number of bh's in the readahead
220 int ra_ptr = 0; /* Current index into readahead
229 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
230 start = OCFS2_I(dir)->ip_dir_start_lookup;
231 if (start >= nblocks)
238 * We deal with the read-ahead logic here.
240 if (ra_ptr >= ra_max) {
241 /* Refill the readahead buffer */
244 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
246 * Terminate if we reach the end of the
247 * directory and must wrap, or if our
248 * search has finished at this block.
250 if (b >= nblocks || (num && block == start)) {
251 bh_use[ra_max] = NULL;
256 bh = ocfs2_bread(dir, b++, &err, 1);
260 if ((bh = bh_use[ra_ptr++]) == NULL)
263 if (!buffer_uptodate(bh)) {
264 /* read error, skip block & hope for the best */
265 ocfs2_error(dir->i_sb, "reading directory %llu, "
267 (unsigned long long)OCFS2_I(dir)->ip_blkno,
272 i = ocfs2_search_dirblock(bh, dir, name, namelen,
273 block << sb->s_blocksize_bits,
274 bh->b_data, sb->s_blocksize,
277 OCFS2_I(dir)->ip_dir_start_lookup = block;
279 goto cleanup_and_exit;
283 goto cleanup_and_exit;
286 if (++block >= nblocks)
288 } while (block != start);
291 * If the directory has grown while we were searching, then
292 * search the last part of the directory before giving up.
295 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
296 if (block < nblocks) {
302 /* Clean up the read-ahead blocks */
303 for (; ra_ptr < ra_max; ra_ptr++)
304 brelse(bh_use[ra_ptr]);
311 * Try to find an entry of the provided name within 'dir'.
313 * If nothing was found, NULL is returned. Otherwise, a buffer_head
314 * and pointer to the dir entry are passed back.
316 * Caller can NOT assume anything about the contents of the
317 * buffer_head - it is passed back only so that it can be passed into
318 * any one of the manipulation functions (add entry, delete entry,
319 * etc). As an example, bh in the extent directory case is a data
320 * block, in the inline-data case it actually points to an inode.
322 struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
324 struct ocfs2_dir_entry **res_dir)
328 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
329 return ocfs2_find_entry_id(name, namelen, dir, res_dir);
331 return ocfs2_find_entry_el(name, namelen, dir, res_dir);
334 int ocfs2_update_entry(struct inode *dir, handle_t *handle,
335 struct buffer_head *de_bh, struct ocfs2_dir_entry *de,
336 struct inode *new_entry_inode)
340 ret = ocfs2_journal_access(handle, dir, de_bh,
341 OCFS2_JOURNAL_ACCESS_WRITE);
347 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
348 ocfs2_set_de_type(de, new_entry_inode->i_mode);
350 ocfs2_journal_dirty(handle, de_bh);
357 * ocfs2_delete_entry deletes a directory entry by merging it with the
360 int ocfs2_delete_entry(handle_t *handle,
362 struct ocfs2_dir_entry *de_del,
363 struct buffer_head *bh)
365 struct ocfs2_dir_entry *de, *pde;
366 int i, status = -ENOENT;
368 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
372 de = (struct ocfs2_dir_entry *) bh->b_data;
373 while (i < bh->b_size) {
374 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
380 status = ocfs2_journal_access(handle, dir, bh,
381 OCFS2_JOURNAL_ACCESS_WRITE);
389 cpu_to_le16(le16_to_cpu(pde->rec_len) +
390 le16_to_cpu(de->rec_len));
394 status = ocfs2_journal_dirty(handle, bh);
397 i += le16_to_cpu(de->rec_len);
399 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
407 * Check whether 'de' has enough room to hold an entry of
408 * 'new_rec_len' bytes.
410 static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
411 unsigned int new_rec_len)
413 unsigned int de_really_used;
415 /* Check whether this is an empty record with enough space */
416 if (le64_to_cpu(de->inode) == 0 &&
417 le16_to_cpu(de->rec_len) >= new_rec_len)
421 * Record might have free space at the end which we can
424 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
425 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
431 /* we don't always have a dentry for what we want to add, so people
432 * like orphan dir can call this instead.
434 * If you pass me insert_bh, I'll skip the search of the other dir
435 * blocks and put the record in there.
437 int __ocfs2_add_entry(handle_t *handle,
439 const char *name, int namelen,
440 struct inode *inode, u64 blkno,
441 struct buffer_head *parent_fe_bh,
442 struct buffer_head *insert_bh)
444 unsigned long offset;
445 unsigned short rec_len;
446 struct ocfs2_dir_entry *de, *de1;
447 struct super_block *sb;
457 rec_len = OCFS2_DIR_REC_LEN(namelen);
459 de = (struct ocfs2_dir_entry *) insert_bh->b_data;
461 BUG_ON((char *)de >= sb->s_blocksize + insert_bh->b_data);
462 /* These checks should've already been passed by the
463 * prepare function, but I guess we can leave them
465 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
469 if (ocfs2_match(namelen, name, de)) {
474 if (ocfs2_dirent_would_fit(de, rec_len)) {
475 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
476 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
482 status = ocfs2_journal_access(handle, dir, insert_bh,
483 OCFS2_JOURNAL_ACCESS_WRITE);
484 /* By now the buffer is marked for journaling */
485 offset += le16_to_cpu(de->rec_len);
486 if (le64_to_cpu(de->inode)) {
487 de1 = (struct ocfs2_dir_entry *)((char *) de +
488 OCFS2_DIR_REC_LEN(de->name_len));
490 cpu_to_le16(le16_to_cpu(de->rec_len) -
491 OCFS2_DIR_REC_LEN(de->name_len));
492 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
495 de->file_type = OCFS2_FT_UNKNOWN;
497 de->inode = cpu_to_le64(blkno);
498 ocfs2_set_de_type(de, inode->i_mode);
501 de->name_len = namelen;
502 memcpy(de->name, name, namelen);
505 status = ocfs2_journal_dirty(handle, insert_bh);
509 offset += le16_to_cpu(de->rec_len);
510 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
513 /* when you think about it, the assert above should prevent us
514 * from ever getting here. */
522 static int ocfs2_dir_foreach_blk_id(struct inode *inode,
523 unsigned long *f_version,
524 loff_t *f_pos, void *priv,
527 int ret, i, filldir_ret;
528 unsigned long offset = *f_pos;
529 struct buffer_head *di_bh = NULL;
530 struct ocfs2_dinode *di;
531 struct ocfs2_inline_data *data;
532 struct ocfs2_dir_entry *de;
534 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), OCFS2_I(inode)->ip_blkno,
535 &di_bh, OCFS2_BH_CACHED, inode);
537 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
538 (unsigned long long)OCFS2_I(inode)->ip_blkno);
542 di = (struct ocfs2_dinode *)di_bh->b_data;
543 data = &di->id2.i_data;
545 while (*f_pos < i_size_read(inode)) {
547 /* If the dir block has changed since the last call to
548 * readdir(2), then we might be pointing to an invalid
549 * dirent right now. Scan from the start of the block
551 if (*f_version != inode->i_version) {
552 for (i = 0; i < i_size_read(inode) && i < offset; ) {
553 de = (struct ocfs2_dir_entry *)
555 /* It's too expensive to do a full
556 * dirent test each time round this
557 * loop, but we do have to test at
558 * least that it is non-zero. A
559 * failure will be detected in the
560 * dirent test below. */
561 if (le16_to_cpu(de->rec_len) <
562 OCFS2_DIR_REC_LEN(1))
564 i += le16_to_cpu(de->rec_len);
567 *f_version = inode->i_version;
570 de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
571 if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
572 /* On error, skip the f_pos to the end. */
573 *f_pos = i_size_read(inode);
576 offset += le16_to_cpu(de->rec_len);
577 if (le64_to_cpu(de->inode)) {
578 /* We might block in the next section
579 * if the data destination is
580 * currently swapped out. So, use a
581 * version stamp to detect whether or
582 * not the directory has been modified
583 * during the copy operation.
585 unsigned long version = *f_version;
586 unsigned char d_type = DT_UNKNOWN;
588 if (de->file_type < OCFS2_FT_MAX)
589 d_type = ocfs2_filetype_table[de->file_type];
591 filldir_ret = filldir(priv, de->name,
594 le64_to_cpu(de->inode),
598 if (version != *f_version)
601 *f_pos += le16_to_cpu(de->rec_len);
610 static int ocfs2_dir_foreach_blk_el(struct inode *inode,
611 unsigned long *f_version,
612 loff_t *f_pos, void *priv,
616 unsigned long offset, blk, last_ra_blk = 0;
618 struct buffer_head * bh, * tmp;
619 struct ocfs2_dir_entry * de;
621 struct super_block * sb = inode->i_sb;
622 unsigned int ra_sectors = 16;
627 offset = (*f_pos) & (sb->s_blocksize - 1);
629 while (!error && !stored && *f_pos < i_size_read(inode)) {
630 blk = (*f_pos) >> sb->s_blocksize_bits;
631 bh = ocfs2_bread(inode, blk, &err, 0);
634 "directory #%llu contains a hole at offset %lld\n",
635 (unsigned long long)OCFS2_I(inode)->ip_blkno,
637 *f_pos += sb->s_blocksize - offset;
641 /* The idea here is to begin with 8k read-ahead and to stay
642 * 4k ahead of our current position.
644 * TODO: Use the pagecache for this. We just need to
645 * make sure it's cluster-safe... */
647 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
648 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
650 tmp = ocfs2_bread(inode, ++blk, &err, 1);
659 /* If the dir block has changed since the last call to
660 * readdir(2), then we might be pointing to an invalid
661 * dirent right now. Scan from the start of the block
663 if (*f_version != inode->i_version) {
664 for (i = 0; i < sb->s_blocksize && i < offset; ) {
665 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
666 /* It's too expensive to do a full
667 * dirent test each time round this
668 * loop, but we do have to test at
669 * least that it is non-zero. A
670 * failure will be detected in the
671 * dirent test below. */
672 if (le16_to_cpu(de->rec_len) <
673 OCFS2_DIR_REC_LEN(1))
675 i += le16_to_cpu(de->rec_len);
678 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
680 *f_version = inode->i_version;
683 while (!error && *f_pos < i_size_read(inode)
684 && offset < sb->s_blocksize) {
685 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
686 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
687 /* On error, skip the f_pos to the
689 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
693 offset += le16_to_cpu(de->rec_len);
694 if (le64_to_cpu(de->inode)) {
695 /* We might block in the next section
696 * if the data destination is
697 * currently swapped out. So, use a
698 * version stamp to detect whether or
699 * not the directory has been modified
700 * during the copy operation.
702 unsigned long version = *f_version;
703 unsigned char d_type = DT_UNKNOWN;
705 if (de->file_type < OCFS2_FT_MAX)
706 d_type = ocfs2_filetype_table[de->file_type];
707 error = filldir(priv, de->name,
710 le64_to_cpu(de->inode),
714 if (version != *f_version)
718 *f_pos += le16_to_cpu(de->rec_len);
729 static int ocfs2_dir_foreach_blk(struct inode *inode, unsigned long *f_version,
730 loff_t *f_pos, void *priv, filldir_t filldir)
732 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
733 return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
736 return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir);
740 * This is intended to be called from inside other kernel functions,
741 * so we fake some arguments.
743 int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
747 unsigned long version = inode->i_version;
749 while (*f_pos < i_size_read(inode)) {
750 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
763 int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
766 struct inode *inode = filp->f_path.dentry->d_inode;
769 mlog_entry("dirino=%llu\n",
770 (unsigned long long)OCFS2_I(inode)->ip_blkno);
772 error = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
773 if (lock_level && error >= 0) {
774 /* We release EX lock which used to update atime
775 * and get PR lock again to reduce contention
776 * on commonly accessed directories. */
777 ocfs2_meta_unlock(inode, 1);
779 error = ocfs2_meta_lock(inode, NULL, 0);
782 if (error != -ENOENT)
784 /* we haven't got any yet, so propagate the error. */
788 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
791 ocfs2_meta_unlock(inode, lock_level);
800 * NOTE: this should always be called with parent dir i_mutex taken.
802 int ocfs2_find_files_on_disk(const char *name,
806 struct buffer_head **dirent_bh,
807 struct ocfs2_dir_entry **dirent)
809 int status = -ENOENT;
811 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
812 namelen, name, blkno, inode, dirent_bh, dirent);
814 *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
815 if (!*dirent_bh || !*dirent) {
820 *blkno = le64_to_cpu((*dirent)->inode);
837 * Convenience function for callers which just want the block number
838 * mapped to a name and don't require the full dirent info, etc.
840 int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
841 int namelen, u64 *blkno)
844 struct buffer_head *bh = NULL;
845 struct ocfs2_dir_entry *dirent = NULL;
847 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &bh, &dirent);
853 /* Check for a name within a directory.
855 * Return 0 if the name does not exist
856 * Return -EEXIST if the directory contains the name
858 * Callers should have i_mutex + a cluster lock on dir
860 int ocfs2_check_dir_for_entry(struct inode *dir,
865 struct buffer_head *dirent_bh = NULL;
866 struct ocfs2_dir_entry *dirent = NULL;
868 mlog_entry("dir %llu, name '%.*s'\n",
869 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
872 dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
885 struct ocfs2_empty_dir_priv {
887 unsigned seen_dot_dot;
890 static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
891 loff_t pos, u64 ino, unsigned type)
893 struct ocfs2_empty_dir_priv *p = priv;
896 * Check the positions of "." and ".." records to be sure
897 * they're in the correct place.
899 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
904 if (name_len == 2 && !strncmp("..", name, 2) &&
905 pos == OCFS2_DIR_REC_LEN(1)) {
914 * routine to check that the specified directory is empty (for rmdir)
916 * Returns 1 if dir is empty, zero otherwise.
918 int ocfs2_empty_dir(struct inode *inode)
922 struct ocfs2_empty_dir_priv priv;
924 memset(&priv, 0, sizeof(priv));
926 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
930 if (!priv.seen_dot || !priv.seen_dot_dot) {
931 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
932 (unsigned long long)OCFS2_I(inode)->ip_blkno);
934 * XXX: Is it really safe to allow an unlink to continue?
939 return !priv.seen_other;
942 int ocfs2_fill_new_dir(struct ocfs2_super *osb,
944 struct inode *parent,
946 struct buffer_head *fe_bh,
947 struct ocfs2_alloc_context *data_ac)
950 struct buffer_head *new_bh = NULL;
951 struct ocfs2_dir_entry *de = NULL;
955 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
956 data_ac, NULL, &new_bh);
962 ocfs2_set_new_buffer_uptodate(inode, new_bh);
964 status = ocfs2_journal_access(handle, inode, new_bh,
965 OCFS2_JOURNAL_ACCESS_CREATE);
970 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
972 de = (struct ocfs2_dir_entry *) new_bh->b_data;
973 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
976 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
977 strcpy(de->name, ".");
978 ocfs2_set_de_type(de, S_IFDIR);
979 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
980 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
981 de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize -
982 OCFS2_DIR_REC_LEN(1));
984 strcpy(de->name, "..");
985 ocfs2_set_de_type(de, S_IFDIR);
987 status = ocfs2_journal_dirty(handle, new_bh);
993 i_size_write(inode, inode->i_sb->s_blocksize);
995 inode->i_blocks = ocfs2_inode_sector_count(inode);
996 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
1011 /* returns a bh of the 1st new block in the allocation. */
1012 static int ocfs2_do_extend_dir(struct super_block *sb,
1015 struct buffer_head *parent_fe_bh,
1016 struct ocfs2_alloc_context *data_ac,
1017 struct ocfs2_alloc_context *meta_ac,
1018 struct buffer_head **new_bh)
1022 u64 p_blkno, v_blkno;
1024 spin_lock(&OCFS2_I(dir)->ip_lock);
1025 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
1026 spin_unlock(&OCFS2_I(dir)->ip_lock);
1029 u32 offset = OCFS2_I(dir)->ip_clusters;
1031 status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, &offset,
1032 1, 0, parent_fe_bh, handle,
1033 data_ac, meta_ac, NULL);
1034 BUG_ON(status == -EAGAIN);
1041 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
1042 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
1048 *new_bh = sb_getblk(sb, p_blkno);
1060 /* assumes you already have a cluster lock on the directory. */
1061 static int ocfs2_extend_dir(struct ocfs2_super *osb,
1063 struct buffer_head *parent_fe_bh,
1064 struct buffer_head **new_de_bh)
1067 int credits, num_free_extents, drop_alloc_sem = 0;
1069 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1070 struct ocfs2_alloc_context *data_ac = NULL;
1071 struct ocfs2_alloc_context *meta_ac = NULL;
1072 handle_t *handle = NULL;
1073 struct buffer_head *new_bh = NULL;
1074 struct ocfs2_dir_entry * de;
1075 struct super_block *sb = osb->sb;
1079 dir_i_size = i_size_read(dir);
1080 mlog(0, "extending dir %llu (i_size = %lld)\n",
1081 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
1083 /* dir->i_size is always block aligned. */
1084 spin_lock(&OCFS2_I(dir)->ip_lock);
1085 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
1086 spin_unlock(&OCFS2_I(dir)->ip_lock);
1087 num_free_extents = ocfs2_num_free_extents(osb, dir, fe);
1088 if (num_free_extents < 0) {
1089 status = num_free_extents;
1094 if (!num_free_extents) {
1095 status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac);
1097 if (status != -ENOSPC)
1103 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
1105 if (status != -ENOSPC)
1110 credits = ocfs2_calc_extend_credits(sb, fe, 1);
1112 spin_unlock(&OCFS2_I(dir)->ip_lock);
1113 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1116 down_write(&OCFS2_I(dir)->ip_alloc_sem);
1119 handle = ocfs2_start_trans(osb, credits);
1120 if (IS_ERR(handle)) {
1121 status = PTR_ERR(handle);
1127 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
1128 data_ac, meta_ac, &new_bh);
1134 ocfs2_set_new_buffer_uptodate(dir, new_bh);
1136 status = ocfs2_journal_access(handle, dir, new_bh,
1137 OCFS2_JOURNAL_ACCESS_CREATE);
1142 memset(new_bh->b_data, 0, sb->s_blocksize);
1143 de = (struct ocfs2_dir_entry *) new_bh->b_data;
1145 de->rec_len = cpu_to_le16(sb->s_blocksize);
1146 status = ocfs2_journal_dirty(handle, new_bh);
1152 dir_i_size += dir->i_sb->s_blocksize;
1153 i_size_write(dir, dir_i_size);
1154 dir->i_blocks = ocfs2_inode_sector_count(dir);
1155 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1161 *new_de_bh = new_bh;
1165 up_write(&OCFS2_I(dir)->ip_alloc_sem);
1167 ocfs2_commit_trans(osb, handle);
1170 ocfs2_free_alloc_context(data_ac);
1172 ocfs2_free_alloc_context(meta_ac);
1182 * Search the dir for a good spot, extending it if necessary. The
1183 * block containing an appropriate record is returned in ret_de_bh.
1185 int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
1187 struct buffer_head *parent_fe_bh,
1190 struct buffer_head **ret_de_bh)
1192 unsigned long offset;
1193 struct buffer_head * bh = NULL;
1194 unsigned short rec_len;
1195 struct ocfs2_dinode *fe;
1196 struct ocfs2_dir_entry *de;
1197 struct super_block *sb;
1202 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
1203 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
1205 BUG_ON(!S_ISDIR(dir->i_mode));
1206 fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1207 BUG_ON(le64_to_cpu(fe->i_size) != i_size_read(dir));
1217 bh = ocfs2_bread(dir, 0, &status, 0);
1223 rec_len = OCFS2_DIR_REC_LEN(namelen);
1225 de = (struct ocfs2_dir_entry *) bh->b_data;
1227 if ((char *)de >= sb->s_blocksize + bh->b_data) {
1231 if (i_size_read(dir) <= offset) {
1232 status = ocfs2_extend_dir(osb,
1245 bh = ocfs2_bread(dir,
1246 offset >> sb->s_blocksize_bits,
1253 /* move to next block */
1254 de = (struct ocfs2_dir_entry *) bh->b_data;
1256 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1260 if (ocfs2_match(namelen, name, de)) {
1264 if (ocfs2_dirent_would_fit(de, rec_len)) {
1265 /* Ok, we found a spot. Return this bh and let
1266 * the caller actually fill it in. */
1272 offset += le16_to_cpu(de->rec_len);
1273 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));