add loglevel to printk in fs/afs/cmservice.c
[linux-2.6] / fs / ocfs2 / dir.c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * dir.c
5  *
6  * Creates, reads, walks and deletes directory-nodes
7  *
8  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
9  *
10  *  Portions of this code from linux/fs/ext3/dir.c
11  *
12  *  Copyright (C) 1992, 1993, 1994, 1995
13  *  Remy Card (card@masi.ibp.fr)
14  *  Laboratoire MASI - Institut Blaise pascal
15  *  Universite Pierre et Marie Curie (Paris VI)
16  *
17  *   from
18  *
19  *   linux/fs/minix/dir.c
20  *
21  *   Copyright (C) 1991, 1992 Linux Torvalds
22  *
23  * This program is free software; you can redistribute it and/or
24  * modify it under the terms of the GNU General Public
25  * License as published by the Free Software Foundation; either
26  * version 2 of the License, or (at your option) any later version.
27  *
28  * This program is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
31  * General Public License for more details.
32  *
33  * You should have received a copy of the GNU General Public
34  * License along with this program; if not, write to the
35  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
36  * Boston, MA 021110-1307, USA.
37  */
38
39 #include <linux/fs.h>
40 #include <linux/types.h>
41 #include <linux/slab.h>
42 #include <linux/highmem.h>
43
44 #define MLOG_MASK_PREFIX ML_NAMEI
45 #include <cluster/masklog.h>
46
47 #include "ocfs2.h"
48
49 #include "alloc.h"
50 #include "dir.h"
51 #include "dlmglue.h"
52 #include "extent_map.h"
53 #include "file.h"
54 #include "inode.h"
55 #include "journal.h"
56 #include "namei.h"
57 #include "suballoc.h"
58 #include "uptodate.h"
59
60 #include "buffer_head_io.h"
61
62 static unsigned char ocfs2_filetype_table[] = {
63         DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
64 };
65
66 static int ocfs2_extend_dir(struct ocfs2_super *osb,
67                             struct inode *dir,
68                             struct buffer_head *parent_fe_bh,
69                             struct buffer_head **new_de_bh);
70 /*
71  * ocfs2_readdir()
72  *
73  */
74 int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
75 {
76         int error = 0;
77         unsigned long offset, blk;
78         int i, num, stored;
79         struct buffer_head * bh, * tmp;
80         struct ocfs2_dir_entry * de;
81         int err;
82         struct inode *inode = filp->f_dentry->d_inode;
83         struct super_block * sb = inode->i_sb;
84         int have_disk_lock = 0;
85
86         mlog_entry("dirino=%"MLFu64"\n", OCFS2_I(inode)->ip_blkno);
87
88         stored = 0;
89         bh = NULL;
90
91         error = ocfs2_meta_lock(inode, NULL, NULL, 0);
92         if (error < 0) {
93                 if (error != -ENOENT)
94                         mlog_errno(error);
95                 /* we haven't got any yet, so propagate the error. */
96                 stored = error;
97                 goto bail;
98         }
99         have_disk_lock = 1;
100
101         offset = filp->f_pos & (sb->s_blocksize - 1);
102
103         while (!error && !stored && filp->f_pos < i_size_read(inode)) {
104                 blk = (filp->f_pos) >> sb->s_blocksize_bits;
105                 bh = ocfs2_bread(inode, blk, &err, 0);
106                 if (!bh) {
107                         mlog(ML_ERROR, "directory #%"MLFu64" contains a hole "
108                                        "at offset %lld\n",
109                              OCFS2_I(inode)->ip_blkno,
110                              filp->f_pos);
111                         filp->f_pos += sb->s_blocksize - offset;
112                         continue;
113                 }
114
115                 /*
116                  * Do the readahead (8k)
117                  */
118                 if (!offset) {
119                         for (i = 16 >> (sb->s_blocksize_bits - 9), num = 0;
120                              i > 0; i--) {
121                                 tmp = ocfs2_bread(inode, ++blk, &err, 1);
122                                 if (tmp)
123                                         brelse(tmp);
124                         }
125                 }
126
127 revalidate:
128                 /* If the dir block has changed since the last call to
129                  * readdir(2), then we might be pointing to an invalid
130                  * dirent right now.  Scan from the start of the block
131                  * to make sure. */
132                 if (filp->f_version != inode->i_version) {
133                         for (i = 0; i < sb->s_blocksize && i < offset; ) {
134                                 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
135                                 /* It's too expensive to do a full
136                                  * dirent test each time round this
137                                  * loop, but we do have to test at
138                                  * least that it is non-zero.  A
139                                  * failure will be detected in the
140                                  * dirent test below. */
141                                 if (le16_to_cpu(de->rec_len) <
142                                     OCFS2_DIR_REC_LEN(1))
143                                         break;
144                                 i += le16_to_cpu(de->rec_len);
145                         }
146                         offset = i;
147                         filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1))
148                                 | offset;
149                         filp->f_version = inode->i_version;
150                 }
151
152                 while (!error && filp->f_pos < i_size_read(inode)
153                        && offset < sb->s_blocksize) {
154                         de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
155                         if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
156                                 /* On error, skip the f_pos to the
157                                    next block. */
158                                 filp->f_pos = (filp->f_pos |
159                                                (sb->s_blocksize - 1)) + 1;
160                                 brelse(bh);
161                                 goto bail;
162                         }
163                         offset += le16_to_cpu(de->rec_len);
164                         if (le64_to_cpu(de->inode)) {
165                                 /* We might block in the next section
166                                  * if the data destination is
167                                  * currently swapped out.  So, use a
168                                  * version stamp to detect whether or
169                                  * not the directory has been modified
170                                  * during the copy operation.
171                                  */
172                                 unsigned long version = filp->f_version;
173                                 unsigned char d_type = DT_UNKNOWN;
174
175                                 if (de->file_type < OCFS2_FT_MAX)
176                                         d_type = ocfs2_filetype_table[de->file_type];
177                                 error = filldir(dirent, de->name,
178                                                 de->name_len,
179                                                 filp->f_pos,
180                                                 ino_from_blkno(sb, le64_to_cpu(de->inode)),
181                                                 d_type);
182                                 if (error)
183                                         break;
184                                 if (version != filp->f_version)
185                                         goto revalidate;
186                                 stored ++;
187                         }
188                         filp->f_pos += le16_to_cpu(de->rec_len);
189                 }
190                 offset = 0;
191                 brelse(bh);
192         }
193
194         stored = 0;
195 bail:
196         if (have_disk_lock)
197                 ocfs2_meta_unlock(inode, 0);
198
199         mlog_exit(stored);
200
201         return stored;
202 }
203
204 /*
205  * NOTE: this should always be called with parent dir i_mutex taken.
206  */
207 int ocfs2_find_files_on_disk(const char *name,
208                              int namelen,
209                              u64 *blkno,
210                              struct inode *inode,
211                              struct buffer_head **dirent_bh,
212                              struct ocfs2_dir_entry **dirent)
213 {
214         int status = -ENOENT;
215         struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
216
217         mlog_entry("(osb=%p, parent=%"MLFu64", name='%.*s', blkno=%p, "
218                    "inode=%p)\n",
219                    osb, OCFS2_I(inode)->ip_blkno, namelen, name, blkno, inode);
220
221         *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
222         if (!*dirent_bh || !*dirent) {
223                 status = -ENOENT;
224                 goto leave;
225         }
226
227         *blkno = le64_to_cpu((*dirent)->inode);
228
229         status = 0;
230 leave:
231         if (status < 0) {
232                 *dirent = NULL;
233                 if (*dirent_bh) {
234                         brelse(*dirent_bh);
235                         *dirent_bh = NULL;
236                 }
237         }
238
239         mlog_exit(status);
240         return status;
241 }
242
243 /* Check for a name within a directory.
244  *
245  * Return 0 if the name does not exist
246  * Return -EEXIST if the directory contains the name
247  *
248  * Callers should have i_mutex + a cluster lock on dir
249  */
250 int ocfs2_check_dir_for_entry(struct inode *dir,
251                               const char *name,
252                               int namelen)
253 {
254         int ret;
255         struct buffer_head *dirent_bh = NULL;
256         struct ocfs2_dir_entry *dirent = NULL;
257
258         mlog_entry("dir %"MLFu64", name '%.*s'\n", OCFS2_I(dir)->ip_blkno,
259                    namelen, name);
260
261         ret = -EEXIST;
262         dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
263         if (dirent_bh)
264                 goto bail;
265
266         ret = 0;
267 bail:
268         if (dirent_bh)
269                 brelse(dirent_bh);
270
271         mlog_exit(ret);
272         return ret;
273 }
274
275 /*
276  * routine to check that the specified directory is empty (for rmdir)
277  */
278 int ocfs2_empty_dir(struct inode *inode)
279 {
280         unsigned long offset;
281         struct buffer_head * bh;
282         struct ocfs2_dir_entry * de, * de1;
283         struct super_block * sb;
284         int err;
285
286         sb = inode->i_sb;
287         if ((i_size_read(inode) <
288              (OCFS2_DIR_REC_LEN(1) + OCFS2_DIR_REC_LEN(2))) ||
289             !(bh = ocfs2_bread(inode, 0, &err, 0))) {
290                 mlog(ML_ERROR, "bad directory (dir #%"MLFu64") - "
291                                "no data block\n",
292                      OCFS2_I(inode)->ip_blkno);
293                 return 1;
294         }
295
296         de = (struct ocfs2_dir_entry *) bh->b_data;
297         de1 = (struct ocfs2_dir_entry *)
298                         ((char *)de + le16_to_cpu(de->rec_len));
299         if ((le64_to_cpu(de->inode) != OCFS2_I(inode)->ip_blkno) ||
300                         !le64_to_cpu(de1->inode) ||
301                         strcmp(".", de->name) ||
302                         strcmp("..", de1->name)) {
303                 mlog(ML_ERROR, "bad directory (dir #%"MLFu64") - "
304                                "no `.' or `..'\n",
305                      OCFS2_I(inode)->ip_blkno);
306                 brelse(bh);
307                 return 1;
308         }
309         offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len);
310         de = (struct ocfs2_dir_entry *)((char *)de1 + le16_to_cpu(de1->rec_len));
311         while (offset < i_size_read(inode) ) {
312                 if (!bh || (void *)de >= (void *)(bh->b_data + sb->s_blocksize)) {
313                         brelse(bh);
314                         bh = ocfs2_bread(inode,
315                                          offset >> sb->s_blocksize_bits, &err, 0);
316                         if (!bh) {
317                                 mlog(ML_ERROR, "directory #%"MLFu64" contains "
318                                                "a hole at offset %lu\n",
319                                      OCFS2_I(inode)->ip_blkno, offset);
320                                 offset += sb->s_blocksize;
321                                 continue;
322                         }
323                         de = (struct ocfs2_dir_entry *) bh->b_data;
324                 }
325                 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
326                         brelse(bh);
327                         return 1;
328                 }
329                 if (le64_to_cpu(de->inode)) {
330                         brelse(bh);
331                         return 0;
332                 }
333                 offset += le16_to_cpu(de->rec_len);
334                 de = (struct ocfs2_dir_entry *)
335                         ((char *)de + le16_to_cpu(de->rec_len));
336         }
337         brelse(bh);
338         return 1;
339 }
340
341 /* returns a bh of the 1st new block in the allocation. */
342 int ocfs2_do_extend_dir(struct super_block *sb,
343                         struct ocfs2_journal_handle *handle,
344                         struct inode *dir,
345                         struct buffer_head *parent_fe_bh,
346                         struct ocfs2_alloc_context *data_ac,
347                         struct ocfs2_alloc_context *meta_ac,
348                         struct buffer_head **new_bh)
349 {
350         int status;
351         int extend;
352         u64 p_blkno;
353
354         spin_lock(&OCFS2_I(dir)->ip_lock);
355         extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
356         spin_unlock(&OCFS2_I(dir)->ip_lock);
357
358         if (extend) {
359                 status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, 1,
360                                                     parent_fe_bh, handle,
361                                                     data_ac, meta_ac, NULL);
362                 BUG_ON(status == -EAGAIN);
363                 if (status < 0) {
364                         mlog_errno(status);
365                         goto bail;
366                 }
367         }
368
369         status = ocfs2_extent_map_get_blocks(dir, (dir->i_blocks >>
370                                                    (sb->s_blocksize_bits - 9)),
371                                              1, &p_blkno, NULL);
372         if (status < 0) {
373                 mlog_errno(status);
374                 goto bail;
375         }
376
377         *new_bh = sb_getblk(sb, p_blkno);
378         if (!*new_bh) {
379                 status = -EIO;
380                 mlog_errno(status);
381                 goto bail;
382         }
383         status = 0;
384 bail:
385         mlog_exit(status);
386         return status;
387 }
388
389 /* assumes you already have a cluster lock on the directory. */
390 static int ocfs2_extend_dir(struct ocfs2_super *osb,
391                             struct inode *dir,
392                             struct buffer_head *parent_fe_bh,
393                             struct buffer_head **new_de_bh)
394 {
395         int status = 0;
396         int credits, num_free_extents;
397         loff_t dir_i_size;
398         struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
399         struct ocfs2_alloc_context *data_ac = NULL;
400         struct ocfs2_alloc_context *meta_ac = NULL;
401         struct ocfs2_journal_handle *handle = NULL;
402         struct buffer_head *new_bh = NULL;
403         struct ocfs2_dir_entry * de;
404         struct super_block *sb = osb->sb;
405
406         mlog_entry_void();
407
408         dir_i_size = i_size_read(dir);
409         mlog(0, "extending dir %"MLFu64" (i_size = %lld)\n",
410              OCFS2_I(dir)->ip_blkno, dir_i_size);
411
412         handle = ocfs2_alloc_handle(osb);
413         if (handle == NULL) {
414                 status = -ENOMEM;
415                 mlog_errno(status);
416                 goto bail;
417         }
418
419         /* dir->i_size is always block aligned. */
420         spin_lock(&OCFS2_I(dir)->ip_lock);
421         if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
422                 spin_unlock(&OCFS2_I(dir)->ip_lock);
423                 num_free_extents = ocfs2_num_free_extents(osb, dir, fe);
424                 if (num_free_extents < 0) {
425                         status = num_free_extents;
426                         mlog_errno(status);
427                         goto bail;
428                 }
429
430                 if (!num_free_extents) {
431                         status = ocfs2_reserve_new_metadata(osb, handle,
432                                                             fe, &meta_ac);
433                         if (status < 0) {
434                                 if (status != -ENOSPC)
435                                         mlog_errno(status);
436                                 goto bail;
437                         }
438                 }
439
440                 status = ocfs2_reserve_clusters(osb, handle, 1, &data_ac);
441                 if (status < 0) {
442                         if (status != -ENOSPC)
443                                 mlog_errno(status);
444                         goto bail;
445                 }
446
447                 credits = ocfs2_calc_extend_credits(sb, fe, 1);
448         } else {
449                 spin_unlock(&OCFS2_I(dir)->ip_lock);
450                 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
451         }
452
453         handle = ocfs2_start_trans(osb, handle, credits);
454         if (IS_ERR(handle)) {
455                 status = PTR_ERR(handle);
456                 handle = NULL;
457                 mlog_errno(status);
458                 goto bail;
459         }
460
461         status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
462                                      data_ac, meta_ac, &new_bh);
463         if (status < 0) {
464                 mlog_errno(status);
465                 goto bail;
466         }
467
468         ocfs2_set_new_buffer_uptodate(dir, new_bh);
469
470         status = ocfs2_journal_access(handle, dir, new_bh,
471                                       OCFS2_JOURNAL_ACCESS_CREATE);
472         if (status < 0) {
473                 mlog_errno(status);
474                 goto bail;
475         }
476         memset(new_bh->b_data, 0, sb->s_blocksize);
477         de = (struct ocfs2_dir_entry *) new_bh->b_data;
478         de->inode = 0;
479         de->rec_len = cpu_to_le16(sb->s_blocksize);
480         status = ocfs2_journal_dirty(handle, new_bh);
481         if (status < 0) {
482                 mlog_errno(status);
483                 goto bail;
484         }
485
486         dir_i_size += dir->i_sb->s_blocksize;
487         i_size_write(dir, dir_i_size);
488         dir->i_blocks = ocfs2_align_bytes_to_sectors(dir_i_size);
489         status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
490         if (status < 0) {
491                 mlog_errno(status);
492                 goto bail;
493         }
494
495         *new_de_bh = new_bh;
496         get_bh(*new_de_bh);
497 bail:
498         if (handle)
499                 ocfs2_commit_trans(handle);
500
501         if (data_ac)
502                 ocfs2_free_alloc_context(data_ac);
503         if (meta_ac)
504                 ocfs2_free_alloc_context(meta_ac);
505
506         if (new_bh)
507                 brelse(new_bh);
508
509         mlog_exit(status);
510         return status;
511 }
512
513 /*
514  * Search the dir for a good spot, extending it if necessary. The
515  * block containing an appropriate record is returned in ret_de_bh.
516  */
517 int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
518                                  struct inode *dir,
519                                  struct buffer_head *parent_fe_bh,
520                                  const char *name,
521                                  int namelen,
522                                  struct buffer_head **ret_de_bh)
523 {
524         unsigned long offset;
525         struct buffer_head * bh = NULL;
526         unsigned short rec_len;
527         struct ocfs2_dinode *fe;
528         struct ocfs2_dir_entry *de;
529         struct super_block *sb;
530         int status;
531
532         mlog_entry_void();
533
534         mlog(0, "getting ready to insert namelen %d into dir %"MLFu64"\n",
535              namelen, OCFS2_I(dir)->ip_blkno);
536
537         BUG_ON(!S_ISDIR(dir->i_mode));
538         fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
539         BUG_ON(le64_to_cpu(fe->i_size) != i_size_read(dir));
540
541         sb = dir->i_sb;
542
543         if (!namelen) {
544                 status = -EINVAL;
545                 mlog_errno(status);
546                 goto bail;
547         }
548
549         bh = ocfs2_bread(dir, 0, &status, 0);
550         if (!bh) {
551                 mlog_errno(status);
552                 goto bail;
553         }
554
555         rec_len = OCFS2_DIR_REC_LEN(namelen);
556         offset = 0;
557         de = (struct ocfs2_dir_entry *) bh->b_data;
558         while (1) {
559                 if ((char *)de >= sb->s_blocksize + bh->b_data) {
560                         brelse(bh);
561                         bh = NULL;
562
563                         if (i_size_read(dir) <= offset) {
564                                 status = ocfs2_extend_dir(osb,
565                                                           dir,
566                                                           parent_fe_bh,
567                                                           &bh);
568                                 if (status < 0) {
569                                         mlog_errno(status);
570                                         goto bail;
571                                 }
572                                 BUG_ON(!bh);
573                                 *ret_de_bh = bh;
574                                 get_bh(*ret_de_bh);
575                                 goto bail;
576                         }
577                         bh = ocfs2_bread(dir,
578                                          offset >> sb->s_blocksize_bits,
579                                          &status,
580                                          0);
581                         if (!bh) {
582                                 mlog_errno(status);
583                                 goto bail;
584                         }
585                         /* move to next block */
586                         de = (struct ocfs2_dir_entry *) bh->b_data;
587                 }
588                 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
589                         status = -ENOENT;
590                         goto bail;
591                 }
592                 if (ocfs2_match(namelen, name, de)) {
593                         status = -EEXIST;
594                         goto bail;
595                 }
596                 if (((le64_to_cpu(de->inode) == 0) &&
597                      (le16_to_cpu(de->rec_len) >= rec_len)) ||
598                     (le16_to_cpu(de->rec_len) >=
599                      (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
600                         /* Ok, we found a spot. Return this bh and let
601                          * the caller actually fill it in. */
602                         *ret_de_bh = bh;
603                         get_bh(*ret_de_bh);
604                         status = 0;
605                         goto bail;
606                 }
607                 offset += le16_to_cpu(de->rec_len);
608                 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
609         }
610
611         status = 0;
612 bail:
613         if (bh)
614                 brelse(bh);
615
616         mlog_exit(status);
617         return status;
618 }