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