]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/ocfs2/dir.c
ocfs2: Read support for directories with inline data
[linux-2.6-omap-h63xx.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
44 #define MLOG_MASK_PREFIX ML_NAMEI
45 #include <cluster/masklog.h>
46
47 #include "ocfs2.h"
48
49 #include "alloc.h"
50 #include "dir.h"
51 #include "dlmglue.h"
52 #include "extent_map.h"
53 #include "file.h"
54 #include "inode.h"
55 #include "journal.h"
56 #include "namei.h"
57 #include "suballoc.h"
58 #include "super.h"
59 #include "uptodate.h"
60
61 #include "buffer_head_io.h"
62
63 #define NAMEI_RA_CHUNKS  2
64 #define NAMEI_RA_BLOCKS  4
65 #define NAMEI_RA_SIZE        (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
66 #define NAMEI_RA_INDEX(c,b)  (((c) * NAMEI_RA_BLOCKS) + (b))
67
68 static unsigned char ocfs2_filetype_table[] = {
69         DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
70 };
71
72 static int ocfs2_extend_dir(struct ocfs2_super *osb,
73                             struct inode *dir,
74                             struct buffer_head *parent_fe_bh,
75                             struct buffer_head **new_de_bh);
76 static int ocfs2_do_extend_dir(struct super_block *sb,
77                                handle_t *handle,
78                                struct inode *dir,
79                                struct buffer_head *parent_fe_bh,
80                                struct ocfs2_alloc_context *data_ac,
81                                struct ocfs2_alloc_context *meta_ac,
82                                struct buffer_head **new_bh);
83
84 /*
85  * bh passed here can be an inode block or a dir data block, depending
86  * on the inode inline data flag.
87  */
88 static int ocfs2_check_dir_entry(struct inode * dir,
89                                  struct ocfs2_dir_entry * de,
90                                  struct buffer_head * bh,
91                                  unsigned long offset)
92 {
93         const char *error_msg = NULL;
94         const int rlen = le16_to_cpu(de->rec_len);
95
96         if (rlen < OCFS2_DIR_REC_LEN(1))
97                 error_msg = "rec_len is smaller than minimal";
98         else if (rlen % 4 != 0)
99                 error_msg = "rec_len % 4 != 0";
100         else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
101                 error_msg = "rec_len is too small for name_len";
102         else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
103                 error_msg = "directory entry across blocks";
104
105         if (error_msg != NULL)
106                 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
107                      "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
108                      (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
109                      offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
110                      de->name_len);
111         return error_msg == NULL ? 1 : 0;
112 }
113
114 static inline int ocfs2_match(int len,
115                               const char * const name,
116                               struct ocfs2_dir_entry *de)
117 {
118         if (len != de->name_len)
119                 return 0;
120         if (!de->inode)
121                 return 0;
122         return !memcmp(name, de->name, len);
123 }
124
125 /*
126  * Returns 0 if not found, -1 on failure, and 1 on success
127  */
128 static int inline ocfs2_search_dirblock(struct buffer_head *bh,
129                                         struct inode *dir,
130                                         const char *name, int namelen,
131                                         unsigned long offset,
132                                         char *first_de,
133                                         unsigned int bytes,
134                                         struct ocfs2_dir_entry **res_dir)
135 {
136         struct ocfs2_dir_entry *de;
137         char *dlimit, *de_buf;
138         int de_len;
139         int ret = 0;
140
141         mlog_entry_void();
142
143         de_buf = first_de;
144         dlimit = de_buf + bytes;
145
146         while (de_buf < dlimit) {
147                 /* this code is executed quadratically often */
148                 /* do minimal checking `by hand' */
149
150                 de = (struct ocfs2_dir_entry *) de_buf;
151
152                 if (de_buf + namelen <= dlimit &&
153                     ocfs2_match(namelen, name, de)) {
154                         /* found a match - just to be sure, do a full check */
155                         if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
156                                 ret = -1;
157                                 goto bail;
158                         }
159                         *res_dir = de;
160                         ret = 1;
161                         goto bail;
162                 }
163
164                 /* prevent looping on a bad block */
165                 de_len = le16_to_cpu(de->rec_len);
166                 if (de_len <= 0) {
167                         ret = -1;
168                         goto bail;
169                 }
170
171                 de_buf += de_len;
172                 offset += de_len;
173         }
174
175 bail:
176         mlog_exit(ret);
177         return ret;
178 }
179
180 static struct buffer_head *ocfs2_find_entry_id(const char *name,
181                                                int namelen,
182                                                struct inode *dir,
183                                                struct ocfs2_dir_entry **res_dir)
184 {
185         int ret, found;
186         struct buffer_head *di_bh = NULL;
187         struct ocfs2_dinode *di;
188         struct ocfs2_inline_data *data;
189
190         ret = ocfs2_read_block(OCFS2_SB(dir->i_sb), OCFS2_I(dir)->ip_blkno,
191                                &di_bh, OCFS2_BH_CACHED, dir);
192         if (ret) {
193                 mlog_errno(ret);
194                 goto out;
195         }
196
197         di = (struct ocfs2_dinode *)di_bh->b_data;
198         data = &di->id2.i_data;
199
200         found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
201                                       data->id_data, i_size_read(dir), res_dir);
202         if (found == 1)
203                 return di_bh;
204
205         brelse(di_bh);
206 out:
207         return NULL;
208 }
209
210 struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
211                                         struct inode *dir,
212                                         struct ocfs2_dir_entry **res_dir)
213 {
214         struct super_block *sb;
215         struct buffer_head *bh_use[NAMEI_RA_SIZE];
216         struct buffer_head *bh, *ret = NULL;
217         unsigned long start, block, b;
218         int ra_max = 0;         /* Number of bh's in the readahead
219                                    buffer, bh_use[] */
220         int ra_ptr = 0;         /* Current index into readahead
221                                    buffer */
222         int num = 0;
223         int nblocks, i, err;
224
225         mlog_entry_void();
226
227         sb = dir->i_sb;
228
229         nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
230         start = OCFS2_I(dir)->ip_dir_start_lookup;
231         if (start >= nblocks)
232                 start = 0;
233         block = start;
234
235 restart:
236         do {
237                 /*
238                  * We deal with the read-ahead logic here.
239                  */
240                 if (ra_ptr >= ra_max) {
241                         /* Refill the readahead buffer */
242                         ra_ptr = 0;
243                         b = block;
244                         for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
245                                 /*
246                                  * Terminate if we reach the end of the
247                                  * directory and must wrap, or if our
248                                  * search has finished at this block.
249                                  */
250                                 if (b >= nblocks || (num && block == start)) {
251                                         bh_use[ra_max] = NULL;
252                                         break;
253                                 }
254                                 num++;
255
256                                 bh = ocfs2_bread(dir, b++, &err, 1);
257                                 bh_use[ra_max] = bh;
258                         }
259                 }
260                 if ((bh = bh_use[ra_ptr++]) == NULL)
261                         goto next;
262                 wait_on_buffer(bh);
263                 if (!buffer_uptodate(bh)) {
264                         /* read error, skip block & hope for the best */
265                         ocfs2_error(dir->i_sb, "reading directory %llu, "
266                                     "offset %lu\n",
267                                     (unsigned long long)OCFS2_I(dir)->ip_blkno,
268                                     block);
269                         brelse(bh);
270                         goto next;
271                 }
272                 i = ocfs2_search_dirblock(bh, dir, name, namelen,
273                                           block << sb->s_blocksize_bits,
274                                           bh->b_data, sb->s_blocksize,
275                                           res_dir);
276                 if (i == 1) {
277                         OCFS2_I(dir)->ip_dir_start_lookup = block;
278                         ret = bh;
279                         goto cleanup_and_exit;
280                 } else {
281                         brelse(bh);
282                         if (i < 0)
283                                 goto cleanup_and_exit;
284                 }
285         next:
286                 if (++block >= nblocks)
287                         block = 0;
288         } while (block != start);
289
290         /*
291          * If the directory has grown while we were searching, then
292          * search the last part of the directory before giving up.
293          */
294         block = nblocks;
295         nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
296         if (block < nblocks) {
297                 start = 0;
298                 goto restart;
299         }
300
301 cleanup_and_exit:
302         /* Clean up the read-ahead blocks */
303         for (; ra_ptr < ra_max; ra_ptr++)
304                 brelse(bh_use[ra_ptr]);
305
306         mlog_exit_ptr(ret);
307         return ret;
308 }
309
310 /*
311  * Try to find an entry of the provided name within 'dir'.
312  *
313  * If nothing was found, NULL is returned. Otherwise, a buffer_head
314  * and pointer to the dir entry are passed back.
315  *
316  * Caller can NOT assume anything about the contents of the
317  * buffer_head - it is passed back only so that it can be passed into
318  * any one of the manipulation functions (add entry, delete entry,
319  * etc). As an example, bh in the extent directory case is a data
320  * block, in the inline-data case it actually points to an inode.
321  */
322 struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
323                                      struct inode *dir,
324                                      struct ocfs2_dir_entry **res_dir)
325 {
326         *res_dir = NULL;
327
328         if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
329                 return ocfs2_find_entry_id(name, namelen, dir, res_dir);
330
331         return ocfs2_find_entry_el(name, namelen, dir, res_dir);
332 }
333
334 int ocfs2_update_entry(struct inode *dir, handle_t *handle,
335                        struct buffer_head *de_bh, struct ocfs2_dir_entry *de,
336                        struct inode *new_entry_inode)
337 {
338         int ret;
339
340         ret = ocfs2_journal_access(handle, dir, de_bh,
341                                    OCFS2_JOURNAL_ACCESS_WRITE);
342         if (ret) {
343                 mlog_errno(ret);
344                 goto out;
345         }
346
347         de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
348         ocfs2_set_de_type(de, new_entry_inode->i_mode);
349
350         ocfs2_journal_dirty(handle, de_bh);
351
352 out:
353         return ret;
354 }
355
356 /*
357  * ocfs2_delete_entry deletes a directory entry by merging it with the
358  * previous entry
359  */
360 int ocfs2_delete_entry(handle_t *handle,
361                        struct inode *dir,
362                        struct ocfs2_dir_entry *de_del,
363                        struct buffer_head *bh)
364 {
365         struct ocfs2_dir_entry *de, *pde;
366         int i, status = -ENOENT;
367
368         mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
369
370         i = 0;
371         pde = NULL;
372         de = (struct ocfs2_dir_entry *) bh->b_data;
373         while (i < bh->b_size) {
374                 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
375                         status = -EIO;
376                         mlog_errno(status);
377                         goto bail;
378                 }
379                 if (de == de_del)  {
380                         status = ocfs2_journal_access(handle, dir, bh,
381                                                       OCFS2_JOURNAL_ACCESS_WRITE);
382                         if (status < 0) {
383                                 status = -EIO;
384                                 mlog_errno(status);
385                                 goto bail;
386                         }
387                         if (pde)
388                                 pde->rec_len =
389                                         cpu_to_le16(le16_to_cpu(pde->rec_len) +
390                                                     le16_to_cpu(de->rec_len));
391                         else
392                                 de->inode = 0;
393                         dir->i_version++;
394                         status = ocfs2_journal_dirty(handle, bh);
395                         goto bail;
396                 }
397                 i += le16_to_cpu(de->rec_len);
398                 pde = de;
399                 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
400         }
401 bail:
402         mlog_exit(status);
403         return status;
404 }
405
406 /*
407  * Check whether 'de' has enough room to hold an entry of
408  * 'new_rec_len' bytes.
409  */
410 static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
411                                          unsigned int new_rec_len)
412 {
413         unsigned int de_really_used;
414
415         /* Check whether this is an empty record with enough space */
416         if (le64_to_cpu(de->inode) == 0 &&
417             le16_to_cpu(de->rec_len) >= new_rec_len)
418                 return 1;
419
420         /*
421          * Record might have free space at the end which we can
422          * use.
423          */
424         de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
425         if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
426             return 1;
427
428         return 0;
429 }
430
431 /* we don't always have a dentry for what we want to add, so people
432  * like orphan dir can call this instead.
433  *
434  * If you pass me insert_bh, I'll skip the search of the other dir
435  * blocks and put the record in there.
436  */
437 int __ocfs2_add_entry(handle_t *handle,
438                       struct inode *dir,
439                       const char *name, int namelen,
440                       struct inode *inode, u64 blkno,
441                       struct buffer_head *parent_fe_bh,
442                       struct buffer_head *insert_bh)
443 {
444         unsigned long offset;
445         unsigned short rec_len;
446         struct ocfs2_dir_entry *de, *de1;
447         struct super_block *sb;
448         int retval, status;
449
450         mlog_entry_void();
451
452         sb = dir->i_sb;
453
454         if (!namelen)
455                 return -EINVAL;
456
457         rec_len = OCFS2_DIR_REC_LEN(namelen);
458         offset = 0;
459         de = (struct ocfs2_dir_entry *) insert_bh->b_data;
460         while (1) {
461                 BUG_ON((char *)de >= sb->s_blocksize + insert_bh->b_data);
462                 /* These checks should've already been passed by the
463                  * prepare function, but I guess we can leave them
464                  * here anyway. */
465                 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
466                         retval = -ENOENT;
467                         goto bail;
468                 }
469                 if (ocfs2_match(namelen, name, de)) {
470                         retval = -EEXIST;
471                         goto bail;
472                 }
473
474                 if (ocfs2_dirent_would_fit(de, rec_len)) {
475                         dir->i_mtime = dir->i_ctime = CURRENT_TIME;
476                         retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
477                         if (retval < 0) {
478                                 mlog_errno(retval);
479                                 goto bail;
480                         }
481
482                         status = ocfs2_journal_access(handle, dir, insert_bh,
483                                                       OCFS2_JOURNAL_ACCESS_WRITE);
484                         /* By now the buffer is marked for journaling */
485                         offset += le16_to_cpu(de->rec_len);
486                         if (le64_to_cpu(de->inode)) {
487                                 de1 = (struct ocfs2_dir_entry *)((char *) de +
488                                         OCFS2_DIR_REC_LEN(de->name_len));
489                                 de1->rec_len =
490                                         cpu_to_le16(le16_to_cpu(de->rec_len) -
491                                         OCFS2_DIR_REC_LEN(de->name_len));
492                                 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
493                                 de = de1;
494                         }
495                         de->file_type = OCFS2_FT_UNKNOWN;
496                         if (blkno) {
497                                 de->inode = cpu_to_le64(blkno);
498                                 ocfs2_set_de_type(de, inode->i_mode);
499                         } else
500                                 de->inode = 0;
501                         de->name_len = namelen;
502                         memcpy(de->name, name, namelen);
503
504                         dir->i_version++;
505                         status = ocfs2_journal_dirty(handle, insert_bh);
506                         retval = 0;
507                         goto bail;
508                 }
509                 offset += le16_to_cpu(de->rec_len);
510                 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
511         }
512
513         /* when you think about it, the assert above should prevent us
514          * from ever getting here. */
515         retval = -ENOSPC;
516 bail:
517
518         mlog_exit(retval);
519         return retval;
520 }
521
522 static int ocfs2_dir_foreach_blk_id(struct inode *inode,
523                                     unsigned long *f_version,
524                                     loff_t *f_pos, void *priv,
525                                     filldir_t filldir)
526 {
527         int ret, i, filldir_ret;
528         unsigned long offset = *f_pos;
529         struct buffer_head *di_bh = NULL;
530         struct ocfs2_dinode *di;
531         struct ocfs2_inline_data *data;
532         struct ocfs2_dir_entry *de;
533
534         ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), OCFS2_I(inode)->ip_blkno,
535                                &di_bh, OCFS2_BH_CACHED, inode);
536         if (ret) {
537                 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
538                      (unsigned long long)OCFS2_I(inode)->ip_blkno);
539                 goto out;
540         }
541
542         di = (struct ocfs2_dinode *)di_bh->b_data;
543         data = &di->id2.i_data;
544
545         while (*f_pos < i_size_read(inode)) {
546 revalidate:
547                 /* If the dir block has changed since the last call to
548                  * readdir(2), then we might be pointing to an invalid
549                  * dirent right now.  Scan from the start of the block
550                  * to make sure. */
551                 if (*f_version != inode->i_version) {
552                         for (i = 0; i < i_size_read(inode) && i < offset; ) {
553                                 de = (struct ocfs2_dir_entry *)
554                                         (data->id_data + i);
555                                 /* It's too expensive to do a full
556                                  * dirent test each time round this
557                                  * loop, but we do have to test at
558                                  * least that it is non-zero.  A
559                                  * failure will be detected in the
560                                  * dirent test below. */
561                                 if (le16_to_cpu(de->rec_len) <
562                                     OCFS2_DIR_REC_LEN(1))
563                                         break;
564                                 i += le16_to_cpu(de->rec_len);
565                         }
566                         *f_pos = offset = i;
567                         *f_version = inode->i_version;
568                 }
569
570                 de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
571                 if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
572                         /* On error, skip the f_pos to the end. */
573                         *f_pos = i_size_read(inode);
574                         goto out;
575                 }
576                 offset += le16_to_cpu(de->rec_len);
577                 if (le64_to_cpu(de->inode)) {
578                         /* We might block in the next section
579                          * if the data destination is
580                          * currently swapped out.  So, use a
581                          * version stamp to detect whether or
582                          * not the directory has been modified
583                          * during the copy operation.
584                          */
585                         unsigned long version = *f_version;
586                         unsigned char d_type = DT_UNKNOWN;
587
588                         if (de->file_type < OCFS2_FT_MAX)
589                                 d_type = ocfs2_filetype_table[de->file_type];
590
591                         filldir_ret = filldir(priv, de->name,
592                                               de->name_len,
593                                               *f_pos,
594                                               le64_to_cpu(de->inode),
595                                               d_type);
596                         if (filldir_ret)
597                                 break;
598                         if (version != *f_version)
599                                 goto revalidate;
600                 }
601                 *f_pos += le16_to_cpu(de->rec_len);
602         }
603
604 out:
605         brelse(di_bh);
606
607         return 0;
608 }
609
610 static int ocfs2_dir_foreach_blk_el(struct inode *inode,
611                                     unsigned long *f_version,
612                                     loff_t *f_pos, void *priv,
613                                     filldir_t filldir)
614 {
615         int error = 0;
616         unsigned long offset, blk, last_ra_blk = 0;
617         int i, stored;
618         struct buffer_head * bh, * tmp;
619         struct ocfs2_dir_entry * de;
620         int err;
621         struct super_block * sb = inode->i_sb;
622         unsigned int ra_sectors = 16;
623
624         stored = 0;
625         bh = NULL;
626
627         offset = (*f_pos) & (sb->s_blocksize - 1);
628
629         while (!error && !stored && *f_pos < i_size_read(inode)) {
630                 blk = (*f_pos) >> sb->s_blocksize_bits;
631                 bh = ocfs2_bread(inode, blk, &err, 0);
632                 if (!bh) {
633                         mlog(ML_ERROR,
634                              "directory #%llu contains a hole at offset %lld\n",
635                              (unsigned long long)OCFS2_I(inode)->ip_blkno,
636                              *f_pos);
637                         *f_pos += sb->s_blocksize - offset;
638                         continue;
639                 }
640
641                 /* The idea here is to begin with 8k read-ahead and to stay
642                  * 4k ahead of our current position.
643                  *
644                  * TODO: Use the pagecache for this. We just need to
645                  * make sure it's cluster-safe... */
646                 if (!last_ra_blk
647                     || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
648                         for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
649                              i > 0; i--) {
650                                 tmp = ocfs2_bread(inode, ++blk, &err, 1);
651                                 if (tmp)
652                                         brelse(tmp);
653                         }
654                         last_ra_blk = blk;
655                         ra_sectors = 8;
656                 }
657
658 revalidate:
659                 /* If the dir block has changed since the last call to
660                  * readdir(2), then we might be pointing to an invalid
661                  * dirent right now.  Scan from the start of the block
662                  * to make sure. */
663                 if (*f_version != inode->i_version) {
664                         for (i = 0; i < sb->s_blocksize && i < offset; ) {
665                                 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
666                                 /* It's too expensive to do a full
667                                  * dirent test each time round this
668                                  * loop, but we do have to test at
669                                  * least that it is non-zero.  A
670                                  * failure will be detected in the
671                                  * dirent test below. */
672                                 if (le16_to_cpu(de->rec_len) <
673                                     OCFS2_DIR_REC_LEN(1))
674                                         break;
675                                 i += le16_to_cpu(de->rec_len);
676                         }
677                         offset = i;
678                         *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
679                                 | offset;
680                         *f_version = inode->i_version;
681                 }
682
683                 while (!error && *f_pos < i_size_read(inode)
684                        && offset < sb->s_blocksize) {
685                         de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
686                         if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
687                                 /* On error, skip the f_pos to the
688                                    next block. */
689                                 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
690                                 brelse(bh);
691                                 goto out;
692                         }
693                         offset += le16_to_cpu(de->rec_len);
694                         if (le64_to_cpu(de->inode)) {
695                                 /* We might block in the next section
696                                  * if the data destination is
697                                  * currently swapped out.  So, use a
698                                  * version stamp to detect whether or
699                                  * not the directory has been modified
700                                  * during the copy operation.
701                                  */
702                                 unsigned long version = *f_version;
703                                 unsigned char d_type = DT_UNKNOWN;
704
705                                 if (de->file_type < OCFS2_FT_MAX)
706                                         d_type = ocfs2_filetype_table[de->file_type];
707                                 error = filldir(priv, de->name,
708                                                 de->name_len,
709                                                 *f_pos,
710                                                 le64_to_cpu(de->inode),
711                                                 d_type);
712                                 if (error)
713                                         break;
714                                 if (version != *f_version)
715                                         goto revalidate;
716                                 stored ++;
717                         }
718                         *f_pos += le16_to_cpu(de->rec_len);
719                 }
720                 offset = 0;
721                 brelse(bh);
722         }
723
724         stored = 0;
725 out:
726         return stored;
727 }
728
729 static int ocfs2_dir_foreach_blk(struct inode *inode, unsigned long *f_version,
730                                  loff_t *f_pos, void *priv, filldir_t filldir)
731 {
732         if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
733                 return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
734                                                 filldir);
735
736         return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir);
737 }
738
739 /*
740  * This is intended to be called from inside other kernel functions,
741  * so we fake some arguments.
742  */
743 int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
744                       filldir_t filldir)
745 {
746         int ret = 0;
747         unsigned long version = inode->i_version;
748
749         while (*f_pos < i_size_read(inode)) {
750                 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
751                                             filldir);
752                 if (ret)
753                         break;
754         }
755
756         return 0;
757 }
758
759 /*
760  * ocfs2_readdir()
761  *
762  */
763 int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
764 {
765         int error = 0;
766         struct inode *inode = filp->f_path.dentry->d_inode;
767         int lock_level = 0;
768
769         mlog_entry("dirino=%llu\n",
770                    (unsigned long long)OCFS2_I(inode)->ip_blkno);
771
772         error = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
773         if (lock_level && error >= 0) {
774                 /* We release EX lock which used to update atime
775                  * and get PR lock again to reduce contention
776                  * on commonly accessed directories. */
777                 ocfs2_meta_unlock(inode, 1);
778                 lock_level = 0;
779                 error = ocfs2_meta_lock(inode, NULL, 0);
780         }
781         if (error < 0) {
782                 if (error != -ENOENT)
783                         mlog_errno(error);
784                 /* we haven't got any yet, so propagate the error. */
785                 goto bail_nolock;
786         }
787
788         error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
789                                       dirent, filldir);
790
791         ocfs2_meta_unlock(inode, lock_level);
792
793 bail_nolock:
794         mlog_exit(error);
795
796         return error;
797 }
798
799 /*
800  * NOTE: this should always be called with parent dir i_mutex taken.
801  */
802 int ocfs2_find_files_on_disk(const char *name,
803                              int namelen,
804                              u64 *blkno,
805                              struct inode *inode,
806                              struct buffer_head **dirent_bh,
807                              struct ocfs2_dir_entry **dirent)
808 {
809         int status = -ENOENT;
810
811         mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
812                    namelen, name, blkno, inode, dirent_bh, dirent);
813
814         *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
815         if (!*dirent_bh || !*dirent) {
816                 status = -ENOENT;
817                 goto leave;
818         }
819
820         *blkno = le64_to_cpu((*dirent)->inode);
821
822         status = 0;
823 leave:
824         if (status < 0) {
825                 *dirent = NULL;
826                 if (*dirent_bh) {
827                         brelse(*dirent_bh);
828                         *dirent_bh = NULL;
829                 }
830         }
831
832         mlog_exit(status);
833         return status;
834 }
835
836 /*
837  * Convenience function for callers which just want the block number
838  * mapped to a name and don't require the full dirent info, etc.
839  */
840 int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
841                                int namelen, u64 *blkno)
842 {
843         int ret;
844         struct buffer_head *bh = NULL;
845         struct ocfs2_dir_entry *dirent = NULL;
846
847         ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &bh, &dirent);
848         brelse(bh);
849
850         return ret;
851 }
852
853 /* Check for a name within a directory.
854  *
855  * Return 0 if the name does not exist
856  * Return -EEXIST if the directory contains the name
857  *
858  * Callers should have i_mutex + a cluster lock on dir
859  */
860 int ocfs2_check_dir_for_entry(struct inode *dir,
861                               const char *name,
862                               int namelen)
863 {
864         int ret;
865         struct buffer_head *dirent_bh = NULL;
866         struct ocfs2_dir_entry *dirent = NULL;
867
868         mlog_entry("dir %llu, name '%.*s'\n",
869                    (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
870
871         ret = -EEXIST;
872         dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
873         if (dirent_bh)
874                 goto bail;
875
876         ret = 0;
877 bail:
878         if (dirent_bh)
879                 brelse(dirent_bh);
880
881         mlog_exit(ret);
882         return ret;
883 }
884
885 struct ocfs2_empty_dir_priv {
886         unsigned seen_dot;
887         unsigned seen_dot_dot;
888         unsigned seen_other;
889 };
890 static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
891                                    loff_t pos, u64 ino, unsigned type)
892 {
893         struct ocfs2_empty_dir_priv *p = priv;
894
895         /*
896          * Check the positions of "." and ".." records to be sure
897          * they're in the correct place.
898          */
899         if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
900                 p->seen_dot = 1;
901                 return 0;
902         }
903
904         if (name_len == 2 && !strncmp("..", name, 2) &&
905             pos == OCFS2_DIR_REC_LEN(1)) {
906                 p->seen_dot_dot = 1;
907                 return 0;
908         }
909
910         p->seen_other = 1;
911         return 1;
912 }
913 /*
914  * routine to check that the specified directory is empty (for rmdir)
915  *
916  * Returns 1 if dir is empty, zero otherwise.
917  */
918 int ocfs2_empty_dir(struct inode *inode)
919 {
920         int ret;
921         loff_t start = 0;
922         struct ocfs2_empty_dir_priv priv;
923
924         memset(&priv, 0, sizeof(priv));
925
926         ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
927         if (ret)
928                 mlog_errno(ret);
929
930         if (!priv.seen_dot || !priv.seen_dot_dot) {
931                 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
932                      (unsigned long long)OCFS2_I(inode)->ip_blkno);
933                 /*
934                  * XXX: Is it really safe to allow an unlink to continue?
935                  */
936                 return 1;
937         }
938
939         return !priv.seen_other;
940 }
941
942 int ocfs2_fill_new_dir(struct ocfs2_super *osb,
943                        handle_t *handle,
944                        struct inode *parent,
945                        struct inode *inode,
946                        struct buffer_head *fe_bh,
947                        struct ocfs2_alloc_context *data_ac)
948 {
949         int status;
950         struct buffer_head *new_bh = NULL;
951         struct ocfs2_dir_entry *de = NULL;
952
953         mlog_entry_void();
954
955         status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
956                                      data_ac, NULL, &new_bh);
957         if (status < 0) {
958                 mlog_errno(status);
959                 goto bail;
960         }
961
962         ocfs2_set_new_buffer_uptodate(inode, new_bh);
963
964         status = ocfs2_journal_access(handle, inode, new_bh,
965                                       OCFS2_JOURNAL_ACCESS_CREATE);
966         if (status < 0) {
967                 mlog_errno(status);
968                 goto bail;
969         }
970         memset(new_bh->b_data, 0, osb->sb->s_blocksize);
971
972         de = (struct ocfs2_dir_entry *) new_bh->b_data;
973         de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
974         de->name_len = 1;
975         de->rec_len =
976                 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
977         strcpy(de->name, ".");
978         ocfs2_set_de_type(de, S_IFDIR);
979         de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
980         de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
981         de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize -
982                                   OCFS2_DIR_REC_LEN(1));
983         de->name_len = 2;
984         strcpy(de->name, "..");
985         ocfs2_set_de_type(de, S_IFDIR);
986
987         status = ocfs2_journal_dirty(handle, new_bh);
988         if (status < 0) {
989                 mlog_errno(status);
990                 goto bail;
991         }
992
993         i_size_write(inode, inode->i_sb->s_blocksize);
994         inode->i_nlink = 2;
995         inode->i_blocks = ocfs2_inode_sector_count(inode);
996         status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
997         if (status < 0) {
998                 mlog_errno(status);
999                 goto bail;
1000         }
1001
1002         status = 0;
1003 bail:
1004         if (new_bh)
1005                 brelse(new_bh);
1006
1007         mlog_exit(status);
1008         return status;
1009 }
1010
1011 /* returns a bh of the 1st new block in the allocation. */
1012 static int ocfs2_do_extend_dir(struct super_block *sb,
1013                                handle_t *handle,
1014                                struct inode *dir,
1015                                struct buffer_head *parent_fe_bh,
1016                                struct ocfs2_alloc_context *data_ac,
1017                                struct ocfs2_alloc_context *meta_ac,
1018                                struct buffer_head **new_bh)
1019 {
1020         int status;
1021         int extend;
1022         u64 p_blkno, v_blkno;
1023
1024         spin_lock(&OCFS2_I(dir)->ip_lock);
1025         extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
1026         spin_unlock(&OCFS2_I(dir)->ip_lock);
1027
1028         if (extend) {
1029                 u32 offset = OCFS2_I(dir)->ip_clusters;
1030
1031                 status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, &offset,
1032                                                     1, 0, parent_fe_bh, handle,
1033                                                     data_ac, meta_ac, NULL);
1034                 BUG_ON(status == -EAGAIN);
1035                 if (status < 0) {
1036                         mlog_errno(status);
1037                         goto bail;
1038                 }
1039         }
1040
1041         v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
1042         status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
1043         if (status < 0) {
1044                 mlog_errno(status);
1045                 goto bail;
1046         }
1047
1048         *new_bh = sb_getblk(sb, p_blkno);
1049         if (!*new_bh) {
1050                 status = -EIO;
1051                 mlog_errno(status);
1052                 goto bail;
1053         }
1054         status = 0;
1055 bail:
1056         mlog_exit(status);
1057         return status;
1058 }
1059
1060 /* assumes you already have a cluster lock on the directory. */
1061 static int ocfs2_extend_dir(struct ocfs2_super *osb,
1062                             struct inode *dir,
1063                             struct buffer_head *parent_fe_bh,
1064                             struct buffer_head **new_de_bh)
1065 {
1066         int status = 0;
1067         int credits, num_free_extents, drop_alloc_sem = 0;
1068         loff_t dir_i_size;
1069         struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1070         struct ocfs2_alloc_context *data_ac = NULL;
1071         struct ocfs2_alloc_context *meta_ac = NULL;
1072         handle_t *handle = NULL;
1073         struct buffer_head *new_bh = NULL;
1074         struct ocfs2_dir_entry * de;
1075         struct super_block *sb = osb->sb;
1076
1077         mlog_entry_void();
1078
1079         dir_i_size = i_size_read(dir);
1080         mlog(0, "extending dir %llu (i_size = %lld)\n",
1081              (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
1082
1083         /* dir->i_size is always block aligned. */
1084         spin_lock(&OCFS2_I(dir)->ip_lock);
1085         if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
1086                 spin_unlock(&OCFS2_I(dir)->ip_lock);
1087                 num_free_extents = ocfs2_num_free_extents(osb, dir, fe);
1088                 if (num_free_extents < 0) {
1089                         status = num_free_extents;
1090                         mlog_errno(status);
1091                         goto bail;
1092                 }
1093
1094                 if (!num_free_extents) {
1095                         status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac);
1096                         if (status < 0) {
1097                                 if (status != -ENOSPC)
1098                                         mlog_errno(status);
1099                                 goto bail;
1100                         }
1101                 }
1102
1103                 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
1104                 if (status < 0) {
1105                         if (status != -ENOSPC)
1106                                 mlog_errno(status);
1107                         goto bail;
1108                 }
1109
1110                 credits = ocfs2_calc_extend_credits(sb, fe, 1);
1111         } else {
1112                 spin_unlock(&OCFS2_I(dir)->ip_lock);
1113                 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
1114         }
1115
1116         down_write(&OCFS2_I(dir)->ip_alloc_sem);
1117         drop_alloc_sem = 1;
1118
1119         handle = ocfs2_start_trans(osb, credits);
1120         if (IS_ERR(handle)) {
1121                 status = PTR_ERR(handle);
1122                 handle = NULL;
1123                 mlog_errno(status);
1124                 goto bail;
1125         }
1126
1127         status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
1128                                      data_ac, meta_ac, &new_bh);
1129         if (status < 0) {
1130                 mlog_errno(status);
1131                 goto bail;
1132         }
1133
1134         ocfs2_set_new_buffer_uptodate(dir, new_bh);
1135
1136         status = ocfs2_journal_access(handle, dir, new_bh,
1137                                       OCFS2_JOURNAL_ACCESS_CREATE);
1138         if (status < 0) {
1139                 mlog_errno(status);
1140                 goto bail;
1141         }
1142         memset(new_bh->b_data, 0, sb->s_blocksize);
1143         de = (struct ocfs2_dir_entry *) new_bh->b_data;
1144         de->inode = 0;
1145         de->rec_len = cpu_to_le16(sb->s_blocksize);
1146         status = ocfs2_journal_dirty(handle, new_bh);
1147         if (status < 0) {
1148                 mlog_errno(status);
1149                 goto bail;
1150         }
1151
1152         dir_i_size += dir->i_sb->s_blocksize;
1153         i_size_write(dir, dir_i_size);
1154         dir->i_blocks = ocfs2_inode_sector_count(dir);
1155         status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1156         if (status < 0) {
1157                 mlog_errno(status);
1158                 goto bail;
1159         }
1160
1161         *new_de_bh = new_bh;
1162         get_bh(*new_de_bh);
1163 bail:
1164         if (drop_alloc_sem)
1165                 up_write(&OCFS2_I(dir)->ip_alloc_sem);
1166         if (handle)
1167                 ocfs2_commit_trans(osb, handle);
1168
1169         if (data_ac)
1170                 ocfs2_free_alloc_context(data_ac);
1171         if (meta_ac)
1172                 ocfs2_free_alloc_context(meta_ac);
1173
1174         if (new_bh)
1175                 brelse(new_bh);
1176
1177         mlog_exit(status);
1178         return status;
1179 }
1180
1181 /*
1182  * Search the dir for a good spot, extending it if necessary. The
1183  * block containing an appropriate record is returned in ret_de_bh.
1184  */
1185 int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
1186                                  struct inode *dir,
1187                                  struct buffer_head *parent_fe_bh,
1188                                  const char *name,
1189                                  int namelen,
1190                                  struct buffer_head **ret_de_bh)
1191 {
1192         unsigned long offset;
1193         struct buffer_head * bh = NULL;
1194         unsigned short rec_len;
1195         struct ocfs2_dinode *fe;
1196         struct ocfs2_dir_entry *de;
1197         struct super_block *sb;
1198         int status;
1199
1200         mlog_entry_void();
1201
1202         mlog(0, "getting ready to insert namelen %d into dir %llu\n",
1203              namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
1204
1205         BUG_ON(!S_ISDIR(dir->i_mode));
1206         fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1207         BUG_ON(le64_to_cpu(fe->i_size) != i_size_read(dir));
1208
1209         sb = dir->i_sb;
1210
1211         if (!namelen) {
1212                 status = -EINVAL;
1213                 mlog_errno(status);
1214                 goto bail;
1215         }
1216
1217         bh = ocfs2_bread(dir, 0, &status, 0);
1218         if (!bh) {
1219                 mlog_errno(status);
1220                 goto bail;
1221         }
1222
1223         rec_len = OCFS2_DIR_REC_LEN(namelen);
1224         offset = 0;
1225         de = (struct ocfs2_dir_entry *) bh->b_data;
1226         while (1) {
1227                 if ((char *)de >= sb->s_blocksize + bh->b_data) {
1228                         brelse(bh);
1229                         bh = NULL;
1230
1231                         if (i_size_read(dir) <= offset) {
1232                                 status = ocfs2_extend_dir(osb,
1233                                                           dir,
1234                                                           parent_fe_bh,
1235                                                           &bh);
1236                                 if (status < 0) {
1237                                         mlog_errno(status);
1238                                         goto bail;
1239                                 }
1240                                 BUG_ON(!bh);
1241                                 *ret_de_bh = bh;
1242                                 get_bh(*ret_de_bh);
1243                                 goto bail;
1244                         }
1245                         bh = ocfs2_bread(dir,
1246                                          offset >> sb->s_blocksize_bits,
1247                                          &status,
1248                                          0);
1249                         if (!bh) {
1250                                 mlog_errno(status);
1251                                 goto bail;
1252                         }
1253                         /* move to next block */
1254                         de = (struct ocfs2_dir_entry *) bh->b_data;
1255                 }
1256                 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1257                         status = -ENOENT;
1258                         goto bail;
1259                 }
1260                 if (ocfs2_match(namelen, name, de)) {
1261                         status = -EEXIST;
1262                         goto bail;
1263                 }
1264                 if (ocfs2_dirent_would_fit(de, rec_len)) {
1265                         /* Ok, we found a spot. Return this bh and let
1266                          * the caller actually fill it in. */
1267                         *ret_de_bh = bh;
1268                         get_bh(*ret_de_bh);
1269                         status = 0;
1270                         goto bail;
1271                 }
1272                 offset += le16_to_cpu(de->rec_len);
1273                 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
1274         }
1275
1276         status = 0;
1277 bail:
1278         if (bh)
1279                 brelse(bh);
1280
1281         mlog_exit(status);
1282         return status;
1283 }