76ffb5c10b3e0a0a8c21ddbf22d774b5cc55cf5a
[linux-2.6.git] / 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 #include <linux/quotaops.h>
44
45 #define MLOG_MASK_PREFIX ML_NAMEI
46 #include <cluster/masklog.h>
47
48 #include "ocfs2.h"
49
50 #include "alloc.h"
51 #include "blockcheck.h"
52 #include "dir.h"
53 #include "dlmglue.h"
54 #include "extent_map.h"
55 #include "file.h"
56 #include "inode.h"
57 #include "journal.h"
58 #include "namei.h"
59 #include "suballoc.h"
60 #include "super.h"
61 #include "uptodate.h"
62
63 #include "buffer_head_io.h"
64
65 #define NAMEI_RA_CHUNKS  2
66 #define NAMEI_RA_BLOCKS  4
67 #define NAMEI_RA_SIZE        (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
68 #define NAMEI_RA_INDEX(c,b)  (((c) * NAMEI_RA_BLOCKS) + (b))
69
70 static unsigned char ocfs2_filetype_table[] = {
71         DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
72 };
73
74 static int ocfs2_extend_dir(struct ocfs2_super *osb,
75                             struct inode *dir,
76                             struct buffer_head *parent_fe_bh,
77                             unsigned int blocks_wanted,
78                             struct buffer_head **new_de_bh);
79 static int ocfs2_do_extend_dir(struct super_block *sb,
80                                handle_t *handle,
81                                struct inode *dir,
82                                struct buffer_head *parent_fe_bh,
83                                struct ocfs2_alloc_context *data_ac,
84                                struct ocfs2_alloc_context *meta_ac,
85                                struct buffer_head **new_bh);
86
87 /*
88  * These are distinct checks because future versions of the file system will
89  * want to have a trailing dirent structure independent of indexing.
90  */
91 static int ocfs2_dir_has_trailer(struct inode *dir)
92 {
93         if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
94                 return 0;
95
96         return ocfs2_meta_ecc(OCFS2_SB(dir->i_sb));
97 }
98
99 static int ocfs2_supports_dir_trailer(struct ocfs2_super *osb)
100 {
101         return ocfs2_meta_ecc(osb);
102 }
103
104 static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block *sb)
105 {
106         return sb->s_blocksize - sizeof(struct ocfs2_dir_block_trailer);
107 }
108
109 #define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb))))
110
111 /* XXX ocfs2_block_dqtrailer() is similar but not quite - can we make
112  * them more consistent? */
113 struct ocfs2_dir_block_trailer *ocfs2_dir_trailer_from_size(int blocksize,
114                                                             void *data)
115 {
116         char *p = data;
117
118         p += blocksize - sizeof(struct ocfs2_dir_block_trailer);
119         return (struct ocfs2_dir_block_trailer *)p;
120 }
121
122 /*
123  * XXX: This is executed once on every dirent. We should consider optimizing
124  * it.
125  */
126 static int ocfs2_skip_dir_trailer(struct inode *dir,
127                                   struct ocfs2_dir_entry *de,
128                                   unsigned long offset,
129                                   unsigned long blklen)
130 {
131         unsigned long toff = blklen - sizeof(struct ocfs2_dir_block_trailer);
132
133         if (!ocfs2_dir_has_trailer(dir))
134                 return 0;
135
136         if (offset != toff)
137                 return 0;
138
139         return 1;
140 }
141
142 static void ocfs2_init_dir_trailer(struct inode *inode,
143                                    struct buffer_head *bh)
144 {
145         struct ocfs2_dir_block_trailer *trailer;
146
147         trailer = ocfs2_trailer_from_bh(bh, inode->i_sb);
148         strcpy(trailer->db_signature, OCFS2_DIR_TRAILER_SIGNATURE);
149         trailer->db_compat_rec_len =
150                         cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer));
151         trailer->db_parent_dinode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
152         trailer->db_blkno = cpu_to_le64(bh->b_blocknr);
153 }
154
155 void ocfs2_free_dir_lookup_result(struct ocfs2_dir_lookup_result *res)
156 {
157         brelse(res->dl_leaf_bh);
158 }
159
160 /*
161  * bh passed here can be an inode block or a dir data block, depending
162  * on the inode inline data flag.
163  */
164 static int ocfs2_check_dir_entry(struct inode * dir,
165                                  struct ocfs2_dir_entry * de,
166                                  struct buffer_head * bh,
167                                  unsigned long offset)
168 {
169         const char *error_msg = NULL;
170         const int rlen = le16_to_cpu(de->rec_len);
171
172         if (rlen < OCFS2_DIR_REC_LEN(1))
173                 error_msg = "rec_len is smaller than minimal";
174         else if (rlen % 4 != 0)
175                 error_msg = "rec_len % 4 != 0";
176         else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
177                 error_msg = "rec_len is too small for name_len";
178         else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
179                 error_msg = "directory entry across blocks";
180
181         if (error_msg != NULL)
182                 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
183                      "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
184                      (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
185                      offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
186                      de->name_len);
187         return error_msg == NULL ? 1 : 0;
188 }
189
190 static inline int ocfs2_match(int len,
191                               const char * const name,
192                               struct ocfs2_dir_entry *de)
193 {
194         if (len != de->name_len)
195                 return 0;
196         if (!de->inode)
197                 return 0;
198         return !memcmp(name, de->name, len);
199 }
200
201 /*
202  * Returns 0 if not found, -1 on failure, and 1 on success
203  */
204 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
205                                         struct inode *dir,
206                                         const char *name, int namelen,
207                                         unsigned long offset,
208                                         char *first_de,
209                                         unsigned int bytes,
210                                         struct ocfs2_dir_entry **res_dir)
211 {
212         struct ocfs2_dir_entry *de;
213         char *dlimit, *de_buf;
214         int de_len;
215         int ret = 0;
216
217         mlog_entry_void();
218
219         de_buf = first_de;
220         dlimit = de_buf + bytes;
221
222         while (de_buf < dlimit) {
223                 /* this code is executed quadratically often */
224                 /* do minimal checking `by hand' */
225
226                 de = (struct ocfs2_dir_entry *) de_buf;
227
228                 if (de_buf + namelen <= dlimit &&
229                     ocfs2_match(namelen, name, de)) {
230                         /* found a match - just to be sure, do a full check */
231                         if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
232                                 ret = -1;
233                                 goto bail;
234                         }
235                         *res_dir = de;
236                         ret = 1;
237                         goto bail;
238                 }
239
240                 /* prevent looping on a bad block */
241                 de_len = le16_to_cpu(de->rec_len);
242                 if (de_len <= 0) {
243                         ret = -1;
244                         goto bail;
245                 }
246
247                 de_buf += de_len;
248                 offset += de_len;
249         }
250
251 bail:
252         mlog_exit(ret);
253         return ret;
254 }
255
256 static struct buffer_head *ocfs2_find_entry_id(const char *name,
257                                                int namelen,
258                                                struct inode *dir,
259                                                struct ocfs2_dir_entry **res_dir)
260 {
261         int ret, found;
262         struct buffer_head *di_bh = NULL;
263         struct ocfs2_dinode *di;
264         struct ocfs2_inline_data *data;
265
266         ret = ocfs2_read_inode_block(dir, &di_bh);
267         if (ret) {
268                 mlog_errno(ret);
269                 goto out;
270         }
271
272         di = (struct ocfs2_dinode *)di_bh->b_data;
273         data = &di->id2.i_data;
274
275         found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
276                                       data->id_data, i_size_read(dir), res_dir);
277         if (found == 1)
278                 return di_bh;
279
280         brelse(di_bh);
281 out:
282         return NULL;
283 }
284
285 static int ocfs2_validate_dir_block(struct super_block *sb,
286                                     struct buffer_head *bh)
287 {
288         int rc;
289         struct ocfs2_dir_block_trailer *trailer =
290                 ocfs2_trailer_from_bh(bh, sb);
291
292
293         /*
294          * We don't validate dirents here, that's handled
295          * in-place when the code walks them.
296          */
297         mlog(0, "Validating dirblock %llu\n",
298              (unsigned long long)bh->b_blocknr);
299
300         BUG_ON(!buffer_uptodate(bh));
301
302         /*
303          * If the ecc fails, we return the error but otherwise
304          * leave the filesystem running.  We know any error is
305          * local to this block.
306          *
307          * Note that we are safe to call this even if the directory
308          * doesn't have a trailer.  Filesystems without metaecc will do
309          * nothing, and filesystems with it will have one.
310          */
311         rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &trailer->db_check);
312         if (rc)
313                 mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
314                      (unsigned long long)bh->b_blocknr);
315
316         return rc;
317 }
318
319 /*
320  * This function forces all errors to -EIO for consistency with its
321  * predecessor, ocfs2_bread().  We haven't audited what returning the
322  * real error codes would do to callers.  We log the real codes with
323  * mlog_errno() before we squash them.
324  */
325 static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
326                                 struct buffer_head **bh, int flags)
327 {
328         int rc = 0;
329         struct buffer_head *tmp = *bh;
330         struct ocfs2_dir_block_trailer *trailer;
331
332         rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
333                                     ocfs2_validate_dir_block);
334         if (rc) {
335                 mlog_errno(rc);
336                 goto out;
337         }
338
339         /*
340          * We check the trailer here rather than in
341          * ocfs2_validate_dir_block() because that function doesn't have
342          * the inode to test.
343          */
344         if (!(flags & OCFS2_BH_READAHEAD) &&
345             ocfs2_dir_has_trailer(inode)) {
346                 trailer = ocfs2_trailer_from_bh(tmp, inode->i_sb);
347                 if (!OCFS2_IS_VALID_DIR_TRAILER(trailer)) {
348                         rc = -EINVAL;
349                         ocfs2_error(inode->i_sb,
350                                     "Invalid dirblock #%llu: "
351                                     "signature = %.*s\n",
352                                     (unsigned long long)tmp->b_blocknr, 7,
353                                     trailer->db_signature);
354                         goto out;
355                 }
356                 if (le64_to_cpu(trailer->db_blkno) != tmp->b_blocknr) {
357                         rc = -EINVAL;
358                         ocfs2_error(inode->i_sb,
359                                     "Directory block #%llu has an invalid "
360                                     "db_blkno of %llu",
361                                     (unsigned long long)tmp->b_blocknr,
362                                     (unsigned long long)le64_to_cpu(trailer->db_blkno));
363                         goto out;
364                 }
365                 if (le64_to_cpu(trailer->db_parent_dinode) !=
366                     OCFS2_I(inode)->ip_blkno) {
367                         rc = -EINVAL;
368                         ocfs2_error(inode->i_sb,
369                                     "Directory block #%llu on dinode "
370                                     "#%llu has an invalid parent_dinode "
371                                     "of %llu",
372                                     (unsigned long long)tmp->b_blocknr,
373                                     (unsigned long long)OCFS2_I(inode)->ip_blkno,
374                                     (unsigned long long)le64_to_cpu(trailer->db_blkno));
375                         goto out;
376                 }
377         }
378
379         /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
380         if (!*bh)
381                 *bh = tmp;
382
383 out:
384         return rc ? -EIO : 0;
385 }
386
387 static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
388                                                struct inode *dir,
389                                                struct ocfs2_dir_entry **res_dir)
390 {
391         struct super_block *sb;
392         struct buffer_head *bh_use[NAMEI_RA_SIZE];
393         struct buffer_head *bh, *ret = NULL;
394         unsigned long start, block, b;
395         int ra_max = 0;         /* Number of bh's in the readahead
396                                    buffer, bh_use[] */
397         int ra_ptr = 0;         /* Current index into readahead
398                                    buffer */
399         int num = 0;
400         int nblocks, i, err;
401
402         mlog_entry_void();
403
404         sb = dir->i_sb;
405
406         nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
407         start = OCFS2_I(dir)->ip_dir_start_lookup;
408         if (start >= nblocks)
409                 start = 0;
410         block = start;
411
412 restart:
413         do {
414                 /*
415                  * We deal with the read-ahead logic here.
416                  */
417                 if (ra_ptr >= ra_max) {
418                         /* Refill the readahead buffer */
419                         ra_ptr = 0;
420                         b = block;
421                         for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
422                                 /*
423                                  * Terminate if we reach the end of the
424                                  * directory and must wrap, or if our
425                                  * search has finished at this block.
426                                  */
427                                 if (b >= nblocks || (num && block == start)) {
428                                         bh_use[ra_max] = NULL;
429                                         break;
430                                 }
431                                 num++;
432
433                                 bh = NULL;
434                                 err = ocfs2_read_dir_block(dir, b++, &bh,
435                                                            OCFS2_BH_READAHEAD);
436                                 bh_use[ra_max] = bh;
437                         }
438                 }
439                 if ((bh = bh_use[ra_ptr++]) == NULL)
440                         goto next;
441                 if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
442                         /* read error, skip block & hope for the best.
443                          * ocfs2_read_dir_block() has released the bh. */
444                         ocfs2_error(dir->i_sb, "reading directory %llu, "
445                                     "offset %lu\n",
446                                     (unsigned long long)OCFS2_I(dir)->ip_blkno,
447                                     block);
448                         goto next;
449                 }
450                 i = ocfs2_search_dirblock(bh, dir, name, namelen,
451                                           block << sb->s_blocksize_bits,
452                                           bh->b_data, sb->s_blocksize,
453                                           res_dir);
454                 if (i == 1) {
455                         OCFS2_I(dir)->ip_dir_start_lookup = block;
456                         ret = bh;
457                         goto cleanup_and_exit;
458                 } else {
459                         brelse(bh);
460                         if (i < 0)
461                                 goto cleanup_and_exit;
462                 }
463         next:
464                 if (++block >= nblocks)
465                         block = 0;
466         } while (block != start);
467
468         /*
469          * If the directory has grown while we were searching, then
470          * search the last part of the directory before giving up.
471          */
472         block = nblocks;
473         nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
474         if (block < nblocks) {
475                 start = 0;
476                 goto restart;
477         }
478
479 cleanup_and_exit:
480         /* Clean up the read-ahead blocks */
481         for (; ra_ptr < ra_max; ra_ptr++)
482                 brelse(bh_use[ra_ptr]);
483
484         mlog_exit_ptr(ret);
485         return ret;
486 }
487
488 /*
489  * Try to find an entry of the provided name within 'dir'.
490  *
491  * If nothing was found, -ENOENT is returned. Otherwise, zero is
492  * returned and the struct 'res' will contain information useful to
493  * other directory manipulation functions.
494  *
495  * Caller can NOT assume anything about the contents of the
496  * buffer_heads - they are passed back only so that it can be passed into
497  * any one of the manipulation functions (add entry, delete entry,
498  * etc). As an example, bh in the extent directory case is a data
499  * block, in the inline-data case it actually points to an inode.
500  */
501 int ocfs2_find_entry(const char *name, int namelen,
502                      struct inode *dir, struct ocfs2_dir_lookup_result *lookup)
503 {
504         struct buffer_head *bh;
505         struct ocfs2_dir_entry *res_dir = NULL;
506
507         if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
508                 bh = ocfs2_find_entry_id(name, namelen, dir, &res_dir);
509         else
510                 bh = ocfs2_find_entry_el(name, namelen, dir, &res_dir);
511
512         if (bh == NULL)
513                 return -ENOENT;
514
515         lookup->dl_leaf_bh = bh;
516         lookup->dl_entry = res_dir;
517         return 0;
518 }
519
520 /*
521  * Update inode number and type of a previously found directory entry.
522  */
523 int ocfs2_update_entry(struct inode *dir, handle_t *handle,
524                        struct ocfs2_dir_lookup_result *res,
525                        struct inode *new_entry_inode)
526 {
527         int ret;
528         ocfs2_journal_access_func access = ocfs2_journal_access_db;
529         struct ocfs2_dir_entry *de = res->dl_entry;
530         struct buffer_head *de_bh = res->dl_leaf_bh;
531
532         /*
533          * The same code works fine for both inline-data and extent
534          * based directories, so no need to split this up.  The only
535          * difference is the journal_access function.
536          */
537
538         if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
539                 access = ocfs2_journal_access_di;
540
541         ret = access(handle, dir, de_bh, OCFS2_JOURNAL_ACCESS_WRITE);
542         if (ret) {
543                 mlog_errno(ret);
544                 goto out;
545         }
546
547         de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
548         ocfs2_set_de_type(de, new_entry_inode->i_mode);
549
550         ocfs2_journal_dirty(handle, de_bh);
551
552 out:
553         return ret;
554 }
555
556 static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
557                                 struct ocfs2_dir_entry *de_del,
558                                 struct buffer_head *bh, char *first_de,
559                                 unsigned int bytes)
560 {
561         struct ocfs2_dir_entry *de, *pde;
562         int i, status = -ENOENT;
563         ocfs2_journal_access_func access = ocfs2_journal_access_db;
564
565         mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
566
567         if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
568                 access = ocfs2_journal_access_di;
569
570         i = 0;
571         pde = NULL;
572         de = (struct ocfs2_dir_entry *) first_de;
573         while (i < bytes) {
574                 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
575                         status = -EIO;
576                         mlog_errno(status);
577                         goto bail;
578                 }
579                 if (de == de_del)  {
580                         status = access(handle, dir, bh,
581                                         OCFS2_JOURNAL_ACCESS_WRITE);
582                         if (status < 0) {
583                                 status = -EIO;
584                                 mlog_errno(status);
585                                 goto bail;
586                         }
587                         if (pde)
588                                 le16_add_cpu(&pde->rec_len,
589                                                 le16_to_cpu(de->rec_len));
590                         else
591                                 de->inode = 0;
592                         dir->i_version++;
593                         status = ocfs2_journal_dirty(handle, bh);
594                         goto bail;
595                 }
596                 i += le16_to_cpu(de->rec_len);
597                 pde = de;
598                 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
599         }
600 bail:
601         mlog_exit(status);
602         return status;
603 }
604
605 static inline int ocfs2_delete_entry_id(handle_t *handle,
606                                         struct inode *dir,
607                                         struct ocfs2_dir_entry *de_del,
608                                         struct buffer_head *bh)
609 {
610         int ret;
611         struct buffer_head *di_bh = NULL;
612         struct ocfs2_dinode *di;
613         struct ocfs2_inline_data *data;
614
615         ret = ocfs2_read_inode_block(dir, &di_bh);
616         if (ret) {
617                 mlog_errno(ret);
618                 goto out;
619         }
620
621         di = (struct ocfs2_dinode *)di_bh->b_data;
622         data = &di->id2.i_data;
623
624         ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
625                                    i_size_read(dir));
626
627         brelse(di_bh);
628 out:
629         return ret;
630 }
631
632 static inline int ocfs2_delete_entry_el(handle_t *handle,
633                                         struct inode *dir,
634                                         struct ocfs2_dir_entry *de_del,
635                                         struct buffer_head *bh)
636 {
637         return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
638                                     bh->b_size);
639 }
640
641 /*
642  * ocfs2_delete_entry deletes a directory entry by merging it with the
643  * previous entry
644  */
645 int ocfs2_delete_entry(handle_t *handle,
646                        struct inode *dir,
647                        struct ocfs2_dir_lookup_result *res)
648 {
649         if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
650                 return ocfs2_delete_entry_id(handle, dir, res->dl_entry,
651                                              res->dl_leaf_bh);
652
653         return ocfs2_delete_entry_el(handle, dir, res->dl_entry,
654                                      res->dl_leaf_bh);
655 }
656
657 /*
658  * Check whether 'de' has enough room to hold an entry of
659  * 'new_rec_len' bytes.
660  */
661 static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
662                                          unsigned int new_rec_len)
663 {
664         unsigned int de_really_used;
665
666         /* Check whether this is an empty record with enough space */
667         if (le64_to_cpu(de->inode) == 0 &&
668             le16_to_cpu(de->rec_len) >= new_rec_len)
669                 return 1;
670
671         /*
672          * Record might have free space at the end which we can
673          * use.
674          */
675         de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
676         if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
677             return 1;
678
679         return 0;
680 }
681
682 /* we don't always have a dentry for what we want to add, so people
683  * like orphan dir can call this instead.
684  *
685  * The lookup context must have been filled from
686  * ocfs2_prepare_dir_for_insert.
687  */
688 int __ocfs2_add_entry(handle_t *handle,
689                       struct inode *dir,
690                       const char *name, int namelen,
691                       struct inode *inode, u64 blkno,
692                       struct buffer_head *parent_fe_bh,
693                       struct ocfs2_dir_lookup_result *lookup)
694 {
695         unsigned long offset;
696         unsigned short rec_len;
697         struct ocfs2_dir_entry *de, *de1;
698         struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
699         struct super_block *sb = dir->i_sb;
700         int retval, status;
701         unsigned int size = sb->s_blocksize;
702         struct buffer_head *insert_bh = lookup->dl_leaf_bh;
703         char *data_start = insert_bh->b_data;
704
705         mlog_entry_void();
706
707         if (!namelen)
708                 return -EINVAL;
709
710         if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
711                 data_start = di->id2.i_data.id_data;
712                 size = i_size_read(dir);
713
714                 BUG_ON(insert_bh != parent_fe_bh);
715         }
716
717         rec_len = OCFS2_DIR_REC_LEN(namelen);
718         offset = 0;
719         de = (struct ocfs2_dir_entry *) data_start;
720         while (1) {
721                 BUG_ON((char *)de >= (size + data_start));
722
723                 /* These checks should've already been passed by the
724                  * prepare function, but I guess we can leave them
725                  * here anyway. */
726                 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
727                         retval = -ENOENT;
728                         goto bail;
729                 }
730                 if (ocfs2_match(namelen, name, de)) {
731                         retval = -EEXIST;
732                         goto bail;
733                 }
734
735                 /* We're guaranteed that we should have space, so we
736                  * can't possibly have hit the trailer...right? */
737                 mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size),
738                                 "Hit dir trailer trying to insert %.*s "
739                                 "(namelen %d) into directory %llu.  "
740                                 "offset is %lu, trailer offset is %d\n",
741                                 namelen, name, namelen,
742                                 (unsigned long long)parent_fe_bh->b_blocknr,
743                                 offset, ocfs2_dir_trailer_blk_off(dir->i_sb));
744
745                 if (ocfs2_dirent_would_fit(de, rec_len)) {
746                         dir->i_mtime = dir->i_ctime = CURRENT_TIME;
747                         retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
748                         if (retval < 0) {
749                                 mlog_errno(retval);
750                                 goto bail;
751                         }
752
753                         if (insert_bh == parent_fe_bh)
754                                 status = ocfs2_journal_access_di(handle, dir,
755                                                                  insert_bh,
756                                                                  OCFS2_JOURNAL_ACCESS_WRITE);
757                         else
758                                 status = ocfs2_journal_access_db(handle, dir,
759                                                                  insert_bh,
760                                                                  OCFS2_JOURNAL_ACCESS_WRITE);
761                         /* By now the buffer is marked for journaling */
762                         offset += le16_to_cpu(de->rec_len);
763                         if (le64_to_cpu(de->inode)) {
764                                 de1 = (struct ocfs2_dir_entry *)((char *) de +
765                                         OCFS2_DIR_REC_LEN(de->name_len));
766                                 de1->rec_len =
767                                         cpu_to_le16(le16_to_cpu(de->rec_len) -
768                                         OCFS2_DIR_REC_LEN(de->name_len));
769                                 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
770                                 de = de1;
771                         }
772                         de->file_type = OCFS2_FT_UNKNOWN;
773                         if (blkno) {
774                                 de->inode = cpu_to_le64(blkno);
775                                 ocfs2_set_de_type(de, inode->i_mode);
776                         } else
777                                 de->inode = 0;
778                         de->name_len = namelen;
779                         memcpy(de->name, name, namelen);
780
781                         dir->i_version++;
782                         status = ocfs2_journal_dirty(handle, insert_bh);
783                         retval = 0;
784                         goto bail;
785                 }
786
787                 offset += le16_to_cpu(de->rec_len);
788                 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
789         }
790
791         /* when you think about it, the assert above should prevent us
792          * from ever getting here. */
793         retval = -ENOSPC;
794 bail:
795
796         mlog_exit(retval);
797         return retval;
798 }
799
800 static int ocfs2_dir_foreach_blk_id(struct inode *inode,
801                                     u64 *f_version,
802                                     loff_t *f_pos, void *priv,
803                                     filldir_t filldir, int *filldir_err)
804 {
805         int ret, i, filldir_ret;
806         unsigned long offset = *f_pos;
807         struct buffer_head *di_bh = NULL;
808         struct ocfs2_dinode *di;
809         struct ocfs2_inline_data *data;
810         struct ocfs2_dir_entry *de;
811
812         ret = ocfs2_read_inode_block(inode, &di_bh);
813         if (ret) {
814                 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
815                      (unsigned long long)OCFS2_I(inode)->ip_blkno);
816                 goto out;
817         }
818
819         di = (struct ocfs2_dinode *)di_bh->b_data;
820         data = &di->id2.i_data;
821
822         while (*f_pos < i_size_read(inode)) {
823 revalidate:
824                 /* If the dir block has changed since the last call to
825                  * readdir(2), then we might be pointing to an invalid
826                  * dirent right now.  Scan from the start of the block
827                  * to make sure. */
828                 if (*f_version != inode->i_version) {
829                         for (i = 0; i < i_size_read(inode) && i < offset; ) {
830                                 de = (struct ocfs2_dir_entry *)
831                                         (data->id_data + i);
832                                 /* It's too expensive to do a full
833                                  * dirent test each time round this
834                                  * loop, but we do have to test at
835                                  * least that it is non-zero.  A
836                                  * failure will be detected in the
837                                  * dirent test below. */
838                                 if (le16_to_cpu(de->rec_len) <
839                                     OCFS2_DIR_REC_LEN(1))
840                                         break;
841                                 i += le16_to_cpu(de->rec_len);
842                         }
843                         *f_pos = offset = i;
844                         *f_version = inode->i_version;
845                 }
846
847                 de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
848                 if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
849                         /* On error, skip the f_pos to the end. */
850                         *f_pos = i_size_read(inode);
851                         goto out;
852                 }
853                 offset += le16_to_cpu(de->rec_len);
854                 if (le64_to_cpu(de->inode)) {
855                         /* We might block in the next section
856                          * if the data destination is
857                          * currently swapped out.  So, use a
858                          * version stamp to detect whether or
859                          * not the directory has been modified
860                          * during the copy operation.
861                          */
862                         u64 version = *f_version;
863                         unsigned char d_type = DT_UNKNOWN;
864
865                         if (de->file_type < OCFS2_FT_MAX)
866                                 d_type = ocfs2_filetype_table[de->file_type];
867
868                         filldir_ret = filldir(priv, de->name,
869                                               de->name_len,
870                                               *f_pos,
871                                               le64_to_cpu(de->inode),
872                                               d_type);
873                         if (filldir_ret) {
874                                 if (filldir_err)
875                                         *filldir_err = filldir_ret;
876                                 break;
877                         }
878                         if (version != *f_version)
879                                 goto revalidate;
880                 }
881                 *f_pos += le16_to_cpu(de->rec_len);
882         }
883
884 out:
885         brelse(di_bh);
886
887         return 0;
888 }
889
890 static int ocfs2_dir_foreach_blk_el(struct inode *inode,
891                                     u64 *f_version,
892                                     loff_t *f_pos, void *priv,
893                                     filldir_t filldir, int *filldir_err)
894 {
895         int error = 0;
896         unsigned long offset, blk, last_ra_blk = 0;
897         int i, stored;
898         struct buffer_head * bh, * tmp;
899         struct ocfs2_dir_entry * de;
900         struct super_block * sb = inode->i_sb;
901         unsigned int ra_sectors = 16;
902
903         stored = 0;
904         bh = NULL;
905
906         offset = (*f_pos) & (sb->s_blocksize - 1);
907
908         while (!error && !stored && *f_pos < i_size_read(inode)) {
909                 blk = (*f_pos) >> sb->s_blocksize_bits;
910                 if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
911                         /* Skip the corrupt dirblock and keep trying */
912                         *f_pos += sb->s_blocksize - offset;
913                         continue;
914                 }
915
916                 /* The idea here is to begin with 8k read-ahead and to stay
917                  * 4k ahead of our current position.
918                  *
919                  * TODO: Use the pagecache for this. We just need to
920                  * make sure it's cluster-safe... */
921                 if (!last_ra_blk
922                     || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
923                         for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
924                              i > 0; i--) {
925                                 tmp = NULL;
926                                 if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
927                                                           OCFS2_BH_READAHEAD))
928                                         brelse(tmp);
929                         }
930                         last_ra_blk = blk;
931                         ra_sectors = 8;
932                 }
933
934 revalidate:
935                 /* If the dir block has changed since the last call to
936                  * readdir(2), then we might be pointing to an invalid
937                  * dirent right now.  Scan from the start of the block
938                  * to make sure. */
939                 if (*f_version != inode->i_version) {
940                         for (i = 0; i < sb->s_blocksize && i < offset; ) {
941                                 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
942                                 /* It's too expensive to do a full
943                                  * dirent test each time round this
944                                  * loop, but we do have to test at
945                                  * least that it is non-zero.  A
946                                  * failure will be detected in the
947                                  * dirent test below. */
948                                 if (le16_to_cpu(de->rec_len) <
949                                     OCFS2_DIR_REC_LEN(1))
950                                         break;
951                                 i += le16_to_cpu(de->rec_len);
952                         }
953                         offset = i;
954                         *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
955                                 | offset;
956                         *f_version = inode->i_version;
957                 }
958
959                 while (!error && *f_pos < i_size_read(inode)
960                        && offset < sb->s_blocksize) {
961                         de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
962                         if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
963                                 /* On error, skip the f_pos to the
964                                    next block. */
965                                 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
966                                 brelse(bh);
967                                 goto out;
968                         }
969                         offset += le16_to_cpu(de->rec_len);
970                         if (le64_to_cpu(de->inode)) {
971                                 /* We might block in the next section
972                                  * if the data destination is
973                                  * currently swapped out.  So, use a
974                                  * version stamp to detect whether or
975                                  * not the directory has been modified
976                                  * during the copy operation.
977                                  */
978                                 unsigned long version = *f_version;
979                                 unsigned char d_type = DT_UNKNOWN;
980
981                                 if (de->file_type < OCFS2_FT_MAX)
982                                         d_type = ocfs2_filetype_table[de->file_type];
983                                 error = filldir(priv, de->name,
984                                                 de->name_len,
985                                                 *f_pos,
986                                                 le64_to_cpu(de->inode),
987                                                 d_type);
988                                 if (error) {
989                                         if (filldir_err)
990                                                 *filldir_err = error;
991                                         break;
992                                 }
993                                 if (version != *f_version)
994                                         goto revalidate;
995                                 stored ++;
996                         }
997                         *f_pos += le16_to_cpu(de->rec_len);
998                 }
999                 offset = 0;
1000                 brelse(bh);
1001                 bh = NULL;
1002         }
1003
1004         stored = 0;
1005 out:
1006         return stored;
1007 }
1008
1009 static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
1010                                  loff_t *f_pos, void *priv, filldir_t filldir,
1011                                  int *filldir_err)
1012 {
1013         if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1014                 return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
1015                                                 filldir, filldir_err);
1016
1017         return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir,
1018                                         filldir_err);
1019 }
1020
1021 /*
1022  * This is intended to be called from inside other kernel functions,
1023  * so we fake some arguments.
1024  */
1025 int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
1026                       filldir_t filldir)
1027 {
1028         int ret = 0, filldir_err = 0;
1029         u64 version = inode->i_version;
1030
1031         while (*f_pos < i_size_read(inode)) {
1032                 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
1033                                             filldir, &filldir_err);
1034                 if (ret || filldir_err)
1035                         break;
1036         }
1037
1038         if (ret > 0)
1039                 ret = -EIO;
1040
1041         return 0;
1042 }
1043
1044 /*
1045  * ocfs2_readdir()
1046  *
1047  */
1048 int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
1049 {
1050         int error = 0;
1051         struct inode *inode = filp->f_path.dentry->d_inode;
1052         int lock_level = 0;
1053
1054         mlog_entry("dirino=%llu\n",
1055                    (unsigned long long)OCFS2_I(inode)->ip_blkno);
1056
1057         error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
1058         if (lock_level && error >= 0) {
1059                 /* We release EX lock which used to update atime
1060                  * and get PR lock again to reduce contention
1061                  * on commonly accessed directories. */
1062                 ocfs2_inode_unlock(inode, 1);
1063                 lock_level = 0;
1064                 error = ocfs2_inode_lock(inode, NULL, 0);
1065         }
1066         if (error < 0) {
1067                 if (error != -ENOENT)
1068                         mlog_errno(error);
1069                 /* we haven't got any yet, so propagate the error. */
1070                 goto bail_nolock;
1071         }
1072
1073         error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
1074                                       dirent, filldir, NULL);
1075
1076         ocfs2_inode_unlock(inode, lock_level);
1077
1078 bail_nolock:
1079         mlog_exit(error);
1080
1081         return error;
1082 }
1083
1084 /*
1085  * NOTE: this should always be called with parent dir i_mutex taken.
1086  */
1087 int ocfs2_find_files_on_disk(const char *name,
1088                              int namelen,
1089                              u64 *blkno,
1090                              struct inode *inode,
1091                              struct ocfs2_dir_lookup_result *lookup)
1092 {
1093         int status = -ENOENT;
1094
1095         mlog(0, "name=%.*s, blkno=%p, inode=%llu\n", namelen, name, blkno,
1096              (unsigned long long)OCFS2_I(inode)->ip_blkno);
1097
1098         status = ocfs2_find_entry(name, namelen, inode, lookup);
1099         if (status)
1100                 goto leave;
1101
1102         *blkno = le64_to_cpu(lookup->dl_entry->inode);
1103
1104         status = 0;
1105 leave:
1106
1107         return status;
1108 }
1109
1110 /*
1111  * Convenience function for callers which just want the block number
1112  * mapped to a name and don't require the full dirent info, etc.
1113  */
1114 int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
1115                                int namelen, u64 *blkno)
1116 {
1117         int ret;
1118         struct ocfs2_dir_lookup_result lookup = { NULL, };
1119
1120         ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &lookup);
1121         ocfs2_free_dir_lookup_result(&lookup);
1122
1123         return ret;
1124 }
1125
1126 /* Check for a name within a directory.
1127  *
1128  * Return 0 if the name does not exist
1129  * Return -EEXIST if the directory contains the name
1130  *
1131  * Callers should have i_mutex + a cluster lock on dir
1132  */
1133 int ocfs2_check_dir_for_entry(struct inode *dir,
1134                               const char *name,
1135                               int namelen)
1136 {
1137         int ret;
1138         struct ocfs2_dir_lookup_result lookup = { NULL, };
1139
1140         mlog_entry("dir %llu, name '%.*s'\n",
1141                    (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
1142
1143         ret = -EEXIST;
1144         if (ocfs2_find_entry(name, namelen, dir, &lookup) == 0)
1145                 goto bail;
1146
1147         ret = 0;
1148 bail:
1149         ocfs2_free_dir_lookup_result(&lookup);
1150
1151         mlog_exit(ret);
1152         return ret;
1153 }
1154
1155 struct ocfs2_empty_dir_priv {
1156         unsigned seen_dot;
1157         unsigned seen_dot_dot;
1158         unsigned seen_other;
1159 };
1160 static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
1161                                    loff_t pos, u64 ino, unsigned type)
1162 {
1163         struct ocfs2_empty_dir_priv *p = priv;
1164
1165         /*
1166          * Check the positions of "." and ".." records to be sure
1167          * they're in the correct place.
1168          */
1169         if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
1170                 p->seen_dot = 1;
1171                 return 0;
1172         }
1173
1174         if (name_len == 2 && !strncmp("..", name, 2) &&
1175             pos == OCFS2_DIR_REC_LEN(1)) {
1176                 p->seen_dot_dot = 1;
1177                 return 0;
1178         }
1179
1180         p->seen_other = 1;
1181         return 1;
1182 }
1183 /*
1184  * routine to check that the specified directory is empty (for rmdir)
1185  *
1186  * Returns 1 if dir is empty, zero otherwise.
1187  */
1188 int ocfs2_empty_dir(struct inode *inode)
1189 {
1190         int ret;
1191         loff_t start = 0;
1192         struct ocfs2_empty_dir_priv priv;
1193
1194         memset(&priv, 0, sizeof(priv));
1195
1196         ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
1197         if (ret)
1198                 mlog_errno(ret);
1199
1200         if (!priv.seen_dot || !priv.seen_dot_dot) {
1201                 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
1202                      (unsigned long long)OCFS2_I(inode)->ip_blkno);
1203                 /*
1204                  * XXX: Is it really safe to allow an unlink to continue?
1205                  */
1206                 return 1;
1207         }
1208
1209         return !priv.seen_other;
1210 }
1211
1212 /*
1213  * Fills "." and ".." dirents in a new directory block. Returns dirent for
1214  * "..", which might be used during creation of a directory with a trailing
1215  * header. It is otherwise safe to ignore the return code.
1216  */
1217 static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode,
1218                                                           struct inode *parent,
1219                                                           char *start,
1220                                                           unsigned int size)
1221 {
1222         struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
1223
1224         de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
1225         de->name_len = 1;
1226         de->rec_len =
1227                 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1228         strcpy(de->name, ".");
1229         ocfs2_set_de_type(de, S_IFDIR);
1230
1231         de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
1232         de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
1233         de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
1234         de->name_len = 2;
1235         strcpy(de->name, "..");
1236         ocfs2_set_de_type(de, S_IFDIR);
1237
1238         return de;
1239 }
1240
1241 /*
1242  * This works together with code in ocfs2_mknod_locked() which sets
1243  * the inline-data flag and initializes the inline-data section.
1244  */
1245 static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
1246                                  handle_t *handle,
1247                                  struct inode *parent,
1248                                  struct inode *inode,
1249                                  struct buffer_head *di_bh)
1250 {
1251         int ret;
1252         struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1253         struct ocfs2_inline_data *data = &di->id2.i_data;
1254         unsigned int size = le16_to_cpu(data->id_count);
1255
1256         ret = ocfs2_journal_access_di(handle, inode, di_bh,
1257                                       OCFS2_JOURNAL_ACCESS_WRITE);
1258         if (ret) {
1259                 mlog_errno(ret);
1260                 goto out;
1261         }
1262
1263         ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
1264
1265         ocfs2_journal_dirty(handle, di_bh);
1266         if (ret) {
1267                 mlog_errno(ret);
1268                 goto out;
1269         }
1270
1271         i_size_write(inode, size);
1272         inode->i_nlink = 2;
1273         inode->i_blocks = ocfs2_inode_sector_count(inode);
1274
1275         ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
1276         if (ret < 0)
1277                 mlog_errno(ret);
1278
1279 out:
1280         return ret;
1281 }
1282
1283 static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
1284                                  handle_t *handle,
1285                                  struct inode *parent,
1286                                  struct inode *inode,
1287                                  struct buffer_head *fe_bh,
1288                                  struct ocfs2_alloc_context *data_ac)
1289 {
1290         int status;
1291         unsigned int size = osb->sb->s_blocksize;
1292         struct buffer_head *new_bh = NULL;
1293         struct ocfs2_dir_entry *de;
1294
1295         mlog_entry_void();
1296
1297         if (ocfs2_supports_dir_trailer(osb))
1298                 size = ocfs2_dir_trailer_blk_off(parent->i_sb);
1299
1300         status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
1301                                      data_ac, NULL, &new_bh);
1302         if (status < 0) {
1303                 mlog_errno(status);
1304                 goto bail;
1305         }
1306
1307         ocfs2_set_new_buffer_uptodate(inode, new_bh);
1308
1309         status = ocfs2_journal_access_db(handle, inode, new_bh,
1310                                          OCFS2_JOURNAL_ACCESS_CREATE);
1311         if (status < 0) {
1312                 mlog_errno(status);
1313                 goto bail;
1314         }
1315         memset(new_bh->b_data, 0, osb->sb->s_blocksize);
1316
1317         de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size);
1318         if (ocfs2_supports_dir_trailer(osb))
1319                 ocfs2_init_dir_trailer(inode, new_bh);
1320
1321         status = ocfs2_journal_dirty(handle, new_bh);
1322         if (status < 0) {
1323                 mlog_errno(status);
1324                 goto bail;
1325         }
1326
1327         i_size_write(inode, inode->i_sb->s_blocksize);
1328         inode->i_nlink = 2;
1329         inode->i_blocks = ocfs2_inode_sector_count(inode);
1330         status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
1331         if (status < 0) {
1332                 mlog_errno(status);
1333                 goto bail;
1334         }
1335
1336         status = 0;
1337 bail:
1338         brelse(new_bh);
1339
1340         mlog_exit(status);
1341         return status;
1342 }
1343
1344 int ocfs2_fill_new_dir(struct ocfs2_super *osb,
1345                        handle_t *handle,
1346                        struct inode *parent,
1347                        struct inode *inode,
1348                        struct buffer_head *fe_bh,
1349                        struct ocfs2_alloc_context *data_ac)
1350 {
1351         BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
1352
1353         if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1354                 return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
1355
1356         return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
1357                                      data_ac);
1358 }
1359
1360 /*
1361  * Expand rec_len of the rightmost dirent in a directory block so that it
1362  * contains the end of our valid space for dirents. We do this during
1363  * expansion from an inline directory to one with extents. The first dir block
1364  * in that case is taken from the inline data portion of the inode block.
1365  *
1366  * We add the dir trailer if this filesystem wants it.
1367  */
1368 static void ocfs2_expand_last_dirent(char *start, unsigned int old_size,
1369                                      struct super_block *sb)
1370 {
1371         struct ocfs2_dir_entry *de;
1372         struct ocfs2_dir_entry *prev_de;
1373         char *de_buf, *limit;
1374         unsigned int new_size = sb->s_blocksize;
1375         unsigned int bytes;
1376
1377         if (ocfs2_supports_dir_trailer(OCFS2_SB(sb)))
1378                 new_size = ocfs2_dir_trailer_blk_off(sb);
1379
1380         bytes = new_size - old_size;
1381
1382         limit = start + old_size;
1383         de_buf = start;
1384         de = (struct ocfs2_dir_entry *)de_buf;
1385         do {
1386                 prev_de = de;
1387                 de_buf += le16_to_cpu(de->rec_len);
1388                 de = (struct ocfs2_dir_entry *)de_buf;
1389         } while (de_buf < limit);
1390
1391         le16_add_cpu(&prev_de->rec_len, bytes);
1392 }
1393
1394 /*
1395  * We allocate enough clusters to fulfill "blocks_wanted", but set
1396  * i_size to exactly one block. Ocfs2_extend_dir() will handle the
1397  * rest automatically for us.
1398  *
1399  * *first_block_bh is a pointer to the 1st data block allocated to the
1400  *  directory.
1401  */
1402 static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
1403                                    unsigned int blocks_wanted,
1404                                    struct buffer_head **first_block_bh)
1405 {
1406         u32 alloc, bit_off, len;
1407         struct super_block *sb = dir->i_sb;
1408         int ret, credits = ocfs2_inline_to_extents_credits(sb);
1409         u64 blkno, bytes = blocks_wanted << sb->s_blocksize_bits;
1410         struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
1411         struct ocfs2_inode_info *oi = OCFS2_I(dir);
1412         struct ocfs2_alloc_context *data_ac;
1413         struct buffer_head *dirdata_bh = NULL;
1414         struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1415         handle_t *handle;
1416         struct ocfs2_extent_tree et;
1417         int did_quota = 0;
1418
1419         ocfs2_init_dinode_extent_tree(&et, dir, di_bh);
1420
1421         alloc = ocfs2_clusters_for_bytes(sb, bytes);
1422
1423         /*
1424          * We should never need more than 2 clusters for this -
1425          * maximum dirent size is far less than one block. In fact,
1426          * the only time we'd need more than one cluster is if
1427          * blocksize == clustersize and the dirent won't fit in the
1428          * extra space that the expansion to a single block gives. As
1429          * of today, that only happens on 4k/4k file systems.
1430          */
1431         BUG_ON(alloc > 2);
1432
1433         ret = ocfs2_reserve_clusters(osb, alloc, &data_ac);
1434         if (ret) {
1435                 mlog_errno(ret);
1436                 goto out;
1437         }
1438
1439         down_write(&oi->ip_alloc_sem);
1440
1441         /*
1442          * Prepare for worst case allocation scenario of two separate
1443          * extents.
1444          */
1445         if (alloc == 2)
1446                 credits += OCFS2_SUBALLOC_ALLOC;
1447
1448         handle = ocfs2_start_trans(osb, credits);
1449         if (IS_ERR(handle)) {
1450                 ret = PTR_ERR(handle);
1451                 mlog_errno(ret);
1452                 goto out_sem;
1453         }
1454
1455         if (vfs_dq_alloc_space_nodirty(dir,
1456                                 ocfs2_clusters_to_bytes(osb->sb, alloc))) {
1457                 ret = -EDQUOT;
1458                 goto out_commit;
1459         }
1460         did_quota = 1;
1461         /*
1462          * Try to claim as many clusters as the bitmap can give though
1463          * if we only get one now, that's enough to continue. The rest
1464          * will be claimed after the conversion to extents.
1465          */
1466         ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
1467         if (ret) {
1468                 mlog_errno(ret);
1469                 goto out_commit;
1470         }
1471
1472         /*
1473          * Operations are carefully ordered so that we set up the new
1474          * data block first. The conversion from inline data to
1475          * extents follows.
1476          */
1477         blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1478         dirdata_bh = sb_getblk(sb, blkno);
1479         if (!dirdata_bh) {
1480                 ret = -EIO;
1481                 mlog_errno(ret);
1482                 goto out_commit;
1483         }
1484
1485         ocfs2_set_new_buffer_uptodate(dir, dirdata_bh);
1486
1487         ret = ocfs2_journal_access_db(handle, dir, dirdata_bh,
1488                                       OCFS2_JOURNAL_ACCESS_CREATE);
1489         if (ret) {
1490                 mlog_errno(ret);
1491                 goto out_commit;
1492         }
1493
1494         memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
1495         memset(dirdata_bh->b_data + i_size_read(dir), 0,
1496                sb->s_blocksize - i_size_read(dir));
1497         ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), sb);
1498         if (ocfs2_supports_dir_trailer(osb))
1499                 ocfs2_init_dir_trailer(dir, dirdata_bh);
1500
1501         ret = ocfs2_journal_dirty(handle, dirdata_bh);
1502         if (ret) {
1503                 mlog_errno(ret);
1504                 goto out_commit;
1505         }
1506
1507         /*
1508          * Set extent, i_size, etc on the directory. After this, the
1509          * inode should contain the same exact dirents as before and
1510          * be fully accessible from system calls.
1511          *
1512          * We let the later dirent insert modify c/mtime - to the user
1513          * the data hasn't changed.
1514          */
1515         ret = ocfs2_journal_access_di(handle, dir, di_bh,
1516                                       OCFS2_JOURNAL_ACCESS_CREATE);
1517         if (ret) {
1518                 mlog_errno(ret);
1519                 goto out_commit;
1520         }
1521
1522         spin_lock(&oi->ip_lock);
1523         oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
1524         di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1525         spin_unlock(&oi->ip_lock);
1526
1527         ocfs2_dinode_new_extent_list(dir, di);
1528
1529         i_size_write(dir, sb->s_blocksize);
1530         dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1531
1532         di->i_size = cpu_to_le64(sb->s_blocksize);
1533         di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
1534         di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
1535
1536         /*
1537          * This should never fail as our extent list is empty and all
1538          * related blocks have been journaled already.
1539          */
1540         ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len,
1541                                   0, NULL);
1542         if (ret) {
1543                 mlog_errno(ret);
1544                 goto out_commit;
1545         }
1546
1547         /*
1548          * Set i_blocks after the extent insert for the most up to
1549          * date ip_clusters value.
1550          */
1551         dir->i_blocks = ocfs2_inode_sector_count(dir);
1552
1553         ret = ocfs2_journal_dirty(handle, di_bh);
1554         if (ret) {
1555                 mlog_errno(ret);
1556                 goto out_commit;
1557         }
1558
1559         /*
1560          * We asked for two clusters, but only got one in the 1st
1561          * pass. Claim the 2nd cluster as a separate extent.
1562          */
1563         if (alloc > len) {
1564                 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
1565                                            &len);
1566                 if (ret) {
1567                         mlog_errno(ret);
1568                         goto out_commit;
1569                 }
1570                 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
1571
1572                 ret = ocfs2_insert_extent(osb, handle, dir, &et, 1,
1573                                           blkno, len, 0, NULL);
1574                 if (ret) {
1575                         mlog_errno(ret);
1576                         goto out_commit;
1577                 }
1578         }
1579
1580         *first_block_bh = dirdata_bh;
1581         dirdata_bh = NULL;
1582
1583 out_commit:
1584         if (ret < 0 && did_quota)
1585                 vfs_dq_free_space_nodirty(dir,
1586                         ocfs2_clusters_to_bytes(osb->sb, 2));
1587         ocfs2_commit_trans(osb, handle);
1588
1589 out_sem:
1590         up_write(&oi->ip_alloc_sem);
1591
1592 out:
1593         if (data_ac)
1594                 ocfs2_free_alloc_context(data_ac);
1595
1596         brelse(dirdata_bh);
1597
1598         return ret;
1599 }
1600
1601 /* returns a bh of the 1st new block in the allocation. */
1602 static int ocfs2_do_extend_dir(struct super_block *sb,
1603                                handle_t *handle,
1604                                struct inode *dir,
1605                                struct buffer_head *parent_fe_bh,
1606                                struct ocfs2_alloc_context *data_ac,
1607                                struct ocfs2_alloc_context *meta_ac,
1608                                struct buffer_head **new_bh)
1609 {
1610         int status;
1611         int extend, did_quota = 0;
1612         u64 p_blkno, v_blkno;
1613
1614         spin_lock(&OCFS2_I(dir)->ip_lock);
1615         extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
1616         spin_unlock(&OCFS2_I(dir)->ip_lock);
1617
1618         if (extend) {
1619                 u32 offset = OCFS2_I(dir)->ip_clusters;
1620
1621                 if (vfs_dq_alloc_space_nodirty(dir,
1622                                         ocfs2_clusters_to_bytes(sb, 1))) {
1623                         status = -EDQUOT;
1624                         goto bail;
1625                 }
1626                 did_quota = 1;
1627
1628                 status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
1629                                               1, 0, parent_fe_bh, handle,
1630                                               data_ac, meta_ac, NULL);
1631                 BUG_ON(status == -EAGAIN);
1632                 if (status < 0) {
1633                         mlog_errno(status);
1634                         goto bail;
1635                 }
1636         }
1637
1638         v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
1639         status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
1640         if (status < 0) {
1641                 mlog_errno(status);
1642                 goto bail;
1643         }
1644
1645         *new_bh = sb_getblk(sb, p_blkno);
1646         if (!*new_bh) {
1647                 status = -EIO;
1648                 mlog_errno(status);
1649                 goto bail;
1650         }
1651         status = 0;
1652 bail:
1653         if (did_quota && status < 0)
1654                 vfs_dq_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
1655         mlog_exit(status);
1656         return status;
1657 }
1658
1659 /*
1660  * Assumes you already have a cluster lock on the directory.
1661  *
1662  * 'blocks_wanted' is only used if we have an inline directory which
1663  * is to be turned into an extent based one. The size of the dirent to
1664  * insert might be larger than the space gained by growing to just one
1665  * block, so we may have to grow the inode by two blocks in that case.
1666  */
1667 static int ocfs2_extend_dir(struct ocfs2_super *osb,
1668                             struct inode *dir,
1669                             struct buffer_head *parent_fe_bh,
1670                             unsigned int blocks_wanted,
1671                             struct buffer_head **new_de_bh)
1672 {
1673         int status = 0;
1674         int credits, num_free_extents, drop_alloc_sem = 0;
1675         loff_t dir_i_size;
1676         struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1677         struct ocfs2_extent_list *el = &fe->id2.i_list;
1678         struct ocfs2_alloc_context *data_ac = NULL;
1679         struct ocfs2_alloc_context *meta_ac = NULL;
1680         handle_t *handle = NULL;
1681         struct buffer_head *new_bh = NULL;
1682         struct ocfs2_dir_entry * de;
1683         struct super_block *sb = osb->sb;
1684         struct ocfs2_extent_tree et;
1685
1686         mlog_entry_void();
1687
1688         if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1689                 status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
1690                                                  blocks_wanted, &new_bh);
1691                 if (status) {
1692                         mlog_errno(status);
1693                         goto bail;
1694                 }
1695
1696                 if (blocks_wanted == 1) {
1697                         /*
1698                          * If the new dirent will fit inside the space
1699                          * created by pushing out to one block, then
1700                          * we can complete the operation
1701                          * here. Otherwise we have to expand i_size
1702                          * and format the 2nd block below.
1703                          */
1704                         BUG_ON(new_bh == NULL);
1705                         goto bail_bh;
1706                 }
1707
1708                 /*
1709                  * Get rid of 'new_bh' - we want to format the 2nd
1710                  * data block and return that instead.
1711                  */
1712                 brelse(new_bh);
1713                 new_bh = NULL;
1714
1715                 dir_i_size = i_size_read(dir);
1716                 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1717                 goto do_extend;
1718         }
1719
1720         dir_i_size = i_size_read(dir);
1721         mlog(0, "extending dir %llu (i_size = %lld)\n",
1722              (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
1723
1724         /* dir->i_size is always block aligned. */
1725         spin_lock(&OCFS2_I(dir)->ip_lock);
1726         if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
1727                 spin_unlock(&OCFS2_I(dir)->ip_lock);
1728                 ocfs2_init_dinode_extent_tree(&et, dir, parent_fe_bh);
1729                 num_free_extents = ocfs2_num_free_extents(osb, dir, &et);
1730                 if (num_free_extents < 0) {
1731                         status = num_free_extents;
1732                         mlog_errno(status);
1733                         goto bail;
1734                 }
1735
1736                 if (!num_free_extents) {
1737                         status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
1738                         if (status < 0) {
1739                                 if (status != -ENOSPC)
1740                                         mlog_errno(status);
1741                                 goto bail;
1742                         }
1743                 }
1744
1745                 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
1746                 if (status < 0) {
1747                         if (status != -ENOSPC)
1748                                 mlog_errno(status);
1749                         goto bail;
1750                 }
1751
1752                 credits = ocfs2_calc_extend_credits(sb, el, 1);
1753         } else {
1754                 spin_unlock(&OCFS2_I(dir)->ip_lock);
1755                 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1756         }
1757
1758 do_extend:
1759         down_write(&OCFS2_I(dir)->ip_alloc_sem);
1760         drop_alloc_sem = 1;
1761
1762         handle = ocfs2_start_trans(osb, credits);
1763         if (IS_ERR(handle)) {
1764                 status = PTR_ERR(handle);
1765                 handle = NULL;
1766                 mlog_errno(status);
1767                 goto bail;
1768         }
1769
1770         status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
1771                                      data_ac, meta_ac, &new_bh);
1772         if (status < 0) {
1773                 mlog_errno(status);
1774                 goto bail;
1775         }
1776
1777         ocfs2_set_new_buffer_uptodate(dir, new_bh);
1778
1779         status = ocfs2_journal_access_db(handle, dir, new_bh,
1780                                          OCFS2_JOURNAL_ACCESS_CREATE);
1781         if (status < 0) {
1782                 mlog_errno(status);
1783                 goto bail;
1784         }
1785         memset(new_bh->b_data, 0, sb->s_blocksize);
1786
1787         de = (struct ocfs2_dir_entry *) new_bh->b_data;
1788         de->inode = 0;
1789         if (ocfs2_dir_has_trailer(dir)) {
1790                 de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb));
1791                 ocfs2_init_dir_trailer(dir, new_bh);
1792         } else {
1793                 de->rec_len = cpu_to_le16(sb->s_blocksize);
1794         }
1795         status = ocfs2_journal_dirty(handle, new_bh);
1796         if (status < 0) {
1797                 mlog_errno(status);
1798                 goto bail;
1799         }
1800
1801         dir_i_size += dir->i_sb->s_blocksize;
1802         i_size_write(dir, dir_i_size);
1803         dir->i_blocks = ocfs2_inode_sector_count(dir);
1804         status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1805         if (status < 0) {
1806                 mlog_errno(status);
1807                 goto bail;
1808         }
1809
1810 bail_bh:
1811         *new_de_bh = new_bh;
1812         get_bh(*new_de_bh);
1813 bail:
1814         if (drop_alloc_sem)
1815                 up_write(&OCFS2_I(dir)->ip_alloc_sem);
1816         if (handle)
1817                 ocfs2_commit_trans(osb, handle);
1818
1819         if (data_ac)
1820                 ocfs2_free_alloc_context(data_ac);
1821         if (meta_ac)
1822                 ocfs2_free_alloc_context(meta_ac);
1823
1824         brelse(new_bh);
1825
1826         mlog_exit(status);
1827         return status;
1828 }
1829
1830 static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
1831                                    const char *name, int namelen,
1832                                    struct buffer_head **ret_de_bh,
1833                                    unsigned int *blocks_wanted)
1834 {
1835         int ret;
1836         struct super_block *sb = dir->i_sb;
1837         struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1838         struct ocfs2_dir_entry *de, *last_de = NULL;
1839         char *de_buf, *limit;
1840         unsigned long offset = 0;
1841         unsigned int rec_len, new_rec_len, free_space = dir->i_sb->s_blocksize;
1842
1843         /*
1844          * This calculates how many free bytes we'd have in block zero, should
1845          * this function force expansion to an extent tree.
1846          */
1847         if (ocfs2_supports_dir_trailer(OCFS2_SB(sb)))
1848                 free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir);
1849         else
1850                 free_space = dir->i_sb->s_blocksize - i_size_read(dir);
1851
1852         de_buf = di->id2.i_data.id_data;
1853         limit = de_buf + i_size_read(dir);
1854         rec_len = OCFS2_DIR_REC_LEN(namelen);
1855
1856         while (de_buf < limit) {
1857                 de = (struct ocfs2_dir_entry *)de_buf;
1858
1859                 if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
1860                         ret = -ENOENT;
1861                         goto out;
1862                 }
1863                 if (ocfs2_match(namelen, name, de)) {
1864                         ret = -EEXIST;
1865                         goto out;
1866                 }
1867                 /*
1868                  * No need to check for a trailing dirent record here as
1869                  * they're not used for inline dirs.
1870                  */
1871
1872                 if (ocfs2_dirent_would_fit(de, rec_len)) {
1873                         /* Ok, we found a spot. Return this bh and let
1874                          * the caller actually fill it in. */
1875                         *ret_de_bh = di_bh;
1876                         get_bh(*ret_de_bh);
1877                         ret = 0;
1878                         goto out;
1879                 }
1880
1881                 last_de = de;
1882                 de_buf += le16_to_cpu(de->rec_len);
1883                 offset += le16_to_cpu(de->rec_len);
1884         }
1885
1886         /*
1887          * We're going to require expansion of the directory - figure
1888          * out how many blocks we'll need so that a place for the
1889          * dirent can be found.
1890          */
1891         *blocks_wanted = 1;
1892         new_rec_len = le16_to_cpu(last_de->rec_len) + free_space;
1893         if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
1894                 *blocks_wanted = 2;
1895
1896         ret = -ENOSPC;
1897 out:
1898         return ret;
1899 }
1900
1901 static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
1902                                    int namelen, struct buffer_head **ret_de_bh)
1903 {
1904         unsigned long offset;
1905         struct buffer_head *bh = NULL;
1906         unsigned short rec_len;
1907         struct ocfs2_dir_entry *de;
1908         struct super_block *sb = dir->i_sb;
1909         int status;
1910         int blocksize = dir->i_sb->s_blocksize;
1911
1912         status = ocfs2_read_dir_block(dir, 0, &bh, 0);
1913         if (status) {
1914                 mlog_errno(status);
1915                 goto bail;
1916         }
1917
1918         rec_len = OCFS2_DIR_REC_LEN(namelen);
1919         offset = 0;
1920         de = (struct ocfs2_dir_entry *) bh->b_data;
1921         while (1) {
1922                 if ((char *)de >= sb->s_blocksize + bh->b_data) {
1923                         brelse(bh);
1924                         bh = NULL;
1925
1926                         if (i_size_read(dir) <= offset) {
1927                                 /*
1928                                  * Caller will have to expand this
1929                                  * directory.
1930                                  */
1931                                 status = -ENOSPC;
1932                                 goto bail;
1933                         }
1934                         status = ocfs2_read_dir_block(dir,
1935                                              offset >> sb->s_blocksize_bits,
1936                                              &bh, 0);
1937                         if (status) {
1938                                 mlog_errno(status);
1939                                 goto bail;
1940                         }
1941                         /* move to next block */
1942                         de = (struct ocfs2_dir_entry *) bh->b_data;
1943                 }
1944                 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1945                         status = -ENOENT;
1946                         goto bail;
1947                 }
1948                 if (ocfs2_match(namelen, name, de)) {
1949                         status = -EEXIST;
1950                         goto bail;
1951                 }
1952
1953                 if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize,
1954                                            blocksize))
1955                         goto next;
1956
1957                 if (ocfs2_dirent_would_fit(de, rec_len)) {
1958                         /* Ok, we found a spot. Return this bh and let
1959                          * the caller actually fill it in. */
1960                         *ret_de_bh = bh;
1961                         get_bh(*ret_de_bh);
1962                         status = 0;
1963                         goto bail;
1964                 }
1965 next:
1966                 offset += le16_to_cpu(de->rec_len);
1967                 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
1968         }
1969
1970         status = 0;
1971 bail:
1972         brelse(bh);
1973
1974         mlog_exit(status);
1975         return status;
1976 }
1977
1978 /*
1979  * Get a directory ready for insert. Any directory allocation required
1980  * happens here. Success returns zero, and enough context in the dir
1981  * lookup result that ocfs2_add_entry() will be able complete the task
1982  * with minimal performance impact.
1983  */
1984 int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
1985                                  struct inode *dir,
1986                                  struct buffer_head *parent_fe_bh,
1987                                  const char *name,
1988                                  int namelen,
1989                                  struct ocfs2_dir_lookup_result *lookup)
1990 {
1991         int ret;
1992         unsigned int blocks_wanted = 1;
1993         struct buffer_head *bh = NULL;
1994
1995         mlog(0, "getting ready to insert namelen %d into dir %llu\n",
1996              namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
1997
1998         if (!namelen) {
1999                 ret = -EINVAL;
2000                 mlog_errno(ret);
2001                 goto out;
2002         }
2003
2004         if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2005                 ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
2006                                               namelen, &bh, &blocks_wanted);
2007         } else
2008                 ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
2009
2010         if (ret && ret != -ENOSPC) {
2011                 mlog_errno(ret);
2012                 goto out;
2013         }
2014
2015         if (ret == -ENOSPC) {
2016                 /*
2017                  * We have to expand the directory to add this name.
2018                  */
2019                 BUG_ON(bh);
2020
2021                 ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
2022                                        &bh);
2023                 if (ret) {
2024                         if (ret != -ENOSPC)
2025                                 mlog_errno(ret);
2026                         goto out;
2027                 }
2028
2029                 BUG_ON(!bh);
2030         }
2031
2032         lookup->dl_leaf_bh = bh;
2033         bh = NULL;
2034 out:
2035         brelse(bh);
2036         return ret;
2037 }