ocfs2: Write support for inline data
[deliverable/linux.git] / fs / ocfs2 / dir.c
CommitLineData
ccd979bd
MF
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"
316f4b9f 58#include "super.h"
ccd979bd
MF
59#include "uptodate.h"
60
61#include "buffer_head_io.h"
62
316f4b9f
MF
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
ccd979bd
MF
68static unsigned char ocfs2_filetype_table[] = {
69 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
70};
71
72static 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);
316f4b9f
MF
76static 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
5eae5b96
MF
84static int ocfs2_check_dir_entry(struct inode * dir,
85 struct ocfs2_dir_entry * de,
86 struct buffer_head * bh,
87 unsigned long offset)
316f4b9f
MF
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
110static 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 */
124static 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
169bail:
170 mlog_exit(ret);
171 return ret;
172}
173
174struct 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
200restart:
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
265cleanup_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
38760e24
MF
274int ocfs2_update_entry(struct inode *dir, handle_t *handle,
275 struct buffer_head *de_bh, struct ocfs2_dir_entry *de,
276 struct inode *new_entry_inode)
277{
278 int ret;
279
280 ret = ocfs2_journal_access(handle, dir, de_bh,
281 OCFS2_JOURNAL_ACCESS_WRITE);
282 if (ret) {
283 mlog_errno(ret);
284 goto out;
285 }
286
287 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
288 ocfs2_set_de_type(de, new_entry_inode->i_mode);
289
290 ocfs2_journal_dirty(handle, de_bh);
291
292out:
293 return ret;
294}
295
316f4b9f
MF
296/*
297 * ocfs2_delete_entry deletes a directory entry by merging it with the
298 * previous entry
299 */
300int ocfs2_delete_entry(handle_t *handle,
301 struct inode *dir,
302 struct ocfs2_dir_entry *de_del,
303 struct buffer_head *bh)
304{
305 struct ocfs2_dir_entry *de, *pde;
306 int i, status = -ENOENT;
307
308 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
309
310 i = 0;
311 pde = NULL;
312 de = (struct ocfs2_dir_entry *) bh->b_data;
313 while (i < bh->b_size) {
314 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
315 status = -EIO;
316 mlog_errno(status);
317 goto bail;
318 }
319 if (de == de_del) {
320 status = ocfs2_journal_access(handle, dir, bh,
321 OCFS2_JOURNAL_ACCESS_WRITE);
322 if (status < 0) {
323 status = -EIO;
324 mlog_errno(status);
325 goto bail;
326 }
327 if (pde)
328 pde->rec_len =
329 cpu_to_le16(le16_to_cpu(pde->rec_len) +
330 le16_to_cpu(de->rec_len));
331 else
332 de->inode = 0;
333 dir->i_version++;
334 status = ocfs2_journal_dirty(handle, bh);
335 goto bail;
336 }
337 i += le16_to_cpu(de->rec_len);
338 pde = de;
339 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
340 }
341bail:
342 mlog_exit(status);
343 return status;
344}
345
8553cf4f
MF
346/*
347 * Check whether 'de' has enough room to hold an entry of
348 * 'new_rec_len' bytes.
349 */
350static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
351 unsigned int new_rec_len)
352{
353 unsigned int de_really_used;
354
355 /* Check whether this is an empty record with enough space */
356 if (le64_to_cpu(de->inode) == 0 &&
357 le16_to_cpu(de->rec_len) >= new_rec_len)
358 return 1;
359
360 /*
361 * Record might have free space at the end which we can
362 * use.
363 */
364 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
365 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
366 return 1;
367
368 return 0;
369}
370
316f4b9f
MF
371/* we don't always have a dentry for what we want to add, so people
372 * like orphan dir can call this instead.
373 *
374 * If you pass me insert_bh, I'll skip the search of the other dir
375 * blocks and put the record in there.
376 */
377int __ocfs2_add_entry(handle_t *handle,
378 struct inode *dir,
379 const char *name, int namelen,
380 struct inode *inode, u64 blkno,
381 struct buffer_head *parent_fe_bh,
382 struct buffer_head *insert_bh)
383{
384 unsigned long offset;
385 unsigned short rec_len;
386 struct ocfs2_dir_entry *de, *de1;
387 struct super_block *sb;
388 int retval, status;
389
390 mlog_entry_void();
391
392 sb = dir->i_sb;
393
394 if (!namelen)
395 return -EINVAL;
396
397 rec_len = OCFS2_DIR_REC_LEN(namelen);
398 offset = 0;
399 de = (struct ocfs2_dir_entry *) insert_bh->b_data;
400 while (1) {
401 BUG_ON((char *)de >= sb->s_blocksize + insert_bh->b_data);
402 /* These checks should've already been passed by the
403 * prepare function, but I guess we can leave them
404 * here anyway. */
405 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
406 retval = -ENOENT;
407 goto bail;
408 }
409 if (ocfs2_match(namelen, name, de)) {
410 retval = -EEXIST;
411 goto bail;
412 }
8553cf4f
MF
413
414 if (ocfs2_dirent_would_fit(de, rec_len)) {
316f4b9f
MF
415 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
416 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
417 if (retval < 0) {
418 mlog_errno(retval);
419 goto bail;
420 }
421
422 status = ocfs2_journal_access(handle, dir, insert_bh,
423 OCFS2_JOURNAL_ACCESS_WRITE);
424 /* By now the buffer is marked for journaling */
425 offset += le16_to_cpu(de->rec_len);
426 if (le64_to_cpu(de->inode)) {
427 de1 = (struct ocfs2_dir_entry *)((char *) de +
428 OCFS2_DIR_REC_LEN(de->name_len));
429 de1->rec_len =
430 cpu_to_le16(le16_to_cpu(de->rec_len) -
431 OCFS2_DIR_REC_LEN(de->name_len));
432 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
433 de = de1;
434 }
435 de->file_type = OCFS2_FT_UNKNOWN;
436 if (blkno) {
437 de->inode = cpu_to_le64(blkno);
438 ocfs2_set_de_type(de, inode->i_mode);
439 } else
440 de->inode = 0;
441 de->name_len = namelen;
442 memcpy(de->name, name, namelen);
443
444 dir->i_version++;
445 status = ocfs2_journal_dirty(handle, insert_bh);
446 retval = 0;
447 goto bail;
448 }
449 offset += le16_to_cpu(de->rec_len);
450 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
451 }
452
453 /* when you think about it, the assert above should prevent us
454 * from ever getting here. */
455 retval = -ENOSPC;
456bail:
457
458 mlog_exit(retval);
459 return retval;
460}
461
b8bc5f4f
MF
462static int ocfs2_dir_foreach_blk(struct inode *inode, unsigned long *f_version,
463 loff_t *f_pos, void *priv, filldir_t filldir)
ccd979bd
MF
464{
465 int error = 0;
aa958874
MF
466 unsigned long offset, blk, last_ra_blk = 0;
467 int i, stored;
ccd979bd
MF
468 struct buffer_head * bh, * tmp;
469 struct ocfs2_dir_entry * de;
470 int err;
ccd979bd 471 struct super_block * sb = inode->i_sb;
aa958874 472 unsigned int ra_sectors = 16;
ccd979bd
MF
473
474 stored = 0;
475 bh = NULL;
476
b8bc5f4f 477 offset = (*f_pos) & (sb->s_blocksize - 1);
ccd979bd 478
b8bc5f4f
MF
479 while (!error && !stored && *f_pos < i_size_read(inode)) {
480 blk = (*f_pos) >> sb->s_blocksize_bits;
ccd979bd
MF
481 bh = ocfs2_bread(inode, blk, &err, 0);
482 if (!bh) {
b0697053
MF
483 mlog(ML_ERROR,
484 "directory #%llu contains a hole at offset %lld\n",
485 (unsigned long long)OCFS2_I(inode)->ip_blkno,
b8bc5f4f
MF
486 *f_pos);
487 *f_pos += sb->s_blocksize - offset;
ccd979bd
MF
488 continue;
489 }
490
aa958874
MF
491 /* The idea here is to begin with 8k read-ahead and to stay
492 * 4k ahead of our current position.
493 *
494 * TODO: Use the pagecache for this. We just need to
495 * make sure it's cluster-safe... */
496 if (!last_ra_blk
497 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
498 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
ccd979bd
MF
499 i > 0; i--) {
500 tmp = ocfs2_bread(inode, ++blk, &err, 1);
501 if (tmp)
502 brelse(tmp);
503 }
aa958874
MF
504 last_ra_blk = blk;
505 ra_sectors = 8;
ccd979bd
MF
506 }
507
508revalidate:
509 /* If the dir block has changed since the last call to
510 * readdir(2), then we might be pointing to an invalid
511 * dirent right now. Scan from the start of the block
512 * to make sure. */
b8bc5f4f 513 if (*f_version != inode->i_version) {
ccd979bd
MF
514 for (i = 0; i < sb->s_blocksize && i < offset; ) {
515 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
516 /* It's too expensive to do a full
517 * dirent test each time round this
518 * loop, but we do have to test at
519 * least that it is non-zero. A
520 * failure will be detected in the
521 * dirent test below. */
522 if (le16_to_cpu(de->rec_len) <
523 OCFS2_DIR_REC_LEN(1))
524 break;
525 i += le16_to_cpu(de->rec_len);
526 }
527 offset = i;
b8bc5f4f 528 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
ccd979bd 529 | offset;
b8bc5f4f 530 *f_version = inode->i_version;
ccd979bd
MF
531 }
532
b8bc5f4f 533 while (!error && *f_pos < i_size_read(inode)
ccd979bd
MF
534 && offset < sb->s_blocksize) {
535 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
536 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
537 /* On error, skip the f_pos to the
538 next block. */
b8bc5f4f 539 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
ccd979bd 540 brelse(bh);
b8bc5f4f 541 goto out;
ccd979bd
MF
542 }
543 offset += le16_to_cpu(de->rec_len);
544 if (le64_to_cpu(de->inode)) {
545 /* We might block in the next section
546 * if the data destination is
547 * currently swapped out. So, use a
548 * version stamp to detect whether or
549 * not the directory has been modified
550 * during the copy operation.
551 */
b8bc5f4f 552 unsigned long version = *f_version;
ccd979bd
MF
553 unsigned char d_type = DT_UNKNOWN;
554
555 if (de->file_type < OCFS2_FT_MAX)
556 d_type = ocfs2_filetype_table[de->file_type];
b8bc5f4f 557 error = filldir(priv, de->name,
ccd979bd 558 de->name_len,
b8bc5f4f 559 *f_pos,
7e853679 560 le64_to_cpu(de->inode),
ccd979bd
MF
561 d_type);
562 if (error)
563 break;
b8bc5f4f 564 if (version != *f_version)
ccd979bd
MF
565 goto revalidate;
566 stored ++;
567 }
b8bc5f4f 568 *f_pos += le16_to_cpu(de->rec_len);
ccd979bd
MF
569 }
570 offset = 0;
571 brelse(bh);
572 }
573
574 stored = 0;
b8bc5f4f
MF
575out:
576 return stored;
577}
578
5eae5b96
MF
579/*
580 * This is intended to be called from inside other kernel functions,
581 * so we fake some arguments.
582 */
583int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
584 filldir_t filldir)
585{
586 int ret = 0;
587 unsigned long version = inode->i_version;
588
589 while (*f_pos < i_size_read(inode)) {
590 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
591 filldir);
592 if (ret)
593 break;
594 }
595
596 return 0;
597}
598
b8bc5f4f
MF
599/*
600 * ocfs2_readdir()
601 *
602 */
603int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
604{
605 int error = 0;
606 struct inode *inode = filp->f_path.dentry->d_inode;
607 int lock_level = 0;
608
609 mlog_entry("dirino=%llu\n",
610 (unsigned long long)OCFS2_I(inode)->ip_blkno);
611
612 error = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
613 if (lock_level && error >= 0) {
614 /* We release EX lock which used to update atime
615 * and get PR lock again to reduce contention
616 * on commonly accessed directories. */
617 ocfs2_meta_unlock(inode, 1);
618 lock_level = 0;
619 error = ocfs2_meta_lock(inode, NULL, 0);
620 }
621 if (error < 0) {
622 if (error != -ENOENT)
623 mlog_errno(error);
624 /* we haven't got any yet, so propagate the error. */
625 goto bail_nolock;
626 }
627
628 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
629 dirent, filldir);
630
25899dee 631 ocfs2_meta_unlock(inode, lock_level);
ccd979bd 632
aa958874 633bail_nolock:
b8bc5f4f 634 mlog_exit(error);
ccd979bd 635
b8bc5f4f 636 return error;
ccd979bd
MF
637}
638
639/*
1b1dcc1b 640 * NOTE: this should always be called with parent dir i_mutex taken.
ccd979bd
MF
641 */
642int ocfs2_find_files_on_disk(const char *name,
643 int namelen,
644 u64 *blkno,
645 struct inode *inode,
646 struct buffer_head **dirent_bh,
647 struct ocfs2_dir_entry **dirent)
648{
649 int status = -ENOENT;
ccd979bd 650
2b388c67
JB
651 mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
652 namelen, name, blkno, inode, dirent_bh, dirent);
ccd979bd
MF
653
654 *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
655 if (!*dirent_bh || !*dirent) {
656 status = -ENOENT;
657 goto leave;
658 }
659
660 *blkno = le64_to_cpu((*dirent)->inode);
661
662 status = 0;
663leave:
664 if (status < 0) {
665 *dirent = NULL;
666 if (*dirent_bh) {
667 brelse(*dirent_bh);
668 *dirent_bh = NULL;
669 }
670 }
671
672 mlog_exit(status);
673 return status;
674}
675
be94d117
MF
676/*
677 * Convenience function for callers which just want the block number
678 * mapped to a name and don't require the full dirent info, etc.
679 */
680int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
681 int namelen, u64 *blkno)
682{
683 int ret;
684 struct buffer_head *bh = NULL;
685 struct ocfs2_dir_entry *dirent = NULL;
686
687 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &bh, &dirent);
688 brelse(bh);
689
690 return ret;
691}
692
ccd979bd
MF
693/* Check for a name within a directory.
694 *
695 * Return 0 if the name does not exist
696 * Return -EEXIST if the directory contains the name
697 *
1b1dcc1b 698 * Callers should have i_mutex + a cluster lock on dir
ccd979bd
MF
699 */
700int ocfs2_check_dir_for_entry(struct inode *dir,
701 const char *name,
702 int namelen)
703{
704 int ret;
705 struct buffer_head *dirent_bh = NULL;
706 struct ocfs2_dir_entry *dirent = NULL;
707
b0697053
MF
708 mlog_entry("dir %llu, name '%.*s'\n",
709 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
ccd979bd
MF
710
711 ret = -EEXIST;
712 dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
713 if (dirent_bh)
714 goto bail;
715
716 ret = 0;
717bail:
718 if (dirent_bh)
719 brelse(dirent_bh);
720
721 mlog_exit(ret);
722 return ret;
723}
724
0bfbbf62
MF
725struct ocfs2_empty_dir_priv {
726 unsigned seen_dot;
727 unsigned seen_dot_dot;
728 unsigned seen_other;
729};
730static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
731 loff_t pos, u64 ino, unsigned type)
732{
733 struct ocfs2_empty_dir_priv *p = priv;
734
735 /*
736 * Check the positions of "." and ".." records to be sure
737 * they're in the correct place.
738 */
739 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
740 p->seen_dot = 1;
741 return 0;
742 }
743
744 if (name_len == 2 && !strncmp("..", name, 2) &&
745 pos == OCFS2_DIR_REC_LEN(1)) {
746 p->seen_dot_dot = 1;
747 return 0;
748 }
749
750 p->seen_other = 1;
751 return 1;
752}
ccd979bd
MF
753/*
754 * routine to check that the specified directory is empty (for rmdir)
0bfbbf62
MF
755 *
756 * Returns 1 if dir is empty, zero otherwise.
ccd979bd
MF
757 */
758int ocfs2_empty_dir(struct inode *inode)
759{
0bfbbf62
MF
760 int ret;
761 loff_t start = 0;
762 struct ocfs2_empty_dir_priv priv;
ccd979bd 763
0bfbbf62 764 memset(&priv, 0, sizeof(priv));
ccd979bd 765
0bfbbf62
MF
766 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
767 if (ret)
768 mlog_errno(ret);
769
770 if (!priv.seen_dot || !priv.seen_dot_dot) {
771 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
b0697053 772 (unsigned long long)OCFS2_I(inode)->ip_blkno);
0bfbbf62
MF
773 /*
774 * XXX: Is it really safe to allow an unlink to continue?
775 */
ccd979bd
MF
776 return 1;
777 }
0bfbbf62
MF
778
779 return !priv.seen_other;
ccd979bd
MF
780}
781
316f4b9f
MF
782int ocfs2_fill_new_dir(struct ocfs2_super *osb,
783 handle_t *handle,
784 struct inode *parent,
785 struct inode *inode,
786 struct buffer_head *fe_bh,
787 struct ocfs2_alloc_context *data_ac)
788{
789 int status;
790 struct buffer_head *new_bh = NULL;
791 struct ocfs2_dir_entry *de = NULL;
792
793 mlog_entry_void();
794
795 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
796 data_ac, NULL, &new_bh);
797 if (status < 0) {
798 mlog_errno(status);
799 goto bail;
800 }
801
802 ocfs2_set_new_buffer_uptodate(inode, new_bh);
803
804 status = ocfs2_journal_access(handle, inode, new_bh,
805 OCFS2_JOURNAL_ACCESS_CREATE);
806 if (status < 0) {
807 mlog_errno(status);
808 goto bail;
809 }
810 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
811
812 de = (struct ocfs2_dir_entry *) new_bh->b_data;
813 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
814 de->name_len = 1;
815 de->rec_len =
816 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
817 strcpy(de->name, ".");
818 ocfs2_set_de_type(de, S_IFDIR);
819 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
820 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
821 de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize -
822 OCFS2_DIR_REC_LEN(1));
823 de->name_len = 2;
824 strcpy(de->name, "..");
825 ocfs2_set_de_type(de, S_IFDIR);
826
827 status = ocfs2_journal_dirty(handle, new_bh);
828 if (status < 0) {
829 mlog_errno(status);
830 goto bail;
831 }
832
833 i_size_write(inode, inode->i_sb->s_blocksize);
834 inode->i_nlink = 2;
835 inode->i_blocks = ocfs2_inode_sector_count(inode);
836 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
837 if (status < 0) {
838 mlog_errno(status);
839 goto bail;
840 }
841
842 status = 0;
843bail:
844 if (new_bh)
845 brelse(new_bh);
846
847 mlog_exit(status);
848 return status;
849}
850
ccd979bd 851/* returns a bh of the 1st new block in the allocation. */
316f4b9f
MF
852static int ocfs2_do_extend_dir(struct super_block *sb,
853 handle_t *handle,
854 struct inode *dir,
855 struct buffer_head *parent_fe_bh,
856 struct ocfs2_alloc_context *data_ac,
857 struct ocfs2_alloc_context *meta_ac,
858 struct buffer_head **new_bh)
ccd979bd
MF
859{
860 int status;
861 int extend;
8110b073 862 u64 p_blkno, v_blkno;
ccd979bd
MF
863
864 spin_lock(&OCFS2_I(dir)->ip_lock);
865 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
866 spin_unlock(&OCFS2_I(dir)->ip_lock);
867
868 if (extend) {
dcd0538f
MF
869 u32 offset = OCFS2_I(dir)->ip_clusters;
870
871 status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, &offset,
2ae99a60 872 1, 0, parent_fe_bh, handle,
ccd979bd
MF
873 data_ac, meta_ac, NULL);
874 BUG_ON(status == -EAGAIN);
875 if (status < 0) {
876 mlog_errno(status);
877 goto bail;
878 }
879 }
880
8110b073
MF
881 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
882 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
ccd979bd
MF
883 if (status < 0) {
884 mlog_errno(status);
885 goto bail;
886 }
887
888 *new_bh = sb_getblk(sb, p_blkno);
889 if (!*new_bh) {
890 status = -EIO;
891 mlog_errno(status);
892 goto bail;
893 }
894 status = 0;
895bail:
896 mlog_exit(status);
897 return status;
898}
899
900/* assumes you already have a cluster lock on the directory. */
901static int ocfs2_extend_dir(struct ocfs2_super *osb,
902 struct inode *dir,
903 struct buffer_head *parent_fe_bh,
904 struct buffer_head **new_de_bh)
905{
906 int status = 0;
ee19a779 907 int credits, num_free_extents, drop_alloc_sem = 0;
ccd979bd
MF
908 loff_t dir_i_size;
909 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
910 struct ocfs2_alloc_context *data_ac = NULL;
911 struct ocfs2_alloc_context *meta_ac = NULL;
1fabe148 912 handle_t *handle = NULL;
ccd979bd
MF
913 struct buffer_head *new_bh = NULL;
914 struct ocfs2_dir_entry * de;
915 struct super_block *sb = osb->sb;
916
917 mlog_entry_void();
918
919 dir_i_size = i_size_read(dir);
b0697053
MF
920 mlog(0, "extending dir %llu (i_size = %lld)\n",
921 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
ccd979bd 922
ccd979bd
MF
923 /* dir->i_size is always block aligned. */
924 spin_lock(&OCFS2_I(dir)->ip_lock);
925 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
926 spin_unlock(&OCFS2_I(dir)->ip_lock);
927 num_free_extents = ocfs2_num_free_extents(osb, dir, fe);
928 if (num_free_extents < 0) {
929 status = num_free_extents;
930 mlog_errno(status);
931 goto bail;
932 }
933
934 if (!num_free_extents) {
da5cbf2f 935 status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac);
ccd979bd
MF
936 if (status < 0) {
937 if (status != -ENOSPC)
938 mlog_errno(status);
939 goto bail;
940 }
941 }
942
da5cbf2f 943 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
ccd979bd
MF
944 if (status < 0) {
945 if (status != -ENOSPC)
946 mlog_errno(status);
947 goto bail;
948 }
949
950 credits = ocfs2_calc_extend_credits(sb, fe, 1);
951 } else {
952 spin_unlock(&OCFS2_I(dir)->ip_lock);
953 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
954 }
955
ee19a779
JB
956 down_write(&OCFS2_I(dir)->ip_alloc_sem);
957 drop_alloc_sem = 1;
958
65eff9cc 959 handle = ocfs2_start_trans(osb, credits);
ccd979bd
MF
960 if (IS_ERR(handle)) {
961 status = PTR_ERR(handle);
962 handle = NULL;
963 mlog_errno(status);
964 goto bail;
965 }
966
967 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
968 data_ac, meta_ac, &new_bh);
969 if (status < 0) {
970 mlog_errno(status);
971 goto bail;
972 }
973
974 ocfs2_set_new_buffer_uptodate(dir, new_bh);
975
976 status = ocfs2_journal_access(handle, dir, new_bh,
977 OCFS2_JOURNAL_ACCESS_CREATE);
978 if (status < 0) {
979 mlog_errno(status);
980 goto bail;
981 }
982 memset(new_bh->b_data, 0, sb->s_blocksize);
983 de = (struct ocfs2_dir_entry *) new_bh->b_data;
984 de->inode = 0;
985 de->rec_len = cpu_to_le16(sb->s_blocksize);
986 status = ocfs2_journal_dirty(handle, new_bh);
987 if (status < 0) {
988 mlog_errno(status);
989 goto bail;
990 }
991
992 dir_i_size += dir->i_sb->s_blocksize;
993 i_size_write(dir, dir_i_size);
8110b073 994 dir->i_blocks = ocfs2_inode_sector_count(dir);
ccd979bd
MF
995 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
996 if (status < 0) {
997 mlog_errno(status);
998 goto bail;
999 }
1000
1001 *new_de_bh = new_bh;
1002 get_bh(*new_de_bh);
1003bail:
ee19a779
JB
1004 if (drop_alloc_sem)
1005 up_write(&OCFS2_I(dir)->ip_alloc_sem);
ccd979bd 1006 if (handle)
02dc1af4 1007 ocfs2_commit_trans(osb, handle);
ccd979bd
MF
1008
1009 if (data_ac)
1010 ocfs2_free_alloc_context(data_ac);
1011 if (meta_ac)
1012 ocfs2_free_alloc_context(meta_ac);
1013
1014 if (new_bh)
1015 brelse(new_bh);
1016
1017 mlog_exit(status);
1018 return status;
1019}
1020
1021/*
1022 * Search the dir for a good spot, extending it if necessary. The
1023 * block containing an appropriate record is returned in ret_de_bh.
1024 */
1025int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
1026 struct inode *dir,
1027 struct buffer_head *parent_fe_bh,
1028 const char *name,
1029 int namelen,
1030 struct buffer_head **ret_de_bh)
1031{
1032 unsigned long offset;
1033 struct buffer_head * bh = NULL;
1034 unsigned short rec_len;
1035 struct ocfs2_dinode *fe;
1036 struct ocfs2_dir_entry *de;
1037 struct super_block *sb;
1038 int status;
1039
1040 mlog_entry_void();
1041
b0697053
MF
1042 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
1043 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
ccd979bd
MF
1044
1045 BUG_ON(!S_ISDIR(dir->i_mode));
1046 fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
1047 BUG_ON(le64_to_cpu(fe->i_size) != i_size_read(dir));
1048
1049 sb = dir->i_sb;
1050
1051 if (!namelen) {
1052 status = -EINVAL;
1053 mlog_errno(status);
1054 goto bail;
1055 }
1056
1057 bh = ocfs2_bread(dir, 0, &status, 0);
1058 if (!bh) {
1059 mlog_errno(status);
1060 goto bail;
1061 }
1062
1063 rec_len = OCFS2_DIR_REC_LEN(namelen);
1064 offset = 0;
1065 de = (struct ocfs2_dir_entry *) bh->b_data;
1066 while (1) {
1067 if ((char *)de >= sb->s_blocksize + bh->b_data) {
1068 brelse(bh);
1069 bh = NULL;
1070
1071 if (i_size_read(dir) <= offset) {
1072 status = ocfs2_extend_dir(osb,
1073 dir,
1074 parent_fe_bh,
1075 &bh);
1076 if (status < 0) {
1077 mlog_errno(status);
1078 goto bail;
1079 }
1080 BUG_ON(!bh);
1081 *ret_de_bh = bh;
1082 get_bh(*ret_de_bh);
1083 goto bail;
1084 }
1085 bh = ocfs2_bread(dir,
1086 offset >> sb->s_blocksize_bits,
1087 &status,
1088 0);
1089 if (!bh) {
1090 mlog_errno(status);
1091 goto bail;
1092 }
1093 /* move to next block */
1094 de = (struct ocfs2_dir_entry *) bh->b_data;
1095 }
1096 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1097 status = -ENOENT;
1098 goto bail;
1099 }
1100 if (ocfs2_match(namelen, name, de)) {
1101 status = -EEXIST;
1102 goto bail;
1103 }
8553cf4f 1104 if (ocfs2_dirent_would_fit(de, rec_len)) {
ccd979bd
MF
1105 /* Ok, we found a spot. Return this bh and let
1106 * the caller actually fill it in. */
1107 *ret_de_bh = bh;
1108 get_bh(*ret_de_bh);
1109 status = 0;
1110 goto bail;
1111 }
1112 offset += le16_to_cpu(de->rec_len);
1113 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
1114 }
1115
1116 status = 0;
1117bail:
1118 if (bh)
1119 brelse(bh);
1120
1121 mlog_exit(status);
1122 return status;
1123}
This page took 0.27717 seconds and 5 git commands to generate.