]> err.no Git - linux-2.6/blob - fs/ocfs2/dir.c
ocfs2: Pass raw u64 to filldir
[linux-2.6] / fs / ocfs2 / dir.c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * dir.c
5  *
6  * Creates, reads, walks and deletes directory-nodes
7  *
8  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
9  *
10  *  Portions of this code from linux/fs/ext3/dir.c
11  *
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)
16  *
17  *   from
18  *
19  *   linux/fs/minix/dir.c
20  *
21  *   Copyright (C) 1991, 1992 Linux Torvalds
22  *
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.
27  *
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.
32  *
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.
37  */
38
39 #include <linux/fs.h>
40 #include <linux/types.h>
41 #include <linux/slab.h>
42 #include <linux/highmem.h>
43
44 #define MLOG_MASK_PREFIX ML_NAMEI
45 #include <cluster/masklog.h>
46
47 #include "ocfs2.h"
48
49 #include "alloc.h"
50 #include "dir.h"
51 #include "dlmglue.h"
52 #include "extent_map.h"
53 #include "file.h"
54 #include "inode.h"
55 #include "journal.h"
56 #include "namei.h"
57 #include "suballoc.h"
58 #include "super.h"
59 #include "uptodate.h"
60
61 #include "buffer_head_io.h"
62
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))
67
68 static unsigned char ocfs2_filetype_table[] = {
69         DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
70 };
71
72 static int ocfs2_extend_dir(struct ocfs2_super *osb,
73                             struct inode *dir,
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,
77                                handle_t *handle,
78                                struct inode *dir,
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);
83
84 int ocfs2_check_dir_entry(struct inode * dir,
85                           struct ocfs2_dir_entry * de,
86                           struct buffer_head * bh,
87                           unsigned long offset)
88 {
89         const char *error_msg = NULL;
90         const int rlen = le16_to_cpu(de->rec_len);
91
92         if (rlen < OCFS2_DIR_REC_LEN(1))
93                 error_msg = "rec_len is smaller than minimal";
94         else if (rlen % 4 != 0)
95                 error_msg = "rec_len % 4 != 0";
96         else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
97                 error_msg = "rec_len is too small for name_len";
98         else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
99                 error_msg = "directory entry across blocks";
100
101         if (error_msg != NULL)
102                 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
103                      "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
104                      (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
105                      offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
106                      de->name_len);
107         return error_msg == NULL ? 1 : 0;
108 }
109
110 static inline int ocfs2_match(int len,
111                               const char * const name,
112                               struct ocfs2_dir_entry *de)
113 {
114         if (len != de->name_len)
115                 return 0;
116         if (!de->inode)
117                 return 0;
118         return !memcmp(name, de->name, len);
119 }
120
121 /*
122  * Returns 0 if not found, -1 on failure, and 1 on success
123  */
124 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
125                                         struct inode *dir,
126                                         const char *name, int namelen,
127                                         unsigned long offset,
128                                         struct ocfs2_dir_entry **res_dir)
129 {
130         struct ocfs2_dir_entry *de;
131         char *dlimit, *de_buf;
132         int de_len;
133         int ret = 0;
134
135         mlog_entry_void();
136
137         de_buf = bh->b_data;
138         dlimit = de_buf + dir->i_sb->s_blocksize;
139
140         while (de_buf < dlimit) {
141                 /* this code is executed quadratically often */
142                 /* do minimal checking `by hand' */
143
144                 de = (struct ocfs2_dir_entry *) de_buf;
145
146                 if (de_buf + namelen <= dlimit &&
147                     ocfs2_match(namelen, name, de)) {
148                         /* found a match - just to be sure, do a full check */
149                         if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
150                                 ret = -1;
151                                 goto bail;
152                         }
153                         *res_dir = de;
154                         ret = 1;
155                         goto bail;
156                 }
157
158                 /* prevent looping on a bad block */
159                 de_len = le16_to_cpu(de->rec_len);
160                 if (de_len <= 0) {
161                         ret = -1;
162                         goto bail;
163                 }
164
165                 de_buf += de_len;
166                 offset += de_len;
167         }
168
169 bail:
170         mlog_exit(ret);
171         return ret;
172 }
173
174 struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
175                                      struct inode *dir,
176                                      struct ocfs2_dir_entry **res_dir)
177 {
178         struct super_block *sb;
179         struct buffer_head *bh_use[NAMEI_RA_SIZE];
180         struct buffer_head *bh, *ret = NULL;
181         unsigned long start, block, b;
182         int ra_max = 0;         /* Number of bh's in the readahead
183                                    buffer, bh_use[] */
184         int ra_ptr = 0;         /* Current index into readahead
185                                    buffer */
186         int num = 0;
187         int nblocks, i, err;
188
189         mlog_entry_void();
190
191         *res_dir = NULL;
192         sb = dir->i_sb;
193
194         nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
195         start = OCFS2_I(dir)->ip_dir_start_lookup;
196         if (start >= nblocks)
197                 start = 0;
198         block = start;
199
200 restart:
201         do {
202                 /*
203                  * We deal with the read-ahead logic here.
204                  */
205                 if (ra_ptr >= ra_max) {
206                         /* Refill the readahead buffer */
207                         ra_ptr = 0;
208                         b = block;
209                         for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
210                                 /*
211                                  * Terminate if we reach the end of the
212                                  * directory and must wrap, or if our
213                                  * search has finished at this block.
214                                  */
215                                 if (b >= nblocks || (num && block == start)) {
216                                         bh_use[ra_max] = NULL;
217                                         break;
218                                 }
219                                 num++;
220
221                                 bh = ocfs2_bread(dir, b++, &err, 1);
222                                 bh_use[ra_max] = bh;
223                         }
224                 }
225                 if ((bh = bh_use[ra_ptr++]) == NULL)
226                         goto next;
227                 wait_on_buffer(bh);
228                 if (!buffer_uptodate(bh)) {
229                         /* read error, skip block & hope for the best */
230                         ocfs2_error(dir->i_sb, "reading directory %llu, "
231                                     "offset %lu\n",
232                                     (unsigned long long)OCFS2_I(dir)->ip_blkno,
233                                     block);
234                         brelse(bh);
235                         goto next;
236                 }
237                 i = ocfs2_search_dirblock(bh, dir, name, namelen,
238                                           block << sb->s_blocksize_bits,
239                                           res_dir);
240                 if (i == 1) {
241                         OCFS2_I(dir)->ip_dir_start_lookup = block;
242                         ret = bh;
243                         goto cleanup_and_exit;
244                 } else {
245                         brelse(bh);
246                         if (i < 0)
247                                 goto cleanup_and_exit;
248                 }
249         next:
250                 if (++block >= nblocks)
251                         block = 0;
252         } while (block != start);
253
254         /*
255          * If the directory has grown while we were searching, then
256          * search the last part of the directory before giving up.
257          */
258         block = nblocks;
259         nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
260         if (block < nblocks) {
261                 start = 0;
262                 goto restart;
263         }
264
265 cleanup_and_exit:
266         /* Clean up the read-ahead blocks */
267         for (; ra_ptr < ra_max; ra_ptr++)
268                 brelse(bh_use[ra_ptr]);
269
270         mlog_exit_ptr(ret);
271         return ret;
272 }
273
274 /*
275  * ocfs2_delete_entry deletes a directory entry by merging it with the
276  * previous entry
277  */
278 int ocfs2_delete_entry(handle_t *handle,
279                        struct inode *dir,
280                        struct ocfs2_dir_entry *de_del,
281                        struct buffer_head *bh)
282 {
283         struct ocfs2_dir_entry *de, *pde;
284         int i, status = -ENOENT;
285
286         mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
287
288         i = 0;
289         pde = NULL;
290         de = (struct ocfs2_dir_entry *) bh->b_data;
291         while (i < bh->b_size) {
292                 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
293                         status = -EIO;
294                         mlog_errno(status);
295                         goto bail;
296                 }
297                 if (de == de_del)  {
298                         status = ocfs2_journal_access(handle, dir, bh,
299                                                       OCFS2_JOURNAL_ACCESS_WRITE);
300                         if (status < 0) {
301                                 status = -EIO;
302                                 mlog_errno(status);
303                                 goto bail;
304                         }
305                         if (pde)
306                                 pde->rec_len =
307                                         cpu_to_le16(le16_to_cpu(pde->rec_len) +
308                                                     le16_to_cpu(de->rec_len));
309                         else
310                                 de->inode = 0;
311                         dir->i_version++;
312                         status = ocfs2_journal_dirty(handle, bh);
313                         goto bail;
314                 }
315                 i += le16_to_cpu(de->rec_len);
316                 pde = de;
317                 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
318         }
319 bail:
320         mlog_exit(status);
321         return status;
322 }
323
324 /* we don't always have a dentry for what we want to add, so people
325  * like orphan dir can call this instead.
326  *
327  * If you pass me insert_bh, I'll skip the search of the other dir
328  * blocks and put the record in there.
329  */
330 int __ocfs2_add_entry(handle_t *handle,
331                       struct inode *dir,
332                       const char *name, int namelen,
333                       struct inode *inode, u64 blkno,
334                       struct buffer_head *parent_fe_bh,
335                       struct buffer_head *insert_bh)
336 {
337         unsigned long offset;
338         unsigned short rec_len;
339         struct ocfs2_dir_entry *de, *de1;
340         struct super_block *sb;
341         int retval, status;
342
343         mlog_entry_void();
344
345         sb = dir->i_sb;
346
347         if (!namelen)
348                 return -EINVAL;
349
350         rec_len = OCFS2_DIR_REC_LEN(namelen);
351         offset = 0;
352         de = (struct ocfs2_dir_entry *) insert_bh->b_data;
353         while (1) {
354                 BUG_ON((char *)de >= sb->s_blocksize + insert_bh->b_data);
355                 /* These checks should've already been passed by the
356                  * prepare function, but I guess we can leave them
357                  * here anyway. */
358                 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
359                         retval = -ENOENT;
360                         goto bail;
361                 }
362                 if (ocfs2_match(namelen, name, de)) {
363                         retval = -EEXIST;
364                         goto bail;
365                 }
366                 if (((le64_to_cpu(de->inode) == 0) &&
367                      (le16_to_cpu(de->rec_len) >= rec_len)) ||
368                     (le16_to_cpu(de->rec_len) >=
369                      (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
370                         dir->i_mtime = dir->i_ctime = CURRENT_TIME;
371                         retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
372                         if (retval < 0) {
373                                 mlog_errno(retval);
374                                 goto bail;
375                         }
376
377                         status = ocfs2_journal_access(handle, dir, insert_bh,
378                                                       OCFS2_JOURNAL_ACCESS_WRITE);
379                         /* By now the buffer is marked for journaling */
380                         offset += le16_to_cpu(de->rec_len);
381                         if (le64_to_cpu(de->inode)) {
382                                 de1 = (struct ocfs2_dir_entry *)((char *) de +
383                                         OCFS2_DIR_REC_LEN(de->name_len));
384                                 de1->rec_len =
385                                         cpu_to_le16(le16_to_cpu(de->rec_len) -
386                                         OCFS2_DIR_REC_LEN(de->name_len));
387                                 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
388                                 de = de1;
389                         }
390                         de->file_type = OCFS2_FT_UNKNOWN;
391                         if (blkno) {
392                                 de->inode = cpu_to_le64(blkno);
393                                 ocfs2_set_de_type(de, inode->i_mode);
394                         } else
395                                 de->inode = 0;
396                         de->name_len = namelen;
397                         memcpy(de->name, name, namelen);
398
399                         dir->i_version++;
400                         status = ocfs2_journal_dirty(handle, insert_bh);
401                         retval = 0;
402                         goto bail;
403                 }
404                 offset += le16_to_cpu(de->rec_len);
405                 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
406         }
407
408         /* when you think about it, the assert above should prevent us
409          * from ever getting here. */
410         retval = -ENOSPC;
411 bail:
412
413         mlog_exit(retval);
414         return retval;
415 }
416
417 static int ocfs2_dir_foreach_blk(struct inode *inode, unsigned long *f_version,
418                                  loff_t *f_pos, void *priv, filldir_t filldir)
419 {
420         int error = 0;
421         unsigned long offset, blk, last_ra_blk = 0;
422         int i, stored;
423         struct buffer_head * bh, * tmp;
424         struct ocfs2_dir_entry * de;
425         int err;
426         struct super_block * sb = inode->i_sb;
427         unsigned int ra_sectors = 16;
428
429         stored = 0;
430         bh = NULL;
431
432         offset = (*f_pos) & (sb->s_blocksize - 1);
433
434         while (!error && !stored && *f_pos < i_size_read(inode)) {
435                 blk = (*f_pos) >> sb->s_blocksize_bits;
436                 bh = ocfs2_bread(inode, blk, &err, 0);
437                 if (!bh) {
438                         mlog(ML_ERROR,
439                              "directory #%llu contains a hole at offset %lld\n",
440                              (unsigned long long)OCFS2_I(inode)->ip_blkno,
441                              *f_pos);
442                         *f_pos += sb->s_blocksize - offset;
443                         continue;
444                 }
445
446                 /* The idea here is to begin with 8k read-ahead and to stay
447                  * 4k ahead of our current position.
448                  *
449                  * TODO: Use the pagecache for this. We just need to
450                  * make sure it's cluster-safe... */
451                 if (!last_ra_blk
452                     || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
453                         for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
454                              i > 0; i--) {
455                                 tmp = ocfs2_bread(inode, ++blk, &err, 1);
456                                 if (tmp)
457                                         brelse(tmp);
458                         }
459                         last_ra_blk = blk;
460                         ra_sectors = 8;
461                 }
462
463 revalidate:
464                 /* If the dir block has changed since the last call to
465                  * readdir(2), then we might be pointing to an invalid
466                  * dirent right now.  Scan from the start of the block
467                  * to make sure. */
468                 if (*f_version != inode->i_version) {
469                         for (i = 0; i < sb->s_blocksize && i < offset; ) {
470                                 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
471                                 /* It's too expensive to do a full
472                                  * dirent test each time round this
473                                  * loop, but we do have to test at
474                                  * least that it is non-zero.  A
475                                  * failure will be detected in the
476                                  * dirent test below. */
477                                 if (le16_to_cpu(de->rec_len) <
478                                     OCFS2_DIR_REC_LEN(1))
479                                         break;
480                                 i += le16_to_cpu(de->rec_len);
481                         }
482                         offset = i;
483                         *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
484                                 | offset;
485                         *f_version = inode->i_version;
486                 }
487
488                 while (!error && *f_pos < i_size_read(inode)
489                        && offset < sb->s_blocksize) {
490                         de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
491                         if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
492                                 /* On error, skip the f_pos to the
493                                    next block. */
494                                 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
495                                 brelse(bh);
496                                 goto out;
497                         }
498                         offset += le16_to_cpu(de->rec_len);
499                         if (le64_to_cpu(de->inode)) {
500                                 /* We might block in the next section
501                                  * if the data destination is
502                                  * currently swapped out.  So, use a
503                                  * version stamp to detect whether or
504                                  * not the directory has been modified
505                                  * during the copy operation.
506                                  */
507                                 unsigned long version = *f_version;
508                                 unsigned char d_type = DT_UNKNOWN;
509
510                                 if (de->file_type < OCFS2_FT_MAX)
511                                         d_type = ocfs2_filetype_table[de->file_type];
512                                 error = filldir(priv, de->name,
513                                                 de->name_len,
514                                                 *f_pos,
515                                                 le64_to_cpu(de->inode),
516                                                 d_type);
517                                 if (error)
518                                         break;
519                                 if (version != *f_version)
520                                         goto revalidate;
521                                 stored ++;
522                         }
523                         *f_pos += le16_to_cpu(de->rec_len);
524                 }
525                 offset = 0;
526                 brelse(bh);
527         }
528
529         stored = 0;
530 out:
531         return stored;
532 }
533
534 /*
535  * ocfs2_readdir()
536  *
537  */
538 int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
539 {
540         int error = 0;
541         struct inode *inode = filp->f_path.dentry->d_inode;
542         int lock_level = 0;
543
544         mlog_entry("dirino=%llu\n",
545                    (unsigned long long)OCFS2_I(inode)->ip_blkno);
546
547         error = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
548         if (lock_level && error >= 0) {
549                 /* We release EX lock which used to update atime
550                  * and get PR lock again to reduce contention
551                  * on commonly accessed directories. */
552                 ocfs2_meta_unlock(inode, 1);
553                 lock_level = 0;
554                 error = ocfs2_meta_lock(inode, NULL, 0);
555         }
556         if (error < 0) {
557                 if (error != -ENOENT)
558                         mlog_errno(error);
559                 /* we haven't got any yet, so propagate the error. */
560                 goto bail_nolock;
561         }
562
563         error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
564                                       dirent, filldir);
565
566         ocfs2_meta_unlock(inode, lock_level);
567
568 bail_nolock:
569         mlog_exit(error);
570
571         return error;
572 }
573
574 /*
575  * NOTE: this should always be called with parent dir i_mutex taken.
576  */
577 int ocfs2_find_files_on_disk(const char *name,
578                              int namelen,
579                              u64 *blkno,
580                              struct inode *inode,
581                              struct buffer_head **dirent_bh,
582                              struct ocfs2_dir_entry **dirent)
583 {
584         int status = -ENOENT;
585
586         mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
587                    namelen, name, blkno, inode, dirent_bh, dirent);
588
589         *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
590         if (!*dirent_bh || !*dirent) {
591                 status = -ENOENT;
592                 goto leave;
593         }
594
595         *blkno = le64_to_cpu((*dirent)->inode);
596
597         status = 0;
598 leave:
599         if (status < 0) {
600                 *dirent = NULL;
601                 if (*dirent_bh) {
602                         brelse(*dirent_bh);
603                         *dirent_bh = NULL;
604                 }
605         }
606
607         mlog_exit(status);
608         return status;
609 }
610
611 /* Check for a name within a directory.
612  *
613  * Return 0 if the name does not exist
614  * Return -EEXIST if the directory contains the name
615  *
616  * Callers should have i_mutex + a cluster lock on dir
617  */
618 int ocfs2_check_dir_for_entry(struct inode *dir,
619                               const char *name,
620                               int namelen)
621 {
622         int ret;
623         struct buffer_head *dirent_bh = NULL;
624         struct ocfs2_dir_entry *dirent = NULL;
625
626         mlog_entry("dir %llu, name '%.*s'\n",
627                    (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
628
629         ret = -EEXIST;
630         dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
631         if (dirent_bh)
632                 goto bail;
633
634         ret = 0;
635 bail:
636         if (dirent_bh)
637                 brelse(dirent_bh);
638
639         mlog_exit(ret);
640         return ret;
641 }
642
643 /*
644  * routine to check that the specified directory is empty (for rmdir)
645  */
646 int ocfs2_empty_dir(struct inode *inode)
647 {
648         unsigned long offset;
649         struct buffer_head * bh;
650         struct ocfs2_dir_entry * de, * de1;
651         struct super_block * sb;
652         int err;
653
654         sb = inode->i_sb;
655         if ((i_size_read(inode) <
656              (OCFS2_DIR_REC_LEN(1) + OCFS2_DIR_REC_LEN(2))) ||
657             !(bh = ocfs2_bread(inode, 0, &err, 0))) {
658                 mlog(ML_ERROR, "bad directory (dir #%llu) - no data block\n",
659                      (unsigned long long)OCFS2_I(inode)->ip_blkno);
660                 return 1;
661         }
662
663         de = (struct ocfs2_dir_entry *) bh->b_data;
664         de1 = (struct ocfs2_dir_entry *)
665                         ((char *)de + le16_to_cpu(de->rec_len));
666         if ((le64_to_cpu(de->inode) != OCFS2_I(inode)->ip_blkno) ||
667                         !le64_to_cpu(de1->inode) ||
668                         strcmp(".", de->name) ||
669                         strcmp("..", de1->name)) {
670                 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
671                      (unsigned long long)OCFS2_I(inode)->ip_blkno);
672                 brelse(bh);
673                 return 1;
674         }
675         offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len);
676         de = (struct ocfs2_dir_entry *)((char *)de1 + le16_to_cpu(de1->rec_len));
677         while (offset < i_size_read(inode) ) {
678                 if (!bh || (void *)de >= (void *)(bh->b_data + sb->s_blocksize)) {
679                         brelse(bh);
680                         bh = ocfs2_bread(inode,
681                                          offset >> sb->s_blocksize_bits, &err, 0);
682                         if (!bh) {
683                                 mlog(ML_ERROR, "dir %llu has a hole at %lu\n",
684                                      (unsigned long long)OCFS2_I(inode)->ip_blkno, offset);
685                                 offset += sb->s_blocksize;
686                                 continue;
687                         }
688                         de = (struct ocfs2_dir_entry *) bh->b_data;
689                 }
690                 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
691                         brelse(bh);
692                         return 1;
693                 }
694                 if (le64_to_cpu(de->inode)) {
695                         brelse(bh);
696                         return 0;
697                 }
698                 offset += le16_to_cpu(de->rec_len);
699                 de = (struct ocfs2_dir_entry *)
700                         ((char *)de + le16_to_cpu(de->rec_len));
701         }
702         brelse(bh);
703         return 1;
704 }
705
706 int ocfs2_fill_new_dir(struct ocfs2_super *osb,
707                        handle_t *handle,
708                        struct inode *parent,
709                        struct inode *inode,
710                        struct buffer_head *fe_bh,
711                        struct ocfs2_alloc_context *data_ac)
712 {
713         int status;
714         struct buffer_head *new_bh = NULL;
715         struct ocfs2_dir_entry *de = NULL;
716
717         mlog_entry_void();
718
719         status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
720                                      data_ac, NULL, &new_bh);
721         if (status < 0) {
722                 mlog_errno(status);
723                 goto bail;
724         }
725
726         ocfs2_set_new_buffer_uptodate(inode, new_bh);
727
728         status = ocfs2_journal_access(handle, inode, new_bh,
729                                       OCFS2_JOURNAL_ACCESS_CREATE);
730         if (status < 0) {
731                 mlog_errno(status);
732                 goto bail;
733         }
734         memset(new_bh->b_data, 0, osb->sb->s_blocksize);
735
736         de = (struct ocfs2_dir_entry *) new_bh->b_data;
737         de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
738         de->name_len = 1;
739         de->rec_len =
740                 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
741         strcpy(de->name, ".");
742         ocfs2_set_de_type(de, S_IFDIR);
743         de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
744         de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
745         de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize -
746                                   OCFS2_DIR_REC_LEN(1));
747         de->name_len = 2;
748         strcpy(de->name, "..");
749         ocfs2_set_de_type(de, S_IFDIR);
750
751         status = ocfs2_journal_dirty(handle, new_bh);
752         if (status < 0) {
753                 mlog_errno(status);
754                 goto bail;
755         }
756
757         i_size_write(inode, inode->i_sb->s_blocksize);
758         inode->i_nlink = 2;
759         inode->i_blocks = ocfs2_inode_sector_count(inode);
760         status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
761         if (status < 0) {
762                 mlog_errno(status);
763                 goto bail;
764         }
765
766         status = 0;
767 bail:
768         if (new_bh)
769                 brelse(new_bh);
770
771         mlog_exit(status);
772         return status;
773 }
774
775 /* returns a bh of the 1st new block in the allocation. */
776 static int ocfs2_do_extend_dir(struct super_block *sb,
777                                handle_t *handle,
778                                struct inode *dir,
779                                struct buffer_head *parent_fe_bh,
780                                struct ocfs2_alloc_context *data_ac,
781                                struct ocfs2_alloc_context *meta_ac,
782                                struct buffer_head **new_bh)
783 {
784         int status;
785         int extend;
786         u64 p_blkno, v_blkno;
787
788         spin_lock(&OCFS2_I(dir)->ip_lock);
789         extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
790         spin_unlock(&OCFS2_I(dir)->ip_lock);
791
792         if (extend) {
793                 u32 offset = OCFS2_I(dir)->ip_clusters;
794
795                 status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, &offset,
796                                                     1, 0, parent_fe_bh, handle,
797                                                     data_ac, meta_ac, NULL);
798                 BUG_ON(status == -EAGAIN);
799                 if (status < 0) {
800                         mlog_errno(status);
801                         goto bail;
802                 }
803         }
804
805         v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
806         status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
807         if (status < 0) {
808                 mlog_errno(status);
809                 goto bail;
810         }
811
812         *new_bh = sb_getblk(sb, p_blkno);
813         if (!*new_bh) {
814                 status = -EIO;
815                 mlog_errno(status);
816                 goto bail;
817         }
818         status = 0;
819 bail:
820         mlog_exit(status);
821         return status;
822 }
823
824 /* assumes you already have a cluster lock on the directory. */
825 static int ocfs2_extend_dir(struct ocfs2_super *osb,
826                             struct inode *dir,
827                             struct buffer_head *parent_fe_bh,
828                             struct buffer_head **new_de_bh)
829 {
830         int status = 0;
831         int credits, num_free_extents, drop_alloc_sem = 0;
832         loff_t dir_i_size;
833         struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
834         struct ocfs2_alloc_context *data_ac = NULL;
835         struct ocfs2_alloc_context *meta_ac = NULL;
836         handle_t *handle = NULL;
837         struct buffer_head *new_bh = NULL;
838         struct ocfs2_dir_entry * de;
839         struct super_block *sb = osb->sb;
840
841         mlog_entry_void();
842
843         dir_i_size = i_size_read(dir);
844         mlog(0, "extending dir %llu (i_size = %lld)\n",
845              (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
846
847         /* dir->i_size is always block aligned. */
848         spin_lock(&OCFS2_I(dir)->ip_lock);
849         if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
850                 spin_unlock(&OCFS2_I(dir)->ip_lock);
851                 num_free_extents = ocfs2_num_free_extents(osb, dir, fe);
852                 if (num_free_extents < 0) {
853                         status = num_free_extents;
854                         mlog_errno(status);
855                         goto bail;
856                 }
857
858                 if (!num_free_extents) {
859                         status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac);
860                         if (status < 0) {
861                                 if (status != -ENOSPC)
862                                         mlog_errno(status);
863                                 goto bail;
864                         }
865                 }
866
867                 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
868                 if (status < 0) {
869                         if (status != -ENOSPC)
870                                 mlog_errno(status);
871                         goto bail;
872                 }
873
874                 credits = ocfs2_calc_extend_credits(sb, fe, 1);
875         } else {
876                 spin_unlock(&OCFS2_I(dir)->ip_lock);
877                 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
878         }
879
880         down_write(&OCFS2_I(dir)->ip_alloc_sem);
881         drop_alloc_sem = 1;
882
883         handle = ocfs2_start_trans(osb, credits);
884         if (IS_ERR(handle)) {
885                 status = PTR_ERR(handle);
886                 handle = NULL;
887                 mlog_errno(status);
888                 goto bail;
889         }
890
891         status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
892                                      data_ac, meta_ac, &new_bh);
893         if (status < 0) {
894                 mlog_errno(status);
895                 goto bail;
896         }
897
898         ocfs2_set_new_buffer_uptodate(dir, new_bh);
899
900         status = ocfs2_journal_access(handle, dir, new_bh,
901                                       OCFS2_JOURNAL_ACCESS_CREATE);
902         if (status < 0) {
903                 mlog_errno(status);
904                 goto bail;
905         }
906         memset(new_bh->b_data, 0, sb->s_blocksize);
907         de = (struct ocfs2_dir_entry *) new_bh->b_data;
908         de->inode = 0;
909         de->rec_len = cpu_to_le16(sb->s_blocksize);
910         status = ocfs2_journal_dirty(handle, new_bh);
911         if (status < 0) {
912                 mlog_errno(status);
913                 goto bail;
914         }
915
916         dir_i_size += dir->i_sb->s_blocksize;
917         i_size_write(dir, dir_i_size);
918         dir->i_blocks = ocfs2_inode_sector_count(dir);
919         status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
920         if (status < 0) {
921                 mlog_errno(status);
922                 goto bail;
923         }
924
925         *new_de_bh = new_bh;
926         get_bh(*new_de_bh);
927 bail:
928         if (drop_alloc_sem)
929                 up_write(&OCFS2_I(dir)->ip_alloc_sem);
930         if (handle)
931                 ocfs2_commit_trans(osb, handle);
932
933         if (data_ac)
934                 ocfs2_free_alloc_context(data_ac);
935         if (meta_ac)
936                 ocfs2_free_alloc_context(meta_ac);
937
938         if (new_bh)
939                 brelse(new_bh);
940
941         mlog_exit(status);
942         return status;
943 }
944
945 /*
946  * Search the dir for a good spot, extending it if necessary. The
947  * block containing an appropriate record is returned in ret_de_bh.
948  */
949 int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
950                                  struct inode *dir,
951                                  struct buffer_head *parent_fe_bh,
952                                  const char *name,
953                                  int namelen,
954                                  struct buffer_head **ret_de_bh)
955 {
956         unsigned long offset;
957         struct buffer_head * bh = NULL;
958         unsigned short rec_len;
959         struct ocfs2_dinode *fe;
960         struct ocfs2_dir_entry *de;
961         struct super_block *sb;
962         int status;
963
964         mlog_entry_void();
965
966         mlog(0, "getting ready to insert namelen %d into dir %llu\n",
967              namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
968
969         BUG_ON(!S_ISDIR(dir->i_mode));
970         fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
971         BUG_ON(le64_to_cpu(fe->i_size) != i_size_read(dir));
972
973         sb = dir->i_sb;
974
975         if (!namelen) {
976                 status = -EINVAL;
977                 mlog_errno(status);
978                 goto bail;
979         }
980
981         bh = ocfs2_bread(dir, 0, &status, 0);
982         if (!bh) {
983                 mlog_errno(status);
984                 goto bail;
985         }
986
987         rec_len = OCFS2_DIR_REC_LEN(namelen);
988         offset = 0;
989         de = (struct ocfs2_dir_entry *) bh->b_data;
990         while (1) {
991                 if ((char *)de >= sb->s_blocksize + bh->b_data) {
992                         brelse(bh);
993                         bh = NULL;
994
995                         if (i_size_read(dir) <= offset) {
996                                 status = ocfs2_extend_dir(osb,
997                                                           dir,
998                                                           parent_fe_bh,
999                                                           &bh);
1000                                 if (status < 0) {
1001                                         mlog_errno(status);
1002                                         goto bail;
1003                                 }
1004                                 BUG_ON(!bh);
1005                                 *ret_de_bh = bh;
1006                                 get_bh(*ret_de_bh);
1007                                 goto bail;
1008                         }
1009                         bh = ocfs2_bread(dir,
1010                                          offset >> sb->s_blocksize_bits,
1011                                          &status,
1012                                          0);
1013                         if (!bh) {
1014                                 mlog_errno(status);
1015                                 goto bail;
1016                         }
1017                         /* move to next block */
1018                         de = (struct ocfs2_dir_entry *) bh->b_data;
1019                 }
1020                 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1021                         status = -ENOENT;
1022                         goto bail;
1023                 }
1024                 if (ocfs2_match(namelen, name, de)) {
1025                         status = -EEXIST;
1026                         goto bail;
1027                 }
1028                 if (((le64_to_cpu(de->inode) == 0) &&
1029                      (le16_to_cpu(de->rec_len) >= rec_len)) ||
1030                     (le16_to_cpu(de->rec_len) >=
1031                      (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
1032                         /* Ok, we found a spot. Return this bh and let
1033                          * the caller actually fill it in. */
1034                         *ret_de_bh = bh;
1035                         get_bh(*ret_de_bh);
1036                         status = 0;
1037                         goto bail;
1038                 }
1039                 offset += le16_to_cpu(de->rec_len);
1040                 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
1041         }
1042
1043         status = 0;
1044 bail:
1045         if (bh)
1046                 brelse(bh);
1047
1048         mlog_exit(status);
1049         return status;
1050 }