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